Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
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 '10 at 21:48
1  
Related: gamedev.stackexchange.com/questions/14171/… –  finnw Sep 5 '11 at 14:25

2 Answers 2

up vote 8 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.