• Advertisement
Sign in to follow this  

[DirectX 11] FPS Camera Help

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

Hey,

I made a FIrst Person shooter style camera, And i am having a problem with camera stutter when i rotate the camera as it is moving. The Camera Rotates/looks around fine when it is stationary. and moves fine when it is not rotateing/looking.

 

Here is my Cameras render function.
 

 

void C_D3D_Camera::Render()
{
    up.x = 0.0f;
    up.y = 1.0f;
    up.z = 0.0f;

    position.x = m_PositionX;
    position.y = m_PositionY;
    position.z = m_PositionZ;

    lookAt.x = 0.0f;
    lookAt.y = 0.0f;
    lookAt.z = 1.0f;

    pitch = m_RotationX * 0.0174532925f;
    yaw   = m_RotationY * 0.0174532925f;
    roll  = m_RotationZ * 0.0174532925f;

    D3DXMatrixRotationYawPitchRoll(&rotationMatrix, yaw, pitch, roll);

    D3DXVec3TransformCoord(&lookAt, &lookAt, &rotationMatrix);
    D3DXVec3TransformCoord(&up, &up, &rotationMatrix);

    lookAt = position + lookAt;

    D3DXMatrixLookAtLH(&m_ViewMatrix, &position, &lookAt, &up);

    return;

};

 

Here is my FPS Camera Move forward and move backwards functions.
 

void C_FPSCamera::MoveForward(bool keyDown)
{
    if(keyDown)
    {
        m_Direction.x = sinf(m_RotationY * 0.0174532925f);
        m_Direction.y =    0.0f;
        m_Direction.z =    cosf(m_RotationY * 0.0174532925f);

        D3DXVec3Normalize(&m_Direction, &m_Direction);

        m_Position += (m_Direction * m_ForwardSpeed) * m_DeltaTime;
    };

    return;
};

void C_FPSCamera::MoveBackward(bool keyDown)
{
    if(keyDown)
    {
        m_Direction.x = sinf(m_RotationY * 0.0174532925f);
        m_Direction.y =  0;
        m_Direction.z = cosf(m_RotationY * 0.0174532925f);    

        D3DXVec3Normalize(&m_Direction, &m_Direction);

        m_Position -= (m_Direction * m_ForwardSpeed) * m_DeltaTime;
    };

    return;
};

 

And here is my mouse look function.
 

 

void C_FPSCamera::MouseLook(int mouseX, int mouseY)
{

    int MouseMovedX = mouseX - m_LastMouseX;
    int MouseMovedY = mouseY - m_LastMouseY;

    m_LastMouseX = mouseX;
    m_LastMouseY = mouseY;

    m_Sensitivity = 0.01f * m_DeltaTime;

    m_RotationX += MouseMovedY * m_Sensitivity;
    m_RotationY += MouseMovedX * m_Sensitivity;
    
    if(m_RotationX > 89)
    {
        m_RotationX = 89;
    };

    if(m_RotationX < -89)
    {
        m_RotationX = -89;
    };

    while(m_RotationY < -180)
    {
        m_RotationY += 360;
    };
    while(m_RotationY > 180)
    {
        m_RotationY -= 360;
    };
};

 

Share this post


Link to post
Share on other sites
Advertisement

Hi, just thought I'd stick my 0.02 worth in here.

 

I noticed lots of hard coded raw floating point constants in there like 0.0174532925f.

 

you should probably avoid this and define named constants for example:

 

const float multiplier = 0.0174532925f;

 

this helps keep code readable and cuts out potential for typos...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement