• 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
RaidenF8

Converting from Quaternions to Euler Angles

11 posts in this topic

Hey guys,

I am trying to do the object manipulation of the character in the game that I'm currently working on. So, the object should always face the character while it is being carried. In order to this, I can easily set the rotation of the object to the rotation of the character. But Havok physics don't work this way, and whenever there is collision, physics would freak out.

In order for physics to work smoothly, I would get the current rotation of the object which is in quaternions. Then I would know my desired rotation in quaternion based on the rotation of the camera, and then I would apply angular impulse to the object in a way that it goes from current quaternion to desired quaternion.

The problem here is that ApplyAngularImpulse in havok gets euler angles as argument. So, what I did was that I converted the current and desired quaternions to Euler Angles, and then I substracted them, and applied the agular impulse to the object. (Of course I damped it so that it would stop in the desired rotation). However, this method only works for half of the space. Because when you convert from quaternions to euler angles, for half of the space it changes continiously, but for the other half, there are gaps between different parts of the space. So the object would rotate smoothly on the one half, but on the other, it wouldn't know which way to rotate because of those gaps.

I also have tried getting the quaternion that goes from current quaternion to the desired one, and then converting it to euler, and applying angular impulse based on that, but that also has a similar kind of problem.

I am really stuck here, and don't know what to do at this point. If anyone can point me in the right direction, I would really appreciate it.

Thank you.
0

Share this post


Link to post
Share on other sites
I don't fully understand your problem, but your description suggests a possible solution: Instead of converting both quaternions to Euler angles and then subtracting, try dividing the quaternions and then converting the result to Euler angles. [EDIT: Although a later sentence seems to indicate you have already tried that...]
0

Share this post


Link to post
Share on other sites
Thank you for your answer alvaro. But yeah, I have tried that too. So, here is what happens to the desired rotation after I convert to Euler angles:

For the half of space that it works:

y goes from -PI/2 to PI/2
x goes from (-PI/2 + alpha) (Looking Down) to (PI/2 - alpha) (Looking up)
Note that alpha exists because the degree of freedom for the camera is limited as we look all the way downwards or upwards.
z is zero

For the half of space that it doesn't work:

y goes from -PI/2 to PI/2
x goes from (PI/2 + alpha) to PI (This is for looking all the way down to looking straight) and from -PI to (-PI/2 - alpha) (This is for looking straight to looking all the way up)
Notice the gap between PI and -PI when looking straight
z has the same kind of behavior as x, only that it is even worse and it has much more gaps. It is either -PI or PI.

So, now you can see what goes bad when it wants to go from current to desired. Because of the gaps and these inconsistencies, it wouldn't know which direction to go, and then it would freak out.

When I say it has a similar problem when converting the quaternion that goes from current to desired into Euler angles, it is because of the fact that these gaps still exist in the converted quaternion.

Thank you for your time, I would really appreciate any help.
0

Share this post


Link to post
Share on other sites
Several things:[list]
[*]If you could isolate one instance where your computations do the wrong thing and post it, it would be much easier to help you.
[*]I don't think ApplyAngularImpulse gets Euler angles as input: An angular impulse is a three-dimensional vector and that's what the function takes.
[*]I have another idea as to what might be wrong: Are you aware that the same attitude can be represented with two quaternions, q and -q? Perhaps you need to flip the sign of one of them (target and current) to avoid a whole flip?
[/list]
0

Share this post


Link to post
Share on other sites
Thanks so much for the answer.
[list]
[*]For ApplyAngularImpulse you are absolutely right, it takes a three dimensional vector. But those three values would have the same effect of roll, pitch, and yaw in the Euler angles, please correct me if I'm wrong about this.
[*]About flipping the quaternions, I tried that too. But, the problem doesn't come from there. Let me give you an example of when the computations go wrong:
[/list]

[b]For the Good Side(the side that works):[/b]

[b]When looking all the way up:[/b]
[b]desired quaternion:[/b][list]
[*]x: 0.623505
[*]y: 0.18474
[*]z: -0.154878
[*]w: 0.743724167
[/list]
[b]converted desired euler:[/b][list]
[*]x: 1.39539
[*]y: 0.486941
[*]z: 0
[/list]

[b]When looking all the way down:[/b]
[b]desired quaternion:[/b][list]
[*]x: -0.602746
[*]y: 0.265235
[*]z: 0.222362
[*]w: 0.718959987
[/list]
[b]converted desired euler:[/b][list]
[*]x: -1.39539
[*]y: 0.70685
[*]z: 0
[/list]


[b]On the BAD Side:[/b]

[b]When looking all the way up:[/b]
[b]desired quaternion:[/b][list]
[*]x: 0.176007
[*]y: 0.737006
[*]z: -0.617873
[*]w: 0.209943473
[/list]
[b]desired euler:[/b][list]
[*]x: -1.7462
[*]y: 0.555019
[*]z: 3.14159
[/list]

