Why do joints disconnect when setting the global-position of a connected body

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By st_phan

I have multiple PinJoint2D that connect a bigger RigidBody2D to multiple smaller RigidBody2D.

When I set global_position in _integrate_forces (Link to docs) the PinJoints disconnect and the RigidBodies fall to the ground.

How can I make the bodies not disconnect?

:bust_in_silhouette: Reply From: st_phan

It seemed setting global_position messed with the physics engine, so I ended up manipulating linear_velocity instead:

func _integrate_forces(state):
	var target_position = get_global_mouse_position() # Just an example
	linear_velocity = (target_position - global_position) / state.step

So instead of setting the position itself, I set the linear_velocity in a way that the body “jumps” directly to the target position. This seems to work better with the physics engine.

Note: I also tried setting state.transform.origin which I learned here, but after a few collision everything deviated a few pixels from the desired position so I can’t recommend this approach.