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?
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.
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.
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.