Uninomicon

Documenting the dark corners of the Unity Engine.

User Tools

Site Tools


hingejoint

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Last revision Both sides next revision
hingejoint [2021/05/01 16:46]
73.95.178.156
hingejoint [2021/05/01 16:58]
73.95.178.156
Line 1: Line 1:
 ====== HingeJoint ====== ====== HingeJoint ======
  
-=== Misconceptions === 
   * ''HingeJoint'' does not constrain the absolute ''rotation'' or ''localRotation'' of the Transform that it is attached to. It constrains the **relative** rotation between its own RigidBody and the Rigidbody in the ''ConnectedBody'' field.   * ''HingeJoint'' does not constrain the absolute ''rotation'' or ''localRotation'' of the Transform that it is attached to. It constrains the **relative** rotation between its own RigidBody and the Rigidbody in the ''ConnectedBody'' field.
  
-  * ''HingeJoint.angle'' and ''HingJoint.axis'' do not represent the rotation of the Transform that it is attached to. Ex: ''Quaternion.AngleAxis(hinge.angle, hinge.axis)'' is not equivalent to''hinge.transform.rotation'' or ''hinge.transform.localRotation''. +  * ''HingeJoint.angle'' and ''HingJoint.axis'' do not represent the rotation of the Transform that it is attached to. Ex:<code csharp>Quaternion.AngleAxis(hinge.angle, hinge.axis)  
- +// is *notequivalent to either of 
-These two misconceptions, taken together, can make a ''HingeJoint'' behave quite differently from what you might expect. +hinge.transform.rotation 
 +hinge.transform.localRotation 
 +</code> 
  
 To quote the [[https://docs.unity3d.com/ScriptReference/HingeJoint-angle.html|official docs]]: To quote the [[https://docs.unity3d.com/ScriptReference/HingeJoint-angle.html|official docs]]:
Line 12: Line 13:
 > The rest angle between the bodies is always zero at the beginning of the simulation. > The rest angle between the bodies is always zero at the beginning of the simulation.
  
-The consequence of this is that ''HingeJoint.angle'' does not depend on the starting orientation of either the joint's ''RigidBody'' or the connected ''RigidBody''. And because ''HingeJoint.limits'' represents limits on ''HingeJoint.angle'', the limits are also independent of any starting orientation of the bodies. +The consequence of this is that ''HingeJoint.angle'' does not depend on the starting orientation of the joint's ''RigidBody'' or the connected ''RigidBody''. And because ''HingeJoint.limits'' represents limits on ''HingeJoint.angle'', the limits are also independent of any starting orientation of the bodies. 
  
 An example: Consider a ''GameObject'' with rotation ''(0, 0, 0)'' and a ''HingeJoint'' on the x-axis with limits of ''[-90, 90]''. When played as-is, this ''GameObject'' will be free to rotate between ''(-90, 0, 0)'' and ''(90, 0, 0)''. However, if you rotate this ''GameObject'' in the editor such that it starts with rotation ''(90, 0, 0)'' and play it, the ''GameObject'' will then be free to rotate between ''(0, 0, 0)'' and ''(180, 0, 0)'' - even though the angle limits have not changed. An example: Consider a ''GameObject'' with rotation ''(0, 0, 0)'' and a ''HingeJoint'' on the x-axis with limits of ''[-90, 90]''. When played as-is, this ''GameObject'' will be free to rotate between ''(-90, 0, 0)'' and ''(90, 0, 0)''. However, if you rotate this ''GameObject'' in the editor such that it starts with rotation ''(90, 0, 0)'' and play it, the ''GameObject'' will then be free to rotate between ''(0, 0, 0)'' and ''(180, 0, 0)'' - even though the angle limits have not changed.
  
-=== Things That May Solve Problems With HingeJoint: ===+=== HingeJoints with moving parents: ===
  
-In the vast majority of use cases, your ''HingeJoint'' should have a ''ConnectedBody'' assigned. This field is optional, but if left empty it causes your ''HingeJoint'' to be anchored to the global physics coordinate system. If your ''HingeJoint'' happens to be a child of some other object that moves, the joint will continue applying its limits within the global physics coordinate system - not in the joint's local coordinate system. This is usually not what you want.+In most cases, ''HingeJoint'' should have a ''ConnectedBody'' assigned. If left empty it causes your ''HingeJoint'' to be anchored to the global physics coordinate system. If your ''HingeJoint'' is the child of an object that moves, the joint will apply its limits within the global physics coordinate system - not in the joint's local coordinate system.
  
-A good way to ensure that your ''HingeJoint'' behaves nicely is to create a ''RigidBody'' on the parent object (if one doesn't already exist) and set that as the ''ConnectedBody''. The parent body can have ''IsKinematic'' enabled if you don't want it to act like a physics object. In this configuration, the ''axis'' and ''angle'' of the ''HingeJoint'' will actually correspond to the ''localRotation'' of the ''Transform'' (though it will still be relative to any initial ''localRotation''). This makes the joint behavior much easier to reason about!+To ensure that your ''HingeJoint'' behaves nicelycreate a ''RigidBody'' on the parent object and set that as the ''ConnectedBody''.((The parent body can have ''IsKinematic'' enabled if you don't want it to act like a physics object.)) In this configuration, the ''axis'' and ''angle'' of the ''HingeJoint'' will correspond to the ''localRotation'' of the ''Transform'' ((though it will still be relative to any initial ''localRotation'')). This makes the joint behavior much easier to reason about!
  
hingejoint.txt · Last modified: 2021/05/01 16:59 by 73.95.178.156