I originally posted this on my blog. It might be easier to read there as wel.
http://kurie.us/how-to-construct-2-explosion-example/
I recently got an idea for a game. To build that game though I needed a way to create explosive effects, more specifically, the physical effects of an explosion. The graphics could be handled easily enough one way or another. Ultimately though, I needed a way to exert force on all surrounding objects near the explosion and have that exerted force diminish with distance. The explosion effects also needed to be programmatic since I would have no control over where and when the explosion would actually take place.
This problem sounds easier to handle then what in practice actually was to design. If I were writing code, I could have easily written something that mimicked normal physics equations and have been done with it. As powerful as Construct 2 is, it still has some limitations. I couldn’t just throw together some copied physics algorithm and be done with it.
So here’s what I came up with. Hopefully, others might find it useful too.
Example URL: http://www.kurie.us/Games/explosionExample/
First, and foremost, take a look at the actual example at the URL above. The purple and blue block buttons are labeled accordingly. The orange rectangle acts as the explosive, or the TNT. It can be clicked and dragged. The red blocks can be clicked and dragged around as well. In this example, I left the explosive force at 300. It’s best to see the example if a single block is left close to the TNT and a single block is placed farther away. I have not adjusted this example for density or friction or anything else.
Now that you’ve taken a look at that, let me explain how it works. Here’s a screenshot of the entire instruction base.
It’s not that scary. Essentially what happens is when the user initiates the explosion, the system calculates the explosive force on each object in the system and exerts the given force. Otherwise, the game will cycle through each object, see how far away that object is from the explosive device, or the TNT, and push it with a certain force depending on how far away it is. If the game designer has more than one object they want to react to the explosion, it’s as simple as replacing the last instruction with objects that need to be effected.
Here’s where most of the magic happens.
First, the Impulse line.
TL;DR version: IF force imposed < 0 THEN apply 0 force, ELSE apply force
This is essentially a long if-then statement. I take the maximum amount of force the explosion can exert and subtract the distance from the object. This tells me how much force to impose on the object. I need to make sure it’s not a negative force though. If a negative force were imposed, it would just suck the object towards the explosion, hence the if-then. So, after I figure out if the force is less than 0, then I act accordingly. If the force is less than 0, I do nothing. If the force is more than 0, then I apply the force (with a bit of randomness for fun) to the object.
It should be noted though, that in Construct 2, density and friction of objects should be changed to give items a more realistic effect. As it stands right now, if an object collides with another object, both objects are sent careening off the screen.
Next, the Angle line.
TL;DR (Angle of object to explosion + 180) mod(%) 360
This line is pretty easy. I figure out the angle of the object facing the explosion. Then I add 180 that measurement to rotate the angle a half circle around, or I turn the angle around. Then, to make sure I don’t end up with some weird angle measurement like 485, I wrap the measurement around the 360-degree curve with the mod operator.That way, instead of ending up with a measurement like 485, I have a usable angle of 125. This essentially just tells the object which direction to go flying in.
So there you have it. It’s that easy.