# How to implement 2d explosions that will cause damage taking into account covers and distance?

How can I implement a 2D explosion in a way that it will cause less damage to characters that hide behind crates or other cover? Ideally damage will depend on the strength of the cover (e.g. metal wall will protect better then wooden crate). Ideally damage will decrease with distance as well.

I am going to use a port of Box2D physics engine - Farseer.

Thank you.

• Wiki (goo.gl/c1OJx) recommends using particles which might actually make sense since it kind-of simulates actual physical processes. Has anyone considered such an approach?
– Den
Nov 25, 2010 at 21:48
• Sep 5, 2011 at 14:25

One relatively simplistic way of doing this is to draw a ray from the center of the explosion to the center of every entity where DISTANCE(explosion,entity) < exlosion.max_radius (pseudo-code).

For each entity, follow this ray, decreasing base damage as you go. Whenever you hit an object subtract it's cover value from the damage (essentially shortening the ray).

When the ray hits the entity, whatever damage is left is your explosion damage.

For this approach, you probably want a pretty high base damage that decreases rapidly with distance (inversely proportional to the square of the distance should do nicely).

Another approach is to draw a sector fully containing the entity; but no wider; and all the way out to .max_radius. Subtract from the area of this sector the area covered by any cover between the explosion and the entity, multiplied by its cover value. The remaining area is your blast damage.

One neat side effect of this is that the closer the entity is to the explosion, the wider the sector gets, and the more damage you take (as you count all of the area of the sector, and just subtract the area of cover between the entity and explosion.

It could also be useful to treat cover as entities; and simply subtract damage taken by closer entities from those further out, but that might get gnarly.

There undoubtedly exists an even more elegant and clever approach, but it probably requires some pretty nasty calculus. It always does.

Damage decreasing with distance should be a simple task. When you create the explosion you should know the coordinates of the centre of the explosion. Since you are working in 2d, it will be just x and y.

All you need to do is calculate the player(s) distances from the coordinates of the centre of the explosion using Pythagoras' theorem then use that against the max radius to calculate a percentage of the total damage.

For example, take the coordinates (x1,y1) as the centre point of the expliosion, and (x2,y2) as the coordinates of the player. We can then calculate the damage taken as follows:

Distance from centre = sqrt((x2-x1)² + (y2-y1)²)
Damage taken = (Distance from centre/Explosion radius) * max damage


As for things in the way, I'm too tired to think of the answer, but you'd basically be looking for intersections along the line from the centre of the explosion to the player.