Rotate object in Direction to Target 3D Space

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
JFT
Posts: 11
Joined: Mon Feb 02, 2009 5:32 pm

Rotate object in Direction to Target 3D Space

Post by JFT »

Hi,

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?

Code: Select all

    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));
Thanks for your help!

JFT
nespa
Posts: 167
Joined: Wed Feb 24, 2010 12:02 pm

Post by nespa »

Code: Select all


      ISceneNode* Spaceship,Planet;
      vector3df origin,destin,result;
		vector3df rot;

		origin = Spaceship->getAbsolutePosition();
		destin = Planet->getAbsolutePosition();

		result = destin - origin;
		result = result.getHorizontalAngle();

		rot = result;

		Spaceship->setRotation(rot);


JFT
Posts: 11
Joined: Mon Feb 02, 2009 5:32 pm

Post by JFT »

thanks for your quick answer.

But i need a code where the ship is turning with a given turning speed... turning should take time, i want to see the ship turning around.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

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.
nespa
Posts: 167
Joined: Wed Feb 24, 2010 12:02 pm

Post by nespa »

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;

Code: Select all

      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 
nespa
Posts: 167
Joined: Wed Feb 24, 2010 12:02 pm

Post by nespa »

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 ;
Post Reply