0
votes

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. 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.

2
Trust me, you don't want to roll your own linear algebra. Do us all a favor and use something like eigen.tuxfamily.org/index.php?title=Main_Page . - Wolfgang Brehm
@OldProgrammer I didn't really understand how it was explained there. And the case seems to be different. - Free Renca

2 Answers

1
votes

Since you are implementing your own 3D vector class, you probably want to implement some basic matrix operations too. In particular, you want rotation matrices. The linked Wikipedia section shows the simple basecases for rotation around the X, Y, or Z axis.

Once you have that, you will find that "yaw, roll, pitch", i.e. Euler angles or Tait-Bryan angles are just a way of applying rotations about the principal axes in a given order.

0
votes

You can use Rotation matrix to do the trick. https://en.wikipedia.org/wiki/Rotation_matrix

All you need to do is to multiply the vector by the matrix of needed rotation.