I've made my own Camera object, with a forward
vector as well as a up
vector. I happen to be programming in Android using OpenGL ES 2.0 but I think this question is relevant to all frameworks, it seems to be some math issue I'm not understanding.
Here's a gif of my Pitch/Yaw working correctly:
Here's a gif of Pitch/Yaw in a circle, causing the square to rotate, because the camera is tilting (the up
vector becomes negative as I continue).
Here's pseudocode for my pan/yaw functions:
function pitch(amount) {
if (amount == 0) return;
forward = normalize(forward + up * amount);
// Now fix the incorrect up vector
right = cross(forward, up);
up = normalize(cross(right, forward));
}
function yaw(amount) {
if (amount == 0) return;
right = cross(forward, up);
forward = normalize(forward + right * amount);
}
I get the same issue if I use Vector rotations instead of Vector Addition (amount is an angle and I use a Rotation Matrix in the X or Y axis)
Pitch: forward = normalize(rotated(forward, angle, Vector3(1, 0, 0)));
Yaw: forward = normalize(rotated(forward, angle, Vector3(0, 1, 0)));
Why is this happening? Is there a way to stop it?
EDIT: I followed the instructions at Why is the camera tilting around the z axis when I only specified x and y? to pitch using the local X axis and yaw using the world Y axis, same result.
Here's the new pseudocode:
function pitch(angle) {
if (angle == 0) return;
right = cross(forward, up);
forward = normalized(rotated(forward, angle, right));
// Fix the now incorrect up vector
up = normalized(cross(right, forward));
}
function yaw(angle) {
if (angle == 0) return;
forward = normalized(rotated(forward, angle, Vector3(0, 1, 0)));
}
You can see all the code at https://github.com/repwolfe/paper-escaper-android/tree/master/app/src/main/java/com/studioussoftware/paperescaper
up
vector. Try replacingright = cross(forward, up)
withright = normalized(cross(forward, Vector3(0, 1, 0)))
if you don't need to look directly up/down. If you're usingup
to construct your matrix then you'll want to update it when yawing too. – DMGregory Sep 04 '16 at 17:34