- Home /
how to get the signed angle between two quaternion?
Hi,
Quaternion.Angle returns the absolute angle between two transform.rotation. Is there a technic to get it signed, I am currently using cross products and vector comparison to check. It seems to me very cumbersome.
Is there a better technic or something built in actually?
Thanks for your help,
Jean
Answer by duck · Sep 07, 2010 at 10:30 AM
To get the unsigned angle between two quaternions, you can use the Quaternion.Angle function:
var angle = Quaternion.Angle( rotationA, rotationB );
However to get a signed angle doesn't really make a lot of sense in 3D space, because the rotation could be in any 3d direction (not just in one of two 'flat' directions).
Unity does however provide the function "Mathf.DeltaAngle" to get the signed difference between two angles (not rotations), so perhaps what you want to do is convert your 3D rotations into 2D angles relative to a certain direction on a certain plane (using Atan2). For example, to compare the angle of rotationA with rotationB, projected on the X-Z plane, you could do this:
// get a "forward vector" for each rotation var forwardA = rotationA * Vector3.forward; var forwardB = rotationB * Vector3.forward;
// get a numeric angle for each vector, on the X-Z plane (relative to world forward) var angleA = Mathf.Atan2(forwardA.x, forwardA.z) Mathf.Rad2Deg; var angleB = Mathf.Atan2(forwardB.x, forwardB.z) Mathf.Rad2Deg;
// get the signed difference in these angles var angleDiff = Mathf.DeltaAngle( angleA, angleB );
Angle differences on the X-Z plane would be suitable for "top down" angle differences - eg, determining the directional differences between cars on a terrain. For other situations, you might need to use a different plane such as X-Y or Y-Z.
Hope this solves your problem!
Thanks Duck, yes, signed angle make sense only against a plane or an axis, that's what I am am after. The code is as involving as is $$anonymous$$e currently.
Super old answer, but it was the solution to getting AngularSpeed into $$anonymous$$ecanim so that a Nav$$anonymous$$esh can control the movement, $$anonymous$$ecanim can still show the proper animations. (This is because the "S$$anonymous$$lth" tutorial 4.3 is not out yet, so I had to figure it out myself)
Hey, Duck, sorry to bother you, but for this example, is it the Z rotation? And are you assu$$anonymous$$g that the rotation is in eulerAngles?
Adding onto this, my code doesn't seem to be working: using UnityEngine; using System.Collections;
public class positionmang3 : $$anonymous$$onoBehaviour {
private Quaternion transRotTemp;
private Quaternion gyroRotTemp;
private Vector3 transRotVect;
private Vector3 gyroRotVect;
private float gyroAngleTemp;
private float transAngleTemp;
void Start () {
Input.gyro.enabled = true;
}
void Update () {
/*gyroDiff = remote.localRotation.eulerAngles - transform.localRotation.eulerAngles;
transform.rotation *= Quaternion.AngleAxis (gyroDiff.x, transform.right);
transform.rotation *= Quaternion.AngleAxis (gyroDiff.y, transform.up);
transform.rotation *= Quaternion.AngleAxis (gyroDiff.z, transform.forward);*/
transform.rotation *= Quaternion.AngleAxis (rotDiff (transform.rotation, Input.gyro.attitude, "x"), transform.right);
Debug.Log(rotDiff(transform.rotation, Input.gyro.attitude, "x"));
}
float rotDiff (Quaternion transRot, Quaternion gyroRot, string axis) {
//TE$$anonymous$$P
transRotTemp = transRot;
gyroRotTemp = gyroRot;
if (axis == "x") {
transRotVect = transRotTemp.eulerAngles;
gyroRotVect = gyroRotTemp.eulerAngles;
transRotVect = Vector3.Scale(transRotTemp.eulerAngles, transform.forward);
gyroRotVect = Vector3.Scale(gyroRotTemp.eulerAngles, transform.forward);
gyroAngleTemp = $$anonymous$$athf.Atan2 (gyroRotVect.y, gyroRotVect.z) * $$anonymous$$athf.Rad2Deg;
transAngleTemp = $$anonymous$$athf.Atan2 (gyroRotVect.y, gyroRotVect.z) * $$anonymous$$athf.Rad2Deg;
return $$anonymous$$athf.DeltaAngle (gyroAngleTemp, transAngleTemp);
} else {
Debug.LogError ("ERROR: Invalid axis in rotDiff() function");
return 0;
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613072829im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Calculate Angle: From & To 3 Answers
how do I limit the angle that a bone can move using quaternions? 2 Answers
Coroutine with Quaternion.Angle 1 Answer
Rotate vector around vector? 2 Answers