#### Archived

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

# Camera facing

This topic is 5969 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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.

##### Share on other sites
For this, you will possibly have to use a little trig (sin and cos). It really depends on how you are moving the camera in the first place.

Moe''s site

##### 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 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?

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013743
×