Archived

This topic is now archived and is closed to further replies.

shakazed

Camera facing

Recommended Posts

I´m working on a fps (first person shooter) as a school project and have a question about moving around in the world. How would I do to get the camera to move to the point it´s facing? Like in most fps games you point the camera in one direction and when you push the forward key you move in that direction.
Bad Monkey Productions

Share this post


Link to post
Share on other sites
Hi,
Quiet simple really,
Heres some functions that will pretty much allow you to move in any direction

      
void CGame::LookUp()
{
if((m_Position.w += LOOK_RATE) <= 90)
m_Position.w += LOOK_RATE;
}

void CGame::LookDown()
{
if((m_Position.w -= LOOK_RATE) >= -90)
m_Position.w -= LOOK_RATE;
}

void CGame::TurnLeft()
{
m_Angle -= TURN_RATE;

if(m_Angle < 0)
m_Angle = 359;
}

void CGame::TurnRight()
{
m_Angle += TURN_RATE;

if(m_Angle > 359)
m_Angle = 0;
}

void CGame::MoveUp()
{
m_Position.y += MOVEMENT_RATE;
}

void CGame::MoveDown()
{
m_Position.y -= MOVEMENT_RATE;
}
void CGame::MoveForwards()
{
m_Position.x += (float)MOVEMENT_RATE*sinf(D3DXToRadian(m_Angle));

m_Position.z += (float)MOVEMENT_RATE*cosf(D3DXToRadian(m_Angle));
}

void CGame::MoveBackwards()
{
m_Position.x -= (float)MOVEMENT_RATE*sinf(D3DXToRadian(m_Angle));

m_Position.z -= (float)MOVEMENT_RATE*cosf(D3DXToRadian(m_Angle));

}

void CGame::MoveLeft()
{
m_Position.x -= MOVEMENT_RATE;
}

void CGame::MoveRight()
{
m_Position.x += MOVEMENT_RATE;
}



Then when you set your camera you just do this:


  
D3DXMatrixLookAtLH(&matView,
// Camera Position.

&D3DXVECTOR3(m_Position.x,
m_Position.y, m_Position.z),
// Look At Position.

&D3DXVECTOR3(m_Position.x+sinf(D3DXToRadian(m_Angle)),
m_Position.y+sinf(D3DXToRadian(m_Position.w)),
m_Position.z+cosf(D3DXToRadian(m_Angle))),

// Up Direction.

&D3DXVECTOR3(0.0f, 1.0f, 0.0f));



Hope this helps


[edited by - MegadethUK on August 16, 2002 12:49:54 PM]

[edited by - MegadethUK on August 16, 2002 12:51:36 PM]

Share this post


Link to post
Share on other sites
It definitely helped me, one question your m_position variable is a vector then? and your TURN_RATE is a constant that defines the amount you want to deviate with each frame? Am i following you? What then would the output from the mouse look like if i was capturing the output in a text box? doesn''t it just return the currentX and currenty location of the cursor?

Share this post


Link to post
Share on other sites