currently i'm trying to rotate a Spaceship so its facing the targetplanet. But it is not working really well.
Maybe someone knows a better method to rotate an object around all x,y,z axis?
irr::core::vector3df toTarget = (targetpos- Node->getAbsolutePosition());
irr::core::vector3df reRot = -toTarget.getHorizontalAngle();//needed Rotation... Object is somehow backwards so there needs to be a minus
irr::core::vector3df rot = Node->getRotation();
if (rot != reRot)
{
if (rot.X< reRot.X)rot.X += 200.0f*frameDeltaTime;
if (rot.X> reRot.X)rot.X -= 200.0f*frameDeltaTime;
if (rot.Y< reRot.Y)rot.Y += 200.0f*frameDeltaTime;
if (rot.Y> reRot.Y)rot.Y -= 200.0f*frameDeltaTime;
if (rot.Z< reRot.Z)rot.Z += 200.0f*frameDeltaTime;
if (rot.Z> reRot.Z)rot.Z -= 200.0f*frameDeltaTime;
}
Node->setRotation(irr::core::vector3df(rot.X,rot.Y,rot.Z));
You can use quaternions and slerp the start and end quaternion in a [0..1] interval. This will create a perfect rotation around the shortest way from any direction to any other.
this code rotates the Spaceship around its local axes, egs, if X=1, rotates each frame around its local X axe with 1 degree;
if You have a manual control, it is what you want;
If You want to be done automatically , You must calculate the delta angles;
ISceneNode* Spaceship;
vector3df rp(X,Y,Z);
vector3df r = Spaceship->getRotation(); //get current rotation (euler)
matrix4 m;
matrix4 mp;
m.setRotationDegrees(r); //set matrix to current rotation
mp.setRotationDegrees(rp); //set second matrix to rotation to add
m *= mp; //multipy them
r = m.getRotationDegrees(); //get rotation vector from matrix (euler)
Spaceship->setRotation(r); //rotate node
for an automated movement, but just in the horizontal plane, I made likes this:
-make a little meshnode with the same orientation as the Spaceship;
in the main loop :
-position the meshnode each frame at the same position as the Spaceship;
-point the meshnode to the target Planet position;
-calculate total rotation for meshnode;
-calculate total rotation for Spaceship;
-calculate delta angles (take care at :if (delta_angle>180) {delta_angle=-(360-delta_angle)};
-turn your Spaceship , rating delta angles ;