Sign in to follow this  
TheStranger

How do I make an object face the direction it is moving?

Recommended Posts

TheStranger    122
Ok my object is facing a direction and it is traveling in another, how do I get it to rotate so it faces the direction of the destination vector? I have a start point and a destination point. I was thinking about using dot products to get the angle between the vectors of the direction vector and the vector that the object is facing towards, but I don't know which axis x,y,z to rotate in? I'm using opengl which gives you options of rotating on the x,y, or z axis. I saw a few solutions in this forum but they never mentioned which axis to rotate the object and only gave you the angle. Thanks in advance.

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
I'm using opengl which gives you options of rotating on the x,y, or z axis.
You can rotate about any axis using glRotate*(), not just the cardinal axes. As for which axis to rotate about, it depends. If it's an FPS-type setting, you might just want to yaw about the world up axis (whatever that is for you). If your objects are arbitrarily oriented, you can construct an axis-angle pair from the cross product of the current forward vector and the target forward vector. If you need more details, you might give some more info about what you're doing.

Share this post


Link to post
Share on other sites
BeanDog    1065
Take the vector (v1) going from you to where you're currently facing. Then another vector (v2) going from you to where you WANT to be facing. Normalize them both (divide them by their lengths to get length 1). Take the cross product of them to get a vector (v3) that's perpendicular to them both. You need to rotate around v3 to get v2 to become v1. How much? Take the dot product of v1 and v2 (when they're already length 1), and you get the cosine of the angle you need to rotate by.

You may have to play with the positives and negatives to get it to rotate in the right direction, but this should do it. It's exactly what I did for heat-seeking missiles :-)



~BenDilts( void );

Share this post


Link to post
Share on other sites
PhilVaz    144
Hey this is exactly my question. It must be a popular one. :-)

One way is to use atan2(dz,dx) where dz = how much to move on z, and dx = how much to move on x. It should work and is shorter than having to take dot/cross products. The formula to convert to degrees would be

angle = atan2(dz,dx) * 180 / PI

My thread above Rotation on Y

If your Z is into/out of the screen, and your X is left/right, then you want to rotate on Y (up/down). In OpenGL

glRotatef(Enemy[i].Angle, 0.0f, 1.0f, 0.0f); // rotate on Y

Phil P

Share this post


Link to post
Share on other sites
zekis    122
I am new to 3d maths... but hey everyone has to start somewhere.
What i would like to know is would the obove maths work for aligning an object like a cube to a face.. i can get the normal of the face but i cant work out how to rotate the cube so that it is aligned with the face..

Zekis

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