A joint connects two collision objects using a constraint. Defold supports joints in both 2D and 3D physics, through different APIs.
This manual describes the 2D joints exposed by the physics module. Since Defold 1.13.2, 3D projects can create and control constraints through bullet3d.constraint, including hinges, sliders and springs. Use that API with rigid bodies obtained through bullet3d.get_rigid_body().
The supported joint types in the 2D physics API are:
bodyB to a line on bodyA. The wheel joint also provides a suspension spring. In Box2D referred to as a Wheel joint.The 2D joints described here are created programmatically using physics.create_joint():
Editor support for creating joints is planned but no release date has been decided.
-- connect two collision objects with a fixed joint constraint (rope)
physics.create_joint(physics.JOINT_TYPE_FIXED, "obj_a#collisionobject", "my_test_joint", vmath.vector3(10, 0, 0), "obj_b#collisionobject", vmath.vector3(0, 20, 0), { max_length = 20 })
The above will create a fixed joint with id my_test_joint connected between the two collision object obj_a#collisionobject and obj_b#collisionobject. The joint is connected 10 pixels to the right of the center of collision object obj_a#collisionobject and 20 pixels above the center of collision object obj_b#collisionobject. The maximum length of the joint is 20 pixels.
A joint can be destroyed using physics.destroy_joint():
-- destroy a joint previously connected to the first collision object
physics.destroy_joint("obj_a#collisionobject", "my_test_joint")
The properties of a joint can be read using physics.get_joint_properties() and set using physics.set_joint_properties():
function update(self, dt)
if self.accelerating then
local hinge_props = physics.get_joint_properties("obj_a#collisionobject", "my_hinge")
-- increase motor speed by 100 revolutions per second
hinge_props.motor_speed = hinge_props.motor_speed + 100 * 2 * math.pi * dt
physics.set_joint_properties("obj_a#collisionobject", "my_hinge", hinge_props)
end
end
The reaction force and torque applied to a joint can be read using physics.get_joint_reaction_force() and physics.get_joint_reaction_torque().