Moving node in the corect direction

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
MasterM
Posts: 128
Joined: Sat Oct 20, 2007 2:38 am
Location: netherlands antilles, Curacao

Moving node in the corect direction

Post 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.
C++ is not the Magdalena...it takes patience...shes like the well aged prostitute, it takes years to learn her tricks! she is cruel...laughs at you when you are naked...
Life of a programmer == Const abuse
cobra
Posts: 371
Joined: Fri Jan 23, 2009 2:56 am
Location: United States
Contact:

Post 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]
Josiah Hartzell
Image
MasterM
Posts: 128
Joined: Sat Oct 20, 2007 2:38 am
Location: netherlands antilles, Curacao

Post by MasterM »

Thanks, it works :)
Ill look more into matrices to fully understand the code :)
C++ is not the Magdalena...it takes patience...shes like the well aged prostitute, it takes years to learn her tricks! she is cruel...laughs at you when you are naked...
Life of a programmer == Const abuse
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post 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.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
Post Reply