How does apply_impulse() work on RigidBody2D?

Godot Version

Godot Engine v4.2.2.stable.official.15073afe3

Question

I’m creating a game called, Pen Fight, where I need to apply an impulse to a rectangular rigidbody2d.
The weird behaviour that I’m seeing is, apply_impulse(impulse_vector, position) function applies the impulse to the rigidbody even when the position of the impulse is outside the dimensions of the rigidbody. How can I make sure the impulse applies to the place where I want to hit on the rigidbody?

Cast a ray at it and use the collision position.

Would you be able to explain how the apply_impulse(impulse_vector, impulse_position) work?
Why the impulse position can be outside the rigidbody and still affect it?

The code doesn’t check if the force is actually on the collision body. Usually the collision system will provide a point to apply forces. If you are doing it manually you will need to provide that point.

An impulse at an outside position from the body’s center of mass should theoretically create properties like a long invisible handle and mostly torque, spin, the object.

2 Likes

It an impulse applied to a ball from a que. Except this math is considered from the ball"s perspective. Meaning the position is where the que enters.

Yes, the first hit is manual, that is, based on user input, code will compute the position to hit. After that first hit, all the collisions with other rigidbodys are as expected.

Based on some experiments I did, I think your answer it correct. If the position parameter provided in the apply_impulse() is outside the rigidbody shape, the engine seem to create an imaginary handle from the center of gravity to the position outside rigidbody, and then apply the force vector on that handle.

Here is the source code and what it does.

1 Like

Thanks for sharing the code. Now it all makes sense. You can give any position vector, it don’t matter if its within the rigid body, as long as it’s not in the direction of center of mass, it will generate torque.