Jump to content
  • Advertisement
Sign in to follow this  

Rotating to "sit" a unit on a triangle

This topic is 4073 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!