Answers for "Angles from quaternion/vector Problem."
http://answers.unity.com/questions/599393/angles-from-quaternionvector-problem.html
The latest answers for the question "Angles from quaternion/vector Problem."Answer by HideAndSeek
http://answers.unity.com/answers/1158105/view.html
This answer might be a little late, but in case anyone needs it:
If you are searching for the signed angle between two quaternions where you know the axis of rotation (since signed angles without known rotation axis make no sense) and assuming you actually only rotate around that arbitrary but specific axis, you can simply do the following:
- Compute the Quaternion.ToAngleAxis of your current rotation that gives you the unsigned angle and the rotation axis in world space
- Compute the Vector3.Angle between your known "axisOfRotation" (given in world space) and the computed rotation axis by Quaternion.ToAngleAxis
- If Vector3.Angle is greater than 90° (usually 180°), you know that the computed axis by Quaternion.ToAngleAxis points the other way around.
- Normalize the angle again to [-180,180] since one quaternion rotation cycle contains 720° (for example, from (1,0,0) to (-1,0,0))
Simple as that :) Here goes the code:
public static float GetSignedAngle(Quaternion A, Quaternion B, Vector3 axis) {
float angle = 0f;
Vector3 angleAxis = Vector3.zero;
(B*Quaternion.Inverse(A)).ToAngleAxis(out angle, out angleAxis);
if(Vector3.Angle(axis, angleAxis) > 90f) {
angle = -angle;
}
return Mathf.DeltaAngle(0f, angle);
}
Hope this works for anyone who reads this in future :)
Note again: This code really assumes that you actually only rotate around the axis of rotation! Otherwise, the computed angle might not be correct anymore.
Cheers!Sun, 20 Mar 2016 16:31:37 GMTHideAndSeekAnswer by KellyThomas
http://answers.unity.com/answers/599417/view.html
Quaternion.Angle will return the smallest angle connecting to two rotations with no regard to axes. The number will be in the range: 0 <= n <= 180
If you want the signed angles for each axes the you need to convert your rotations to euler angles:
private Vector3 EulerAnglesBetween(Quaternion from, Quaternion to) {
Vector3 delta = to.eulerAngles - from.eulerAngles;
if (delta.x > 180)
delta.x -= 360;
else if (delta.x < -180)
delta.x += 360;
if (delta.y > 180)
delta.y -= 360;
else if (delta.y < -180)
delta.y += 360;
if (delta.z > 180)
delta.z -= 360;
else if (delta.z < -180)
delta.z += 360;
return delta;
}
This starts with a simple difference then corrects the results to compensate for those cases when the smallest angle crosses the 0 degree / 360 degree threshold.
Each component of the Vector3 will be in the range: -180 <= n <= 180Wed, 18 Dec 2013 16:39:24 GMTKellyThomas