0 votes

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):
    ship.apply_central_impulse(some_force_1)
    ship.apply_central_impulse(some_force_2)

In the Spaceship:

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

How can I find the current impulse of a RigidBody?

in Engine by (48 points)

1 Answer

0 votes

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

by (2,991 points)

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.

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.

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.

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.

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.

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.

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