Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This comes up a lot in game development -- there have been many GDC talks and Gamasutra/Game Developer magazine feature articles on this very subject. The best for your purposes I think is <a href="http://www.dgp.toronto.edu/~stam/reality/Research/pub.html" rel="nofollow noreferrer">Jos Stam</a>'s "<a href="http://www.google.com/url?sa=t&amp;source=web&amp;ct=res&amp;cd=1&amp;ved=0CAcQFjAA&amp;url=http%3A%2F%2Fwww.dgp.toronto.edu%2Fpeople%2Fstam%2Freality%2FResearch%2Fpdf%2FGDC03.pdf&amp;ei=DqE1S4DUCIffnAfN2qX1CA&amp;usg=AFQjCNEcsNAukYsrtzmEwYFIfPM0YiawOg&amp;sig2=h0MvgVqF0d7qkkdMsOln2A" rel="nofollow noreferrer">Real-Time Fluid Dynamics for Games</a>" from the 2003 GDC. </p> <p>He describes a simplified way of performing <em>advection</em> via a <em>linear backtrace</em> which suffers from some problems but works quite well for incompressible fluids (that is to say, it works better for water than for gas). Linear backtrace means basically that he sets up a grid representing fluid density at each point in space (that's your tiles), and then for each frame visits each point and asks, "based on the pressure at the surrounding points, where is the fluid likely to <em>come from</em>?" This turns out to be easier than solving it the other way ("where is the fluid from this point going to go?").</p> <p><a href="http://www.gamasutra.com/view/feature/1549/practical_fluid_dynamics_part_1.php" rel="nofollow noreferrer">Mick West's article on fluid dynamics</a> for Gamasutra extends Stam's paper in some ways that may improve performance, so you may want to start there.</p> <p>There is also <a href="http://www.gamasutra.com/view/feature/4164/sponsored_feature_fluid_.php" rel="nofollow noreferrer">a recent Intel-sponsored GameDev article that offers a more complete solution</a>, but it's rather complicated and focused more on the graphical rendering side in 3d.</p> <p>You can look at <em>Dwarf Fortress</em> for an example of tiled 2d fluid mechanics, but his solution seems to have a bunch of problems dealing with fast-flowing or pressurized fluids; sometimes his water moves way more sluggishly than you'd expect, or gets caught up in blocks and corners.</p> <p>Those papers summarize the math and algorithm way better than I could cram into a Stack Overflow box, but there's two general points I'd like to make too:</p> <ol> <li>Water simulation is very computationally expensive. Since you're working in Python, this may result in a real performance problem -- be sure to set up some means of profiling your algorithm to find the hotspots in case your loop takes so long that it kills framerate. You may have to resort to <a href="http://numpy.scipy.org/" rel="nofollow noreferrer">Numeric Python</a> to get fast C-based array operations.</li> <li>There is way more math in game development than most people expect!</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload