How do I use local Movement?[solved]

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
trollger
Posts: 84
Joined: Tue Jun 01, 2010 2:17 am
Location: At my computer

How do I use local Movement?[solved]

Post by trollger »

I Don't know how to use local Movement in irrlicht.
can anyone help me?
Last edited by trollger on Fri Jul 23, 2010 5:46 pm, edited 1 time in total.
terier
Posts: 34
Joined: Sun Oct 05, 2008 4:46 pm

Post by terier »

get position, increment, set position.
trollger
Posts: 84
Joined: Tue Jun 01, 2010 2:17 am
Location: At my computer

Post by trollger »

Thanks but I mean lets say I have a cube that is controlled with the WASD keys and it turns left and right depending on the mouses position. The problem I have right now is when I rotate the cube and then move it forward it does not move toward the new direction it continues to move toward its absolute XY and Z not local
terier
Posts: 34
Joined: Sun Oct 05, 2008 4:46 pm

Post by terier »

Have you ever heard of trigonometric functions?
You probably need something like this:

Code: Select all

vector3df pos = node->getPosition();
f32 rot = node->getRotation().Y;
f32 distance = 10.f; //or speed, if you'd like to put it that way

pos.X += cos(rot * DEGTORAD) * distance;
pos.Z += sin(rot * DEGTORAD) * distance;

node->setPosition(pos);
lazerblade
Posts: 194
Joined: Thu Mar 18, 2010 3:31 am
Contact:

Post by lazerblade »

Three functions which you should copy and use,


For translation:

Code: Select all


/*

  ==========

    translateNodeLoc -- translate a scene node locally

  ==========

*/

void translateNodeLoc(ISceneNode *node, vector3df amt)

{

    irr::core::matrix4 m;

    m.setRotationDegrees(node->getRotation());

    m.transformVect(amt);

    node->setPosition(node->getPosition() + vel);

    node->updateAbsolutePosition();

}

Example:

Code: Select all

translateNodeLoc(mySceneNode, vector3df(0,0,1));
Will move the node +1 on it's local Z axis.



For rotation:

Code: Select all


/*

  ==========

    getMatrixRotation -- gets the real rotation of a matrix

  ==========

 */

vector3df getMatrixRotation(const matrix4& mx)

{

   core::vector3df r;

   r.X = asinf(-mx(2,1));

   const f32 t = cosf(r.X);

   if (.001f < t) {

      r.Z = atan2f(mx(0,1), mx(1,1));

      r.Y = atan2f(mx(2,0), mx(2,2));

   }

   else {

      r.Z = atan2f(-mx (1,0), mx (0,0));

      r.Y = 0.f;

   }

   return (r * (180/PI));

}


/*

  ==========

    rotateNodeLoc -- rotate a scene node locally
  ==========

*/

void rotateNodeLoc(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( getMatrixRotation(m) );

    node->updateAbsolutePosition();

}


Example:

Code: Select all

rotateNodeLoc(mySceneNode, vector3df(1,0,0));
Will rotate the node +1 along it's local X axis.


Hope that helps.
LazerBlade

When your mind is racing, make sure it's not racing in a circle.

3d game engine: http://sites.google.com/site/lazerbladegames/home/ray3d
lazerBlade blog: http://lazerbladegames.blogspot.com/
trollger
Posts: 84
Joined: Tue Jun 01, 2010 2:17 am
Location: At my computer

Post by trollger »

awesome thanks :D
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

lazerblade wrote:Three functions which you should copy and use
hmm, I had a quick look at the functions and I wonder how this one works:

Code: Select all

void translateNodeLoc(ISceneNode *node, vector3df amt)

{

    irr::core::matrix4 m;

    m.setRotationDegrees(node->getRotation());

    m.transformVect(amt);

    node->setPosition(node->getPosition() + vel);

    node->updateAbsolutePosition();

}
I mean what do you use the matrix for (I see not using it) and how is vel defined ??? :shock:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

Acki, I think he may have meant to put amt instead of vel, that would make much more sense.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Lonesome Ducky wrote:Acki, I think he may have meant to put amt instead of vel, that would make much more sense.
my guess was that vel is the speed (velocity) and has to be multiplied by amt...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
lazerblade
Posts: 194
Joined: Thu Mar 18, 2010 3:31 am
Contact:

Post by lazerblade »

Oops... I think I made a mistake when I copied it over...

This is what that function was supposed to be:

Code: Select all

/*

  ==========

    translateNodeLoc -- translate a scene node locally

  ==========

*/

void translateNodeLoc(ISceneNode *node, vector3df amt)

{

    irr::core::matrix4 m;

    m.setRotationDegrees(node->getRotation());

    m.transformVect(amt);

    node->setPosition(node->getPosition() + amt);

    node->updateAbsolutePosition();

}
LazerBlade

When your mind is racing, make sure it's not racing in a circle.

3d game engine: http://sites.google.com/site/lazerbladegames/home/ray3d
lazerBlade blog: http://lazerbladegames.blogspot.com/
Lonesome Ducky
Competition winner
Posts: 1123
Joined: Sun Jun 10, 2007 11:14 pm

Post by Lonesome Ducky »

YES! I WIN! HAHAHAHA!
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Lonesome Ducky wrote:YES! I WIN! HAHAHAHA!
:cry:

:P :lol:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Post Reply