Trouble with quaternions / euler angles (3D) and player rotations

This topic is 1613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I'm working on a 3D game engine and I'm having some trouble with the math behind player orientations / rotations.

My coordinate system is right-handed with (1,0,0) = right,(0,1,0) = up and (0,0,-1) = forward.

I have the following data available for the player character:

viewOrientation - A quaternion representing the way the player is currently facing

upDirection - A vector representing the up-direction of the player, by default it's the same as the world's up vector (0,1,0), but the player is able to walk on walls as well, in which case the up direction would be perpendicular to the surface below the player

forwardDirection,rightDirection - If the up-direction is changed, these change respectively. By default all three direction vectors correspond to the world axes

upRotation - Quaternion representing the rotation from the world's up vector to the upDirection of the player

Now, what I need to do is:

- Limit the player's pitch axis to 90 degree up and down (Can't look further than straight down / up)

- Extract a movement direction from the player's orientation

As long as the player's direction vectors correspond to the world axes, both of these are easy to implement.

But problems arise if the up direction is anything else but (0,1,0).

Usually I would just transform the viewOrientation by the inverse of the upRotation, convert that to euler angles, apply the limit and then convert them back to a quaternion and transform them back via the upRotation.

Unfortunately this is prone to errors, since different euler angles can represent the same rotation. For example if I have the euler angles (pitch=0,yaw=0,roll=0), then pitch is obviously 0. On the other hand, if I have the euler angles (180,180,180), which is essentially the same rotation, I get the pitch as 180, which is obviously not what I want. Since I'm working with quaternions, and convert them to euler angles, I can't be sure which ones I get.

Are there any alternative ways to do this?

As for the second problem, it's supposed to be like this:

If the player looks straight forward, he's supposed to move at full speed in that direction. If he looks 45 degree downwards, he's supposed to move at half the speed, but no downwards force should be applied, so the direction vector should stay the same.

Problem is, I'm unsure on how to extract the direction vector from the viewOrientation. Again, usually I'd just grab the euler angles same way as above, remove the pitch and roll components, and use the yaw, but the above problem applies here as well.

I'm at a loss, any nudge in the right direction would be very much appreciated.

Share on other sites

. If he looks 45 degree downwards, he's supposed to move at half the speed, but no downwards force should be applied, so the direction vector should stay the same.

If direction vector differs, but you still wish to keep (forwardDirection I believe) corespond to the pseudo direction of the looking vector but remain in a plane , you may grab the x and y components of look vector and set z to 0 and normalize this vector- this will keep the direction refer to look at vector but remain in x,y plane (or pick some other plane of the three).

