c++ - 如何使用偏航、俯仰和滚动来旋转 3D vector ?

标签 c++ vector 3d rotation trigonometry

我不知道我会怎么做,有谁知道我如何使用偏航、俯仰和滚动来旋转 x、y 和 z?我只能设法进行 2D 旋转,但事实并非如此我在找什么。这是我当前的代码。

偏航和俯仰以及横滚应该以度为单位。不是弧度。

class Vector3
{//
public:
    float x, y, z;

    void rotate(float yaw, float pitch, float roll) {

    }
};

欢迎使用不需要外部库的解决方案。

编辑:

    void rotate(float yaw, float pitch, float roll) { //X Y Z Rotation
        float cosa = cos_r(yaw); float cosb = cos_r(pitch); float cosc = cos_r(roll);
        float sina = sin_r(yaw); float sinb = sin_r(pitch); float sinc = sin_r(roll);

        float Axx = cosa * cosb;
        float Axy = cosa * sinb * sinc - sina * cosc;
        float Axz = cosa * sinb * cosc + sina * sinc;

        float Ayx = sina * cosb;
        float Ayy = sina * sinb * sinc + cosa * cosc;
        float Ayz = sina * sinb * cosc - cosa * sinc;

        float Azx = -sinb;
        float Azy = cosb * sinc;
        float Azz = cosb * cosc;

        float px = x; float py = y; float pz = z;
        x = Axx * px + Axy * py + Axz * pz;
        y = Ayx * px + Ayy * py + Ayz * pz;
        z = Azx * px + Azy * py + Azz * pz;
    }

我试过了,但没用。 cos_r 和 sin_r 是取度数的函数。

最佳答案

由于您要实现自己的 3D vector 类,您可能也想实现一些基本的矩阵运算。特别是,你想要 rotation matrices .链接的维基百科部分显示了围绕 X、Y 或 Z 轴旋转的简单基础。

一旦你有了它,你会发现“偏航、滚动、俯仰”,即 Euler angles or Tait-Bryan angles只是一种以给定顺序围绕主轴应用旋转的方法。

关于c++ - 如何使用偏航、俯仰和滚动来旋转 3D vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58149986/

相关文章:

c++ - 为什么在遍历此 vector 时会出现段错误?

c++ - 返回迭代器是 DLL 安全的吗?

python - 在 Python 中生成 3D 希尔伯特空间填充曲线的算法

algorithm - 计算球体的旋转矢量

android - 如何拦截 Android OpenGL ES 中的触摸屏事件?

c++ - 如何让 boost::object_pool 线程安全?

c++ - 在字符串中查找单词或短语的实例

r - 在 R 中,如何查看长度为 5 的向量中字符向量的三个元素是否相同

c++ - 当一个列表元素被删除时,迭代器最后会发生什么?

c++ - 带有参数的方法 (const T *&) or (T * &) or(const T * const &) or(T * const &)