- Home /
reproducing hingejoint.angle
Hi, I currently writing my own Hinge Joint implementation (don't ask me why, I have my reasons). So far i works pretty well, I'm just struggling with my old nemesis Quaternions when trying to write a method that returns the Hinge Joint Angle. My code so far looks like this:
Rigidbody thisBody;
Rigidbody otherBody;
HingeJoint thisJoint;
Quaternion origRot;
void Start ()
{
thisBody = GetComponent<Rigidbody>();
thisJoint = GetComponent<HingeJoint>();
otherBody = thisJoint.connectedBody;
origRot = Quaternion.Inverse(thisBody.rotation)*otherBody.rotation;
}
float getAngle()
{
Quaternion betweenBodies = Quaternion.Inverse(otherBody.rotation) * thisBody.rotation;
Quaternion difToOrig = Quaternion.Inverse(origRot) * betweenBodies;
float angle;
Vector3 axis;
difToOrig.ToAngleAxis(out angle, out axis);
return angle;
}
I'm trying to compare the current rotation to the rotation at the start (rotation is measured as the difference in orientation between the two bodies). Since this orientation can only change around the hinge axis over time I use Quaternion.ToAngleAxis
to get the current hinge angle (I hereby assume that the angle at the start is always 0). When I compare the values to unitys builtin hingejoint.angle
I match the absoulute values, but for some reasons my method always returns positive values for the angle.
I think you have to check the axis variable. When angle is negative, axis should be the inverse of your joint.axis.