How to slow down a moving rigid body?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By gimgones

Hi guys,

how can I slow down a moving rigid body to a speed of zero?

My rigid body is a spaceship that I move by multiple thrusters in space by using the add_force method.

Now I would like to slow it down within 2 seconds to a speed of zero when the user releases the acceleration key on the keyboard/gamepad.

Thank you in advance!

:bust_in_silhouette: Reply From: Zylann

You could make it slow down the same way spaceships slow down in the first place: by applying a force in the opposite direction, proportional to the speed at which the ship goes, the same way you did to make it accelerate.

state.apply_central_force(-state.linear_velocity * some_factor)

I’m not sure how to make that last exactly 2 seconds though, it would need to be tweaked until it approaches that duration. That first approach is the most realistic one.

Another approach might be to get the linear velocity from within _integrate_forces, reduce it manually and set it back to the PhysicsDirectBodyState.
Something like this:

var speed = state.linear_velocity.length()
speed -= deceleration * delta
if speed < 0.0:
	speed = 0.0
state.linear_velocity = state.linear_velocity.normalized() * speed

It’s less realistic (not necessarily a problem tho), but you still need to tweak deceleration so that it slows down to a halt in about 2 seconds. That may depend on the speed at which your ship was going before you decelerate.

Tank you so much Zylann.

Even though I switched from Rigid Body to KinematicBody this helps me a lot. I am new to Godot and I thought that a RB would be better for what I planned but it turned out that KB fits much better.

gimgones | 2019-11-15 16:02