Why apply_impulse() doesn't work in _integrate_forces()

Godot Version

Godot Engine v4.5.1.stable

Question

I’m trying to let the player to apply a force on a ball, but it doesn’t work when putting the apply_impulse() in integrate_forces() like this below

# ball.gd

func _integrate_forces(state):

	if pending_impulse != Vector2.ZERO:
		apply_impulse(pending_impulse, pending_offset)
		pending_impulse = Vector2.ZERO
...

However when i use the apply_impulse() outside _integrate_forces() , it works. For example, in the physics process loop of my player script, I call ball.apply_impulse() when a certain key is pressed.

I’ve been struggling with this issue for a while and would greatly appreciate it if someone could explain the reason and help me get it solved.

Try state.apply_impulse

By default, if a RigidBody isn’t moving it will start to “sleep” and while sleeping, it won’t call _integrate_forces(). Calling apply_impulse() or similar functions or colliding with another object will awake the RigidBody, but if those functions are inside _integrate_forces() they will never get called.

Theoretically, setting can_sleep to false should make it work (at least to test if this really is the issue), but it would be better to just call apply_impulse() from outside of _integrate_forces().
(I’m not sure about this, but for this case it might also work to just set sleeping to false while there is a pending impulse?)