For my game I want a spaceship to become a kinematic body when docking so other spaceships won't knock it off.
This works but when going back to a regular rigid body I experience some glitches, either the position or velocities reset to incorrect values. Trying to resolve this I ended up with the following implementation (I revert the mode to rigid body in the regular _physics_process
call when not docking):
func _integrate_forces(state):
if !dock_locked && dock_state == 0: return
mode = RigidBody2D.MODE_KINEMATIC
position = state.transform.get_origin()
linear_velocity = state.linear_velocity
var impulse = 0.9 * (dock_position - position) - 0.2 * linear_velocity
state.apply_central_impulse(impulse)
state.integrate_forces()
# print_debug(impulse)
My issue is the above works quite well if the print_debug statement is uncommented! If I uncomment the print statement my spaceship smoothly snaps to the position I want and as soon as docking is interrupted it maintains momentum correctly and keeps moving as normal.
But if I comment the debug print the ship stops moving in place while docking.
Can anyone explain this and help me understand what do I need to do to get the behavior I need consistently?
Thanks!