Sign in to follow this  

Implementing a first person camera: directx10, c++

This topic is 1892 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.

[CODE]
GetCursorPos(&curMousePoint);
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);
[/CODE]

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
than:
cameraPosition += cameraLookAt; Edited by Such1

Share this post


Link to post
Share on other sites

This topic is 1892 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.

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