Rotation and movement problem.

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
Ryssus
Posts: 5
Joined: Fri Sep 17, 2010 10:32 am

Rotation and movement problem.

Post by Ryssus »

I recently tried to write code for moving my camera. I read some old articles about using quaternions or transformation matrix, but none of them works for me. Can someone post code for rotating and moving objects? For example I need a function which takes 2 arguments:

Code: Select all

Move(node, vector)
or

Code: Select all

Rotate(node, rotation_vector)
all according to node position and orientation. Maybe if you can't post code, you can explain how to implement such functions?
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

each ISceneNode already has a rotation vector, which you can change:
vector3df getRotation()
void setRotation(vector3df)

so, if you really need these functions, for moving you can use:

Code: Select all

void Move(ISceneNode node, vector3df vector)
{
// you need only 1 line below, choose:
node->setPosition(vector); // this changes position relatively to node's parent
node->setPosition(node->getPosition() + vector); // this changes position relatively to previous position
}
You can write same function for rotation.
Ryssus
Posts: 5
Joined: Fri Sep 17, 2010 10:32 am

Post by Ryssus »

I am not really sure if these response is what I wanted. I know functions getRotation() and setRotation(). But what if I apply some keybord in way that W and S is moving forward and backward when A and D is turning left and right. When I press W I will translate (setPosition) for example 10 units in Z axis. But if I rotate my camera, orientation changes and I need more than setPosition. I think I have to combine Rotation and Translation but I don't know how.
monchito
Posts: 59
Joined: Thu Mar 08, 2007 9:38 pm

Post by monchito »

Maybe this can help

Code: Select all

void Move( ISceneNode* node, const float &distance )
{ 
  vector3df pos = node->getPosition();
  float rot = node->getRotation().Y;
  pos.X += distance * cos(rot * DEGTORAD);
  pos.Z -= distance * sin(rot * DEGTORAD);
  node->setPosition( pos );
}

void Strafe( ISceneNode* node, const float &distance )
{ 
  vector3df pos = node->getPosition();
  float rot = node->getRotation().Y - 90;
  pos.X += distance * cos(rot * DEGTORAD);
  pos.Z -= distance * sin(rot * DEGTORAD);
  node->setPosition( pos );
}

void MovingNode( ISceneNode* node )
{
    if(keys[KEY_KEY_W])  // move in the rotation'Y'  direction
    {
      Move(node, 2);
    } 
    if(keys[KEY_KEY_S])
    { 
      Move(node, -2);
    }
    if(keys[KEY_KEY_A])
    { 
      Strafe(node, 2);
    } 
    if(keys[KEY_KEY_D])
    { 
      Strafe(node, -2);
    }
    
    if(keys[KEY_KEY_Z])   // rotate
    { 
      vector3df rot =node->getRotation();
      rot.Y += 1; node->setRotation( rot )
    }
    
    if(keys[KEY_KEY_X]) 
    {
      vector3df rot = node->getRotation();
      rot.Y-= 1; node->setRotation( rot );
    }
} 
Post Reply