Calculating the Angle between objects
Members - Reputation: 108
Posted 18 July 2014 - 08:32 PM
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.
Crossbones+ - Reputation: 6147
Posted 20 July 2014 - 07:00 AM
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
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.