this week saw a lot of work take place on explosions and how to simulate them. it ended up being quite iterative and a long process to get it into a place where it feels good.
whats the purpose of explosives?
- gameplay wise explosions exist to
- remove large numbers of weak blocks
- make a projectile remain impactful even if its kinetic force is low (mortars are an example of this)
- pierce very hard blocks with shaped explosions
- to change trajectory mid flight
they pay for this cost by being risky within the factory and probably by having strings attached like no welding, and by being quite shitty as physical projectiles; they’re weak and lightweight and so unsuited to being a projectile.
in real life explosions propagate outwards and can reflect of hard surfaces. they decrease with n^2 or n^3 depending on your theory. they are most effected by the conditions of their initial moments, with the shape at the start of the explosion being the most impactful.
this lead to a lot of thought about the best way to simulate them
how do you simulate an explosion?
there are a ton of options
- nothing. it just goes bang :)
- would need manual work for shaped charges that would quickly either limit player agency or become so compex that the other options would be back on the table.
- raycast with reflection <= WINNER
- fire rays in 360º circle, div the force over each ray
- 360 rays isnt really that many so its simple and cheap
- can handle reflections
- explosion waves are not a ray and as such they dont pass though each other so it doesnt capture explosive lensing
- stepped atmosphere simulation
- grid that has a pressure value, each step it passes some to the lowest neighbours
- also pretty cheap but needs iterative sim
- explosion waves aren’t a gas expanding, they have momentum
- this doesnt allow lensing
- Boltzmann lattice
- a 2d lattice with directional valies in 8 or 16 dirs, that track momentum and a centre that tracks pressure
- allows lensing and all sorts of realistic outcomes
- its complicated and i’m not actually that good with math
- really computationally intense and annoying to build as it’ll probably need gpu to do quickly
after getting a model that worked, i needed numbers that work. for beam based explosives we assume the following
- a single cell of explosive is a 4cm cube
- a 4cm cube of tnt weighs 0.1056kg
- tnt yields 4.6 MJ/kg
- 0.1056 * 4,600,000 ≈ 485.760 kilo joules of power expanding @ 8750m/s
- assuming it fires out 360 beams, that’s 12777 j per beam
- div by 10k 1.27 dmg per beam, which is then applied unevenly based on exponential radiant falloff
after sapling an example tnt detonation using pressure at given indices, i settled on about a 6m range for a tnt explosion of ~100g to be effective in any way based on the pressure of the wavefront. at about 6m you have 35kpa which is pretty harmful and looked good enough to me.
so we have these waves propagating outwards that represent force of the explosion. they accumulate on surfaces and then deliver their force to the surface with the armour piercing component being derived from the explosion wavefront speed (~8k for tnt so /1000 to “hardness” 8 which fits with the other projectiles)
when an explosion ray it tests the relative hardness of the impacted surface against this hardness and determines how much of the power will reflect. eg
an 8 hardness ray (ie moving at 8000ms) impacts a hardness 2 wall. averaging out the hardnesses we find that 80% of the damage will be absorbed by the wall and 20% will reflect. if the wall is destroyed before all damage could be accounted for, we reduce the reflected damage by that proportion, and the rest of the damage continues though the destroyed cell.
this lets us have explosions that are computationally cheap but also hit other criteria such as being capable of dynamic shapes and responding to cell shape very well. heres an example of a really big explosion:
- green lines are rays that hit nothing
- red lines hit something
- blue are reflections
here is a nuclear explosion using the raycast technique
all of the damage is governed by tweaking constants and using real physical measurements proved cumbersome. it also does not let us do lensing but this will instead be handled inside the flying munition itself whereby the ray positions and their damage will be changed instead of doing it at impact time.
thats all for now. i’m sick of explosions
