Navier-Stokes fluid simulation explained with Godot game engine

(myzopotamia.dev)

245 points | by myzek 17 days ago

14 comments

  • nlawalker 13 days ago
    If you haven't tried Animal Well, give it a shot. The whole game and its custom engine are like 35MB and it's filled with really cool visuals and physics powered by fluid equations.

    https://store.steampowered.com/app/813230/ANIMAL_WELL/

  • myzek 17 days ago
    So once upon a time I stumbled upon simulating fluids in gamedev and I really wanted to learn how it works. Fast forward 2 months and I decided to write down everything I learned to hopefully make it easier for others in the future!
    • magicalhippo 17 days ago
      Fluid sims are just so darn fun! Nice writeup, very accessible.

      Couple of notes, I think you forgot to apply timestep when adding rocket exhaust velocity, pretty sure it should be

        u[idx] += backward.x * flame_velocity_amount * falloff * delta
        v[idx] += backward.y * flame_velocity_amount * falloff * delta
      
      You need to compensate by scaling up flame_velocity_amount, I used 85, seemed about the same.
      • myzek 16 days ago
        Yeah it seems I missed that, adding the rocket at the end was a cherry on the top and I was so exhausted already I'm surprised it even works lol
        • magicalhippo 16 days ago
          It made it very fun to play with though :D
    • brandonpelfrey 13 days ago
      Great job. I have spent a lot of time working on fluid simulations (I still am). Glad to see more people still mesmerized. If you’re interested, this rabbit hole goes very deep.
    • nick__m 13 days ago
      excellent article, great vulgarisation and human written !

      Thank you

  • markstock 13 days ago
    Before you go adding vorticity confinement, consider performing a higher-order backward advection scheme (Runge-Kutta 2nd or similar), and using a higher-order interpolation method (triangle-shaped cloud instead of bilinear).

    In my implementations I use 4th order for both and vortices stick around a lot longer.

    • magicalhippo 12 days ago
      If you implement this on the GPU, it's my understanding you can get the 4th-order interpolation quite cheaply exploiting the bilinear texture sampling hardware[1].

      So instead of reading 16 grid values and combining them to get the interpolated sample value, you can fetch 4 bilinearly filtered samples and combine those. And thanks to the hardware filtering, those bilinear samples cost basically the same as reading an unfiltered value.

      [1]: https://developer.nvidia.com/gpugems/gpugems2/part-iii-high-...

      • markstock 7 days ago
        Yes, I do 4th order interpolation (M4') on the GPU. This paper is for 3rd order, though, but the methods may extend.

        I suppose because the fetches are generally to similar memory regions, there may not be a substantial performance improvement due to L1 and L2 hits on recent GPUs.

  • Stevvo 13 days ago
    For anyone wanting to dive further, Fluid Simulation for Computer Graphics by Robert Bridson is the definitive textbook.
  • genxy 13 days ago
    The unlinked Jos Stam paper is available from his website https://www.dgp.toronto.edu/public_user/stam/reality/Researc...
    • myzek 11 days ago
      Thanks, I didn't know it was available there - I'll update my blog and add the link as soon as I'm off work
  • jedimastert 13 days ago
    I appreciate the AI disclosure, I hope that becomes normalized
    • titanomachy 12 days ago
      I wonder how it'll read in 20 years. Like all the covid references in blog posts from 2020, or vague references to "the current political situation" from any point in the last 10 years or so.
  • amarant 13 days ago
    This is great! When I have some leftover time I want to try copying this implementation for 3D. I reckon I could get away with minimal modifications to support the third axis...I think...

    That'll perform even worse though, hopefully my CPU can handle it or I'm gonna need a lot of leftover time to make a shader

    • myzek 11 days ago
      Yes, this should be relatively easy to extend to 3D. Performance might pose some issues so I advise to stick to a small grid, or look for optimizations like decreasing the amount of Gauss-Seidel loops etc. Moving this into a C++ library would also probably help a lot
    • markstock 13 days ago
      You are correct: Stable Fluids extends to 3d relatively easily.
  • vismit2000 12 days ago
    Some very cool fluid simulations on this channel: https://www.youtube.com/@SebastianLague
  • chronolitus 12 days ago
  • frankdlc222 17 days ago
    This is really cool. I love how much detail you went into explaining the setup and walking through each piece of the simulation. Definitely bookmarking this to play around with later!
  • CamperBob2 13 days ago
    Very cool. For your next project, how about a DIY 2D EM field solver? You've done a lot of the work already.
    • __patchbit__ 12 days ago
      AI model latent space needs to be instrumented to a DAW and 3D helical visual effects on triangle slices to show the mathsemantic field gears grinding at the prompt: What's on your mind?
  • amelius 13 days ago
    Did they test if it satisfies the relevant conservation laws?
    • brandonpelfrey 13 days ago
      It won’t satisfy those laws. It’s also not a goal though of this post.
      • amelius 13 days ago
        I mean if you're writing a ray tracer and the reflected light has more intensity than the light sources, then that's not desired. You can have the same sort of thing going on with a fluid simulation.
        • markstock 13 days ago
          One of the nice aspects of Stable Fluids is that you don't need to iterate the pressure correction terms to convergence. Just run a fixed number of Jacobi or Gauss-Seidel sweeps and keep performance consistent. The only drawback of this is some mass loss in areas, which for the present purposes is acceptable.
          • markstock 7 days ago
            I should add that this is a major "tell" for detecting when an app uses the Stable Fluids method: obvious mass loss (and very viscous, energy-dissipating flow).
        • dahart 13 days ago
          Sure, but conservation in ray tracing is also a goal that not everyone has and isn’t required for teaching or games or making pretty & even plausible images. There are plenty of situations in both ray tracing and fluid simulation where conservation is not desirable.
  • nryoo 13 days ago
    Nice writeup. One thing worth adding to the limitations: without vorticity confinement, the Gauss-Seidel projection step quietly dissipates the small-scale curl that makes smoke look like smoke.

    The 2001 Fedkiw/Stam/Jensen "Visual Simulation of Smoke" paper added it back as a correction force for exactly this reason. At N=16 it doesn't matter much because the grid itself can't represent fine vortices, but the moment you crank N up the missing confinement becomes visible.

    • myzek 11 days ago
      Thank you, that's good to know when I try to move this to a Compute Shader and try to make it part of a game in the future
  • analog8374 13 days ago
    Oh don't let us pinch zoom. That would be a disaster.
    • zamadatix 13 days ago
      Assuming you mean the site - pinch zoom & pan works for me on Windows and iOS?
      • analog8374 13 days ago
        well android chrome doesn't
        • OsrsNeedsf2P 13 days ago
          Web exports from Godot (and other game engines) played on mobile is a hard place between compatibility, performance, and many other factors. It's getting better, but very slowly. Try on PC