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!

in Engine by (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.

by (27,170 points)
selected by

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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories