• Advertisement
Sign in to follow this  

Implementing a first person camera: directx10, c++

This topic is 1897 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

I have read many different articles on creating a first person camera. Most of them have been xna stuff. I am using c++ and have been going through Frank D. Luna's Directx 10 book. He does not cover first person camera's in that book. Anyway, I want to be able to control the camera with the mouse.

I've read that directInput is deprecated so I'm not using that. Is rawInput what I should be using or is what I have fine for mouse input?

Anyway, here is the section of code dealing with view transformations etc in relation to the camera. Everything else in the program works fine. It's taken directly from Luna's crate example and I'm just changing the view functionality. I believe once I get the rotation part figured out moving the camera around should be relatively easy.

int xCenter = (int)mClientWidth/2 + mWinPosX;
int yCenter = (int)mClientHeight/2 + mWinPosY;
D3DXVECTOR3 lUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f); //local up vector
D3DXVECTOR3 lDir = D3DXVECTOR3(0.0f, 0.0f, 1.0f); //local direction vector

if(curMousePoint.x != xCenter)
deltaX += (curMousePoint.x - xCenter)/500.0f;

//Yaw (around y axis)
D3DXMATRIX yRotMatrix;
D3DXMatrixRotationY(&yRotMatrix, deltaX);
D3DXVec3TransformCoord(&wDir, &lDir, &yRotMatrix); //wDir is world direction vector
if( curMousePoint.y != yCenter)
deltaY += (curMousePoint.y - yCenter)/500.0f;
//Pitch (around x axis)
//D3DXMATRIX xRotMatrix;
//D3DXMatrixRotationX(&xRotMatrix, deltaY);
//D3DXVec3TransformCoord(&wUp, &lUp, &xRotMatrix); //wUp is world up vector
target = mEyePos + wDir; //Should point in direction of target
D3DXMatrixLookAtLH(&mView, &mEyePos, &target, &lUp);

SetCursorPos((int)mClientWidth/2 + mWinPosX, (int)mClientHeight/2 + mWinPosY);

I haven't worried about over rotating yet.

What I would like to know is: is this along the right track? What should be changed?

I have the pitch portion commented out for simplicity

Share this post

Link to post
Share on other sites
Yeah, you are on the right track.
My camera code is structured like this:
cameraLookAt;//Set to the position you want your camera to point at(probably your characters head or something like that)
cameraPosition = D3DXVECTOR3(0, 0, -distance);//change distance to the distance between the camera target and the camera
//now rotate cameraPosition using deltaY and deltaX
cameraPosition += cameraLookAt; Edited by Such1

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement