#### Archived

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

# An important thing - rotating the camera

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

## 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 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;	D3DXMATRIX ViewMat;	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);	D3DXMatrixIdentity(&ViewMat);	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.

Cheers,
-Ben

| particlefield.com |

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

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013378
×