如何朝面向方向移动第一人称对象?(C )

How to Move a First-Person Object in the Facing Direction? (C++)

本文关键字:对象 第一人称 何朝面 方向 移动      更新时间:2023-10-16

我正在在子弹中重现一个基本的厄运,并想知道如何朝我的对象面对的方向移动。

目前,我的框架正在设置,我的相机将其旋转和位置设置为播放器,谁的位置和旋转设置为由子弹创建的物理主体。

这是我目前针对玩家运动的运动代码:

GameObject::Update( deltatime );
if( m_pController == 0 )
    return;
vec3 dir(0, 0, 0);
vec3 rot(0, 0, 0);
if( m_pController->IsButtonPressed( PCB_Up ) )
    dir.z += 1;
if( m_pController->IsButtonPressed( PCB_Down ) )
    dir.z -= 1;
if( m_pController->IsButtonPressed( PCB_Left ) )
    dir.x -= 1;
if( m_pController->IsButtonPressed( PCB_Right ) )
    dir.x += 1;
if (m_pController->IsButtonPressed(PCB_RotLeft))
    rot.y -= 1;
if (m_pController->IsButtonPressed(PCB_RotRight))
    rot.y += 1;
dir.Normalize();
rot.Normalize();
float speed = 10.0f;
btVector3 force = btVector3(dir.x, dir.y, dir.z) * speed;
btVector3 rotForce = btVector3(rot.x, rot.y, rot.z) * speed;
if( m_pBody )
{
    m_pBody->applyForce( force, btVector3(0,0,0) );
    m_pBody->applyTorque(rotForce);
    m_pBody->forceActivationState( DISABLE_DEACTIVATION );
}

我知道可以通过从相机创建一个矩阵并提取视图来完成,但事实是我的相机正在设置为播放器的旋转和翻译。

可以通过使用我的物理体值创建SRT矩阵并从这些数字中计算方向向量,然后具有:

来完成:
if( m_pController->IsButtonPressed( PCB_Up ) )
    dir += ForwardFacing;
if( m_pController->IsButtonPressed( PCB_Down ) )
    dir -= ForwardFacing;

我不确定如何为左右钥匙完成。

无论如何,感谢您的时间!

是的。

mat4 mat;
mat.CreateSRT(m_Scale, m_Rotation, m_Position);
vec3 forceDir = mat.GetAt() * dir.z + mat.GetRight()*dir.x;
btVector3 force = btVector3(forceDir.x, forceDir.y, forceDir.z) * speed;
btVector3 rotForce = btVector3(rot.x, rot.y, rot.z) * speed;