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

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!