So I had the following simple function in my code:
bool CheckAttachment(Vector3 pos1, Quaternion rot1, Vector3 pos2, Quaternion rot2) {
return Quaternion.Angle(rot1, rot2) > (180-MaxSnapAngle) Vector3.Distance(pos1, pos2) < MaxSnapDistance;
}
For the purpose of checking if two objects were close enough together, and back-to-back, to see if they should be considered attached. However, it was falsely attaching objects which were close enough, but at 90 degree angles from each other. In order to debug this further, I changed the function, as follows, to get some debugging output:
bool CheckAttachment(Vector3 pos1, Quaternion rot1, Vector3 pos2, Quaternion rot2) {
float angle = Quaternion.Angle(rot1, rot2);
float dist = Vector3.Distance(pos1, pos2);
if (angle > (180-MaxSnapAngle) dist < MaxSnapDistance) {
Debug.Log("Found Connection - Angle " + angle + ", 1st Angle " + Quaternion.Angle(Quaternion.identity, rot1) + ", 2nd Angle " + Quaternion.Angle(Quaternion.identity, rot2) + ", Dist " + dist);
return true;
}
return false;
}
And I got this output:
Found Connection - Angle 180, 1st Angle 90, 2nd Angle 180, Dist 0
How is it possible for a quaternion to be 180 degrees from the identity quaternion, and ALSO 180 degrees for a quaternion that’s itself 90 degrees away from the identity quaternion? Does it somehow count different “roll” as part of the angle difference?