Search Unity

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

Discussion in 'Physics for ECS' 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. florianhanke

    florianhanke

    Joined:
    Jun 8, 2018
    Posts:
    426