Quaternion camera problem

Started by
6 comments, last by BBeck 7 years, 10 months ago

how to get the yaw pitch, roll of Quaternion.

I have a quaternion camera, and want to get the pitch and invert it. (i.e. pitch = -pitch)

Advertisement

You cannot directly obtain it, What you need is to convert your quaternion to Tait-Bryan or Euler angles.

You might not even need it, if all you want to do is invert your camera you can probably (and more appropriately) do it differently. We'd need to see where your camera gets pitched to see how you are achieving it currently before offering a more specific solution. If you are applying pitch by creating a new quaternion from an axis-angle for example then it might be as simple as swapping a 1 for a -1.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

that's say I need a “pitch amount” to record how much the camera has pitched, and use the amount to invert?

If you have a quaternion representing the camera's rotation and you are trying to get the "pitch" angle, or the angle the camera's target vector is above the world space horizon, the easiest way I can think of is to get a rotation matrix from the quaternion (well documented on how to do this) and get the target vector from there.

I believe the target vector will end up being this:


vec3 target_vec(const quaternion & quat)
{
        return vec3(
        2*(quat.x*quat.z + quat.y*quat.w), 
        2*(quat.y*quat.z - quat.x*quat.w), 
        1 - 2*(quat.x*quat.x + quat.y*quat.y));
}

Then just take the angle between the target vector and a vector representing the horizon - ie the same target vector except with z = 0 (assuming z is your world's vertical coordinate).

that's say I need a “pitch amount” to record how much the camera has pitched, and use the amount to invert?

I would store yaw and pitch separately. If you want to pitch (by mouse movement for example) then you would do something like this:


// A typical way to do mouse look is by resetting the mouse to a known position each time 
// (e.g. centre screen) and working out the change
// this is now either positive or negative
float mouseVerticalDifference = mouse.y - centre.y;
// account for user's sensitivity
float pitchAmount = sensitivity*mouseVerticalDifference;
if(invertPitch)
    pitch += pitchAmount;
else
    pitch -= pitchAmount;
// clamp pitch to sensible values
pitch = Clamp(pitch, minPitch, maxPitch);
// Now you build your camera's orientation by using your pitch/yaw values
cameraOrientation = Quaternion::FromYawPitchRoll(yaw, pitch, 0);

If you use that approach then you already know what pitch is so there's no need to extract it but inverting the pitch is easy without having to extract the pitch. Some camera types will happily let you keep pitching up over and over and do all sorts of crazy things, great for space and so on. If your camera is like that then you will have to do it slightly differently but you still wouldn't need to know what pitch was in order to have a pitch inversion option.

It does occur to me though that this might be a one off flip that you want to do, in that case I guess you would need to know pitch.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

Between what interval are you trying to record? Is it frame-to-frame? I don't understand why you wouldn't know the "pitch amount" already..... The quaternion won't change unless you tell it to rotate. Just invert the amount your program is telling it to go.

If you are talking about the "absolute pitch" from the horizon, convert the quaternion into a rotation matrix and transform vector(0,0,1), then use the Y value as the "pitch from horizon".

The above assumes you are using XZ as the horizon and Y as up/down.

Please don't.

You really need to get the pitch, yaw, roll data storage out of your head. ;-)

Pitch, yaw, and roll, are not a way to store data. They are a way to conceive of orientation, but not a way to store it.

You go to quaternions because you figure out that your pitch/yaw/roll storage is seriously deficient and to be blunt just simply doesn't work. Someone introduces you to quaternions as a fix for the problem. Then you proceed to turn quaternions into what you had before. lol

I have an entire video on storing orientation. I hope you'll check it out and hopefully it will get you looking at quaternions a bit differently.

This topic is closed to new replies.

Advertisement