Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL camera pitch, yaw and roll rotation

I am trying to create a class to control the camera in OpenGL. I have three methods to change the pitch yaw and roll of the camera. The methods take a float parameter to use as the amount of rotation to add.

The code in these methods is where I need help. The rotations are stored in a Vector3. So far for the change pitch method I have:

void changePitch(float degrees)
{
    float rads = MathHelp::degreesToRadians(degrees);
    m_rotation.x += cos(m_rotation.y) * rads;
}

This is as far as I could get by myself. It kind of works, the camera looks up and down while facing up or down the z axis and not while looking down the x axis. I tried adding z rotation:

m_rotation.z += sin(m_rotation.y) * rads;

But that didn't go very well.

like image 783
Obi-Dan Avatar asked Nov 22 '25 01:11

Obi-Dan


1 Answers

Assume you have the upVector, lookAtVector and rightVector 3D vectors that point towards up, looking at direction and right of your camera. Then, to properly increment pitch you should do the computation as follows:

void changePitch(angle) {
    angle = DegreeToRadian(angle);

    // Rotate lookAtVector around the right vector
    // This is where we actually change pitch
    lookAtVector = Normalize3dVector(viewDir * cos(angle) + UpVector * sin(angle));

    // Now update the upVector
    upVector = CrossProduct(rightVector, lookAtVector);
}

In the above excerpt, names of the arbitrarily used functions are self-explanatory.

like image 67
meyumer Avatar answered Nov 24 '25 16:11

meyumer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!