- Home /
Draw a perpendicular line from a vertex of the mesh
I try to draw a debug line from the vertex point P1 on the mesh follow the normal vector at this point. I calculated the normal vector by this code below while I have known the position of three points (P1, P2, P3)
Vector3 side1 = P2 - P1;
Vector3 side2 = P3 - P1;
Vector3 normal = Vector3.Cross(side1, side2);
float perpLength = normal .magnitude;
normal /= perpLength;
Debug.DrawLine(P1, P1 + (normal * 150), Color.green, 150, false);
The result below isn't my expectation. The line I have expected is the red line but it drew the green line in the opposite direction. I don't know how to control this situation to make the green line go outside the mesh. I'm really having trouble with math so I need some ideas to resolve that.
Thank you in advance.
Answer by Bunny83 · Sep 15, 2020 at 09:25 AM
Unity works with a left-handed system. You have to use the left hand for almost all orientation relevant things ^^. You probably remember the right-hand rule for the cross product. However this only holds true in (the mathematical) right handed system.
Just think about it this way:
The cross product is always just
c.x = a.y*b.z - a.z*b.y
c.y = a.z*b.x - a.x*b.z
c.z = a.x*b.y - a.y*b.x
No matter if you're in a left or right handed system. Imagine the X-Y plane is the same in both systems. In that case the z axis of the two systems point in opposite directions. If you pass in two x-y vectors as input for the cross product, the resulting vector will always look the same numerically. However since in one system the z-axis point in one way and in the other system it points in the opposite direction, it points physically in the opposite direction.
To turn a left handed system into a right handed system you would need to flip one axis. If you do you would get the result you expected, yet the numerical coordinates are all still the same. Though keep in mind that flipping space on one axis changes the orientation of everything. So triangles which had a clockwise winding from the outside would now have a counter clockwise winding as seen from the outside.
If you have trouble with linear algebra I can highly recommend the whole linear algebra series by 3b1b. Things relevant for this case would be chapter2 about basis vectors, chapter 9 about duality and chapter 13 about change of basis. Though if you have some time to spare I can recommend the whole series :)
Thanks for your very quality answer and your recommendations about the 3b1b channel :) I just find the solution to my question that to make the normal vector go outside the mesh, I check if P1 is the first point of the triple in triangle array or not. If it isn't the first point then change the only the sign from + to - on this line: Debug.DrawLine(P1, P1 + (normal * 150), Color.green, 150, false);
Your answer
Follow this Question
Related Questions
Is there a cheap way to flip a collider´s normals in real time? 0 Answers
Do we need to call RecalculateNormals() if normal vectors are assigned? 1 Answer
Generating Normals 1 Answer
How to get the normal of a line in relation to a specific point in 3D? 1 Answer
Drawing a cube by a new Mesh 2 Answers