Issue with a Free Fly Camera And Combination of Quaternion Rotations

I'm currently writing a basic 3D engine to get started with OpenGL, and I'm working on mouse inputs. I'm building a Free Fly Camera, which enables the user is able to watch in every direction using Oculus Rift headset and the mouse(yaw only if Oculus is enabled). Using the keyboard, he can strafe, go forward or backward to the direction he's facing. To do that, as I have to combine the mouse induced rotation with the Oculus Headset Rotation, I'm using Quaternions. I'm "multiplying" two quaternions, one built from the OVR orientation (which works perfeclty well on its own) and one built from mouse input:

quat OVRquat = quat(vec3(OVRPitch, OVRYaw, OVRRoll));
quat Mquat = quat(vec3(mPitch, mYaw, 0.0f));

// Multiplying the "init" front vector with the Oculus Rotation and the Mouse induced rotation
front = vec3(0.0f, 0.0f, 1.0f) * mat3_cast(Mquat * OVRquat);
right = cross(front, up);

mPitch and mYaw are build from a relative mouse coordinates a frame time.

If the OVR Quat works like a charm, the mouse Quat is more odd. In fact, As soon as I flip Pi/2 rad around Y axis (so Pi/2 yaw), vertical axis is flipped, which means that if I push the mouse up, the camera goes down, whereas in normal conditions, it goes up. Also, if I'm facing up after a Pi/2 rotation (Note that I can't go over PI/2 on the pitch rotation, up and down), and then looking left or right, the camera follows a strange path going all around the sphere through North and South pole, on the plan normal to Z (like a meridian). It should normally draw a "horizon"-like path

I must have been missing something about Quaternions theory, as I've been reading a lot those last day about that. How can I multiply those two Quaternions and get fluid Camera? Or what solution could I use otherwise? Feel free to ask for any more code or info.

Precisions:

Note that right now, I'm not even using the Oculus: OVRPitch/Yaw/Roll are fixed to 0.0f .

Here's how I create my quaternions:


Your problem has nothing to do with quaternions; this is exactly how euler angles are supposed to work: rotating around one axis changes the orientation of others. The sequence of rotations is therefore important. I suspect, that GLM first makes the pitch, and then the yaw rotation.

In this scenario, if your yaw angle is more than Pi/2(i think that your yaw is actually somewhere around Pi here, which is a half rotation) , it rotates the camera that for instance was facing up after the pitch, to the camera that will face down, thus inverting the facing from "up" to "down".

链接地址: http://www.djcxy.com/p/81766.html

上一篇: 如何在现代opengl中正确实现四元数相机?

下一篇: 问题与自由飞行相机和四元数旋转的组合