# Camera control in directx

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

## Recommended Posts

Hi.i have such a small problem: the camera in positioned at 0.0f,0.0f,10.0f and it looks at 0.0f,0.0f,0.0f... how to control the camera movement with the xbox controller..... i configured xinput and i wish to move forward/backward/left/right with the left analog stick and to look around with right analog stick...help please...

##### Share on other sites
Are you asking how to read input from the XBox controller, or how to do camera movement?

If it's the former, the XNA docs are a good place to start (see here and here). Sorry, didn't notice you're using DirectX. In that case, see here.

If it's the latter, the answer is more complicated.

[Edited by - Gage64 on August 18, 2008 2:04:55 AM]

##### Share on other sites
PS : thx for answering anyway

##### Share on other sites
What you can do is keep track of the position as well as the X and Y rotation of your camera, and use those values to construct a view matrix every frame for your camera. First thing you'd do is update your X and Y rotation values based on the gamepad input:

// dt is the time elapsed since last frame, and // camRotSpeed is some constantcamRotY += (gamePad.sThumbRX / 32767.0f) * camRotSpeed * dt;camRotX += (gamePad.sThumbRY / 32767.0f) * camRotSpeed * dt;// Create a rotation matrix for the camera based on these values:D3DXMATRIX cameraTransform, rotX, rotY;D3DXMatrixRotationX(&rotX, camRotX);D3DXMatrixRotationY(&rotY, camRotY);cameraTransform = rotX * rotY;

Once you have this rotation matrix, you can calculate up, forward, and right vectors for the camera. These vectors will point in those direction based on the camera's current orientation, so you can use them to move the camera's position:

D3DXVECTOR3 upVec (0, 1, 0);D3DXVECTOR3 rightVec (1, 0, 0);D3DXVECTOR3 forwardVec (0, 0, 1);D3DXVec3TransformNormal(&upVec, &upVec, &cameraTransform);D3DXVec3TransformNormal(&rightVec , &rightVec , &cameraTransform);D3DXVec3TransformNormal(&forwardVec , &forwardVec , &cameraTransform);cameraPostion += rightVec * (gamePad.sThumbLX / 32767.0f) * camMoveSpeed * dt; cameraPostion += forwardVec * (gamePad.sThumbLY / 32767.0f) * camMoveSpeed * dt;

Once you have the rotation matrix and position, you can create a world matrix for the camera. The view matrix is then just the inverse of this matrix:

cameraTransform._41 = cameraPosition.x;cameraTransform._42 = cameraPosition.y;cameraTransform._43 = cameraPosition.z;D3DXMATRIX viewMatrix;D3DXMatrixInverse(&viewMatrix, NULL, &cameraTransform);

[Edited by - MJP on August 19, 2008 12:37:20 PM]

##### Share on other sites
the overloaded operator "+=" /* in cameraPostion.x += rightVec * (gamePad.sThumbLX / 32767.0f) * camMoveSpeed * dt; */ returns a D3DXVECTOR3 which causes an conversion error...and all i see is a black screen...

##### Share on other sites
Oops, I wrote out my code wrong. Just remove the ".x" and ".y" from cameraPosition and it should work OK. Sorry about that.

thx a lot...

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 11
• 15
• 11
• 11
• ### Forum Statistics

• Total Topics
634149
• Total Posts
3015830
×