DirectX First Person Camera Help

Started by
1 comment, last by Anddos 11 years, 7 months ago
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);
:)
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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

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

This topic is closed to new replies.

Advertisement