0 votes

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!

asked Sep 17, 2019 in Engine by gimgones (17 points)

1 Answer

0 votes
Best answer

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.

answered Sep 17, 2019 by Zylann (26,157 points)
selected Nov 15, 2019 by gimgones

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.