How do I reset forces on a RigidBody2D

Godot Version

4.2.2

Question

Help Programming Physics
As the title says, I need help setting all the forces on the block after clicking restart to 0, because for some reason they still work even after I do this

func resetBlock() -> void:
	global_position = $"..".global_position
	linear_velocity.y = 0; linear_velocity.x = 0
	rotation_degrees = 0; angular_velocity = 0
	constant_force.x = 0; constant_force.y = 0
	$"../timeOnGround".stop()

Any help is appreciated.
Thanks in advance

It might work better when deferred

func resetBlock() -> void:
	global_position = $"..".global_position
	set_deferred("linear_velocity", Vector2.ZERO)
	set_deferred("angular_velocity", 0)
	set_deferred("constant_force", Vector2.ZERO)

	rotation_degrees = 0
	$"../timeOnGround".stop()

Nothing changed, ty anyways

the solution here might help, it looks like you will need to make it use _integrate_forces

1 Like

I’m not quite sure that I understand _integrate_forces, but just to have confirmation of what I’m thinking, does using _integrate_forces need me to make my own physics by adding gravity, friction and things like that?
If I try using _integrate_forces everything just stops moving and the docs say that it stops all physics process and makes the rigid body moved by a script


_integrate_forces

I might be wrong for this but, last time i tested, it just work properly as like basic physics 2d rigidbody (it still has gravity). give it a try at least
also as you can read, you can just turn off the custom_integrator if you need it behave normally until you really need to change its position, you turn it back on.

i dont know if just switching on and off the custom_integrator will let you stop any additional force before you move it manually like your code do.

Ok, I think the mistake was that I replaced _physics_process(delta) with _integrate_forces(state).
As for now, I have both the functions inside my block, and it almost works.
The block doesn’t maintain any type of forces and stops moving after restarting (this includes gravity that stops working).
On the other hand, after restarting another time OR getting hit by another block, the block starts moving without any force left, perfectly reset like I need

func _integrate_forces(state) -> void:
	if readyForGame != get_parent().get_parent().get_parent().running: custom_integrator = true
	else: custom_integrator = false

If you have any idea on how to simulate a contact between 2 blocks, I could simply add that inside the resetBlock() function and call it a day.
Any other solution is still appreciated

Try this, you do not want a custom_integrator, _integrate_forces can work alongside the usual physics.

var queue_reset: bool = false
func _integrate_forces(state: PhysicsDirectBodyState2D) -> void:
    if queue_reset:
        state.linear_velocity = Vector2.ZERO
        state.angular_velocity = 0
        queue_reset = false

Now when you want to reset the forces you only need to set queue_reset = true

1 Like

It works perfectly, although it shakes the block for like a microsecond before starting (It’s barely noticeable, and it works exactly how I needed)

1 Like

Yeah sadly it is not instant, a frame late

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.