Get the current impulse on a rigid body

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

I have a RigidBody2D Spaceship and multple one-time forces (aka impulses) that are applied (from outside) to the Spaceship via apply_central_impulse (dokumentation). Now I would like to know the sum of the impulses. How can I find this sum? I tried using applied_force but it seems like that is always (0,0), so perhaps it is only for (continously applied) forces, not impulses?

Here is my (condensed) code:

In the parent Scene:

onready var ship : RigidBody2D = get_node("SpaceShip")

func _physics_process(_delta):

In the Spaceship:

func _physics_process(_delta):
    var current_impulse = insert_force_here  # I need help here

How can I find the current impulse of a RigidBody?

:bust_in_silhouette: Reply From: Magso

get_linear_velocity() will get the rigidbody’s global physics movement.

Thank you for your answer, but I need the impulse (the forces that were applied to the body in during the current physics step), not the resulting velocity.

E.g. For a planet running around the sun the force will point towards the sun, the current velocity will be perpendicular to that.

Kaligule | 2020-02-15 17:09

The only way would be to add some_force_1, some_force_2 etc together and normalize it.
It’s not advised to use apply_central_impulse in _physics_process or _process because it acts more as a hit rather than a force. There are no methods to get the resulting impulse other than the rigidbody’s velocity after the impulse has been applied. Try using add_central_force and use Vector2 variables to get the behaviour you’re after.

Magso | 2020-02-15 17:31

Then what should I do if the direction and strength of the force varies over time? add_central_force won’t help me since the force is assumed to be constant.

I know I can add up the forces when they are applied, but this would have to be done outside of the ship. This leaks internal logik of the ship to the outer scene. I had hoped there would be a way to get the forces from inside the ship.

Kaligule | 2020-02-15 17:42

add_central_force won’t help me since the force is assumed to be constant.

This is why you need variables to get the behaviour you’re after. add_central_force will allow for precise movement as opposed to apply_central_impulse which will in essence push the rigidbody one time. It could be possible to emulate apply_central_impulse using applied_force by resetting it to Vector2.ZERO every frame.

Magso | 2020-02-15 18:21

I don’t really see the difference between pushing each frame and having a single force applied once, but resetting it each frame and setting it new. Sorry. Is there some resource where I can read more about the differences between those methods?

Thanks again for explaining this stuff to me.

Kaligule | 2020-02-15 20:36

There are numerous pages and videos about both add_force and apply_impulse, the great thing is that both Unity and Unreal have these same methods so most of the information will also pertain to Godot.

Magso | 2020-02-15 21:16