Page 1 of 1

Rotation and movement problem.

Posted: Sun Sep 19, 2010 10:00 am
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?

Posted: Sun Sep 19, 2010 10:25 am
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.

Posted: Sun Sep 19, 2010 10:36 am
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.

Posted: Sun Sep 19, 2010 12:14 pm
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 );
    }
}