Search Unity

Proper way to add physics to a robot arm?

Discussion in 'Physics' started by JoeStrout, May 5, 2019.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I have a simple robot arm which is a (motorized) chain of parts, arranged in a Transform hierarchy so that rotating (say) the base, properly moves/rotates all the other child objects. Without any Rigidbodies involved, all works great; I can rotate each joint, and the rest of the arm works perfectly.

    As soon as I add physics — even kinematic rigidbodies — it all goes to heck.

    OK, that's not quite true. It all goes to heck when I try to rotate my joints using rb.MoveRotation rather than just assigning a rotation. Because MoveRotation, apparently, does not properly update any child Rigidbodies, so my arm comes apart.

    upload_2019-5-5_8-48-5.png

    If I use the Rigidbody "freeze position" constraints, then it almost works, but not quite; the parts still get offset as the joints rotate. And if I try to restore the proper local positions in FixedUpdate, things really go downhill. When our code and the physics engine fight, everybody loses. :(

    All I want is for my robot arm to be able to properly knock things over. But I've already put two hours into this, and I don't feel I'm any closer. I started the two hours with HingeJoints; from the docs, it sounds like a HingeJoint with the Spring properties would be exactly what I need. But that didn't work either; the spring did not behave as advertised, so I thought maybe it'd be easier to just remove the joint and use MoveRotation myself. Perhaps that was a mistake.

    I'm really trying hard to get over my loathing of Unity physics. Please guide me. What's the correct way to arrange a chain of rigid bodies, such that I can control (from code) the angle of any link in the chain?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Follow-up: I've just found that if I turn off the "freeze" constraints, and instead manually call both MovePosition and MoveRotation in FixedUpdate (using the parent transform to convert the correct position/rotation into world coordinates), then that appears to work. (Whew!)

    Not sure if this is the "standard" way of doing it, but it seems to work well enough for my needs.
     
    Tset_Tsyung likes this.
  3. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Hi Jo,
    Funnily enough I'm trying to do a similar thing for some thing we're developing. We're trying to create a robotic arm that can manipulate non-kinematic Rigidbodies, without wobbling or springing about (as in when using physics - the real thing is a lot more sturdy). But we also, if possible, want it to not be able to go through walls (as in when kinematic).

    So far we have a kinematic arm setup, with each section of the arm being a child of the previous. These all have kinematic RB's on them. Problem is that these can go though walls or other static colliders...

    I, too, was looking into the different joints. They seemed to work "Okay", but would very quickly 'explode' or wobble about as soon as the root RB moved.

    I'd be interested to hear your thoughts given your recent struggles...

    Mike
     
  4. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    Tset_Tsyung and joseph114591452 like this.
  5. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    Hey tjmaul,

    So sorry for the late reply. I was running with your suggestion and testing it out.

    I CAN CONFIRM THIS SEEMS TO BE THE BEST SOLUTION SO FAR! :D:D:D

    Thank you so much for bringing this to our attention. I now will be keeping a closer eye on the blog in future.

    Not only does this work great for our needs, but I found a second use for the articulation body that (subject to testing) is better than joint solutions - you can use spherical Articulation Joints (with stiffness at 0 or 1) to make rope!!!

    So, tjmaul, thank you for your suggestion of using articulation bodies! XD
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yes, I agree, in Unity 2020 and later, Articulation Bodies will be a very welcome addition to the physics toolkit!
     
    Tset_Tsyung likes this.
  7. Klamore74

    Klamore74

    Joined:
    Jun 17, 2013
    Posts:
    103
    Articulation Bodies are very cool, but has someone a solution to link them to a RigidBody? Using FixedJoint is not an option, it works like a big spring and the physics does not fit.
     
    Tset_Tsyung likes this.
  8. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    In my project I needed a way to 'pick up' a rigid body object with an articulation body.

    I believe the way I did it was...
    1. Store all the current settings of the Rigidbody component on the grabbable object into a custom struct/class.
    2. Destroy the Rigidbody component on the grabbable object.
    3. Parent the grabbable object to the articulation body (I didn't use snapping).
    4. Move the articulation body.
    IIRC, the Articulation Body takes into account all the child colliders, meaning that I was able to have the grabbable/grabbed object collide with other objects and have that collision affect the ariculation body.

    When I dropped the grabbable/grabbed object I...

    1. Set the parent of the grabbable/grabbed object to null.
    2. Used
      Code (CSharp):
      1. AddComponent<RigidBody>();
      to add a new RigidBody.
    3. Recopied all of my settings from the custom struct/class back into the RigidBody.
    4. Watched it fall.
    Please note: This was my first time using Articulation Bodies, so please do further research in case this method is no longer viable (I was using 2020.1.2f1), or if there is a better method out there.

    All the best,