Jump to content
  • Advertisement
Sign in to follow this  
GeekPlusPlus

Camera Control Matrices (Like FPSs) *Solved*

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

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
Advertisement
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
Hey, thanks Graham

That clears up a lot, but could you possibly explain what SetDirection() does to get the direction co-ordinates from the heading and pitch?

Thanks

- GeekPlusPlus

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!