www.jpct.net
jPCT  a 3d engine for Java => Support => Topic started by: K24A3 on October 09, 2011, 10:58:07 am

I'm trying to rotate an object so it's facing the way it's moving.
Is there a function in jPCT or Java that can calculate the rotation angle based on an object's translation movement?
If the object is travelling up Z using translation(0,0,2), the angle would be 270 degrees.
If the object is travelling down Z using translation(0,0,2), the angle would be 90 degrees (or PI/2).
If the object is travelling up X using translation(2,0,0), the angle would be 0 degrees.
If the object is travelling down X using translation(2,0,0), the angle would be 180 degrees (or PI).
If the object is travelling down X and up Z using translation(2,0,2), the angle would be 225 (270180).

You can calculate the direction vector from the old position to the new one, call one of the getRotationMatrix()methods on the resulting SimpleVector and use that as the new rotation matrix of yout object. Working with derived angles from a matrix or something usually is a bad idea.

The problem is that the destination vector is unknown since the object is moved on the fly or by random.
I created an algorithm myself which works fine, I was hoping there was a preexisting function to do the math.
Here's the code if anyone needs it for something.
float fCombined = 0;
if(xMove < 0) fCombined = xMove;
else fCombined = xMove;
if(zMove < 0) fCombined += zMove;
else fCombined += zMove;
float fAngle = fPI;
if(zMove > 0) fAngle = fPIhalf*3;
else fAngle = fPIhalf*1;
if(zMove > 0) // 270
{
if(xMove > 0) fAngle += (xMove/fCombined) * fPI/2;
else fAngle = (xMove/fCombined) * fPI/2;
}
else // 90
{
if(xMove > 0) fAngle = (xMove/fCombined) * fPI/2;
else fAngle += (xMove/fCombined) * fPI/2;
}
myobject.clearRotation();
myobject.RotateY(fAngle)

??? But once you know the translation, you know the destination....i don't get the point here. Anyway, if you have something that works for you, all is well...

Hmm I guess I could place the Move variables into a simpleVector then scaleMul it to generate a destination. I'll use that technique if I need Y movement :)