Weird physics behavior in func _integrate_forces(state)

Godot Version

v4.6.stable.official

Question

So I was experimenting with gdscript a bit and and I tried to remake the default character body movement script in to a rigid body movement.

When I run the script in func _process(delta: float) it behaves sort of ok,
but when I run the function in func _integrate_forces(state) I sometimes get stuck somehow and can’t move after landing from a jump.

This confuses me since I have heard that func _integrate_forces(state) is better used for handling physics.

This is the script used for func _process(delta: float)

func move_on_ground_proc():
	if Input.is_action_just_pressed("ui_accept") and ground_ray.is_colliding():
		apply_central_impulse(Vector3(0,JUMP_FORCE,0))

	var input_dir := Input.get_vector("left", "right", "forward", "back")
	var direction := (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		apply_central_impulse(Vector3(direction.x,0,0) * SPEED)
		apply_central_impulse(Vector3(0,0,direction.z) * SPEED)

and this one is for the func _integrate_forces(state)

func move_on_ground(state: PhysicsDirectBodyState3D):
	if Input.is_action_just_pressed("ui_accept") and ground_ray.is_colliding():
		state.apply_central_impulse(Vector3(0,JUMP_FORCE,0))

	var input_dir := Input.get_vector("left", "right", "forward", "back")
	var direction := (head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		state.apply_central_impulse(Vector3(direction.x,0,0) * SPEED)
		state.apply_central_impulse(Vector3(0,0,direction.z) * SPEED)

also here is my object hierarchy

_integrate_forces() only runs when the physics body is active (i.e. not sleeping). Try setting can_sleep to false and see if that helps.


Let me know how it goes.

2 Likes

Yes, thank you. This seems to work. So the object is put to sleep when it is jammed in something hard enough?

1 Like

The following excerpts are from the Godot Documentation.

To answer your follow-up question vaguely: a rigidbody goes to sleep when the physics state is determined to not change by a meaningful amount. It’s a performance optimization which is particularly noticeable in scenarios with a large amount of rigidbodies.


I hope that clears it up for you. You can google the question as well (e.g. “why do rigidbodies sleep?”).

1 Like

I see, Thank you for answering again this was definitely useful.