Using 2D Joints
Problem
You’d like to understand Godot’s Joint2D
nodes.
Solution
Joint are used to constrain the movement of attached physics objects. For any joint node, you need to attach two bodies, which must extend from PhysicsObject2D
.
Properties
These properties are common to all joint nodes:
- Node A and Node B: The assigned physics bodies.
- Bias: The rate at which the joint pulls the two bodies back together if they move apart. Defaults to
0
. - Disable Collisions: Allows the connected bodies to ignore collisions between them. Defaults to
true
.
There are three types of Joint2D
. In all of the following examples, there is one RigidBody2D
connected via a joint to a StaticBody2D
. “Visible Collision Shapes” is enabled in the screen images below so you can see a representation of the joint.
PinJoint2D
The “pin” joint attaches the two bodies at a single point, allowing them to freely rotate.
The pin joint’s Softness property gives some “springiness” to the connection. The value can range from 0
(the default) which allows no movement, to 16
.
DampedSpringJoint2D
This joint connects the two bodies with a spring-like force.
The spring’s behavior can be adjusted with these properties:
- Length: The joint’s maximum length.
- Rest Length:The joint’s length when no forces or movement are applied.
- Stiffness: The spring’s “stretchiness”, i.e. how much it resists forces pulling against it.
- Damping: How quickly the spring stops “bouncing”.
GrooveJoint2D
This joint constrains the attached bodies to move linearly.
By default, the groove runs vertically, but you can change this by rotating the groove node.
These properties control the groove’s behavior:
- Length: The groove’s length. The attached bodies can’t move past this maximum distance.
- Initial Offset: Starting “position” along the groove.