# How would I go about programming atmosphere for a game?

I've recently started making a 2D game, and I want to implement an atmosphere.

My plan is to include many in-game compartments, such as tubes and ventilation. If I open an airlock from a room with zero pressure to a room with high pressure, I should feel a heavy knock back. If I mix gases in a room, and players both human and alike would not be able to breath, they would die.

I have idea of how to do it, but it won't be that sufficient and real. I don't need an real atmosphere; it just needs to feel real.

This is the small list of what I want to implement:

• Gas pressure that effects the environment, such as moving objects and causing damage to objects.
• Simple gas mixing, where I would like to implement 4-5 different gasses.
• Some sort of in-game device to affect atmosphere and check its state.

How would I go about programming atmosphere for a game?

• I guess what I would do first is keep track of information for each room (pressure, a list of possible gases and their concentration percentage, and a weight which represents the room size). Then at every update loop check which rooms are connected through open doors and windows and normalise them (gasses begin to disperse evenly and air pressure averages out depending on the weight of a room). When a door or window is first opened you can apply the pressure knock back forces and during the update you can also apply gas effects based on the rooms current concentration. Commented Jul 17, 2013 at 16:29

It depends on what kind of game you are making. For example, a smallish 2D game might require a different approache than a large 3D one.

My initial suggestion would be a kind of fluid simulation. A grid with values for density, pressure and velocity for each gas. A very good resource for this is a stable fluid solver from Jos Stam. It even has code. The fluid solver can be 2D or 3D, but I would suggest solving a 2D system even if your game is 3D (if possible).

A 'cellular automata' is another approach, similar to a fluid system but can be a quite a bit simpler. Perhaps look into "lattice gas"

If your world is particularly large, you might consider and even more simplified system, such as running the fluid simulation on a very simple waypoint like graph. Although again, this highly depends on your game.

I would make each room a node, (or break each room into nodes for finer granularity) and create a simple Cellular automaton to move gases around. Nodes of equal pressure exchange different gas types at a slow rate, until equalized. Otherwise, gases will move from high to low pressure. Movement between nodes can easily be determined and forces applied to anything inside of those nodes.

I would keep a simple table for each node that contains the percentage of each type of gas that's in that node. Keep it simple and deal with percentages, trying to calculate the density of different gases and how much of some other type of gas they should replace gets too complicated. Nodes only go below 100% when there's vacuum. So a node in space would be 0%.

With this percentage based approach, it's easy to define triggers for the units within the nodes. Say humans need x% of gas y and no more than i% of gas j.

For every environment (like rooms), you can keep an info of gas pressure. High value for room with high pressure etc.

Then, you can apply a force through windows and doors (you know where they are). You can omit walls since nothing will come through it.

The formula of this force might depend on 3 things. Pressures of 2 different environments and distance to border (where these two environments meet).

If you have just opened the door, the pressures are different and you are exposed to maximum force. So something like:

Force (you, pressure1, pressure2, distance){


force = absolute(pressure2-pressure1) * 1; applyforce(you, force);}

if you are just at border, you can keep distance 1, and if you are away, you can decrease the distance up to 0. This way, when you are far enough (which you will decide), multiplication will be zero and no force will be applied.

Meanwhile, you should also make the pressure stable within a certain amount of time, since gases will mix and there will be no force (except gas pressure to objects, which is not important, they are from all directions and likely to cancel each other in the end)

After a while, when pressures are equal, the pressure2-pressure1 thing will be zero and again no force will be applied as expected.

For the unit moving thing, it is the same. When this force is applied onto something, depending on weight and shape of object and friction, it will move accordingly. These should be specified by you for different type of objects.

Basically you need to know where such affects occur (enterance to rooms) and define static force vectors for it.