0

我最近在我的 3d 游戏中将一个刚体连接到相机上,这样它就可以与环境发生碰撞。现在鼠标移动直接旋转刚体。

#include <BULLET/btBulletDynamicsCommon.h>

void Rotate(float Pitch, float Yaw, float Roll, float Speed)
{
    // get current rotation
    btTransform transform = body->getWorldTransform();
    btQuaternion rotation = transform.getRotation();

    // create orientation vectors
    btVector3 up(0, 1, 0);
    btVector3 lookat = quatRotate(rotation, btVector3(0, 0, 1));
    btVector3 forward = btVector3(lookat.getX(), 0, lookat.getZ()).normalize();
    btVector3 side = btCross(up, forward);

    // rotate camera with quaternions created from axis and angle
    rotation = btQuaternion(up,      Amount.getY()) * rotation;
    rotation = btQuaternion(side,    Amount.getX()) * rotation;
    rotation = btQuaternion(forward, Amount.getZ()) * rotation;

    // set new rotation
    transform.setRotation(rotation);
    body->setWorldTransform(transform);
}

我想将相机的间距限制在 to 的范围-80°80°。这有助于玩家保持定向。否则,他将能够将摄像机旋转到更高的位置,然后将自己身后的世界颠倒过来。相比之下,一个真正的人尝试这样做会折断他的脖子。

我让Bullet Physics在四元数中为我存储旋转,因此音高不是直接存储的。如何钳制刚体的间距?

4

1 回答 1

-1

我想出了一个解决方案。我没有限制相机刚体的旋转,而是限制了之前应用了多少旋转。因此,我会跟踪鼠标的总垂直移动。实际上,我存储了应用灵敏度的整体鼠标移动。

float Overallpitch = 0.0f;

如果应用传递的偏航值会导致整体俯仰超过或超过给定的限制,我只需根据需要应用尽可能多的偏航值来达到限制。

#include <BULLET/btBulletDynamicsCommon.h>

void Rotate(float Pitch, float Yaw, float Roll, float Sensitivity)
{
    // apply mouse sensitivity
    Yaw   *= Sensitivity;
    Pitch *= Sensitivity;
    Roll  *= Sensitivity;

    // clamp camera pitch
    const float clamp = 1.0f;
    if     (Overallpitch + Pitch >  clamp) Pitch =  clamp - Overallpitch;
    else if(Overallpitch + Pitch < -clamp) Pitch = -clamp - Overallpitch;
    Overallpitch += Pitch;

    // apply rotation to camera quaternion
    // ...
}

您可以在我自己的问题的另一个答案中找到更多相机代码。

于 2013-05-07T10:09:53.487 回答