Getting "forward" vector from transform

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
ice9
Posts: 28
Joined: Sun Oct 26, 2008 4:09 am

Getting "forward" vector from transform

Post by ice9 »

Often it's useful to get the "forward" vector for an object. This is the unit vector that points forward from the object... you can think of it as where the object is looking.

I know I can get it in this fashion:

vector3df fvec(0.0f, 0.0f, 1.0f);
xform.rotateVect(fvec);

But I don't want to do math like that when the forward vector is sitting happily in the third row of the rotation matrix (contained in the transform).

I want to know if I'm missing an easy way to get this if I have the transform of the object?
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

Code: Select all

transform.getTranslation().normalize();
Is that what you're looking for?

Remember to always check the docs!
ice9
Posts: 28
Joined: Sun Oct 26, 2008 4:09 am

Post by ice9 »

Ion Dune wrote:

Code: Select all

transform.getTranslation().normalize();
Is that what you're looking for?

Remember to always check the docs!
That will give the unit vector that points from the origin towards the object position. I want the unit vector that points out from the object, where it is facing "forward".

For example, if it was a vehicle with the wheels unturned, it would be the direction the vehicle would travel when the gas pedal is pressed.
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

Code: Select all

	virtual vector3df GetIrrIn()
	{
		if (m_AnimatedSceneNode)
		{
			matrix4 mat = m_AnimatedSceneNode->getRelativeTransformation();
			vector3df in(mat[8],mat[9],mat[10]); 
			in.normalize();
			return in;
		}
		else
		if (m_Node)
		{
			matrix4 mat = m_Node->getRelativeTransformation();
			vector3df in(mat[8],mat[9],mat[10]); 
			in.normalize();
			return in;
		}
		return vector3df(0,0,0);
	}

	virtual vector3df GetIrrUp()
	{
		if (m_AnimatedSceneNode)
		{
			matrix4 mat = m_AnimatedSceneNode->getRelativeTransformation();
			vector3df up(mat[4],mat[5],mat[6]); 
			up.normalize();
			return up;
		}
		else
		if (m_Node)
		{
			matrix4 mat = m_Node->getRelativeTransformation();
			vector3df up(mat[4],mat[5],mat[6]); 
			up.normalize();
			return up;
		}
		return vector3df(0,0,0);
	}

	virtual vector3df GetIrrLeft()
	{
		if (m_AnimatedSceneNode)
		{
			matrix4 mat = m_AnimatedSceneNode->getRelativeTransformation();
			vector3df left(mat[0],mat[1],mat[2]); 
			left.normalize();
			return left;
		}
		else
		if (m_Node)
		{
			matrix4 mat = m_Node->getRelativeTransformation();
			vector3df left(mat[0],mat[1],mat[2]); 
			left.normalize();
			return left;
		}
		return vector3df(0,0,0);
	}
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

p.s.

it would be nice if these were built into the scenenodes themselves
bitplane
Admin
Posts: 3204
Joined: Mon Mar 28, 2005 3:45 am
Location: England
Contact:

Post by bitplane »

Only the person who made the mesh can say which way forward is.

Code: Select all

vector3df forwards(0,0,1); // positive Z is forward for this mesh
node->getRelativeTransformation().rotateVect(forwards);
Submit bugs/patches to the tracker!
Need help right now? Visit the chat room
ice9
Posts: 28
Joined: Sun Oct 26, 2008 4:09 am

Post by ice9 »

bitplane wrote:Only the person who made the mesh can say which way forward is.

Code: Select all

vector3df forwards(0,0,1); // positive Z is forward for this mesh
node->getRelativeTransformation().rotateVect(forwards);
By forward, I mean in the positive-Z direction, in the local space of the mesh.

I suppose an artist could orient a mesh in an arbitrary way for each model in the game, but that would quickly turn into a mess.
Last edited by ice9 on Wed Nov 12, 2008 1:00 am, edited 1 time in total.
ice9
Posts: 28
Joined: Sun Oct 26, 2008 4:09 am

Post by ice9 »

Seven -- your code does what I'm talking about, although you do have a healthy dose of paranoia by calling normalize() after getting the elements from the transformation.

By definition if your transforms are valid the result will already be normalized.

I was hoping for some easy access to the forward/up/left vectors directly from the transformation, along the lines of a union. So you could do something like:

matrix4 m;
vector3df new_pos = old_pos + m.fvec * time_delta;
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

as I said :)
Seven wrote:p.s.

it would be nice if these were built into the scenenodes themselves
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

paranoid? I dont think so. Why, what have you heard?
ice9
Posts: 28
Joined: Sun Oct 26, 2008 4:09 am

Post by ice9 »

Seven wrote:paranoid? I dont think so. Why, what have you heard?
Rotation matricies are orthogonal, and one of the properties of an orthogonal matrix is that the magnitude of any row or column will be one.

Assuming your transformations aren't bogus, there should be no reason to normalize() the rows you are extracting from the transform.
Post Reply