I am working on a space orientated game, and I have done some research on the best way to simulate gas with traditional physics technologies (Creating a lots of circles)

The only obstacle I have is how to simulate pressure. For example, if one corner of the room has lots of these little circle physics bodies, how can I make them evenly distribute? If I have a huge vacuum, what is the best way to have the particles get sucked out through the corridors etc of a room into space?

I can't think of any way to do this without iterating over every single particle against every singe other particle. Obviously, I cannot do this in a real-time simulation.

Maybe I could do this on a tile-based level?

The specific technologies I am using are Java and Dyn4j.


  • 4
    \$\begingroup\$ Simulating a gas with a lot of particles may be the most physically accurate in the general case, but indeed it will kill your performance (even moreso in Java). But maybe you don't need physically accurate physics in the general case. Could you provide more information about how your game is modeled? For example, is it tile-based à la FTL, or mesh-based? Is the ship's shape fixed or procedural/modifiable? What impact does air pressure have on the gameplay? There's probably a lot of corners you can cut and still have a solid system. \$\endgroup\$
    – Quentin
    Mar 3 '17 at 8:43
  • \$\begingroup\$ Thanks for the feedback. The game is not technically tile based (i.e, entities can be between desecrate tiles, for example, player movement) but in regards to walls, floors etc they are all usually placed on discrete locations (I.e (0,0) (0,1) (1,1) etc...) The structure of the environment is mostly static with the exception that things can be added and removed (i.e. players can build a wall at a discrete location.) I have done my path-finding on a tile-based level however. Air pressure will at least have an impact on the players ability to survive, but I also need to track the oxygen etc... \$\endgroup\$
    – Jeremy
    Mar 3 '17 at 16:04
  • 2
    \$\begingroup\$ I am not sure if it is applicable for your problem, but you could try looking into Coupled map lattice which are kind of extension to cellular automaton used for fluid simulations. In any case, you will probably have to study scientific articles. \$\endgroup\$
    – wondra
    Mar 4 '17 at 0:25

A simulation is always a number of degrees off from reality. So the question you need to first have an answer to is what degree of reality needs to be mimicked?

I could imagine a simple simulation to basically just apply a force on objects near the vacuum opening where air is whooshing out and have that force increase as the distance decreases and as the size of the hole increases. Done. For some visual flair, Animate particles whooshing out (just near the opening)


The pressure of a gas is a direct result of its particles colliding with each other and their environment, which means you can simulate pressure simply by modeling those collisions.

Particles obviously only collide with things in their immediate vicinity and so you can leverage spatial optimization techniques (simple grid, quadtree, bsp etc.) to avoid quadratically testing each pair.

  • \$\begingroup\$ Hmm, I think simulating on a particle level using any form of optimization isn't going to scale well for the typical size of a space-station etc... I think I would need to approach it with a more abstract approximation, like what brogue does for gas simulation. \$\endgroup\$
    – Jeremy
    Mar 4 '17 at 21:13

I'd propose a simplification, where any single room in the ship (building, whatever) always carries one single air pressure only.

This should be close to reality, since if you have a room with air and

  • open a door to vacuum, all air will escape instantly
  • drill a small hole in the wall, it will escape slowly

In practice, there will in both cases be one single pressure in the room at any time. Air is fast, the bottlenecks (size of hole) will slow down the escaping, but not for example the mass force of air (or it will, but too little to be relevant).

The data model would be a node graph, ie. connected vertices. In the middle of each room there would be one vertex, and at each door (or hole) one. Perhaps at each corner (for example in a corridor) as well. If a door opens into a corridor, the door vertex would apparently connect to two nodes in the corridor. Imagine having these "lines" going through the entire area, potentially also through air channels etc.

Some of the vertices would have a resistance, telling how much air can pass per time unit, and ofc depending on the pressure in connected neighbour nodes. Those that have no resistance would simply be measuring points.

Then do some recursive magic to walk through the entire graph. Remember that even if it models a physical space, it is still only a data set - the graph doesn't know what the space ship looks like, it only has vertices (indeed with positions, that's how you map to physical locations), nodes and weights. The graph need not "look like" the physical room distribution! It doesn't look like anything, it's just a bunch of numbers.

The action would start by any vertex beginning to leak air, so nullify it's pressure. The next vertex (say a door vertex) would not get zeroed completely, as it has a resistance. Calculate how much it can take and remove that much from it's neighbours. Etc, you get the point.

Take "time" into the equation, to make it framerate-independant.

Maybe a few dozen, a few hundred, or less than 1000 vertices would do?


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .