how to rotate mesh to normals using basic math?

Community Forums/General Help/how to rotate mesh to normals using basic math?

Robert Cummings(Posted 2010) [#1]
Hi,

I need to rotate a mesh so it will lie flush against the normals of another mesh, regardless of its angle (it could be upside down). Think decals.

How do I do this reliably? I have the normals but not anything else that works. I was thinking Atan2 but it flipped at random and never really worked properly.

Any suggestions? I can't use slerp because quaternians aren't available to me for what I am doing.


puki(Posted 2010) [#2]
There is some stuff in here - "Ziltch" mentions Atan - but it isn't in his final code example.

http://www.blitzbasic.com/Community/posts.php?topic=17097


Robert Cummings(Posted 2010) [#3]
Thank you puki i knew you would deliver! am i bad for using minib3d for the pc and mac or is that forgivable under the blitz3d banner?


puki(Posted 2010) [#4]
Something else from "caff_":
http://www.blitzbasic.com/Community/posts.php?topic=72469


Robert Cummings(Posted 2010) [#5]
darn they all seem to point at a position rather than rotate to reflect the angle of the normal...

This works fine in Blitz3D but minib3d can't seem to do it.

Last edited 2010


Kryzon(Posted 2010) [#6]
You need to find a way to build a matrix from the normal's coordinates (like position and rotation; no need for scale).
Create a blank Matrix object. LoadIdentity() with it and use Translate() and Rotate() respectively to apply the normal's coordinates to this blank matrix (both are methods from the Matrix class).

Then you can Overwrite() the mesh's entity matrix with that one you generated. A mesh extends the Entity class, so it inherits that "mat" field that holds it's world matrix, which controls the entity's position, rotation and scale. After the overwrite, Null that dummy matrix you created to hold the normal's data.

Easier said than done, of course. And I hope it works because this was a totally untested guess.
Perhaps you might also try a more elegant approach with shaders and multi-pass rendering, with the decals being applied in texture coordinates through a shader calculation, rather than with meshes.
I dislike meshes for decals: they make up hell when there are corners around...

Last edited 2010


Robert Cummings(Posted 2010) [#7]
I definately need to stick with this approach, not shaders or alternative ideas.

Thanks for the heads up though, I will look into matrixes then I guess! Would have to be a portable to C++ solution so thats the tricky bit.

Last edited 2010


Kryzon(Posted 2010) [#8]
Oh, I bet the Matrix class is present in the iMiniB3D code as well, so there shouldn't be a problem.

BTW, that procedure working or not, I'd like to know! keep us posted. Good luck.


Robert Cummings(Posted 2010) [#9]
spent all day on it. i need to use simple cos, sin and atan2 in the right combination.