Search Unity

Fixed Joint behaves like Ball and Socket 2D

Discussion in 'Physics for ECS' started by argibaltzi, Nov 20, 2020.

  1. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    Hi
    I have a strange situation where my fixed joint behaves like a ball and socket would (more or less)

    I connect 2 cubes and i expect them to move and rotate as one object however they both rotate individually. The positions look fine, just rotations

    I compared to the Joints Demo (RIGID0 + RIGID 1) and i have similar setup and data in the debugger....
    I create the joint like this

    Code (CSharp):
    1.  public static Entity CreateFixedJoint(Entity primary, Entity connected, float3 primaryAnchorA, float3 primaryAnchorB, bool collisionBetweenAandB)
    2.     {
    3.         PhysicsJoint joint = PhysicsJoint.CreateFixed(
    4.             new RigidTransform(float3x3.identity, primaryAnchorA),
    5.              new RigidTransform(float3x3.identity, primaryAnchorB));
    6.  
    7.         Entity jointEntity = Game.Instance.EntityWorld.EntityManager.CreateEntity();
    8.         PhysicsAssistant.SetEntityName(jointEntity, "Fixed Joint");
    9.         Game.Instance.EntityWorld.EntityManager.AddComponentData<PhysicsJoint>(jointEntity, joint);
    10.         Game.Instance.EntityWorld.EntityManager.AddComponentData<PhysicsConstrainedBodyPair>(jointEntity, new PhysicsConstrainedBodyPair(primary, connected, collisionBetweenAandB));
    11.  
    12.         return jointEntity;
    13.     }
    Any idea as why they rotate on their own?
    I am also using the inertia tensor to lock on 2d axis, the ball and socket joint works perfect


    EDIT: i just disabled the inertia tensor and it rotates as one object.... but i need to lock the axis as it now rotates on all 3 axis, what can i do?

    EDIT 2: I did some random testing and by removing the axis restriction(X or Y) on the joint like this
    bool3 LockAngularAxes = new bool3(true, false, true);
    gave me what i was looking for....

    this is strange behaviour, what i wrote originally should have worked fine
     
    Last edited: Nov 20, 2020
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Is this the same question as in this thread? In that thread I mentioned the following (bad spelling and all :)) :

    The Rigid Joint is working in 3D, however by setting the inverse inertia tensor in X and Y to 0 effectively stops the Joint from correcting the error.
    If the Rigid Joint is not allowed to work on all 3 axes, it can't lock the rotation around all 3 axes.

    If you set only one body in the pair to have a tweaked inertia tensor the fixed joint should work.
    i.e. what you mention in your EDIT

    Alternatively, you can just a joint that focus on fewer axis (e.g. a limited hinge with zero limits) works are well.
    The best thing would be if you created your only Joint with a single angular Constraint atom that only worked on the remaining free axis of rotation.
    i.e. what you mention in your EDIT 2
     
  3. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    hmm i think i had a slightly different problem then :D

    my ball and socket joint uses inertia tensor on all objects and joint constraints on X and Y angular, Z linear
    it worked perfect so i thought i would use same settings for the fixed joint

    So there are 2 solutions for fixed joints?
    A: Set inertia tensor on cube 1 only, everything else normal with full constraints
    What if i have 10 cubes linked joints ? would i set the inertia tensor on cube 1 only and constraint all axis on joints?

    B: set inertia tensor on all cubes, constraint only 1 angular axis, what about linear?
     
  4. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Can I take a step back and ask what your use-case is?
    As I mentioned on the other thread, the best thing would likely be to make your own custom Joint that fixes the 3 linear axes and affects the remaining single angular axis, which is pretty much what you did with the tweak in EDIT 2.
     
  5. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    its a system to connect dynamic bodies and make something bigger

    like for example in the game bad piggies
     
  6. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    I was able to get this which is more or less or what i am looking for minus the wobble

    https://i.imgur.com/4S18PQ8.mp4

    This is using 2D inertia tensor on all boxes and the following joint locking
    Code (CSharp):
    1. bool3 LockLinearAxes = new bool3(true, true, true);
    2. bool3 LockAngularAxes = new bool3(false, false, true);
    Is it possible to make this any more stiff so it doesn't wobble as much? or should i approach this more creatively and use longer shapes?
     
    Last edited: Nov 21, 2020
  7. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    481
    Nice video. I'm not surprised it is bouncy like that. The constraint solver in Unity Physics is an iterative solver, i.e. it loops over the joints and applies them one after the other over a number of loops. Also, collisions will also have a higher priority that the collisions will knock the joints out of alignment.
    The solver iteration defaults to 4. You can change this via the
    PhysicsStep
    component. Upping the 'Solver Iteration Count' will help the solver converge on a solution, though this will hit performance and there are diminishing returns. You can double up on the Joints as well rather than upping the iteration count for everything.
    Having a chain of bodies like that is a worst case scenario for an iterative solver. If you know you are dealing with a chain like this you can specifically add extra joints between the start and end bodies, rather than relying on the chain of joints to support each end.
    Right now, the strongest solution is not to use Joints at all and instead turn the individual bodies into a single body with a a compound shape.
    In the future, we are looking at exposing the Havok Physics direct solver that would also help with your requirements but we don't have a time frame for that yet.
     
    petarmHavok and argibaltzi like this.
  8. argibaltzi

    argibaltzi

    Joined:
    Nov 13, 2014
    Posts:
    220
    ok i understand, i droped the solver iteration to 3 to improve perfomance :D but i will experiment a bit more. It seems a combination of compound and joints might work better. Adding joints on first to last is also an interesting idea.
     
    petarmHavok likes this.