• Status: Solved
• Priority: Medium
• Security: Public
• Views: 3141

# Calculate Normal Vector for given triangle in Managed DirectX C#

In Managed DirectX C#, I am creating a custome triangle list.

What I want to know, is how to easily (or do it at all i guess) calculate the normal for a given face (triangle). This is sure easy when you have a fave which all to verticies rest on the same X,Y, or Z plane, but how can you calaculate it if you have a face which could be facing any direction?

I am sure it can be calculated from the 3 verticies, but I am not seeing how. I am hoping there is a simple method which returns a normal vector for a given triangle but that's probably hoping for to much.

Thanks,
Matt
0
mattososky
• 4
• 2
1 Solution

Commented:
You can use the vector coross procuct.
Assume triangles have vertices at p1 (x1, y1, z1), p2 (x2, y2, z2) p3 (x3, y3, z3).
Build two vectors:
A = (x2 - x1)i + (y2 - y1)j + (z2 - z1)k = A1i + A2j + A3k
B = (x3 - x1)i + (y3 - y1)j + (z3 - z1)k = B1i + B2j + B3k

C = A X B = (A2B3 - A3B2)i + (A3B1 - A1B3)j + (A1B2 - A2B1)k

C is perpendicular (normal)  to the plane of the triangle.
0

Author Commented:
As I'm reading your comment I don't understand what the variables i,j,k are suppose to represent.
0

Commented:
They identify the components of the vector. i is component parallel to x axis, j is parallel to y, and k is parallel to z. They are not really variables. So if you think of a vector as a 3-tuple, then the "i" value is the first, j the second, and k the third.

0

Commented:
Just to clarify, in case you are new to vectors, it's kind of like the general equation of a line:
aX + bY +cZ + D = 0

Where x, y and z just identify the use of the variables.
0

Author Commented:
perhaps just the language and syntax are the difference. I'm using c# managed directX 9. you can define a Vector3 like Vector3(float x, float y, float z) and access the members by vector.position.x , y , & z;

So if I'm understanding you right. what your saying is i can do this: Create 2 new vectors. NewVectory1 will have x,y,z componets that are the difference of the x,y,z of Triangle points 2 & 1. NewVector2 will have x,y,z, components that are the difference of the x,y,z of Triangle points 3 & 1. Then, multiply the two new vectors together and i will get a vector perpendicular to the face of the Triangle.

Yes?

0

Commented:
Correct, but the components need the operations shown here for the multiplication;
say your vectors are called A and B, then:
C = A X B = new Vector( (AYBZ - AZBY),  (AZBX - AXBZ),  (AXBY - AYBX));

Make sense now?

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.