Movement Of Model Based On Rotation

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
MerlinCAM
Posts: 6
Joined: Sun Jul 08, 2007 9:11 pm

Movement Of Model Based On Rotation

Post by MerlinCAM »

I'm having difficulty with the following code, this seems correct to me, but i'm moving in weird directions. Any help is appreciated.

core::vector3df playerpos = node->getPosition();
core::vector3df playerrot = node->getRotation();

int lastFPS = -1;

f32 angle = 0.0f;
core::vector3df billpos = bill->getPosition();

std::cout << playerrot.X << " " << playerrot.Y << " " << playerrot.Z;

while(device->run())
if (device->isWindowActive())
{

playerpos = node->getPosition();


playerrot = node->getRotation();
angle = playerrot.Y;

if (playerrot.Y < 0)
angle = 360 + angle;

//***** Craig's Event Code For KeyDown Stuff

if (receiver.isKeyPressed(KEY_KEY_W))
pspeed = 1;

if (receiver.isKeyPressed(KEY_KEY_S))
pspeed = - 1;

if (receiver.isKeyPressed(KEY_KEY_A))
playerrot.Y -= 1.0f;


if (receiver.isKeyPressed(KEY_KEY_D))
playerrot.Y += 1.0f;

if (receiver.isKeyPressed(KEY_KEY_I))
billpos.X += 1.0f;

if (receiver.isKeyPressed(KEY_KEY_K))
billpos.X -= 1.0f;

if (receiver.isKeyPressed(KEY_KEY_J))
billpos.Z += 1.0f;

if (receiver.isKeyPressed(KEY_KEY_L))
billpos.Z -= 1.0f;




if (playerrot.Y >= 360 || playerrot.Y <= -360)
playerrot.Y = 0.0f;

if (receiver.isKeyPressed(KEY_KEY_X))
std::cout << " X " << playerrot.X << " Y " << playerrot.Y << " Z " << playerrot.Z << " Angle " << angle << " PlayerX " << playerpos.X << " PlayerZ " << playerpos.Z << " BillX " << billpos.X << " BillY " << billpos.Z << std::endl;


// position code based on direction




// we move + x or - x depending on angle
if (angle > 90 && angle < 270) {
playerpos.X -= pspeed * std::cos(angle);
}
else {
playerpos.X += pspeed * std::cos(angle);
}

if (angle > 0 && angle < 180) {
playerpos.Z -= pspeed * std::sin(angle);
}
else {
playerpos.Z += pspeed * std::sin(angle);
}
// end position code

bill->setPosition(billpos);
node->setPosition(playerpos);
node->setRotation(playerrot);
camera->setRotation(playerrot);
camera->setTarget(playerpos);
pspeed = 0;
//***** End Craig's Code *******************
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Here is a quick blurb for moving a node in the direction it is facing. Some cameras don't really work with it because the camera has a 'target' that it looks at and the 'target' concept is seperate from the rotation.

Code: Select all

     // in your render/run loop 
      core::vector3df pmove(0, 0, 0); 
      if (receiver.isKeyPressed(KEY_KEY_W)) 
        pmove.Z += 1.f; 

      if (receiver.isKeyPressed(KEY_KEY_S)) 
        pmove.Z -= 1.f; 

      // move the player
      core::vector3df pos( player->getPosition() ); 

      // rotate the movement vector into parent coordinate system
      player->getRelativeTransformation().rotateVect(pmove); 

      const core::vector3df velocity(10.f, 0.f, 30.f); // we can move faster forward than sideways 

      // apply movement over elapsed time at provided velocity
      pos += (pmove * (velocity * elapsed)); 

      // update the final position
      player->setPosition(pos); 
Same thing for the rotation...

Code: Select all

      core::vector3df prot(0, 0, 0);
      if (receiver.isKeyPressed(KEY_KEY_A)) 
        prot.X -= 1.f; 

      if (receiver.isKeyPressed(KEY_KEY_D)) 
        prot.X += 1.f; 

      // rotate the player

      core::vector3df rot( player->getRotation() ); 

      const f32 velocity = 1.f;
      rot += (prot * (velocity * elapsed)); 

      // might want to clamp rot.X to (0, 360].

      player->setRotation(rot); 
Travis
MerlinCAM
Posts: 6
Joined: Sun Jul 08, 2007 9:11 pm

Please Explain

Post by MerlinCAM »

if (receiver.isKeyPressed(KEY_KEY_W))
pmove.Z += 1.f;

if (receiver.isKeyPressed(KEY_KEY_S))
pmove.Z -= 1.f;

// move the player
core::vector3df pos( player->getPosition() );

// rotate the movement vector into parent coordinate system
player->getRelativeTransformation().rotateVect(pmove);

I am unclear why in your code you are only moving Z. Most of the time you need to move both X and Z.

Is that what getRelativeTransformation().rotateVect(pmove) does?
I am looking at the documentation for this function, and I do not really understand what it is doing in your example, could you elaborate?
omar shaaban
Posts: 616
Joined: Wed Nov 01, 2006 6:26 pm
Location: Cairo,Egypt
Contact:

Post by omar shaaban »

well u will find a post by me about this code just search!! the forum and u will find it
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The rotateVect() code removes the need to do the sin/cos to figure out how much to move in each direction. If pmove.Z=1, and the player node is rotated 45 degrees on the Y axis, after calling rotateVect(), pmove will be something like (.707, 0, .707), and the node will be moved in both the X and Z directions.

Travis
Post Reply