Calculating normal to a polygon

Help! I need to calculate the normal to a polygon to make a procedurally generated mesh (or least I think I do). Is there a function, or some example code, for this? I’ve googled it but the 3d math goes right over my head.

If you use the EggPolygon interface for creating your polygons (search the forums for examples), Panda can create the normals for you automatically. You can either call poly.recomputePolygonNormal() for each EggPolygon, or you can create the whole mess of them at once and then call group.recomputePolygonNormals() or group.recomputeVertexNormals() on the containing EggGroup.

David

Or, if you use some other means, take two edges of your polygon, obtain their vectors (vec = edgeEndpoint - edgeStartpoint), and then calculate their cross product (vecU.cross(vecV)). The obtained result is polygon’s normal (i.e. vector, perpendicular to both edge vectors). You might want to normalize the result afterwards.
PS: Cross product may be used in 3D space only, its formula doesn’t make sense for 2D space. Just in case…

Thanks, Birukoff. That looks like just what I need.

You will also need to check that the normal points to the required direction. Play around with the input edges to make sure the direction is right.