Page 1 of 1

Moving node in the corect direction

Posted: Sat Jul 18, 2009 3:37 am
by MasterM
Hey guys,
After searching 2 hours on the forum and asking some people out i finally got somewhere...
I manage to let my node get effected by its rotation but its still not moving in the correct way...
here is the code i am using

Code: Select all

if (receiver.IsKeyDown(irr::KEY_KEY_W)) //if "W" is pressed on keyboard
        {
            vector3df oldpos = node->getPosition(); //gets our node rotation
            vector3df oldrot =node->getRotation().rotationToDirection(); //get our rotation of our node and convert it to direction(i gues)
            oldpos.X +=1; //add 1 X to our position
            node->setPosition(oldpos+oldrot); //adds the rotation and position together

        }
         
         if (receiver.IsKeyDown(irr::KEY_KEY_Q)) //if "Q" is pressed on keyboard
        {
            vector3df oldpos = node->getRotation();//gets our node rotation
            oldpos.Y +=1; //add 1 Y to our rotation

            node->setRotation(oldpos); //set our node rotation

        }

I don't know why the nodes act's strange cause i don't see nothing wrong with the code that it should behave like that.
Thanks in advanced.

Posted: Sat Jul 18, 2009 6:28 am
by cobra
To do that in Irrlicht without the use of a proper physics engine, you should use the 4x4 matrix.

Here is a code snippet to move it in the direction it is facing:

Code: Select all

void move(irr::scene::ISceneNode *node, irr::core::vector3df vel)
{
    irr::core::matrix4 m;
    m.setRotationDegrees(node->getRotation());
    m.transformVect(vel);
    node->setPosition(node->getPosition() + vel);
} 
And here to rotate it in its local space:

Code: Select all

void rotate(irr::scene::ISceneNode *node, irr::core::vector3df rot)
{
    irr::core::matrix4 m;
    m.setRotationDegrees(node->getRotation());
    irr::core::matrix4 n;
    n.setRotationDegrees(rot);
    m *= n;
    node->setRotation( m.getRotationDegrees() );
    node->updateAbsolutePosition();
} 

OR

Do it like this:

Code: Select all

myObject->setPosition(myObject->getRotation().rotationToDirection() * amountToMove)

Hope that helps you some.[/code]

Posted: Sat Jul 18, 2009 3:07 pm
by MasterM
Thanks, it works :)
Ill look more into matrices to fully understand the code :)

Posted: Sun Jul 19, 2009 6:35 pm
by MasterGod
Here's the all famous thread that answers this many-time-asked question:
http://irrlicht.sourceforge.net/phpBB2/ ... php?t=4680

It should have all you need.