best way to blow objects away from an explosion

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Gokudomatic2
:warning: Old Version Published before Godot 3 was released.

I created a nice little explosion effect with particles and sprites. Now I have to make it interact with the physics engine. Every rigidbody and kinematic object within the range of the explosion must be pushed away with a given strength.
To do that, I tried 2 ways:

  1. create a staticbody sphere in the explosion, which pushes away everything inside. Very easy but not really realistical. And objects are just teleported away, without velocity.
  2. create an area sphere in the explosion and implement in every possible object the code to be pushed away when it’s in this area. Backdraws are that even simple rigidbody objects (furniture) which don’t need any script must have this bit of script (otherwise they don’t move at all). And objects behind walls can be affected too.

Is there another way to do it?

:bust_in_silhouette: Reply From: La_Blazer

I’m not an expert with 3D games, I focus mainly on 2D games, but I think this should work:

  1. After exploding get all objects in the range of explosion
  2. Cast a ray to each object in the sphere
  3. If the raycast is sucessful and nothing is in the way apply force to the object in the direction from explosion and multiply it with “1-(distance to object)/(explosion range)”

Or, alternatively, apply raycasts in uniformly distributed every direction, and for every successful raycast push the object by some constant amount.

Bojidar Marinov | 2016-04-14 12:12

Alright. I see the idea. But imagine now that there are simultaneously 10 or 15 explosions. Won’t raycasting in every direction for each explosion lag the game?

Gokudomatic2 | 2016-04-14 13:10

If you have so many explosions then you can probably just skip raycast part and just apply force to an object (people wont be able to tell the difference).

kubecz3k | 2016-04-14 13:39

Or, alternatively, apply raycasts in uniformly distributed every direction, and for every successful raycast push the object by some constant amount.

Good idea, but I think that the farther the object is the less velocity it should have, because the force of explosion decreases farther from the bomb…

La_Blazer | 2016-04-14 13:49

By the way, last time I was playing with raycasts intensively, I had to position them and wait one frame before I could read their result. That makes sense. But If I have one raycast and I have to trie every direction (let say 32 directions). I’ll have to wait 32 frames before I checked every direction. Let say we optimize the process and we use an area sphere, like I did already in my 2nd attempt, so we can pinpoint the raycast to every detected object. If there are many objects in the area, the loss of time is proportional to the number of objects.
This is why I’m quite cautious about raycasts.

Gokudomatic2 | 2016-04-14 14:18

@La_Blazer ~ Well, that is covered under my model since object farther away are less likely to get the velocity boost.

Bojidar Marinov | 2016-04-14 19:42

Yeah, I didn’t think of that…

La_Blazer | 2016-04-15 04:38

:bust_in_silhouette: Reply From: rgrams

This guy walks through a few different ways to do it: Explosions - Box2D tutorials - iforce2d He’s working in 2D, but the same methods will work in 3D.