Camera control in directx

Started by
5 comments, last by digital_maniak 15 years, 8 months ago
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...
Advertisement
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]
no..i asked about the camera movement. i did manage to set up the XBox controller already..
PS : thx for answering anyway
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]
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...
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...

This topic is closed to new replies.

Advertisement