Page 1 of 1

Rotate, and then, move

Posted: Thu Dec 27, 2007 5:02 pm
by hellix
Hi (sorry for my bad english)

I am developing a small game where you control a spaceship with the keyboard. You can rotate it and it can do an acceleration.

So, the mesh rotate correctly, but, when I want it to move after, it goes in a puzzling direction (not the good one).

Can you help me ? I am searching for this since 11.00 am.

Thanks.

Posted: Thu Dec 27, 2007 5:10 pm
by konrad
Use this code: http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=25410
when you can controlling Nodes ;p

Code: Select all

if(Keys[KEY_UP])
{
  core::vector3df _pos=Node->getPosition();
  _pos.Z+=1;
  Node->setPosition(_pos);
}

Posted: Thu Dec 27, 2007 5:12 pm
by hellix
I know how to move it with the keyboard, and it works.
However, the mesh don't accelerate in the good direction. Always in the same. I want it to go in the direction where it faces (when I rotate it, it will go in another direction than the previous one (because it faces another point in the space)).

Posted: Thu Dec 27, 2007 5:15 pm
by konrad
You must use 'cos' and 'sin':

Code: Select all

  _pos.X+=_speed*cos((_rot.Y+90)*3.14/180);
  _pos.Z-=_speed*sin((_rot.Y+90)*3.14/180);

Posted: Thu Dec 27, 2007 5:15 pm
by hellix
if(Keys[KEY_UP])
{
core::vector3df _pos=Node->getPosition();
_pos.Z+=1;
Node->setPosition(_pos);
}
I know how to do that. In fact, I want to rotate the axis.

COS & SIN
I tested it. It don't works.

Posted: Thu Dec 27, 2007 5:20 pm
by konrad
Try this:

Code: Select all

core::vector3df _pos=Node->getPosition();
core::vector3df _rot=Node->getRotation();
if(Keys[KEY_UP])
{
  _pos.X+=5*cos((_rot.Y+90)*3.14/180);
  _pos.Z-=5*sin((_rot.Y+90)*3.14/180);
}
if(Keys[KEY_DOWN])
{
  _pos.X+=5*cos((_rot.Y-90)*3.14/180);
  _pos.Z-=5*sin((_rot.Y-90)*3.14/180);
}
if(Keys[KEY_RIGHT]) { _rot.Y+=5; }
if(Keys[KEY_LEFT]) { _rot.Y-=5; }
Node->setPosition(_pos);
Node->setRotation(_rot);

Posted: Thu Dec 27, 2007 6:16 pm
by vitek
I personally think it is much cleaner to use rotation matrices here instead of sin/cos and deg-to-rad conversions. All you need to do is find out what direction 'forward' is, and then move the node along that vector. Code to do this has been posted many times before. Here are links to two threads that do it...

http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=22794
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=16988

Posted: Fri Dec 28, 2007 3:54 pm
by dudMaN
arras has made a helpful function for this.

Paste to the top of your source file:

Code: Select all

//--- move ship acording to its rotation ---
void move(irr::scene::ISceneNode *node, //node to move
            irr::core::vector3df vel) //velocity vector
            // for example to move node 10 units forward use vector3df(0,0,10)
{
    irr::core::matrix4 m;
    m.setRotationDegrees(node->getRotation());
    m.transformVect(vel);
    node->setPosition(node->getPosition() + vel);
    node->updateAbsolutePosition();
}

and then use like this:

Code: Select all

 move(node, vector3df(0.0,1));
and it will move 0,0,1 in the direction it's facing.

-dudMan