I'm trying to simulate pressure with a collection of spherical particles in a Unity game I'm building. A couple notes about the problem:

  • The goal is to fill a constantly changing 2d space/void with small, frictionless spheres. The game is trying to simulate the ever-growing pressure of more objects being shoved into this space.
  • The level itself is constantly scrolling from left to right, meaning if the space's dimensions are not changed by the user it will automatically get smaller (the leftmost part of the space will continually scroll off-screen).

I'm wondering what some approaches are that I can take to tackling these problems...

  1. Knowing when to detect when there is space to fill and then add spheres to the space.
  2. Removing spheres from the space when it is shrinking.
  3. Strategies to simulate pressure on the spheres such that they "explode outwards" when more space is created.

The current approach I am contemplating is using a constantly moving wall, that is off screen and moves with the screen, as this image illustrates:


This moving wall will push and trap the spheres into the space. As for adding new spheres, I was going to have either (1) spheres replicate themselves upon detecting free space, OR (2) spawn them at the left side of the space (where the wall is) - pushing the rest of the spheres to fill the space. I foresee problems with idea #1 because this likely wouldn't really create/simulate pressure; idea #2 seems more promising, but raises the question of how to provide a location for these new sphere particles to spawn (and the ramifications of spawning them when there IS no space).

Thanks so much in advance for your wisdom!


3 Answers 3


I'm not exactly sure what it is that you want to achieve, is the wall pushing the particles as the space rolls off the screen a game mechanic?

As for your three points, I think those behaviors would all emerge if you treat your particles like so: Have each particle repulse every other particle inversely proportional to the distance, and also have them collide with the walls of your space to stop them.

Particles close together would repulse each other and thus spread out to fill empty space until stopped by obstacles. If/when the walls move to make the space smaller, and push the particles, they will get pushed closer to other particles, repulse them stronger which in turn will push on other particles, increasing the overall pressure.

This would be pretty similar to an n-body simulation with negative attraction, so depending on the number of particles, it could get quite expensive to compute repulsion for every particle with every other particle. You could possibly try to simplify this by having each particle being repulsed just by the averaged center of gravity of your mass of particles, that might produce odd behavior in long thin areas with bends though (your gas not expanding into areas that bend away from the center of gravity, for example).

As for creating and deleting particles (to make sure there are just the right amount to visually fill the space I presume?), you would probably want to make sure not to simply add or remove particles when the density is lacking, as that would introduce more particles repulsing other particles, and hence increase or decrease the pressure.

So you might want to introduce a factor into the repulsion calculation, say start out with 1.0 and when you add x particles, make the new factor n/(n+x) where n is the number of total particles before adding the new ones.


Pressure (like in a gas) is just the result of plain elastic collisions between your particles and between the particles and the wall. You just measure the ammount of collisions per time with the walls to get a value for your pressure.

The 2 ideas you give have no direct connection to pressure, as pressure is given with a constant ammount of particles in a closed volumina and a given temperature (which results in the particles moving faster or slower).

If you add particles to this closed system, the pressure should rise, because there is more energy in the whole system (you just added a new particle with a given temperature<->speed).

  • \$\begingroup\$ particles should have a mass for the elastic collisions, as the walls are assumed to have a mass near +infinity \$\endgroup\$
    – Aron_dc
    Jul 22, 2012 at 12:40
  • \$\begingroup\$ I said "pressure is the ammount of collisions per time with the walls" thats noch 100% precise. In a elastic collision you take the impulse of that particle and measure how much impulse is given to the wall. So if one particle collides in a vertical direction to the wall the wall gets an impulse of 2mv (m=mass of particle; v= velocity of particle). So you actually measure the incoming impulso onto the wall. The particle is just reflected(v before the collision = -v after the collision). This even helps to measure the pressure of mixtures of different gasses (different m for particles). \$\endgroup\$
    – Aron_dc
    Jul 22, 2012 at 13:07

Each loop, you should iterate through all of your particles, and then calculate a final force on that particle. Basically, the wall always gives them a positive force vector on the x axis, if they are colliding with it (particle.x - particle.r <=0).

Then, each particle they are colliding with also gives them a force vector, depending on the angle between them. You collect these angles for all particles, calculate the average, and there you go!

There are many optimization techniques you can use here, too, just look up some ball physics tutorial. You basically want to iterate through each pair of particles only once and calculate the force vector for both and store it in them.

This will simulate gasses, basically, and if you give them a constant force, liquids too, I presume.

As for creating and deleting particles, I implement a ParticleEmitter class, which has a position, direction and some other usefull stuff. Your example would be better off with an emitter that can create particles in a certain range, or even better, a rectangle.


You must log in to answer this question.

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