Getting "forward" vector from transform
Getting "forward" vector from transform
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?
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?
Code: Select all
transform.getTranslation().normalize();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".Ion Dune wrote:Is that what you're looking for?Code: Select all
transform.getTranslation().normalize();
Remember to always check the docs!
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.
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);
}
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.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);
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.
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;
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;
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.Seven wrote:paranoid? I dont think so. Why, what have you heard?
Assuming your transformations aren't bogus, there should be no reason to normalize() the rows you are extracting from the transform.