Sign in to follow this  

Camera control in directx

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this