Jump to content
• Advertisement

#### Archived

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

# Rotating object relative to itself?

This topic is 6275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## 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

##### Share on other sites
Advertisement
In matrix multiplication the order of multiplication matters.
Put your rotation first in your multiply.

D3DXMatrixMultiply(&matObj, &matObjRotation, &matObjTranslation);

Jack

#### Share this post

##### Share on other sites
http://therabbithole.redback.inficad.com

Hit the "Tricks" section.

There''s a tutorial on this.

Ben

#### Share this 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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
4. 4
5. 5
Rutin
11
• Advertisement

• 12
• 16
• 9
• 14
• 10
• ### Forum Statistics

• Total Topics
632659
• Total Posts
3007689
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!