简体   繁体   English

如何使用偏航、俯仰和滚动来旋转 3D 矢量?

[英]How do I rotate a 3D vector using yaw, pitch, and roll?

I have no idea how I would go on about this, does anyone know how I can rotate x, y, and z using the yaw, pitch, and roll?, I can only manage to do 2D rotation, but that's not what I am looking for.我不知道我会如何 go 对此,有谁知道我如何使用偏航、俯仰和滚动来旋转 x、y 和 z?,我只能设法进行 2D 旋转,但这不是我的意思寻找。 This is the current code I have.这是我拥有的当前代码。

Yaw and pitch, and roll are supposed to be in degrees.偏航、俯仰和滚动应该以度为单位。 Not radians.不是弧度。

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

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

    }
};

A solution that doesn't require a external library is appreciated.不需要外部库的解决方案值得赞赏。

edit:编辑:

    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;
    }

I tried this but it didn't work.我试过这个,但没有用。 cos_r and sin_r are functions that take degrees. cos_r 和 sin_r 是取度数的函数。

Since you are implementing your own 3D vector class, you probably want to implement some basic matrix operations too.由于您正在实现自己的 3D 向量 class,因此您可能还想实现一些基本的矩阵运算。 In particular, you want rotation matrices .特别是,您需要旋转矩阵 The linked Wikipedia section shows the simple basecases for rotation around the X, Y, or Z axis.链接的 Wikipedia 部分显示了围绕 X、Y 或 Z 轴旋转的简单基本情况。

Once you have that, you will find that "yaw, roll, pitch", ie Euler angles or Tait-Bryan angles are just a way of applying rotations about the principal axes in a given order.一旦你有了它,你会发现“偏航、滚动、俯仰”,即欧拉角或泰特-布赖恩角只是以给定顺序围绕主轴应用旋转的一种方式。

You can use Rotation matrix to do the trick.您可以使用旋转矩阵来解决问题。 https://en.wikipedia.org/wiki/Rotation_matrix https://en.wikipedia.org/wiki/Rotation_matrix

All you need to do is to multiply the vector by the matrix of needed rotation.您需要做的就是将向量乘以所需旋转的矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM