Jump to content
  • Advertisement


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


An important thing - rotating the camera

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

Hello!!! I''m creating now a computer game using DirectX (C++). I want a player (camera) to be in the tunnel and to be able to turn. However, when I use standard D3DXMatrixRotationY function, the camera is rotated around a circle - it doesn''t turn in certain place. So, how can I allow a player to turn without rotating camera around a circle? P.S. Earlier I thought that it would be good to rotate the camera first and then translate it to desired place. However, I don''t know how to do this. Anyway, if you have any idea, include a small example in your answer.

Share this post

Link to post
Share on other sites
You were right, Just rotate it first and then translate it. Here's the code I use. You can translate and rotate any way you want and you won't run into any problems (especially no damn gimbal lock ) :

D3DXMATRIX YawMat, PitchMat, RollMat;

Up_Vector = D3DXVECTOR3(0,1,0);
Right_Vector = D3DXVECTOR3(1,0,0);
Look_Vector = D3DXVECTOR3(0,0,1);

D3DXMatrixRotationAxis(&YawMat, &Up_Vector, Yaw);
D3DXVec3TransformCoord(&Look_Vector, &Look_Vector, &YawMat);
D3DXVec3TransformCoord(&Right_Vector, &Right_Vector, &YawMat);

D3DXMatrixRotationAxis(&PitchMat, &Right_Vector, Pitch);
D3DXVec3TransformCoord(&Look_Vector, &Look_Vector, &PitchMat);
D3DXVec3TransformCoord(&Up_Vector, &Up_Vector, &PitchMat);

D3DXMatrixRotationAxis(&RollMat, &Look_Vector, Roll);
D3DXVec3TransformCoord(&Up_Vector, &Up_Vector, &RollMat);
D3DXVec3TransformCoord(&Right_Vector, &Right_Vector, &RollMat);


ViewMat._11 = Right_Vector.x; ViewMat._12 = Up_Vector.x; ViewMat._13 = Look_Vector.x;
ViewMat._21 = Right_Vector.y; ViewMat._22 = Up_Vector.y; ViewMat._23 = Look_Vector.y;
ViewMat._31 = Right_Vector.z; ViewMat._32 = Up_Vector.z; ViewMat._33 = Look_Vector.z;
ViewMat._41 = - ((X * Right_Vector.x) + (Y * Right_Vector.y) + (Z * Right_Vector.z));
ViewMat._42 = - ((X * Up_Vector.x) + (Y * Up_Vector.y) + (Z * Up_Vector.z));
ViewMat._43 = - ((X * Look_Vector.x) + (Y * Look_Vector.y) + (Z * Look_Vector.z));

D3DDevice->SetTransform(D3DTS_VIEW, &ViewMat);

Yaw, Pitch, Roll, and X, Y, Z are the variables that you need to supply. You may also want to store a copy of the Up_Vector, Right_Vector, and Look_Vector *AFTER* they have been transformed by your Yaw, Pitch, and Roll values. Then you can move using them, ie: to move in the direction you're LOOKING do this:
X += speed * Look_Vector.x;
Y += speed * Look_Vector.y;
Z += speed * Look_Vector.z;

Think of the Up_Vector, Right_Vector, and Look_Vector as the 3 axis of your object's coordinate system. When you want it to change orientation relative to the entire world you rotate those 3 axis (basically rotating the object coordinate system within a bigger world coordinate system).

Hope that helps a little. I don't know if this is the most efficient way to handle transformations but it works and it's not nearly as complicated as quaternions.


| particlefield.com |

[edited by - atreyu on June 1, 2002 3:02:15 AM]

Share this post

Link to post
Share on other sites

  • 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!