夹紧刚体的旋转角度

时间:2013-05-05 08:53:37

标签: c++ camera rotation constraints bulletphysics

我最近在我的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);
}

我想将相机的音高限制在-80°80°的范围内。这有助于玩家保持导向。否则他将能够将相机旋转到更高的位置,并将自己背后的世界颠倒过来。相反,一个试图这样做的真人会打破他的脖子。

我让Bullet Physics在四元数中为我存储旋转,因此音调不会直接存储。如何夹住刚体的音高?

1 个答案:

答案 0 :(得分:-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
    // ...
}

您可以在我自己的问题another answer中找到更多相机代码。