- Home /
Is there no way to get reliable angles from a rigidbody?
I need to get the current angles of a rigidbody so I can apply countertorque to keep it at a particular orientation. Is there really no way to get reliable Euler angles from the rigidbody? Unlike most of the rest of my game, I can't simply keep the current angles in variable and modify them myself because the physics controls the rigidbody.
If there's no way, then why not? It's unbelievably frustrating that everything is stored as stupid Quaternions, when there's no way to get Euler angles from them.
I can't believe I've put this much effort into Unity for nothing.
EDIT:
I don't mean "how do I get Euler angles from a Quaternion", I mean "how do I get reliable Euler angles from a Quaternion".
what are you reading the rotation from? is it just the rigidbody.rotation or is it some kind of rotational speed? If you are using AddTorque like you mentioned in a comment, it doesn't take a rotation, it takes an axis to rotate around I believe so you may want to use Quaternion.ToAngleAxis or whatever it is.
The only time I apply rotational torque is when I think the object may be blocked by something, and need physics to prevent overlap. The math is a pain. Otherwise RotateTowards works fine (in a restricted area, it can force long objects to spin through walls.)
Otherwise I think a lot of people use joints, which compute the torque, etc... for you.Like connecting the front of your object with a spring to an empty always in line with the target.
A few days ago for a question about a HUD display for a plane, I worked out some code for Heading (Yaw), $$anonymous$$ch, and Roll. The code is only lightly tested, and I have not tested it in conjunction with applying angular velocity of a Rigidbody, but it might be helpful for you:
http://answers.unity3d.com/questions/696271/how-create-modern-aircrafts-hud.html
I totally understand the frustration, there are a lot of solutions for tracking the Euler angles of a object you are modifying, but nothing reliable to go the other-way.
I am dealing with hingeJoints that take a angle as an input and there seems to be no way to get or track the rotation of a transform without horrid issues.
You are not alone :(
Answer by homer_3 · Apr 28, 2014 at 03:13 AM
Why did you post that? The problem is not getting Euler angles from Quaternions, the problem is getting them reliably. I did say that in my question...
You asked
Is there really no way to get reliable Euler angles from the rigidbody?
And there is, using the API I linked.
homer_3: the issue happens when you try to look up and use things like transform.rotation.euler.x
and get a "random" result. It's a very common problem here.
To see it, take an unrotated object and tip it forward (around x.) You'll see that after x hits 90, y and z flip to -180 and x starts going back to 0 from 90. Typing in (110,0,0) works, but snaps to (70,-180,-180) if you move a tiny bit. So for that angle, euler.x is 70 or 110, depending.
This is why the docs say not to read or set just one eulerAngle.
@Owen Reynolds Wait, what? Are you saying the angles are reliable if you extract all three at once using rotation.eulerAngles
, or are you saying that while the resultant rotation is identical, the individual parts of the Euler may change randomly?
@homer_3 I apologise for being so rude. I was just frustrated that there was no possible way to actually use the rotations stored in the rigidbody. So...I apologise.
Yes and yes. It's like someone who might tell you the time as 10:45, then a $$anonymous$$ute later as "14 to eleven." But, I've never seen anyone extract all three Euler-angles as a whole and do anything useful with them.
Answer by Owen-Reynolds · Apr 28, 2014 at 05:39 AM
Unity didn't invent Quaternions. It uses them because other game engines do, because they're considered the best way to do 3D angles.
For real, there isn't a 1-1 mapping between Quaternions and the YXZ "Euler" representation. When you pull out a Euler angle, there's no way to know which of the 2(?) possible results you want. You can't get consistent results, because of math.
You can project transform.forward onto planes and compute using trig. Of course, you'll get huge snaps if the object faces the main axis (this is what Quaternions avoid.) Easier is just to do everything in Quaternions. Save the wanted facing as a Quaternion, and figure out how to push from one Quat to another (I use X-product of their "forwards". Oddly, it works as angular velocity numbers.)
1st paragraph: I know, I know...I'm sorry, I didn't mean to be so angry in my question, I was just frustrated (which is no excuse). I mean, there's no way to get the Euler angles out of a rigidbody. I just would have thought someone would have come up with a better solution for rotations by now, given they are so important in games. If they insist on using Quats internally (which does make sense, of course; Quats are a great way to represent rotations), then they need to make the rest of their commands Quaternion-compatible. That is, I need to be able to input a Quat into RigidBody.AddTorque()
to push physics object around.
2nd paragraph: Yeah...I feel like I should just give up on my project because apparently it's physically impossible to get consistent results from rotation.eulerAngles
... I really have no idea what else to do.
3rd paragraph: I have no idea what you just said there... ;)
I have the most luck using a combination of Quaternions and "facing vectors." For example, transform.forward
is the way you're facing, and target.position - my.position
is the way I want to be facing. Then a built-in can give you the angle (Quaternion) between them. But that takes practice and a trig review.
@Owen Reynolds I have no idea what you actually mean (what's a built-in?), although I've been racking my brain for clever alternate ways of achieving what need, and the idea of using position to find angles independent of stored rotations seems like a good place to start. Besides, your method would create a Quaternion, the very thing I'm trying to avoid, as I need Euler angles to calculate torque to apply to a rigidbody.
You can convert quaternion to euler and vice-versa using
Quaternion.Euler(euler angle in vector3)
Quaternion.eulerAngles = new Vector3();
Now there is also a correspondance between quaternion and euler, it is no magic, so if you want to work your own: http://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles but it is waht those two previous methods are doing.
To be honest, I don't really understand your issue, maybe a clear situation would help finding a solution. $$anonymous$$aybe there is a simple way to achieve what you are after but you may be going the wrong direction.