- Home /
Quaternion and rotation angles
Good afternoon. I have the following expression:
Quaternion myq = Quaternion.Euler(0, 0, zAn) * Quaternion.Euler(0, yAn, 0) * Quaternion(xAn, 0, 0);
Prompt how to solve the reverse problem. I have a value of a quaternion of myq, and it is necessary to find angles xAn, yAn, zAn(xAn=?, yAn=?, zAn=?). Maybe formula or function of Unity. Thanks in advance for the help.
Answer by Benproductions1 · Dec 31, 2013 at 06:12 AM
Hello,
When using an API you are not used to, it's a good idea to read through the documentation of something before you use it. Then you wouldn't have to ask questions and waste your own time :)
If you take a look at the documentation for Quaternion
here, you can see that .Euler
makes a new quaternion rotation with euler-angles, and the field eulerAngles
gets the euler-angles from a quaternion.
Note that there are multiple euler-angles for every Quaternion so it might not be what you expect :)
Hope this helps,
Benproductions1
@Benproductions1 If you use Debug.log for myqD.eulerAngles, that you will see values not similar to xAn, yAn, zAn. Can do it the next bug of Unity4.1.2, but precisely I don't know.
@Benproductions1 Thanks. I just started doing the function of conversion of a quaternion in Euler angles. As I noted, quaternions written are equal below:
Quaternion myq = Quaternion.Euler(0, 0, zAn) * Quaternion.Euler(0, yAn, 0) * Quaternion(xAn, 0, 0);
Quaternion myqD = Quaternion.Euler(xAn, yAn, zAn);
Thus, myq = myqD. But Euler angles of these quaternions are various, i.e. (myq.eulerAngles != myqD.eulerAngles). $$anonymous$$y function of converting becomes the decision as I think.
Actually, because of the way the conversion works in Unity, any equal Quaternions
will always give the same EulerAngle
representation. to be more accurate:
Quaternion(x, y, z).eulerAngles != Vector3(x, y, z)
@Benproductions1 I tried to check how Unity calculates quaternions. I found conversion formulas (on wiki). From written above, myq quaternion I receive object orientation in space (being guided by three angles of xAn, yAn, zAn - physical orientation). If to be coordinated with formulas, it turns out that myq=myqD. Though in Unity it not so. Probably, there it is considered on another as y axes and z are traded places (that are this on wiki).
Answer by robertbu · Dec 31, 2013 at 10:05 AM
As @Benproductions1 says, there are multiple euler angle representations for any given "physical" rotation. An eularAngles are derived from the Quaternion, so euler values are not saved. For example, run this code:
transform.eulerAngles = Vector3(180,0,0);
Debug.Log(transform.eulerAngles);
The value you will get back is (0,180,180)...the same physical rotation, but in a representation different then the one you set.
One solution is to preserve your own Vector3 and treat eulerAngles as write-only. For example:
public var speed = 20.0;
private var angles = Vector3.zero;
function Update() {
angles.z += Input.GetAxis("Horizontal") * speed * Time.deltaTime;
angles.x += Input.GetAxis("Vertical") * speed * Time.deltaTime;
transform.eulerAngles = angles;
}
Note that changes are always made in 'angles' and 'eulerAngles' is then set (but never read). If I want the euler values, I can read them from 'angles'. I can always manipulate or normalize 'angles' for whatever my task. This technique does not work for every situation.
@robertbu That you wrote that remarkably is suitable for the majority of cases. But unfortunately not for me. Initially I have the initial quaternion (from a gyroscope). But I need to take from it angles as my platform rotates like a gyroscope, but with angles ((x, 0, y) from a gyroscope).
Your answer
![](https://koobas.hobune.stream/wayback/20220613125149im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Quaternion.FromToRotation misunderstanding 2 Answers
quaternion.eulerAngles 1:1 angle conversion 1 Answer
How to Constrain Quaternion Rotations Over Time? 1 Answer
Quaternion.Angle and 360 degrees 1 Answer
Calculate rotation angle for two side of cube ( like dice ) from Quaternion.identity 0 Answers