I’m having a bit of trouble with this, I have a script that takes a triangle on a collision mesh and tries to calculate the normal, but for some reason the normals are not at all in the right direction.
As you can see in the image, the normal for that particular tri is off to the side.
Is there something that I’m doing wrong here in the code, or is it probably my mesh that is broken?
Ahh, thank you! I need to check for errors like this more often!
This was unintentional, and now that I have switched it to just p2, it works perfectly.
@JupiterSky , You will only get the normal from a cross product if the two vectors are orthogonal (perpendicular). The way it will work 100% regardless of the angle is if you do the following:
Vector3 pointA = new Vector3(...);
Vector3 pointB = new Vector3(...);
Vector3 pointC = new Vector3(...);
Plane plane = new Plane(pointA, pointB, pointC);
Vector3 norm = plane.normal;
where pointA, pointB, and pointC are the vertices of the triangle.
Edit: Furthermore, normalizing your vector does not have anything to do with a surface normal vector.
Please be advised that above is not true, you may want to check your source (see here, Method 3). Any three non-collinear points p1, p2, p3 define a plane, and that plane’s normal vector can be calculated by (p2-p1) x (p3-p1). The resulting normal vector may not be normalized (i.e. may not have a length of 1.0), but neither will be the cross product of any two arbitrary perpendicular vectors.