Hello fellow developers! Here’s a quick and easy tutorial on how to calculate explosion damages precisely!
Normal explosions can be easy, but if you really want to tweak how much damage it does, this tutorial could be very useful. With normal explosion damages, the falloff can be very linear. If you’re right next to it, you die. If you’re far away, you don’t. But it can feel very unrealistic at some times and can be very frustrating!
Ok, lets start off with the equation. It’s pretty complicated at first, but stick with me!!!
function damage_at_distance(distance, ``mu, sigma, A, max_distance, falloff)
local damage = A * math.exp(-((distance - mu)^2)/(2 * sigma^2))
if distance > max_distance then
damage = damage * math.exp(-falloff * (distance - max_distance))
end
return damage
end
This is the function we’re going to be working with. Notice the variables.
- distance is the distance from the explosion to the user
- mu is the point at which it explodes, in most cases it will be zero.
- sigma can be used as kind of an explosion power. Think of it on a graph. A high sigma will have a very slow and late damage drop off as to the damage will be very high until closer to the end. A low sigma on the other hand will have a very quick drop off, so this would be for lighter explosions.
- A represents the maximum damage. For most cases you would want to set it to 100 as it’s the players max health, but it can vary.
- max_distance is self explanatory, any damage above this distance will be erased
- falloff is how quick the damage falls off after the max distance, to prevent a drop off from say 30 immediately to 0 when the max distance is hit. Instead, it will smoothly move that number to 0 quickly or slowly.
Ok, now that we have all the variables noted, let’s test this.
distance = 0
sigma = 20
mu = 0
A = 1
max_distance = 10
falloff = 1
for i = 1,100 do
damage = damage_at_distance(distance, mu, sigma, A, max_distance, falloff)
if damage * 100 > 5 then
print(distance, " | ",damage * 100)
distance += 1
else
print(0)
end
end
Here we have an explosion with a max distance of 10 (so it cannot go beyond 10 studs), a distance of 0 (the distance from the explosion to the player), and a sigma or power of 20. Notice how the sigma is very high. Notice the “if damage * 100 > 5” Statement. Since our damage being returned will be between 1 and 0, we multiply that number by 100 to be realistic, and then we cut it at 5, so that we don’t damage the player a very very tiny amount. Also notice that I’m starting the damage at 0, and then gradually increasing it, for simulation purposes.
Anyways Let’s see the results!
1 | 99.8750780924581
2 | 99.50124791926824
3 | 98.8813044611233
4 | 98.01986733067552
5 | 96.92332344763442
6 | 95.59974818331
7 | 94.0588063364342
8 | 92.31163463866358
9 | 90.3707077873196
10 | 88.24969025845955
11 | 36.787944117144235
12 | 13.53352832366127
Great, but wait… if we set the max distance to 10, then why is it still damaging beyond it?
This is because of our falloff variable. Since we have it set to such a low value (1), it makes sure that that the damage drops off and doesn’t just cut it off straight.
Lets test this again but with a lower sigma. We’ll set it to 5 this time.
0 | 100
1 | 98.01986733067552
2 | 92.31163463866358
3 | 83.5270211411272
4 | 72.6149037073691
5 | 60.653065971263345
6 | 48.67522559599717
7 | 37.531109885139955
8 | 27.803730045319412
9 | 19.789869908361464
10 | 13.53352832366127
11 | 36.787944117144235
12 | 13.53352832366127
As you can see, the decrease of damage over distance is MUCH more noticeable.
Anyways, that’s it for this tutorial. I hope this helped you guys! cya!