• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Silverlan

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

5 posts in this topic

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).

 

Let's start with limiting the player's pitch axis.

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.

0

Share this post


Link to post
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).

 

To your first question:

 

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

2

Share this post


Link to post
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!
 
 

To your first question:

 

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?

0

Share this post


Link to post
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.

0

Share this post


Link to post
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.

-1

Share this post


Link to post
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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0