Followers 0

Calculating the Angle between objects

1 post in this topic

Hey all,

I'll try to state this issue as plainly as I can: in the game I'm developing I have two objects in 3D space. One is player controlled, the other is AI. Currently I'm using a built in function that, when called, causes the objects to look at each other. However, they don't rotate toward each other--they simply jump from facing one direction to another, and this isn't the natural motion that I need.

So what I'm looking for is a formula that will give me the angle (I think) between them, accounting for the direction each is currently facing. That way I can rotate them in a natural way until they're facing each other. Help is greatly appreciated. I would think there's a universal mathematical solution, but if it's relevant I'm using Away3D in conjunction with AS3. Thanks.
0

Share on other sites

Start with the facing vector of one character. Calculate the vector from that character to the other. For just the angle: the angle theta between the facing direction and the "new" direction can be determined from the cross product |a| |b| sin(theta) n, or the dot product |a| |b| sin(theta).

Rather than being directly concerned with the angle, calculate 2 quaternions: one for the facing direction, one for the new direction. SLERP between the two over a short period of time (however long you want it to take for the character to turn).

Quaternion qFace, qNew, qCharacter;
...
qCharacter = SLERP(qFace, qNew, t);
// t goes from 0 to 1 and is the fraction of time to take to turn from one direction to the other
2

Create an account

Register a new account