This is a "falling water" simulation based on a simple Cellular automata algorithm.

Have a look at the attachment.

The problem is I have designed the algorihm and according to my logic it's so simple it should work straight away. But some times there are groups of water that will accumulate mid air and not fall down.

There are 2 options :

1) My knowledge of LUA isn't sufficient and I am making a mistake in code

2) There is this tiny bug somewhere out there and I can't find it.

Code: Select all

```
(....)
MaxMass = 1.0; --The normal, un-pressurized mass of a full water cell
MaxCompress = 0.02; --//How much excess water a cell can store, compared to the cell above it
MinFlow = 0
MinMass = 0.0001; --//Ignore cells that are dry
function simulateCompression()
local Flow = 0
for i = 1, map_y+1 do
for j = 1, map_x+1 do
if grid[i][j] == 1 then
local remaining_mass = mass[map_y+2-i][j]
if remaining_mass > MinMass then
if (grid[map_y+2-i+1][j] == 1) and mass[map_y+2-i+1][j]<(MaxMass+MaxCompress) then
Flow = math.min( remaining_mass, MaxMass+MaxCompress - mass[map_y+2-i+1][j])*math.min(globdt,0.3)
Flow = math.max(0,Flow)
b1x = Flow
remaining_mass = remaining_mass- Flow
mass[map_y+2-i][j] = remaining_mass
newmass[map_y+2-i][j] = mass[map_y+2-i][j]
newmass[map_y+2-i+1][j] = mass[map_y+2-i+1][j] +Flow
end
end
end
end
end
for i = 1, map_y+2 do
for j = 1, map_x+2 do
mass[i][j] = newmass[i][j]
end
end
end
```