Jump to content
  • Advertisement
Sign in to follow this  
DarkWhoppy

Rotating to face an Object

This topic is 5449 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

I need a method for rotating to face another object in a 3D world. Not to instantly face the object, but to rotate towards it at a given speed. Anyone have a function or something for this?

Share this post


Link to post
Share on other sites
Advertisement
Umm... If I get you right, just rotate the object incrementially towards the target, using a counter to keep track of how much its been rotated. Increase that counter, and rotate it gradually (depends what you're using to render)

Or are you asking how to rotate towards the target? If thats the case, you'll need to do some trig to find the desired angle, then do some addition/subtraction to find which way you need to rotate.

Share this post


Link to post
Share on other sites
The simplest way I know to do this...

First find the matrix which represents the targets position relative to the targeter...

For example, say you have a missile (the targeter) and a space ship (the target)... The matrix you want to find is
ship.matrix * missile.matrix.inverse()
(by the way that's a 3d inverse not a mathematical inverse i.e. invert rotation elements and multiply position elements by -1)

Then you just have a simple set of conditions...

if the x_position is negative rotate your targeter left
if the x_position is positive rotate your targeter right
if the y_position is negative rotate your targeter down
if the y_position is positive rotate your targeter up

This is part of what passed for "AI" in my first space game... crap, but very easy to do :)

Share this post


Link to post
Share on other sites
For a more pleasing rotation you may want to consider interpolating your angles with an ease-in ease-out function. Given your start and desired end rotations r(t=0) and r(t=interpTime), where interpTime is the time it takes to complete the interpolation, the angular acceleration constant 'a' for the whole interpolation is determined like this:

a = 2( r(t=interpTime) - r(t=0) ) / interpTime^2

The angular velocity 'w' is zero at the beginning and end of the interpolation. During the first half of the interpolation you want the ang vel to gradually increas swinging the angle round faster as you approach the half way point. Then during the second half you want the speed to decrease evetually bringing it to zero when the angle reaches the desired final angle. This is acheived by doing the following calculations each frame, where t is the time since the start of the interpolation:

if( t < interpTime/2 )
{
w(t) += a*frameTime
r(t) += w(t)*frameTime
}
else
{
w(t) -= a*frameTime
r(t) -= w(t)*frameTime
}

Note that floating point error may cause the roation angle to be slightly off and the ang vel to not be quite zero at the end of the interpTime. They will be close however so simply setting them to the desired vals wont at this point wont be noticable. For three dimensions simply replace 'a' 'w' and 'r' with vectors instead of scalars. Hope this helps

PS: if its a camera orientation you are interpolating you may want to consider quaternion spherical interpolation to get the nicest looking results.

Share this post


Link to post
Share on other sites
Quote:
Original post by Geocyte
This is part of what passed for "AI" in my first space game... crap, but very easy to do :)


And i'm using this for a space game :-P

Share this post


Link to post
Share on other sites
If someone could help me with this... please. The two methods that are posted in this thread already I can't seem to get to work. (the second one I don't even understand) =\

Share this post


Link to post
Share on other sites
What exactly is it you don't understand? Post up how you are representing you game objects' positions and orientations and we may be able to be more specific.

Share this post


Link to post
Share on other sites
I'm assuming here that the object that you wish to rotate has an orientation to begin with. Calculate the direction vector from the object to the target and normalise. Cross-Product this direction vector with your object's orientation vector to find the rotation axis. Dot-Product (full version) the direction vector with the object's orientation vector to find the rotation angle. Then you need to rotate the object's vertices around the rotation axis by the rotation angle to have it facing. Search for 'arbitrary axis rotation' to do this.

Note that this method actually rotates the object physically. If you just want it to visually face its target (ie like glRotatef), then I would recommend using quaternions.

Share this post


Link to post
Share on other sites
there's no full and correct solution in that thread yet....
you need to be more specific. How you store your orientation and position? in 4x4 matrix? Or in vector and quaternion?
Both target and targetter have same representation?

Let's you have missile aiming to the target. It doesn't have to be necessarily missile but i'll call it so.

You need to compute position of target in coordinate system of missile and then turn missile accordinly. Almost as Geocyte said except that
Quote:

(by the way that's a 3d inverse not a mathematical inverse i.e. invert rotation elements and multiply position elements by -1)

Wrong. You need to use mathematical inverse.

And with most representations you don't need to multily matrices there.
You need
missile.LocalToGlobalTransformMatrix().inverse()*target.GlobalPosition();
assuming your matrices is OpenGL-style.

LocalToGlobalTransformMatrix it's matrix you use to move&turn model of missile before drawing it.

[Edited by - Dmytry on October 17, 2004 2:36:00 PM]

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!