Thank you very much for your suggestion, zenbobilly. I spent a bit of time on this and noticed that I cannot set mode to MODE_STATIC from integrateforces, it just has no effect, and if you set it elsewhere integrateforces is not called :( I assume it is not called on static bodies. I need to spend more time on this...
This is what I tried on the RIgidBody2D so far and it didn't work:
extends RigidBody2D
class_name Ball
onready var ball_collision_shape = $CollisionShape2D
var _reset_position: = Vector2.ZERO
var _reset_position_requested = false
var _impulse_to_apply = Vector2.ZERO
var _apply_impulse_requested = false
func _integrate_forces(state):
if _reset_position_requested:
_reset_position_requested = false
mode=RigidBody2D.MODE_STATIC
state.linear_velocity = Vector2.ZERO
state.angular_velocity = 0
state.transform.origin= _reset_position
ball_collision_shape.disabled = true
elif _apply_impulse_requested:
_apply_impulse_requested = false
mode = RigidBody2D.MODE_RIGID
apply_central_impulse(_impulse_to_apply)
ball_collision_shape.disabled = false
func make_static_and_set_position(position:Vector2):
_reset_position = position
_reset_position_requested = true
func make_rigid_and_apply_impulse(impulse:Vector2):
_impulse_to_apply = impulse
_apply_impulse_requested = true
makestaticandsetposition and makerigidandapplyimpulse are called from the parent script. The ball resets now and moves to the initial position, but I cannot make it static again. Maybe I could try setting that in physicsprocess? This whole thing is becoming very hacky IMO for such a simple problem :)
I tried create_timer() too, but it can only create one shot timer, not a repeatable timer.