Aligning to a surface normal

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
haphazardry
Posts: 2
Joined: Sun Aug 02, 2009 2:36 am

Aligning to a surface normal

Post by haphazardry »

Hi everyone. I'm new here but have been enjoying irrlicht for a while. Props to the dev team for making a clean API. I quite often use in my line of work for testing techniques/algorithms because it's so easy to get up and running. Which brings me to a question that I have.

Currently, I am trying to align an object to the surface or the normal it is on. This is easy enough in most case, as I just recalculate the orthonormal basis of the object from it's previous forward, up and right vectors. This allows me to also maintain the rotation of the object. I do this like so:

Code: Select all

core::matrix4 trans = fairyNode->getRelativeTransformation();		

core::vector3df oldY(0.f, 1.f, 0.f);
core::vector3df oldX(1.f, 0.f, 0.f);
core::vector3df oldZ(0.f, 0.f, 1.f);

trans.rotateVect(oldY);
trans.rotateVect(oldZ);
trans.rotateVect(oldX);

oldX.normalize();
oldY.normalize();
oldZ.normalize();

core::vector3df nForward = oldX.crossProduct(normal);
core::vector3df nRight = normal.crossProduct(nForward);
This works fine unless oldX and the surface normal are pointing in the same direction (i.e parallel), which produces the Zero vector and results in incorrect orientation around the normal.

My question: Does anyone know of a simple and elegant (actually, any) way of handling this situation? It's easy enough to test for the zero vector using an epsilon, but then what? I'm thinking I could do a rotation, but I'd much rather do this in vector space.

Any pointers would be appreciated.

Happy coding!
haphazardry
Posts: 2
Joined: Sun Aug 02, 2009 2:36 am

Post by haphazardry »

Nevermind!

I simply checked for the special case of the zero vector when generating my nForward vector. If this is the case, I simply take the cross of the old forward vector and the normal instead, generating the nRight vector, then use it to generate nForward vector. Works like a charm.

Hope this helps someone else one day. :)
Post Reply