Jump to content
  • Advertisement
Sign in to follow this  
Medo Mex

Rotation Problem

This topic is 968 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

I have a vehicle and trying to rotate the vehicle gun to look at the player.

 

The gun rotate to look at the player as expected but after the vehicle itself rotate I get the gun looking at wrong direction.

 

How do I resolve that?

 

Here is how I get the final tank gun world matrix:

FinalWorldMatrix = VehicleWorldMatrix * GunWorldMatrix;

and here is how I rotate the gun to look at the player:

D3DXQUATERNION NewQuaternion;
D3DXQuaternionIdentity(&NewQuaternion);

D3DXVECTOR3 forwardVector = playerPosition - entityPosition;
D3DXVec3Normalize(&forwardVector, &forwardVector);

D3DXVECTOR3 forward(0.0f, 0.0f, 1.0f);
float dot = D3DXVec3Dot(&forward, &forwardVector);

if (abs(dot - (-1.0f)) < 0.000001f)
{
    D3DXQuaternionRotationAxis(&NewQuaternion, &D3DXVECTOR3(0.0f, 1.0f, 0.0f), 3.1415926535897932f);
} else if (abs(dot - (1.0f)) < 0.000001f) {
    D3DXQuaternionIdentity(&NewQuaternion);
} else {
    float rotationAngle = (float)acos(dot);
    D3DXVECTOR3 rotationAxis;
    axis = cross(forward, forwardVector);
    D3DXVec3Normalize(&axis, &axis);
    D3DXQuaternionRotationAxis(&NewQuaternion, &axis, rotationAngle);
}

SetRotation(Index, NewQuaternion); // <-- This line will set the rotation in GunWorldMatrix
Edited by Medo3337

Share this post


Link to post
Share on other sites
Advertisement
Since your gun orientation is relative to the vehicle, you need to make it compute its orientation based on the player's position from the vehicle's perspective.

Multiply playerPosition by the inverse of VehicleWorldMatrix before calculating forwardVector.

Share this post


Link to post
Share on other sites

@Nypyren: I tried to do the following but now the gun is looking at one direction most of the time:

D3DXMATRIX WorldMatrixInversed;
D3DXMatrixInverse(&WorldMatrixInversed, NULL, &VehicleWorldMatrix);
D3DXVec3TransformCoord(&playerPosition, &playerPosition, &WorldMatrixInversed);

D3DXVECTOR3 forwardVector = playerPosition - entityPosition;
// ...
Edited by Medo3337

Share this post


Link to post
Share on other sites
Hmm. entityPosition represents the vehicle's position, right? I think, since your playerPosition is now in the vehicle's space, you don't need to subtract entityPosition anymore:

D3DXVECTOR3 forwardVector = playerPosition;

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

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!