Archived

This topic is now archived and is closed to further replies.

Manteau

Rotating object relative to itself?

Recommended Posts

Hi, How would I go about rotating an object relative to itself? At the moment, I''m using D3DXMatrixRotationX/Y/Z to rotate an object, but this does it in relation to the world matrix. For example, an aircraft pitching up would pitch up in relation to the plane of the wings, so the direction of pitch would change depending on the bank angle. Is there any way to convert between world and object matrixes? Cheers, Manteau

Share this post


Link to post
Share on other sites
Store your object's orientation as a quaternion and rotate the quaternion using the following functions:

D3DXVECTOR3 * MathsTransformVector(D3DXQUATERNION *pOrientation, D3DXVECTOR3 *pAxis)
{
D3DVECTOR AxisVector;
D3DXMATRIX Matrix;

D3DXMatrixRotationQuaternion(&Matrix, pOrientation); // Build a matrix from the quaternion.

AxisVector.x=pAxis->x*Matrix._11+pAxis->y*Matrix._21+pAxis->z*Matrix._31+Matrix._41; // Transform the queried axis vector by the matrix.
AxisVector.y=pAxis->x*Matrix._12+pAxis->y*Matrix._22+pAxis->z*Matrix._32+Matrix._42;
AxisVector.z=pAxis->x*Matrix._13+pAxis->y*Matrix._23+pAxis->z*Matrix._33+Matrix._43;

memcpy(pAxis, &AxisVector, sizeof(AxisVector)); // Copy axis.

return(pAxis);
}


bool MathsRotateAxis(D3DXQUATERNION *pOrientation, D3DXVECTOR3 *pAxis, float Angle)
{
bool Success=false;

if(pOrientation && pAxis)
{
Success=true;
D3DXQUATERNION Rotation;

D3DXQuaternionRotationAxis(&Rotation, MathsTransformVector(pOrientation, pAxis), D3DXToRadian(Angle));
*pOrientation*=Rotation;
}

return(Success);
}


bool MathsRotateXAxis(D3DXQUATERNION *pOrientation, float Angle)
{
bool Success=false;

if(pOrientation)
{
Success=true;
D3DXQUATERNION Rotation;

D3DXQuaternionRotationAxis(&Rotation, MathsTransformVector(pOrientation, &D3DXVECTOR3(1.0f, 0.0f, 0.0f)), D3DXToRadian(Angle));
*pOrientation*=Rotation;
}

return(Success);
}


bool MathsRotateYAxis(D3DXQUATERNION *pOrientation, float Angle)
{
bool Success=false;

if(pOrientation)
{
Success=true;
D3DXQUATERNION Rotation;

D3DXQuaternionRotationAxis(&Rotation, MathsTransformVector(pOrientation, &D3DXVECTOR3(0.0f, 1.0f, 0.0f)), D3DXToRadian(Angle));
*pOrientation*=Rotation;
}

return(Success);
}


bool MathsRotateZAxis(D3DXQUATERNION *pOrientation, float Angle)
{
bool Success=false;

if(pOrientation)
{
Success=true;
D3DXQUATERNION Rotation;

D3DXQuaternionRotationAxis(&Rotation, MathsTransformVector(pOrientation, &D3DXVECTOR3(0.0f, 0.0f, 1.0f)), D3DXToRadian(Angle));
*pOrientation*=Rotation;
}

return(Success);
}


Then use D3DXMatrixRotationQuaternion() to build a rotation matrix. Hope it helps.

Justin.

P.S. The text formatting seems to have disappeared from the source code.

Edited by - Justin Nixon on July 22, 2001 9:01:50 AM

Share this post


Link to post
Share on other sites