Sign in to follow this  
GeekPlusPlus

Camera Control Matrices (Like FPSs) *Solved*

Recommended Posts

Hello, Does anyone have handy or know the matrix transformations that would be required to move the camera around a 3d world using keyboard (AWSD) for camera location and mouse for look coordinates? Obviously I would record the mouse movements and keyboard keypress and hold events. I'd store the input value into variables, but how do I translate that data into a new camera matrix? I'm using C# but talking in C++ is fine as well. Hope someone can help - GeekPlusPlus [Edited by - GeekPlusPlus on June 3, 2005 1:38:29 AM]

Share this post


Link to post
Share on other sites
I have a camera class where I keep track of heading and pitch. I update those value when the mouse if moved.

Here is code I use for WASD

void CCamera::MoveFoward(void)
{
if(FreeLook == true)
{
CamX = ( Speed * sinf(Heading) * cosf(Pitch) ) + CamX;
CamY = ( Speed * sinf(Pitch) ) + CamY;
CamZ = ( Speed * cosf(Heading) * cosf(Pitch) ) + CamZ;
}
else
{
CamX += Speed * sinf(Heading);
CamZ += Speed * cosf(Heading);
}

return;
}

void CCamera::MoveBackward(void)
{
if(FreeLook == true)
{
CamX = CamX - ( Speed * sinf(Heading) * cosf(Pitch) );
CamY = CamY - ( Speed * sinf(Pitch) );
CamZ = CamZ - ( Speed * cosf(Heading) * cosf(Pitch) );
}
else
{
CamX -= Speed * sinf(Heading);
CamZ -= Speed * cosf(Heading);
}

return;
}

void CCamera::LeftStrafe(void)
{
CamX -= Speed * cosf(Heading);
CamZ += Speed * sinf(Heading);
return;
}

void CCamera::RightStrafe(void)
{
CamX += Speed * cosf(Heading);
CamZ -= Speed * sinf(Heading);
return;
}



And code I use for mouse input


void CCamera::MoveMouse(void)
{
float x = 0;
float y = 0;
x = static_cast<float> ( Input.m_mouseState.lX );
y = static_cast<float> ( Input.m_mouseState.lY );

Heading += D3DXToRadian( x / 7.0f );

Pitch -= D3DXToRadian( y / 7.0f );
Pitch = ReAlignPitch(Pitch);

SetDirection();

D3DXMATRIX ViewM;
D3DXMatrixLookAtLH(&ViewM, &D3DXVECTOR3( CamX, CamY, CamZ),
&D3DXVECTOR3( DirX, DirY, DirZ),
&D3DXVECTOR3( 0.0f, 1.0f, 0.0f));
Graphics.m_pd3dDevice->SetTransform(D3DTS_VIEW, &ViewM);
return;
}



Then I set my view matrix based on my Camera position and my heading and pitch.

Share this post


Link to post
Share on other sites
Here is it. In my system heading is 0 when facing down the z axis, 90 when facing to the right. Pitch is 0 when facing same level as current height and goes to 90 when looking higher up the y-axis.


void CCamera::SetDirection(void)
{
DirX = sinf(Heading) * cosf(Pitch) + CamX;
DirY = sinf(Pitch) + CamY;
DirZ = cosf(Heading) * cosf(Pitch) + CamZ;
return;
}

Share this post


Link to post
Share on other sites

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