I am simulating explosions in a 2D maze game.
If an explosion occurs in an open area, it covers a circular region (this is the easy bit.)
However if an explosion occurs in a narrow passage (i.e narrower than the blast range) then it should be "compressed", so that it goes further and also it should go around corners.
Ultimately, unless it is completely boxed-in, then it should cover a constant number of pixels, spreading in whatever direction is necessary to reach this total area.
I have tried using a shortest-path algorithm to pick the nearest N pixels to the origin avoiding walls, but the effect is exaggerated - the blast travels around corners too easily, making U-turns even when there is a clear path in another direction. I don't know whether this is realistic or not but it is counter-intuitive to players who assume that they can hide around a corner and the blast will take the path of least resistance in a different direction.
Is there a well-known (and fast) algorithm for this?
Answer
Look at water/fluid simulations. The basic idea is that the area is divided into little cubes and each one has a velocity that it's pushing and a "pressure" gauge of how hard it's pushing into the adjoining cubes. All things being equal your explosion will grow outwards in a circle/sphere.
In a situation where the cubes can't expand in one direction (hallways) they'll end up pushing towards the way out.
This might be a good place to start: http://cowboyprogramming.com/2008/04/01/practical-fluid-mechanics/
No comments:
Post a Comment