[b]When looking all the way down:[/b]
[b]desired quaternion:[/b][list]
[*]x: -0.154882
[*]y: 0.743722
[*]z: 0.623505
[*]w: 0.184744284
[/list]
[b]converted desired euler: [/b][list]
[*]x: 1.7462
[*]y: 0.486954
[*]z: -3.14159
[/list]
So on the good size, when I move the cursor from all the way up to all the way down, z remains zero, and x goes from 1.39 to -1.39. So as you see, these values change continuously, there is no gap in between which is fine.

However on the bad side, when I move my cursor from all the way up to all the way down, x goes from -1.74 to -3.14(when looking straight). Then it goes from 3.14 to 1.74. So you see that gap of 2*PI when looking straight. z is even worst. When going from all the way up to all the way down, z is constantly jumping(it's either 3.14 or -3.14).

I hope this gives you a better understanding of what is going wrong right now. Because of these gaps that I have on the bad side, current euler cannot go to desired euler which makes the object to freak out. It is constantly changing the direction in which to rotate.

I would really appreciate it if anyone has a solution for this problem. Thank you.
0

Share this post


Link to post
Share on other sites
Also, this is how I calculate my euler based on the given quaternion q.

[code]
float q0 = q->getReal(); //w
float q1 = q->getImag().getComponent(0); //x
float q2 = q->getImag().getComponent(1); //y
float q3 = q->getImag().getComponent(2); //z
float euler_x = atan2(2.0*(q0*q1+q2*q3),1.0-2.0*(q1*q1 + q2*q2));
float euler_y = asin(2.0*(q0*q2-q3*q1));
float euler_z = atan2(2.0*(q0*q3+q1*q2),1.0-2.0*(q2*q2+q3*q3));
[/code]
0

Share this post


Link to post
Share on other sites
So you are converting the quaternions to Euler angles and then you are interpolating between them? That won't work: You interpolate between the quaternions, using either slerp or nlerp. Is that the problem? I don't see why you ever need to convert to Euler angles.
0

Share this post


Link to post
Share on other sites
No, I am not interpolating. I am not trying to get any values between my two quaternions. After I convert my current and desired quaternion to euler angles, I then do:
angular_impulse = desired_euler - current_euler;
applyAngularImpulse(angular_impulse);

If you think I do not have to convert to euler to apply this impulse, then how do you think I can get my angular_impulse based on my current_quaternion and desired_quaternion?

Thank you.
0

Share this post


Link to post
Share on other sites
You cannot subtract two euler angle triplets from each other to get a difference. That is, the expression "angular_impulse = desired_euler - current_euler;" is wrong math.

If your physics function wants the angular impulse input as an euler, first compute the difference as a quaternion (desired_quaternion * current_quaternion^-1), then convert that quaternion to euler and feed it to applyAngularImpulse.
0

Share this post


Link to post
Share on other sites
clb is right about the difference of Euler angles not making sense, and about how (desired_quaternion * conj(current_quaternion)) being the right thing to compute (the conjugate and the inverse are the same because we are dealing with unit quaternions). However, I don't think you need to convert to Euler angles at the end. You may want to compute the log of the result instead, or something like that. But I don't fully understand what you are doing, because the impulse is not something you can use to move from one position to another, but something that you can use to change from one velocity to another. Similarly, an angular impulse can be used to change from one angular velocity to another, not to go from one attitude to another. So I don't really understand what you are doing.
0

Share this post


Link to post
Share on other sites
Thank you guys for the answers.

First, I do not agree that the expression "angular_impulse = desired_euler - current_euler;" is wrong math. Because it seems to be working for half of the space. But since you are both emphasizing on this matter, I'm gonna drop it and focus on the quaternion: rotation_amount = desired_quaternion * current_quaternion^-1.

I made a youtube video of what is going wrong. Hopefully this would give you guys a better understanding of where the problem might come from. Here, I'm applying angular_impulse by just converting the quaternion that goes from current to desired to euler angles.

[media]http://www.youtube.com/watch?v=QwnWyWDx6OI[/media]


Also alvar, doing something on the quaternion itself might help, and it sounds interesting. I have also tried to do some stuff with it, but haven't got anything out of it. I haven't tried the log thing you mentioned yet. Would you please tell me how to get x,y,z for the impulse based on the rotation_amount quaternion and doing log?

Moreover, impulse or force/torque CAN be used to move from one position/rotation to the other. applyLinearForce is what I am using to change the position of the object while holding it. It is the same technique with current position and desired position, and applying linear impulse based on desired-current to get to the desired position. You can see in the video above that it works very smoothly for the position of the object. It's also working for the rotation but unfortunately only for half of the space.

Thanks again, and I appreciate your help [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
The impulse is not in euler angles but a Vector3 that describes the [b]rotational axis[/b].
You can get the quaternion that will rotate from q1 to q2 by:

q' = q1[sup]-1[/sup]q2

then convert q' to angle-axis and put it in ApplyAngularImpulse.

Basically, Euler angles are just a more human readable form of rotation and you should never have to use them directly in your code.
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