我试图弄清楚如何使DirectX相机根据其面对的方向移动。
现在,我移动相机的方法是将相机的当前位置和旋转传递给称为PositionClass的类。PositionClass从另一个称为InputClass的类中获取键盘输入,然后更新摄影机的位置和旋转值,然后将其传递回摄影机类。
我编写了一些代码,这对我来说似乎非常有用,利用摄像机的俯仰和偏航,我可以使它朝我对准摄像机的方向前进。
但是,当摄像机直视(俯仰= 90)或直下(俯仰= -90)时,它仍会更改摄像机的X和Z位置(取决于偏航)。
预期的行为是在向上或向下看时,它只会沿Y轴移动,而不会沿X或Z轴移动。
这是计算新相机位置的代码
void PositionClass::MoveForward(bool keydown)
{
float radiansY, radiansX;
// Update the forward speed movement based on the frame time
// and whether the user is holding the key down or not.
if(keydown)
{
m_forwardSpeed += m_frameTime * m_acceleration;
if(m_forwardSpeed > (m_frameTime * m_maxSpeed))
{
m_forwardSpeed = m_frameTime * m_maxSpeed;
}
}
else
{
m_forwardSpeed -= m_frameTime * m_friction;
if(m_forwardSpeed < 0.0f)
{
m_forwardSpeed = 0.0f;
}
}
// ToRadians() just multiplies degrees by …Run Code Online (Sandbox Code Playgroud)