Sign in to follow this  

Rotating to "sit" a unit on a triangle

Recommended Posts

Hi guys, I'm trying to figure out how to rotate a unit model (RTS game) to "sit" on a triangle of any orientation. I have the 3 points that make the current triangle up, how do I work out how much to rotate on x/y/z to align properly on it? Maths isn't my strong point, and I have to keep hammering away at things like this to figure it out, so any help would be appreciated! :)) Thanks a lot

Share this post

Link to post
Share on other sites
Naive suggestion...

Take the surface normal of the triangle and the "up" vector.
Dot product gives the angle between the two vectors.
Cross product gives a rotation axis.

If the dot product is close to zero, do nothing.

Otherwise, normalize the result of the cross product, then do an arbitrary-axis rotation using acos(dot product) as the amount of rotation. Depending upon ordering of the cross product, you might need to negate the angle.

Or as pseudo code:
float angle = dotProduct(surfaceNormal, upVector);
if (angle > 0.001) {
vector axis = crossProduct(surfaceNormal, upVector);
float radians = acos(angle);
matrix = ArbitraryAxisRotation(axis, radians);

Caveat: I'm a programmer, not a mathematician, so at no time do I know what I'm talking about.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this