How to get a face's normal

Started by
3 comments, last by WaterCrane 15 years, 4 months ago
I'm using D3DXIntersect for my collision detection and I want to find out the normal of the face that the ray hits, so I can calculate vectors when running into hills etc. I get the index of the face but how do I access the mesh's face's normal? BTW: Is using face normals to calculate which direction the character will move, when it hits a plane which isn't horizontal, the way to do that kind of collision detection? Are there faster ways to do it?
Advertisement
Quote:Original post by Hannesnisula
I'm using D3DXIntersect for my collision detection and I want to find out the normal of the face that the ray hits, so I can calculate vectors when running into hills etc. I get the index of the face but how do I access the mesh's face's normal?

BTW: Is using face normals to calculate which direction the character will move, when it hits a plane which isn't horizontal, the way to do that kind of collision detection? Are there faster ways to do it?


I did this before, what you have to do is get 2 normals pointing from one of the vectors to the other 2 then get the cross product.

Though I'd advice that you don't use the D3DXIntersect command and just use a physics library like PhysX.
Remember Codeka is my alternate account, just remember that!
Quote:Original post by CodaKiller
I did this before, what you have to do is get 2 normals pointing from one of the vectors to the other 2 then get the cross product.

Though I'd advice that you don't use the D3DXIntersect command and just use a physics library like PhysX.


Do you know how to get the face normal?

Why is it better to use a physics library instead of creating it yourself? Is it faster?

BTW: Can you see the sourcecode of the physics libraries?
Quote:Original post by Hannesnisula
Do you know how to get the face normal?
You need the 3 corner vertices of the triangle, subtract 2 of them from the third, and take the cross-product of the resulting vectors. There are many tutorials around the internet, or take a look at a geometry textbook.

Quote:Why is it better to use a physics library instead of creating it yourself? Is it faster?
It is certainly faster in terms of development time, than building your own from scratch. In addition, realistic physics simulation is surprisingly complicated, and unless you have a very strong math and physics background, it will take you a very long time to get it right.

Quote:BTW: Can you see the sourcecode of the physics libraries?
You can if they are open source, take a look at Bullet and ODE.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Calculating that cross product will return the normal vector for that face; the principle should appear in any good maths book on vectors. Just remember to normalise the resultant vector though, otherwise your reflected light beam will have a different intensity.

It is probably better to use a physics library because of things like assembly language optimisations that are not obvious or straightforward (i.e. SSE) with high-level code... you can also rest assured that it has been bug-tested and scrutinised for some time and will likely give correct results all of the time.

(EDIT: Beaten to the punch by "swiftcoder"!)

This topic is closed to new replies.

Advertisement