0
votes

I'm trying to set a third person camera, but i'm lost with rotations. My rotation are working for the Y axis, but the others are moving strangely. This is my code :

XMMATRIX Camera2::Render()
{
    return XMMatrixLookAtLH( XMLoadFloat3( &m_vPosition ), XMLoadFloat3( &m_vTargetPos     ), XMLoadFloat3( &( XMFLOAT3( 0.0f, 1.0f, 0.0f ) ) ) );
}

void Camera2::Rotate( float fAngle, int nAxe )
{
    float fToRad = 0.0174532925f;
    fAngle *= fToRad;

    if( nAxe == 0 )
    {
        XMFLOAT3 vPosition = m_vPosition;
        m_vPosition.y = vPosition.y * cos( fAngle ) - vPosition.z * sin( fAngle );
        m_vPosition.z = vPosition.y * sin( fAngle ) + vPosition.z * cos( fAngle );
    }
    else if( nAxe == 1 )
    {
        XMFLOAT3 vPosition = m_vPosition;
        m_vPosition.z = vPosition.z * cos( fAngle ) - vPosition.x * sin( fAngle );
        m_vPosition.x = vPosition.z * sin( fAngle ) + vPosition.x * cos( fAngle );
    }
    else if( nAxe == 2 )
    {
    XMFLOAT3 vPosition = m_vPosition;
    m_vPosition.x = vPosition.x * cos( fAngle ) - vPosition.y * sin( fAngle );
    m_vPosition.y = vPosition.x * sin( fAngle ) + vPosition.y * cos( fAngle );
    }
}

And the code calling the camera functions ( x = 0, y = 1, z = 2 ) :

if( event.IsPushedKey( VK_F1 ) )
    m_pCamera->Rotate( -3.0f, 0 );
else if( event.IsPushedKey( VK_F2 ) )
    m_pCamera->Rotate( -3.0f, 1 );
else if( event.IsPushedKey( VK_F3 ) )
    m_pCamera->Rotate( -3.0f, 2 );
else if( event.IsPushedKey( VK_F4 ) )
    m_pCamera->Rotate( 3.0f, 0 );
else if( event.IsPushedKey( VK_F5 ) )
    m_pCamera->Rotate( 3.0f, 1 );
else if( event.IsPushedKey( VK_F6 ) )
    m_pCamera->Rotate( 3.0f, 2 );

Another question : When i start with XMFLOAT3( 0.0f, 0.0f, 0.0f ) as lookAt variable and have an x and y position equal to 0, nothing is draw. I need to set one of the axe of lookAt to 1.0f to see something. Why?

2

2 Answers

0
votes

Your rotation matrix was wrong for rotation around Y-axis and Z-axis, here is the matrix.

Rotation around X-axis by theta(in radian)

enter image description here

Rotation around Y-axis by theta(in radian)

enter image description here

Rotation around Z-axis by theta(in radian)

enter image description here

0
votes

Imagine the real world.

What happens when you try to focus on your own eye?

It's not possible.