简体   繁体   English

c ++ DirectX 11-第三人称摄影机

[英]c++ DirectX 11 - 3rd person camera

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. 我的旋转适用于Y轴,但其他旋转却奇怪。 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 ) : 以及调用相机功能的代码(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. 另一个问题:当我以XMFLOAT3(0.0f,0.0f,0.0f)作为lookAt变量并且x和y位置等于0时,没有绘制任何内容。 I need to set one of the axe of lookAt to 1.0f to see something. 我需要将lookAt的斧头之一设置为1.0f才能看到内容。 Why? 为什么?

Imagine the real world. 想象一下现实世界。

What happens when you try to focus on your own eye? 当您尝试着自己的眼睛时会发生什么?

It's not possible. 这是不可能的。

Your rotation matrix was wrong for rotation around Y-axis and Z-axis, here is the matrix. 您的旋转矩阵对于绕Y轴和Z轴的旋转是错误的,这是矩阵。

Rotation around X-axis by theta(in radian) θ绕X轴旋转(弧度)

在此处输入图片说明

Rotation around Y-axis by theta(in radian) θ绕Y轴旋转(弧度)

在此处输入图片说明

Rotation around Z-axis by theta(in radian) θ绕Z轴旋转(弧度)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM