im creating a 3d room which you can walk around with a first person camera
i have defined the position of the eyeX eyeY and eyeZ as shown below:
float eyeX = 0;
float eyeY = 100;
float eyeZ = 75;
here is my lookat code:
D3DXMatrixLookAtLH( &g_matView, &D3DXVECTOR3( eyeX, eyeY,eyeZ ),
&D3DXVECTOR3( LookatX, LookatY, LookatZ ),
&D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
g_pd3dDevice->SetTransform( D3DTS_VIEW, &g_matView );
my code allows me to move the camera around but not like a first person camera and i am struggling to achieve this.
// forwards = UP ARROW
// Backwards = DOWN ARROW
// rotate left = LEFT ARROW
// rotate right = RIGHT ARROW
case WM_KEYDOWN:
{
// Handle any non-accelerated key commands
switch (wparam)
{
case VK_RIGHT:
if(eyeX >=-50)
{
--eyeX;
}
return (0);
case VK_LEFT:
if(eyeX <=50)
{
++eyeX;
}
return (0);
case VK_DOWN:
if(eyeZ >=-50)
{
--eyeZ;
}
return (0);
case VK_UP:
if(eyeZ <=50)
{
++eyeZ;
}
return (0);
case VK_SPACE:
if(eyeY >=-50)
{
--eyeY;
}
return (0);
case VK_SHIFT:
if(eyeY <=50)
{
++eyeY;
}
return (0);
}
break;
}
LookatX = eyeX + 5.0f;
LookatY = eyeY;
LookatZ = eyeZ;
case WM_DESTROY:
{
// kill the application
PostQuitMessage(0);
return(0);
}
default:
break;
} // end switch
could anyone suggest some changes which would allow me to move around my room like a first person camera?
Instead of using D3DXMatrixLookAtLH, you could keep a view matrix.
Set up
(Note that I am making up names of functions, you might have to create these yourself)
Start with something like
Then every frame, you set the view matrix(just like you are doing with the matrix you are getting from MatrixLookAtLH)
Moving around
Normally modifing a model matrix is like this.
However, you manipulate the camera backwards
Simply run your switch statement, generate a transformation and update the view matrix.
e.g:
Formulas for genereating these matrices can be found on wikipedia(or DirectX might give them on its own).
(This is all based off of a simple software renderer I made a while ago, but it should apply the same to DirectX)
EDIT:
Oh, it looks like DirectX has all of these functions for you already in http://msdn.microsoft.com/en-us/library/windows/desktop/bb281696(v=vs.85).aspx