Blow RigidBody2D nodes Away from Point

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

I have a multiple RigidBody2D “targets” stacked in a pyramid (like Angry Birds) and I’m trying to make an explosion effect so that when the user clicks, the targets within a certain distance of the click position are “blown away” from the click point.

So far my approach is to instance an Explosion scene at the click location which has a CollisionBody2D child and using the on body entered signal I can get a list of the overlapping targets. However I’m now struggling to figure out what value to call add_central_impulse with to make the overlapping targets “blow away” in the right direction. I think I need to do something with the angle between the centre of the explosion (the mouse click point) and the centre point of the target but I don’t know how to calculate that and if it’s actually the right value to call add_central_impulse with

:bust_in_silhouette: Reply From: Andrea
direction=(object.position-explosion.position).normalized()

this gives you a vector of length=1 in the direction explosion-object

you can then multiply it to the strenght of the explosion and maybe divide it by the distance of the explosion object

force= strength*direction/(object.position-explosion.position).length()

Nice! That works perfectly in terms of direction. Just need to play around with the weight and base force value to get a good impact. Thanks!!

BeepBoop | 2021-09-27 14:39