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.
Rotate, and then, move
Use this code: http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=25410
when you can controlling Nodes ;p
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.
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)).
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)).
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);
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);
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
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=22794
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=16988
arras has made a helpful function for this.
Paste to the top of your source file:
and then use like this:
and it will move 0,0,1 in the direction it's facing.
-dudMan
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));
-dudMan
Complete Irrlicht Beginners Tutorial
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=24898
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=24898