旋转相机沿一个轴失败

发布于 2024-10-10 21:27:26 字数 1729 浏览 2 评论 0原文

编辑:我已经解决了这个特定问题,但创建了一个具有相当相似基本描述的新问题。相机旋转多次后,它开始在不应该的情况下滚动。我将偏航和俯仰绑定到鼠标,所以我知道 0 被传递给滚转。问题是,我实际上没有任何算法方法来知道这种情况是否发生,更不用说纠正问题了。

这是我现有的代码:

D3DXMATRIX CameraRotationMatrix;
D3DXVECTOR3 CameraPosition;
//D3DXVECTOR3 CameraRotation;

inline D3DXMATRIX GetRotationMatrix() {
    return CameraRotationMatrix;
}
inline void TranslateCamera(float x, float y, float z) {
    D3DXVECTOR3 rvec, vec(x, y, z);
    #pragma warning(disable : 4238)
    D3DXVec3TransformNormal(&rvec, &vec, &GetRotationMatrix());
    #pragma warning(default : 4238)
    CameraPosition += rvec;
    RecomputeVPMatrix();
}
inline void RotateCamera(float x, float y, float z) {
    D3DXVECTOR3 RotationRequested(x, y, z);
    D3DXVECTOR3 XAxis, YAxis, ZAxis;
    D3DXMATRIX rotationx, rotationy, rotationz;
    XAxis = D3DXVECTOR3(1, 0, 0);
    YAxis = D3DXVECTOR3(0, 1, 0);
    ZAxis = D3DXVECTOR3(0, 0, 1);

    #pragma warning(disable : 4238)
    D3DXVec3TransformNormal(&XAxis, &XAxis, &GetRotationMatrix());
    D3DXVec3TransformNormal(&YAxis, &YAxis, &GetRotationMatrix());
    D3DXVec3TransformNormal(&ZAxis, &ZAxis, &GetRotationMatrix());
    #pragma warning(default : 4238)
    D3DXMatrixIdentity(&rotationx);
    D3DXMatrixIdentity(&rotationy);
    D3DXMatrixIdentity(&rotationz);
    D3DXMatrixRotationAxis(&rotationx, &XAxis, RotationRequested.x);
    D3DXMatrixRotationAxis(&rotationy, &YAxis, RotationRequested.y);
    D3DXMatrixRotationAxis(&rotationz, &ZAxis, RotationRequested.z);
    CameraRotationMatrix *= rotationx;
    CameraRotationMatrix *= rotationy;
    CameraRotationMatrix *= rotationz;
    RecomputeVPMatrix();
}

Edit: I have solved this specific problem but created a new one with a fairly similar basic description. After the camera is rotated a number of times, then it begins to roll when it shouldn't. I have yaw and pitch bound to the mouse so I know that 0 is passed in for roll. The trouble is, I don't actually have any algorithmic way to even know whether or not this is happening, let alone correct the problem.

This is my existing code:

D3DXMATRIX CameraRotationMatrix;
D3DXVECTOR3 CameraPosition;
//D3DXVECTOR3 CameraRotation;

inline D3DXMATRIX GetRotationMatrix() {
    return CameraRotationMatrix;
}
inline void TranslateCamera(float x, float y, float z) {
    D3DXVECTOR3 rvec, vec(x, y, z);
    #pragma warning(disable : 4238)
    D3DXVec3TransformNormal(&rvec, &vec, &GetRotationMatrix());
    #pragma warning(default : 4238)
    CameraPosition += rvec;
    RecomputeVPMatrix();
}
inline void RotateCamera(float x, float y, float z) {
    D3DXVECTOR3 RotationRequested(x, y, z);
    D3DXVECTOR3 XAxis, YAxis, ZAxis;
    D3DXMATRIX rotationx, rotationy, rotationz;
    XAxis = D3DXVECTOR3(1, 0, 0);
    YAxis = D3DXVECTOR3(0, 1, 0);
    ZAxis = D3DXVECTOR3(0, 0, 1);

    #pragma warning(disable : 4238)
    D3DXVec3TransformNormal(&XAxis, &XAxis, &GetRotationMatrix());
    D3DXVec3TransformNormal(&YAxis, &YAxis, &GetRotationMatrix());
    D3DXVec3TransformNormal(&ZAxis, &ZAxis, &GetRotationMatrix());
    #pragma warning(default : 4238)
    D3DXMatrixIdentity(&rotationx);
    D3DXMatrixIdentity(&rotationy);
    D3DXMatrixIdentity(&rotationz);
    D3DXMatrixRotationAxis(&rotationx, &XAxis, RotationRequested.x);
    D3DXMatrixRotationAxis(&rotationy, &YAxis, RotationRequested.y);
    D3DXMatrixRotationAxis(&rotationz, &ZAxis, RotationRequested.z);
    CameraRotationMatrix *= rotationx;
    CameraRotationMatrix *= rotationy;
    CameraRotationMatrix *= rotationz;
    RecomputeVPMatrix();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥抱没勇气 2024-10-17 21:27:26

如果您使用欧拉角进行了大量旋转,您可能会陷入万向节锁定状态。

有关云台锁的维基百科页面

If you have done a lot of rotations using euler angles you may have got yourself into a gimbal lock.

Wikipedia page on Gimbal lock

猥︴琐丶欲为 2024-10-17 21:27:26

您究竟是如何安排轮换的?如果您只需要偏航和俯仰,则可以通过组合不同侧的旋转来轻松避免不必要的旋转。

例如,如果您有一个新的鼠标输入,可以提供一个 pitch_diff 和一个 yaw_diff 四元数,您可以这样做:my_rotation = yaw_diff * my_rotation *itch_diff代码>.这样,my_rotation 的“左半部分”将始终是偏航,而“右半部分”将始终是俯仰。而且你永远不会得到滚动组件!

How exactly are you composing your rotations? If you just need yaw and pitch, it's easy to avoid unwanted rotations by combining the rotations from different sides.

For example, if you have a new mouse input that gives you a pitch_diff and a yaw_diff quaternion, you could do: my_rotation = yaw_diff * my_rotation * pitch_diff. That way, the "left half" of my_rotation will always be a yaw, and the "right half" will always be pitch. And you never get a roll component!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文