Jump to content
  • Advertisement
Sign in to follow this  
Anddos

DirectX First Person Camera Help

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

ive tried to implement a first person camera in to my code with from what i have learnt from my books
can you tell me if there is anything wrong?, i press w the camera just keeps moving until i press another direction , then it continues in that direction , i want it so if you press w if just moves abit etc..


globals

//camera
D3DXVECTOR3 mPosW = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 mRightW = D3DXVECTOR3(1.0f, 0.0f, 0.0f);
D3DXVECTOR3 mUpW = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
D3DXVECTOR3 mLookW = D3DXVECTOR3(0.0f, 0.0f, 1.0f);
D3DXVECTOR3 dir(0.0f, 0.0f, 0.0f);
float pitch = 0.0f;
float yAngle = 0.0f;
D3DXMATRIX R;
D3DXMATRIX mView;


its all the message loop


while(TRUE)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(msg.message == WM_QUIT)
break;
detect_input(); // update the input data before rendering

if(keystate[DIK_ESCAPE] & 0x80)
PostMessage(hWnd, WM_DESTROY, 0, 0);

QueryPerformanceCounter((LARGE_INTEGER*)&currTimeStamp);
dt = (currTimeStamp - prevTimeStamp)*secsPerCnt;
render_frame();
prevTimeStamp = currTimeStamp;

//camera
if(keystate[DIK_W] & 0x80)
dir += mLookW;
if(keystate[DIK_S] & 0x80)
dir -= mLookW;
if(keystate[DIK_D] & 0x80)
dir += mRightW;
if(keystate[DIK_A] & 0x80)
dir -= mRightW;
D3DXVec3Normalize(&dir, &dir);
mPosW += dir*5.0f*dt;
// We rotate at a fixed speed.
pitch = mousestate.lY / 150.0f;
yAngle = mousestate.lX / 150.0f;

// Rotate camera's look and up vectors around the camera's right vector.
D3DXMatrixRotationAxis(&R, &mRightW, pitch);
D3DXVec3TransformCoord(&mLookW, &mLookW, &R);
D3DXVec3TransformCoord(&mUpW, &mUpW, &R);
// Rotate camera axes about the world's y-axis.
D3DXMatrixRotationY(&R, yAngle);
D3DXVec3TransformCoord(&mRightW, &mRightW, &R);
D3DXVec3TransformCoord(&mUpW, &mUpW, &R);
D3DXVec3TransformCoord(&mLookW, &mLookW, &R);
// Keep camera's axes orthogonal to each other and of unit length.
D3DXVec3Normalize(&mLookW, &mLookW);
D3DXVec3Cross(&mUpW, &mLookW, &mRightW);
D3DXVec3Normalize(&mUpW, &mUpW);
D3DXVec3Cross(&mRightW, &mUpW, &mLookW);
D3DXVec3Normalize(&mRightW, &mRightW);
// Fill in the view matrix entries.
float x = -D3DXVec3Dot(&mPosW, &mRightW);
float y = -D3DXVec3Dot(&mPosW, &mUpW);
float z = -D3DXVec3Dot(&mPosW, &mLookW);
mView(0,0) = mRightW.x;
mView(1,0) = mRightW.y;
mView(2,0) = mRightW.z;
mView(3,0) = x;
mView(0,1) = mUpW.x;
mView(1,1) = mUpW.y;
mView(2,1) = mUpW.z;
mView(3,1) = y;
mView(0,2) = mLookW.x;
mView(1,2) = mLookW.y;
mView(2,2) = mLookW.z;
mView(3,2) = z;
mView(0,3) = 0.0f;
mView(1,3) = 0.0f;
mView(2,3) = 0.0f;
mView(3,3) = 1.0f;
}


in the render function


d3ddev->SetTransform(D3DTS_VIEW, &mView);
Edited by Anddos

Share this post


Link to post
Share on other sites
Advertisement

i press w the camera just keeps moving until i press another direction , then it continues in that direction , i want it so if you press w if just moves abit etc..

Do you ever set dir to a zero vector once you're done pressing movement keys? Every iteration of your loop you call mPosW += dir (with dt modifier). That will continue to move you in SOME direction as long as dir has a non-zero value. I'd recommend either zeroing out dir at the end of the loop (or any time after movement has been applied), or checking for a "key was released" message and stopping movement associated with the released key that way.

Share this post


Link to post
Share on other sites
thanks il try that out
do you have any idea how to make a weapon move with the camera, i thought it was world*proj Edited by Anddos

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!