# how to get the signed angle between two quaternion?

 0 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 more ▼ asked Sep 07 '10 at 07:29 AM Jean Fabre 3.1k ● 68 ● 74 ● 103 add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

 0 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! more ▼ answered Sep 07 '10 at 10:30 AM duck ♦♦ 40.9k ● 92 ● 148 ● 415 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 mine currently. Sep 07 '10 at 12:05 PM Jean Fabre I love you. Seriously, thanks a lot! Dec 30 '12 at 09:07 PM Saturnix add new comment (comments are locked) 10|3000 characters needed characters left ▼ Viewable by all users

By Email:

Topics:

x436
x286
x5

asked: Sep 07 '10 at 07:29 AM

Seen: 5782 times

Last Updated: Dec 30 '12 at 09:07 PM