Playing FortressCraft Evolved, I played with explosives. I was not particularly happy with how the explosives were handled – and I was on vacation, and I had no internet access, so my creativity got into my brain and I tried writing code that would handle explosions better.
I shoved the code here: https://github.com/sunnywiz/explosion2d
Here’s the basics of what I did:
- I did this in 2D instead of 3D because easier to Console.WriteLine()
- I created a TwoDSpace<T> so I could store an arbitrarily large number of T’s in a 2-D plane
- I created a function which mapped out, given a center 0,0, what the distances were to each X,Y.
- I kept distances as squares, so I didn’t have to do square roots.
- I did it with an initial max-radius, but the final version in my head would jit-figure out what it needs and save the resulting calculations in a static – it only needs to calculate it once. Bigger explosion might need to calculate some more.
- That function can then call to a lambda, giving it:
- the X,Y of the point that is being visited by the explosion
- an array of parent points for the explosion and the ratios by which to weight them
- The caller then says, “circle visit an explosion starting from here”, and keeps track of the force per X,Y that is being experienced. The caller is responsible for all the code that says “Ores are hard, but dirt is soft”, for example, or “Diamond cannot be damaged by the explosion”
- I think its pretty fast. O(r^2), which given its a 2D explosion in 2D, is expected.
Fun Fun stuff. I miss the days of mud coding. You just don’t need to do explosions like these in real-world paid programming.