Moving a SceneNode "forward"

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
Post Reply
Jedive
Posts: 146
Joined: Wed Apr 28, 2004 5:51 pm

Moving a SceneNode "forward"

Post by Jedive »

I am trying to move an scene node "forward". This is, that it advances to where it is looking.

I sucessfully made it in the Cipher engine, but porting the same code to IrrLicht does not work.

First, I made a vector3df_AddScale() function:

Code: Select all

void vector3df_AddScale(const vector3df& a, const vector3df& b, float scale, vector3df& result)
{
	result.X = a.X + scale * b.X;
	result.Y = a.Y + scale * b.Y;
	result.Z = a.Z + scale * b.Z;
}
Then, I have the MoveSceneNode() function which moves the node an amount in the x,y,z axis depending on where it is facing.

Code: Select all

void MoveSceneNode(ISceneNode* node, float x, float y, float z)
{
	matrix4 matrix = node->getRelativeTransformation();
	vector3df dest = node->getPosition();
	vector3df axis0(matrix.M[0], matrix.M[1], matrix.M[2]);
	vector3df axis1(matrix.M[3], matrix.M[4], matrix.M[5]);
	vector3df axis2(matrix.M[6], matrix.M[7], matrix.M[8]);
	vector3df_AddScale(dest, axis0, x, dest);
	vector3df_AddScale(dest, axis1, y, dest);
	vector3df_AddScale(dest, axis2, z, dest);
	node->setPosition(dest);
}
I have ported this directly from Cipher to IrrLicht, but it doesn't work. While IrrLicht uses Y for height, Cipher uses Z for height, so I guessed that I should swap some values. But I tried with different combinations with no luck.

Does anibody know what should I change to make it work?

Thanks in advance.
Jedive
Posts: 146
Joined: Wed Apr 28, 2004 5:51 pm

Post by Jedive »

Been looking for a bug for some time, and just oen minute after I post this, I found the answer!!!

Just have to look at the docs. I thought that the matrix4 class was a 4x3 matrix, but it is a 4x4 matrix.

Chaging the MoveSceneNode() function fixes it:

Code: Select all

void MoveSceneNode(ISceneNode* node, float x, float y, float z)
{
   matrix4 matrix = node->getRelativeTransformation();
   vector3df dest = node->getPosition();
   vector3df axis0(matrix.M[0], matrix.M[1], matrix.M[2]);
   vector3df axis1(matrix.M[5], matrix.M[5], matrix.M[6]);
   vector3df axis2(matrix.M[8], matrix.M[9], matrix.M[10]);
   vector3df_AddScale(dest, axis0, x, dest);
   vector3df_AddScale(dest, axis1, y, dest);
   vector3df_AddScale(dest, axis2, z, dest);
   node->setPosition(dest);
}
Now it works perfectly !!!
kaeles-notlogged

Post by kaeles-notlogged »

i dont mean to sound stupid, but could you explain how this works exactly, i suck at matrices, and i want to use it to move "forward"

i did it manually (getting rotation and adding to x and z according to the rotation) and it... uh... sucks...
so, it would be nice... if you could help... :-D
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

I found this code while searching the forum and copy-pasted it into my project. Works perfect! all you need is putting in this code:

Put this in your main game code:

Code: Select all

void vAddScale(const vector3df& a, const vector3df& b, float scale, vector3df& result)
{
	result.X = a.X + scale * b.X;
	result.Y = a.Y + scale * b.Y;
	result.Z = a.Z + scale * b.Z;
}

void nMove(ISceneNode* node, vector3df& length)
{
	matrix4 matrix = node->getRelativeTransformation();
	vector3df dest = length;
	matrix.transformVect(dest);
	node->setPosition( dest );
} 
And put this in your EventReceiver:

Code: Select all

nMove(node, core::vector3df (4,0,0));
This code should move your node, called "node", 4 units forward on the X coordinat. It might be in the wrong direction(depends on your camera position) but then your just change the last parameter to: core::vector3df (0,0,4)

btw: A friend of mine had a look at the original code and made some changes.

/Saku
Call me Wice, Miami Wice!
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

Ok, so I got a question,

you've got a node at position U.
you've got a target at position V.

How to get the angles, for the line U -> V???
Image
Jedive
Posts: 146
Joined: Wed Apr 28, 2004 5:51 pm

Post by Jedive »

That code looks more optimized than mine, Saku, but, what do you need the vAddScale() function for?
puh
Posts: 356
Joined: Tue Aug 26, 2003 3:53 pm

Post by puh »

How to get the angles, for the line U -> V???
Hi, spintz. I use the function getYXangle(core::vector3df r) by arras (thanks him!) http://irrlicht.sourceforge.net/phpBB2/ ... ngle#24911
It works for me! 8)
Spintz
Posts: 1688
Joined: Thu Nov 04, 2004 3:25 pm

Post by Spintz »

sweet that works! :)

I'm gonna add that as a function in NX++ for the line3d class!! Very helpful :)
Image
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

Hmm.. i guess you are rigth about the scale code.
As i said it was my friend who fixed the code so i didn't really think about that.
But thats just makes things better right? :wink:
Call me Wice, Miami Wice!
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

Happy that my code is so popular (just beware that sqrt inside ...quit slow)

To move node forward to where it is facing you can use another function I made: http://irrlicht.sourceforge.net/phpBB2/ ... php?t=4680 look at "Move ship in space" ...I think it is nearly the same Saku is using, just don't know why he need that scale thing???
Post Reply