+1 vote

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?

in Engine by (698 points)

2 Answers

–1 vote
Best answer

This guy walks through a few different ways to do it: http://www.iforce2d.net/b2dtut/explosions He's working in 2D, but the same methods will work in 3D.

by (532 points)
selected by
0 votes

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)"

by (81 points)
edited by

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

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?

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

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

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.

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

Yeah, I didn't think of that...

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.