# Calculate roll from yaw and pitch?

I came across an issue earlier today where I have created a look-at camera in 3D space, and applied yaw and pitch to it to rotate around the target properly. The problem is that introducing local pitch and yaw introduces global roll. I believe the way I manipulate the camera is important for the question and thus I will post some code in C# here.

I can convert most math back to code and since this problem is heavily math based I figured the math site was a better fit for my question.

When the user drags the mouse I use the X and Y coordinates to introduce yaw and pitch via the following method:

Vector3 lookat = Position - Target;
Vector3 right = Vector3.Normalize(Vector3.Cross(lookat, Up));
Vector3 up = Vector3.Normalize(Vector3.Cross(right, lookat));
Vector3 rotationx = right * yaw * lookat.Length();
Vector3 rotationy = up * pitch * lookat.Length();
Vector3 newPosition = Vector3.Normalize(Position + rotationx + rotationy) * Position.Length();
if (Vector3.Dot(up, newPosition) / (up.Length() * newPosition.Length()) < 0.98) {
Position = newPosition;
Up = up;
}


It is important to note the following statements:

• Up is initially set to $$(0, 0, 1)$$ which is North in ECR.
• The x and y coordinates of the mouse from the previous frame are subtracted from the current frame.
• Then they are divided by $$1000$$ to represent $$x = yaw$$ and $$y = pitch$$.

Then, every frame I calculate the new View matrix by using the built in LookAtRH function supplied by DirectX.

View = Matrix.LookAtRH(Position, Target, Up);


Where the Target position is $$(0, 0, 0)$$.

I am wondering the following:

• Is there a way to calculate roll from yaw and pitch?
• If so, how can I use the calculated roll to negate the roll that just occurred?
• Would ensuring the Up vector is always pointing North or South help prevent this issue?
• If so, how do I get over the issue of singularity which causes my camera to just spin frantically around the poles simulating Gimbal lock?
• Is there a way to remove the roll when assigning my View matrix?
• From my understanding some matrix multiplication may help here?
• Are you completely certain that you don't want to learn to use quaternions? I know I get a headache remembering whether I want my axes to rotate with the object or not, and how that choice affects the order in which rotations should be applied, and which order corresponded to which choice. Quaternions are comparatively problem free once you get used to the arithmetic. – Arthur Dec 27 '18 at 0:26