Creating an arm with physics with Godot
Use various physics objects and then pin joint 2D to connect them to create sort-of-ragdoll-y arms.
I created an arm in the following way:
- A KinematicBody2D as the shoulder
- A RigidBody2D for the upper arm
- Another RigidBody 2D for the forearm
- Another KinematicBody 2D for the hand
- Pin joints between each of these pieces
I used KinematicBody2D specifically because I wanted to be able to control both the shoulder and the hand. However, I did experiment with other factors:
- Turning the hand into a Rigidbody make the entire contraption move with physics. It could be useful if you wanted to create a scarf, or something where you don’t need direct control over the end piece (or are willing to control it with physics rather than position)
- Turning the hand into a static body caused the arm pieces to lag when controlling the hand. I’m not sure why that happened. StaticBody is useful if you want one end anchored without player controlling it.
This is the setup I ended up with:
Note the hierarchy in the upper left (with Forearm and UpperArm being RigidBody, and HandSprite being KinematicBody), and the various pin joints (Elbow connects Forearm and UpperArm, Wrist connects Forearm and HandSprite).
- Pins can go through the collision objects. You’ll want to hit ‘Disable Collisions’ in the pin joint settings, though.
- A little softness in the pin joints let the arm move even if the hand doesn’t (e.g. when the main body moves). It’s a bit unsettling, but it’s the effect I’m going for.
- Make sure the pin joint is at the same hierarchy level (or above) the pieces it’s joining together. Things have turned wonky otherwise.
There’s a bug where pin joints can behave oddly on objects turning 360 degrees. As of Godot 3.2, it’s a known bug, but not yet fixed.