Jump to content
  • Advertisement
Sign in to follow this  

Camera control in directx

This topic is 3568 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.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 this post

Link to post
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 this post

Link to post
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 constant
camRotY += (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 this post

Link to post
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...
help please...

Share this post

Link to post
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.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!