Page 1 of 1

Move in the direction your node is pointing? [SOLVED]

Posted: Thu Mar 24, 2011 10:18 pm
by sephoroph
Ok my question is how do you move a node locally instead of globally

This is taken from blender
Image

This is the code i use to move my nodes and its all globally :(

Code: Select all

// declared elsewhere in code.. just throwing this in to show what it is
IAnimatedMeshSceneNode* p;

vector3df pos = p->getPosition();
pos.X++;
p->setPosition(pos);

Thanks in advance!

Re: How do you move in the direction your node is pointing?

Posted: Thu Mar 24, 2011 10:20 pm
by randomMesh
http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=43205

Welcome to the forums and please try the search function next time.

Posted: Thu Mar 24, 2011 10:24 pm
by sephoroph
Thanks

Posted: Thu Mar 24, 2011 10:42 pm
by Seven

Code: Select all

vector3df getIrrIn(ISceneNode* node)
{
         matrix4 mat = node->getRelativeTransformation(); 
         vector3df in(mat[8],mat[9],mat[10]); 
         in.normalize(); 
         return in;
}

// move node 'forward' or "in" at speed
float speed = 10.0f;
vector3df in = getIrrIn(node);
vector3df pos = node->getPosition();
node->setPosition(pos + in * speed);

node is now moved 10 units in the direction it was 'looking'

Posted: Fri Mar 25, 2011 1:40 pm
by Lunacore
I use math functions for it:

Code: Select all

   vector3df Pos = Object->getPosition();
   vector3df Rot = Object->getRotation();
   u32 fps = driver->getFPS();

   //Calculate
   Pos.X -= sin(Rot.Y*PI/180)*60/fps; 
   Pos.Z -= cos(Rot.Y*PI/180)*60/fps;

   Object->setPosition(Pos);
   Object->updateAbsolutePosition();     

Posted: Fri Mar 25, 2011 1:51 pm
by ChaiRuiPeng
Lunacore wrote:I use math functions for it:

Code: Select all

   vector3df Pos = Object->getPosition();
   vector3df Rot = Object->getRotation();
   u32 fps = driver->getFPS();

   //Calculate
   Pos.X -= sin(Rot.Y*PI/180)*60/fps; 
   Pos.Z -= cos(Rot.Y*PI/180)*60/fps;

   Object->setPosition(Pos);
   Object->updateAbsolutePosition();     
just a tip: i think that

Code: Select all

driver->getFPS()
is only an estimate. not accurate. what about using an animator which passes the time in Ms? you could then multiply or whatever:

Code: Select all

pos.X += movespeedPerMsAbsX * timeMs; 

Posted: Fri Mar 25, 2011 2:17 pm
by Radikalizm
just a tip: i think that

Code: Select all

driver->getFPS()
is only an estimate. not accurate. what about using an animator which passes the time in Ms? you could then multiply or whatever:

Code: Select all

pos.X += movespeedPerMsAbsX * timeMs; 
This is correct, the FPS counter is updated every 1,5 seconds, so it is by no means fit to be used in these kinds of calculations, the animator solution is a much better option

Posted: Fri Mar 25, 2011 6:46 pm
by Lunacore
Ohh, I see in the API
virtual s32 irr::video::IVideoDriver::getFPS ( ) const [pure virtual]

Returns current frames per second value.
This value is updated approximately every 1.5 seconds and is only intended to provide a rough guide to the average frame rate.. It is not suitable for use in performing timing calculations or framerate independent movement
But what did you mean with "using using an animator which passes the time in Ms"? Which animator? o0

Posted: Fri Mar 25, 2011 7:26 pm
by ChaiRuiPeng
Lunacore wrote:Ohh, I see in the API
virtual s32 irr::video::IVideoDriver::getFPS ( ) const [pure virtual]

Returns current frames per second value.
This value is updated approximately every 1.5 seconds and is only intended to provide a rough guide to the average frame rate.. It is not suitable for use in performing timing calculations or framerate independent movement
But what did you mean with "using using an animator which passes the time in Ms"? Which animator? o0
i mean

create your own animator and add it to a scene node.

once you define whatever behavior.. well here let me show you really quickly

Code: Select all


//pseudo

class myAnimator : public ISceneNodeAnimator
{

custom_member_type* whatever;

f32 move_speed;

public:

   //these methods are required to animated properly
   animateNode(ISceneNode* node, u32 time_in_ms)
{

//do whatever you want to do to node position and/or rotation here

}

ISceneNodeAnimator* createClone(){return this*};

//you can also multi derive this from IEventReceiver, that is how fps cameras work.

};
now just add it to a ISceneNode

Code: Select all


myAnimator* anim;

IMeshSceneNode* node =IMeshSceneNode(mesh);

node->addAnimator(anim);

//now irrlicht will take care of animating that node in a timely manner.


Posted: Fri Mar 25, 2011 7:55 pm
by nespa
take here :

Code: Select all

ISceneNode* Node;
vector3df move(X,Y,Z);
matrix4 matrix;

matrix = Node->getRelativeTransformation();
matrix.transformVect(move);
Node->setPosition(move);
where X,Y,Z are the movements each frame relative to local axis;