Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[DOTS] Best way to set Unity.Physics limited hinge min/max angle constraint?

Discussion in 'Physics Previews' started by florianhanke, Aug 29, 2019.

  1. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Hi all,

    I have a piece of ugly looking code that I'm unsure if it's the best way to go – perhaps one of you has a better solution.

    I have car-related physics code in which I have a spindle attached to the car's chassis, which turns on a limited hinge joint. I'm using the PhysicsJoint's min/max constraint to turn the spindle.

    So in my VehicleMechanicsSystem, what I am doing is iterate over the PhysicsJoint-s, and if they are a spindle, set the constraints:

    Code (CSharp):
    1. public void Execute(ref PhysicsJoint physicsJoint)
    2. {
    3.     var potentialSpindleEntity = physicsJoint.EntityA;
    4.     if (spindleComponentData.Exists(potentialSpindleEntity))
    5.     {
    6.         var spindle = spindleComponentData[potentialSpindleEntity];
    7.      
    8.         var targetRotRad = spindle.desiredAxisRotationRad;
    9.         var constraints = physicsJoint.JointData.Value.Constraints[0];
    10.         constraints.Min = targetRotRad - 0.01f;
    11.         constraints.Max = targetRotRad + 0.01f;
    12.         physicsJoint.JointData.Value.Constraints[0] = constraints;
    13.      
    14.         spindleComponentData[potentialSpindleEntity] = spindle;
    15.     }
    16. }
    What's bothering though is the `physicsJoint.JointData.Value.Constraints[0]` – this can't be the right way to set the Min and Max…? Or is it?

    Thanks for any help!

    P.S: Setup of the GO (converted into entities):
    Screenshot 2019-08-29 10.25.57.png
     
  2. MaxAbernethy

    MaxAbernethy

    Joined:
    Mar 16, 2019
    Posts:
    53
    Hi Florian, there's no higher level API to edit the joints so you do have to do it like this. I agree it's not a nice interface and I logged an issue.
     
    florianhanke likes this.
  3. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    Hi Max, thanks a lot for letting me know! :)
     
  4. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426
    I've rewritten the above for the new Physics 0.4.0 joints, and I'm wondering if this is much better now. I'm still quite bothered by the `constraints[0]`, since I need to know the index of the constraint – is this the right way of using the interface? Are the constraints always ordered in a specific way for a specific joint?

    Code (CSharp):
    1. var potentialWheelEntity = physicsBodyPairs[j].EntityA;
    2. if (controlEntity == potentialWheelEntity)
    3. {
    4.     // The wheel entity is the controlled entity...
    5.     var constraints = physicsJoints[j].GetConstraints();
    6.     var constraint = constraints[0];
    7.     constraint.Min = targetSteeringLocal - 0.01f;
    8.     constraint.Max = targetSteeringLocal + 0.01f;
    9.     physicsJoints[j].SetConstraints(constraints);
    10. }
     
    Ruchir and Rellfy like this.
  5. Ruchir

    Ruchir

    Joined:
    May 26, 2015
    Posts:
    933
    Any updates?
     
  6. petarmHavok

    petarmHavok

    Joined:
    Nov 20, 2018
    Posts:
    461
    Look at PhysicsJoint.k_... constants (k_LimitedDistanceRangeIndex for example), it gives you indices of particular constraints in the array. Hope that helps.
     
    Ruchir likes this.
  7. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    There was a Joint Modification test scene added that is worth checking on along with the associated script.