Rotate, and then, move

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
hellix
Posts: 3
Joined: Thu Dec 27, 2007 4:59 pm

Rotate, and then, move

Post 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.
konrad
Posts: 15
Joined: Wed Oct 03, 2007 7:07 pm
Location: Poland

Post 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);
}
Last edited by konrad on Thu Dec 27, 2007 5:13 pm, edited 1 time in total.
hellix
Posts: 3
Joined: Thu Dec 27, 2007 4:59 pm

Post 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)).
konrad
Posts: 15
Joined: Wed Oct 03, 2007 7:07 pm
Location: Poland

Post 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);
hellix
Posts: 3
Joined: Thu Dec 27, 2007 4:59 pm

Post 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.
konrad
Posts: 15
Joined: Wed Oct 03, 2007 7:07 pm
Location: Poland

Post 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);
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post 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
dudMaN
Posts: 111
Joined: Fri Mar 02, 2007 6:37 pm

Post 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
Complete Irrlicht Beginners Tutorial
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=24898
Post Reply