90 degree up and down (Can't look further than straight down / up)

you wish to avoid gimbal lock. You may peform dot product of viewOrientation and upDirection vectors, if the absolute value of this dot product is too close to 1.0, stop applying the desired rotation to it (stuck it simply). Be aware though that if a very large rotation request comes, you may end up with smaller dot product but already on the opposite side, resulting in incorrect cross product of those two and bringing gimbal lock for view matrix - you may over come this by applying rather constant or enough small step in rotation requests.

More robust solution is rather less efective but always accurate to determine gimbal event.

C=normalize( viewdirection cross upvector)

D=normalize ( C cross viewdirection)

then if dot(D,upvector) is negative (or positive) view direction has crossed the up vector

Share on other sites

. If he looks 45 degree downwards, he's supposed to move at half the speed, but no downwards force should be applied, so the direction vector should stay the same.

If direction vector differs, but you still wish to keep (forwardDirection I believe) corespond to the pseudo direction of the looking vector but remain in a plane , you may grab the x and y components of look vector and set z to 0 and normalize this vector- this will keep the direction refer to look at vector but remain in x,y plane (or pick some other plane of the three).

Really should have thought of that myself. Either way, thanks, it worked!

90 degree up and down (Can't look further than straight down / up)

you wish to avoid gimbal lock. You may peform dot product of viewOrientation and upDirection vectors, if the absolute value of this dot product is too close to 1.0, stop applying the desired rotation to it (stuck it simply). Be aware though that if a very large rotation request comes, you may end up with smaller dot product but already on the opposite side, resulting in incorrect cross product of those two and bringing gimbal lock for view matrix - you may over come this by applying rather constant or enough small step in rotation requests.

More robust solution is rather less efective but always accurate to determine gimbal event.

C=normalize( viewdirection cross upvector)

D=normalize ( C cross viewdirection)

then if dot(D,upvector) is negative (or positive) view direction has crossed the up vector

Thanks, but using the dot product seems more like a workaround than an actual solution.

I'd like to be able to accurately limit it to 90 degrees, as in, it shouldn't just stop somewhere before the limit depending on the step/request size.

Surely that must be possible somehow?

Share on other sites

I'd like to be able to accurately limit it to 90 degrees, as in, it shouldn't just stop somewhere before the limit depending on the step/request size.

You cannot be looking  90 degrees exactly up. You end up with 0 vector as the cross product, what screws your situtaion just as if they crossed.

Limiting rotation is more up to your setup and design (would you limit a 361 rotation request that results in previous cross product still and so on).

Second method will tell you for sure whaether new vectors earns reverse cross product or not, whaether it is for limitng, refusing, or for "correcting" cross product of view matrix upon it.

Share on other sites

you wish to avoid gimbal lock.

You don't get gimbal lock when using quaternions.  That's the primary reason anyone uses them.  He could potentially introduce gimbal lock by converting from quaternions to euler angles, but as he's only doing this in an attempt to accomplish his clamp... there's no reason for him to introduce it to solve gimbal issues that couldn't exist if he wasn't doing it.  I suspect he's more likely building a game where being able to spin the camera farther than 90 degrees would break the illusion.  For instance, a first person <x>.

You cannot be looking  90 degrees exactly up. You end up with 0 vector as the cross product, what screws your situtaion just as if they crossed.

This is true in a mathematical sense, but not in a practical sense.  "Up" is completely arbitrary, and in scenarios where you are looking straight up you can easily change your view matrix to simply run against a different Up vector (like [1,0,0] for instance).  Pick an appropriate up vector such that the handedness of your system is maintained, and there will be no visible implications.  When you're not looking up anymore... change it back.  Most modern engines will handle this for you automatically.

I'd like to be able to accurately limit it to 90 degrees, as in, it shouldn't just stop somewhere before the limit depending on the step/request size.

The simplest answer would be to avoid using the quaternion as your starting point.  Save off your camera's roll, pitch, and yaw separately... then update those values instead of the values returned by your camera.  Apply your pitch clamp at that point, convert the resulting eulers to a quaternion and send that to your camera.  Note that in this scenario you can introduce the gimbal lock Johnny Code was concerned about.  If roll is not a valid input, using YZX as your rotation order will solve gimbal issues automatically.

Share on other sites

You don't get gimbal lock when using quaternions.  That's the primary reason anyone uses them.  He could potentially introduce gimbal lock by converting from quaternions to euler angles, but as he's only doing this in an attempt to accomplish his clamp... there's no reason for him to introduce it to solve gimbal issues that couldn't exist if he wasn't doing it.

Once one is building view matrix from up and direction vector, he has to consider paralelism or moment of up and direction vectors since their cross product is vital for view matrix. We can pointlessly speak as to what information (quaternion, angle coordinates) are interpreted to finaly build the view matrix.

Pick an appropriate up vector such that the handedness of your system is maintained, and there will be no visible implications.  When you're not looking up anymore... change it back.  Most modern engines will handle this for you automatically.

And this does not defy existence of gimbal lock event - it corrects it. I do not see any miracle conditions of not having to do that thanks to "using quaternions". I would repeat myself at this point

Second method will tell you for sure whaether new vectors earns reverse cross product or not, whaether it is for limitng, refusing, or for "correcting" cross product of view matrix upon it.

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• Forum Statistics

• Total Topics
633701
• Total Posts
3013443
×