Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Unstable Fixed Joint

Discussion in 'Physics' started by amidofu, Jun 27, 2018.

  1. amidofu

    amidofu

    Joined:
    Mar 1, 2016
    Posts:
    11
    Hi,

    We tried to use Fixed Joint between a cube and the VR controller, we found that if the cube is blocked by other obstacles and the controller is away from the cube, the cube starts to jump around (please see the images below). We except the cube to be stable on the obstacle, is there any settings of the Fixed Joint to achieve that?

    BTW, we don't quite understand the usage of "Enable Preprocessing", we checked the docs
    https://docs.unity3d.com/Manual/class-FixedJoint.html but still don't know what it means, can anyone explain it?

    Thank you

    fixed_joint_setting.png
    unstable.gif
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,430
    You shouldn't use a fixed joint then. The fixed joint wants the green cube to be inside the blue cube. As this cannot happen, the physics solver goes nuts trying to resolve the conflict (basically, it puts the green cube outside the blue cube, but the joint keeps putting it inside).

    To achieve the effect you're looking too, you should use a Spring Joint instead. The spring joint allows some distance between the anchor and the attached object, as if they were connected with a spring.
     
  3. amidofu

    amidofu

    Joined:
    Mar 1, 2016
    Posts:
    11
    Hi Edy,

    Thanks for your suggestion. We tried Spring Joint but it turns out that it doesn't meet our expectation when the green cube is not touching anything except the controller, the green cube will swing around but we want the green cube to be static relative to the controller in that case .

    Now we use Configurable Joint with script to control joint's behavior and meet our requirement. We set the XYZ motion of the Configurable Joint to be Free when the green cube touches the static collider(the blue cube) and set the XYZ motion to Locked when the green cube doesn't not touch anything. We also use JointDrive which can attract the cube to the controller when the XYZ motion is free.

    Here is the code snippet:

    Code (CSharp):
    1.     private ConfigurableJoint AddConfigurableJoint()
    2.     {
    3.         objectInHand.transform.position = this.transform.position;
    4.         objectInHand.transform.rotation = this.transform.rotation;
    5.  
    6.         ConfigurableJoint cf = gameObject.AddComponent<ConfigurableJoint>();
    7.         cf.anchor = new Vector3(0, 0, 0);
    8.         cf.autoConfigureConnectedAnchor = false;
    9.         cf.connectedAnchor = new Vector3(0, 0, 0);
    10.  
    11.         cf.xMotion = ConfigurableJointMotion.Locked;
    12.         cf.yMotion = ConfigurableJointMotion.Locked;
    13.         cf.zMotion = ConfigurableJointMotion.Locked;
    14.         cf.angularXMotion = ConfigurableJointMotion.Locked;
    15.         cf.angularYMotion = ConfigurableJointMotion.Locked;
    16.         cf.angularZMotion = ConfigurableJointMotion.Locked;
    17.  
    18.         JointDrive xjd = new JointDrive();
    19.         xjd.positionSpring = 1000.0f;
    20.         xjd.maximumForce = 10000.0f;
    21.         cf.xDrive = xjd;
    22.  
    23.         JointDrive yjd = new JointDrive();
    24.         yjd.positionSpring = 1000.0f;
    25.         yjd.maximumForce = 10000.0f;
    26.         cf.yDrive = yjd;
    27.  
    28.         JointDrive zjd = new JointDrive();
    29.         zjd.positionSpring = 10.0f;
    30.         zjd.maximumForce = 10000.0f;
    31.         cf.zDrive = zjd;
    32.  
    33.         JointDrive rxjd = new JointDrive();
    34.         rxjd.positionSpring = 10.0f;
    35.         rxjd.maximumForce = 10000.0f;
    36.         cf.angularXDrive = rxjd;
    37.  
    38.         JointDrive ryzjd = new JointDrive();
    39.         ryzjd.positionSpring = 10.0f;
    40.         ryzjd.maximumForce = 10000.0f;
    41.         cf.angularYZDrive = ryzjd;
    42.  
    43.         return cf;
    44.     }
    Code (CSharp):
    1.     void checkEnterStaticCollider(Collider other)
    2.     {
    3.         if (!other.isTrigger && null == other.attachedRigidbody)
    4.         {
    5.            
    6.             ConfigurableJoint cf = gameObject.GetComponent<ConfigurableJoint>();
    7.             if (null != cf)
    8.             {
    9.                 cf.xMotion = ConfigurableJointMotion.Free;
    10.                 cf.yMotion = ConfigurableJointMotion.Free;
    11.                 cf.zMotion = ConfigurableJointMotion.Free;
    12.                 cf.angularXMotion = ConfigurableJointMotion.Free;
    13.                 cf.angularYMotion = ConfigurableJointMotion.Free;
    14.                 cf.angularZMotion = ConfigurableJointMotion.Free;
    15.             }
    16.         }
    17.     }
    18.  
    19.     void checkExitStaticCollider(Collider other)
    20.     {
    21.         if (!other.isTrigger && null == other.attachedRigidbody)
    22.         {
    23.            
    24.             ConfigurableJoint cf = gameObject.GetComponent<ConfigurableJoint>();
    25.             if (null != cf)
    26.             {
    27.                 cf.xMotion = ConfigurableJointMotion.Locked;
    28.                 cf.yMotion = ConfigurableJointMotion.Locked;
    29.                 cf.zMotion = ConfigurableJointMotion.Locked;
    30.                 cf.angularXMotion = ConfigurableJointMotion.Locked;
    31.                 cf.angularYMotion = ConfigurableJointMotion.Locked;
    32.                 cf.angularZMotion = ConfigurableJointMotion.Locked;
    33.             }
    34.         }
    35.     }
     
    Deleted User and Edy like this.