Rotating to face an Object

Started by
7 comments, last by Dmytry 19 years, 6 months ago
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?
Visit the 3DHangout
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.
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 :)
Geocyte Has Committed Suicide.
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.
[size="1"] [size="4"]:: SHMUP-DEV ::
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
Visit the 3DHangout
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) =\
Visit the 3DHangout
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.
[size="1"] [size="4"]:: SHMUP-DEV ::
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.
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]

This topic is closed to new replies.

Advertisement