Sign in to follow this  
Tano_ITA

[DX9] Calculate the direction

Recommended Posts

Hello to everyone,
i've my object into my 3d scene, i want to translate it on "Forward Vector". So the formula for the translation is


object_pos += direction*velocity;


I've the Pitch, Roll and Yaw of my game object. How can i calculate the direction vector that object is facing?

Thank for your suggests and sorry for my bad english.

Share this post


Link to post
Share on other sites
If your up axis is z then it's D3DXVector3 direction(cos(yaw), sin(yaw), 0);
But most likely it's y so D3DXVector3 direction(cos(pitch), 0, sin(pitch));

Give few tries and it'll sort out.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tano_ITA


If i've my object with Yaw = 0.0f, Pitch = 0.0f, Roll = 0.0f and i apply that. I've a vector like this (1, 0, 0). And the object translate on X axis..

Wher i wrong?
I'm not sure if Ripiz' answer is entirely correct, but the short answer to your question is, build a matrix from your yaw, pitch, and roll angles, and then extract the direction vectors directly from the matrix (they'll be in the first three rows of the matrix).

Share this post


Link to post
Share on other sites
I've do something like this:

D3DXVECTOR3 GameObject::GetDirection()
{
D3DXMATRIX mxRotation;
D3DXMatrixRotationYawPitchRoll( &mxRotation, transform.yaw, transform.pitch, transform.roll );


D3DXVec3TransformNormal( &transform.direction, &transform.direction, &mxRotation );
D3DXVec3Normalize(&transform.direction, &transform.direction);
return transform.direction;
}


But it works fine only when i go on the Z direction, i will try to use the first row of matrix.

Share this post


Link to post
Share on other sites
Quote:
Original post by Tano_ITA
I've do something like this:

D3DXVECTOR3 GameObject::GetDirection()
{
D3DXMATRIX mxRotation;
D3DXMatrixRotationYawPitchRoll( &mxRotation, transform.yaw, transform.pitch, transform.roll );


D3DXVec3TransformNormal( &transform.direction, &transform.direction, &mxRotation );
D3DXVec3Normalize(&transform.direction, &transform.direction);
return transform.direction;
}


But it works fine only when i go on the Z direction, i will try to use the first row of matrix.
Assuming the object orientation is built from those angles as well, you don't want to transform the existing direction vectors by the rotation transform, but rather the cardinal basis vectors ((1, 0, 0), (0, 1, 0), and (0, 0, 1)). Of course that's the same as simply extracting the direction vectors from the first three rows of the matrix, which again is what I would recommend.

Share this post


Link to post
Share on other sites
I can't understand why, but i've a problem with my method. The direction change at every Update() ! The code for my render is this:


void GameObject::Update()
{
D3DXMatrixScaling(&m_mat_scale, transform.scale.x, transform.scale.y, transform.scale.z);

D3DXMatrixRotationYawPitchRoll( &m_Rotation, transform.yaw, transform.pitch, transform.roll );
D3DXVec3TransformNormal( &transform.direction, &transform.direction, &m_Rotation );
D3DXVec3Normalize(&transform.direction, &transform.direction);

D3DXMatrixTranslation(&m_mat_position, transform.position.x, transform.position.y, transform.position.z);

NinjaEngineDevice::get_ist()->m_d3d_device->SetTransform(D3DTS_WORLD, &( m_Rotation * m_mat_position * m_mat_scale));


for(m_itr = m_GameComponents.begin(); m_itr != m_GameComponents.end(); m_itr++)
{
/*(*m_itr).second->Update();*/
(*m_itr)->Update();
}
}


and my code for GetDirection()


D3DXVECTOR3 GameObject::GetDirection()
{
D3DXMATRIX mxRotation;
D3DXMatrixRotationYawPitchRoll( &mxRotation, transform.yaw, transform.pitch, transform.roll );

D3DXVec3TransformNormal( &transform.direction, &transform.direction, &mxRotation );
D3DXVec3Normalize(&transform.direction, &transform.direction);
return transform.direction;
}


So transform.direction is different from previous frame! I don't rotate or translate my gameobject, so i can't understand why with the same Roll, Pitch and Yaw i've different results!

Any idea? Thanks a lot.

Share this post


Link to post
Share on other sites
Based on a quick look over your code, it looks to me like you're transforming the direction vector each update. If that is in fact what you're doing, the direction vector may change even if the orientation doesn't.

From my earlier post:
Quote:
Assuming the object orientation is built from those angles as well, you don't want to transform the existing direction vectors by the rotation transform, but rather the cardinal basis vectors ((1, 0, 0), (0, 1, 0), and (0, 0, 1)). Of course that's the same as simply extracting the direction vectors from the first three rows of the matrix, which again is what I would recommend.
Maybe I'm misunderstanding what you're wanting, but if the goal is to acquire the local basis vectors of the object, you can just extract them directly from the orientation matrix as I mentioned previously.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this