Skip to main content
  1. Development Blog/

RPB#1: Fluid Simulation with Circuits?

·883 words·5 mins

Welcome to the Rocket Plumber Development Blog! For my first post I would like to dive into the games most fundamental piece: its fluid system based on circuit simulation maths. This is actually the idea that spawned this whole project!

Comparison of ship and its equivalent circuit
Comparison of ship and its equivalent circuit

Plumbing as circuits, how does it work?
#

Let’s start by thinking about how fluids behave generally. If you have a garden hose with a restriction on the end, like say a nozzle, it will limit the amount of water that can flow. If you were to open the tap and increase the pressure in that hose, more water would flow through the same restriction. That relationship between the pressure and flow maps nicely to the behaviour of an electrical circuit, with your pressure being your voltage, the restrictive nozzle being a resistor, and the flow being the current. This is a well established analogy, and it extends beyond just resistors (check out this Wikipedia!). Usually it is just used to make the somewhat abstract behaviour of electricity more intuitive, but I got thinking about it the other way around. I’m an electrical engineer so solving circuits is pretty natural, but simulating fluids can be quite a hard problem to solve for games. This Factorio Friday Facts post talks a lot about that, and was actually the inspiration for me to try this out. If I can map plumbing components in a game to circuit elements, then I can use circuit solving methods to simulate them! There are some limitations in this approach: The analogy only really works for incompressible fluids, and you have to be careful to avoid creating fluid out of nothing with ’negative’ pressures. Despite that, I think this approach provides a model that is simple enough to understand but with enough depth to be the heart of a simulation heavy game like Rocket Plumber!

Why model as circuits?
#

The reason modelling fluids using circuit elements is so useful, is that it means that you can leverage established algorithms to solve for the flow in these circuits. If you’ve taken a circuits 101 class you’ll be familiar with approaches using Kirchhoff’s voltage and current laws that let you solve circuits by hand. Rocket Plumber uses an algorithm called ‘Modified Nodal Analysis’ which is based on those laws. It uses them to construct a large matrix which represents your circuit as a system of equations you can solve with regular linear algebra. This site has a great explanation of the MNA algorithm if you are interested, and I used this as a reference to write my own MNA implementation. I’ll do a post with a deeper dive into this part of the simulation at some point in the future, but the key takeaway is that simulating circuits is well solved and well optimised. This changes the logic for adding new rocket parts to the game. Instead of asking “how do I simulate this part?” I ask “how can I model this part as circuit elements?” and then I get a robust and efficient simulation for free. This approach also scales to large and complicated networks quite well. The matrices can get large, but the sparsity pattern means they can still be solved efficiently. Here’s a screenshot of the teaser map, and the matrix that represents it!

teaser-ship-and-matrix.png

How does this matter for gameplay?
#

Now I think the whole “fluid simulation with circuits” thing is cool in its own right, but it adds something for the player experience too! Here is what I like about it:

Detail
With this simulation there is a lot of detail. The pressure drop across every segment of pipe is modelled, and you are rewarded for compact designs. You can see tanks closer to a pump will empty a little bit faster, but if given the chance they will slowly equalise back out. As your builds scale up I think this adds to a feeling that what you build is complex and almost alive. You might have put it together but you don’t control every aspect of it, and that just makes it more exciting when it works the way you want!

Intuitive Behaviour
The detail of the simulation can make it complex, but I think the behaviour experienced by the player is still intuitive. Nozzles produce more thrust when connected directly to a combustion chamber. Longer pipe runs transfer fluids more slowly. The thrust from a nozzle connected to a gas tank decreases quickly as the pressure in the tank drops. Hopefully these things are all behaving in ways that feel right to the player without much explanation! This is something that I have to work more on for complex parts like turbopumps and pressure fed tanks, and I hope this will continually improve as the game develops.

Uniqueness
As far as I’m aware there haven’t been any games with a fluid system like this, so hopefully the ways of thinking that are required to work with it are something novel to most players. This will hopefully make the challenges and problems faced by the are player new and exciting!

Thanks for reading, and look out for more blog posts and a new release of the game soon!
#

Join the mailing list and stay up to date!