Omnimaga

General Discussion => Other Discussions => Math and Science => Topic started by: Xeda112358 on November 18, 2012, 06:20:34 pm

Title: Observations of Cellular Automata simulating particles
Post by: Xeda112358 on November 18, 2012, 06:20:34 pm
I long while ago, when I was working on Grammer and adding in the particle effects stuff, I added this particular ruleset:

Move down if the space below is unoccupied
Move left or right if the previous move was not possible
Move up if none of the other moves were possible

If you remove the last rule, the particles move kind of like a liquid. The reason that I added the last rule was to allow the particles to navigate obstacles. Otherwise, this happens:
(http://img.removedfromgame.com/imgs/Particle%200%20Ex0.gif)

A real liquid would have "leveled out" so to speak. My thought was that adding the ability to move up would result in more properties of a liquid, including fixing this problem. So, lets see how it worked:
(http://img.removedfromgame.com/imgs/0-Particle%200%20Ex1.gif)

I decided to try another method by letting the particles always be connected to a specific particle. This way, in order for one to move, they all had to go in the same general direction:
(http://img.removedfromgame.com/imgs/Particle%200%20Ex2.gif)
I called them worms and they could do some pretty cool things, such as exhibit capillary action. However, this is very clearly not working like a liquid (Well, if you got a bunch of worms together, it probably would act very much like a liquid, except that they can crawl up each other with no external support!)

However, though it isn't as fluid as the worms, the ruleset I originally gave also exhibits capillary action:
(http://img.removedfromgame.com/imgs/Particle%200%20Ex3.gif)

Anyways, I thought I would share this :) If you can think of other cool effects, feel free to share !
Title: Re: Observations of Cellular Automata simulating particles
Post by: willrandship on November 18, 2012, 07:07:20 pm
I love the improved water CA sim. One thing I've been screwing around with is this:

A langton's ant-style CA, where each cell stores a state with only one moving, but you also consider whether the given Y-coord and X-Coord are odd or even.

Code: [Select]

All occasions toggle the state of the cell. If they didn't it would get stuck on certain values.

           #X%2 Y%2 S
move right #0   0   1
move down  #0   1   1
move left  #1   0   1
move up    #1   1   1
move left  #0   0   0
move up    #0   1   0
move up    #1   0   0
move down  #1   1   0

It gives an interesting pattern as a result. When even dimensions, it will draw vertical lines, then erase them. When odd dimensions, it does the same thing, but with some chaos introduced.
Title: Re: Observations of Cellular Automata simulating particles
Post by: DJ Omnimaga on November 19, 2012, 01:08:29 am
Wow those screenshots looks nice. Two of them almost looks like you're simulating Pepsi/Coke O.O
Title: Re: Observations of Cellular Automata simulating particles
Post by: willrandship on November 19, 2012, 01:38:18 am
I wonder what it would look like to have a physics object like a box hit that water.
Title: Re: Observations of Cellular Automata simulating particles
Post by: ralphdspam on November 19, 2012, 03:18:39 am
These look really neat!  Someone should really make a game based on the capillary action...
Title: Re: Observations of Cellular Automata simulating particles
Post by: Adriweb on November 19, 2012, 04:43:16 am
That kind of physics stuff can't stop to amaze me, it's really cool ! (for such small ruleset !)

these "fuild" mechanics reminded me of a simulation ported to nspire (3.2, because physics) : https://github.com/slembcke/Chipmunk-Physics/blob/master/Demo/Buoyancy.c

(http://www.cloudetal.com/wp-content/uploads/2011/07/buoyancy-and-density-300x195.jpg) (http://www.cloudetal.com/buoyancy-density)
Title: Re: Observations of Cellular Automata simulating particles
Post by: Xeda112358 on November 19, 2012, 07:14:05 am
I was going to do a simulation where there were several particles (one grayscale, the other black, so you could distinguish between the two. You could have a simple fluid one, plus the modified rule and I believe they should interact pretty nicely. If I were to drop a box, there would be no splash (the rules do not take into account transfer of energy), but it would probably sink in the "Pepsi/Coke" one and stay afloat in the other one :D
Title: Re: Observations of Cellular Automata simulating particles
Post by: willrandship on November 19, 2012, 12:56:50 pm
By the way, the fact that it just happened to exhibit capillary action is pretty cool. :D

How much is surface tension reflected? :P The worms might do well at that.
Title: Re: Observations of Cellular Automata simulating particles
Post by: Xeda112358 on November 19, 2012, 01:13:50 pm
It would be very cool to see how the worms react with an object affected by gravity :D As to surface tension, the rule that has capillary action would exhibit good surface tension under certain conditions (the object would need to be big enough), otherwise, it would sink. It would also need to be flat, because if it was pointed, the particles would move out of the way. However, with just the sand/water rule, the surface tension would basically be "perfect" meaning nothing would be able to pass through it. If I added in a way to transmit energy and momentum between particles, that would help things out, too. Lat week, I gave a presentation (it only went about 70 minutes) on cellular automata and in it I described a ruleset that I still need to test. It works like this:

Take all the rules from the "pepsi/coke" ruleset and add in the following:
If the particle didn't move, add 1 to its "energy" level. Then, on the next cycle, check the pixel below. Even if it is black, if the energy level of the pixel below is smaller, swap the two coordinates. Otherwise, check left or right with the same conditions, and then finally check up.

That should cause currents in the particles and it could transfer energy to blocks pretty easily (every time it collides with a block, take a single energy unit and give it to the block, giving it some momentum and possibly buoyancy). It would also maintain capillary action. My hope is that this will give more relatable effects, even if it isn't true to nature :) I imagine that waves and whatnot would be possible, and if energy is only lost to the environment, this would cause a "splash" effect if you drop a bunch of particles at once.

Meh, I really need to find time to actually test this stuff :[
Title: Re: Observations of Cellular Automata simulating particles
Post by: ruler501 on November 19, 2012, 01:16:30 pm
I'd love to see how this works. I've never real done much cellular automata but it seems interesting
Title: Re: Observations of Cellular Automata simulating particles
Post by: shmibs on November 19, 2012, 08:53:10 pm
/me decided to give it a shot as well.
this uses a random insert to avoid too much in the way of predictable patterns. it's not as good at evening out, but it doesn't look as fizzy either.
Code: [Select]
:.A
:DiagnosticOff
:Fix 5
:ClrDraw
:ClrDrawr
:Full
:For(L,0,713
:95→{L1+L}End
:0→A→B→C+1→E
:Repeat getKey(15)
:.D
:If getKey(3)
:→E
:A<95+A→AEnd
:If getKey(2)
:→E
:A-(A>0)→AEnd
:If getKey(1)
:→E
:B<63+B→BEnd
:If getKey(4)
:→E
:B-(B>0)→BEnd
:If E
:If getKey(48)
:Pxl-Change(A,B)r
:0→E
:End
:End
:If getKey(55)
:ClrDrawr
:End
:.END D
:
:RecallPic
:.DRAW/MOVE
:For(L,0,356
:L*2+L1→S
:{S}→T
:{S+1}→U
:
:If T<96 and (U<64)
:!If pxl-Test(T,U+1
:U++
:Else
:If pxl-Test(T+1,U) and pxl-Test(T-1,U
:U--
:Else
:!If rand^4
:U--
:End
:rand^3-1+T→T
:End
:End
:
:!If pxl-Test(T,U
:T→{S}
:U→{S+1}
:End
:Pxl-On(T,U
:End
:
:End
:.END DRW/MV
:
:.ADD
:If getKey(54)
:!If pxl-Test(A,B
:rand^357*2+L1→S
:If {S}>95 or ({S+1}>63)
:A→{S}
:B→{S+1}
:End
:End
:End
:.END ADD
:
:Pxl-Change(A,B)
:DispGraphClrDraw
:End

oh, and how exactly did you manage those worms, xeda?
Title: Re: Observations of Cellular Automata simulating particles
Post by: Juju on November 19, 2012, 09:00:36 pm
That's pretty cool :D
Title: Re: Observations of Cellular Automata simulating particles
Post by: willrandship on November 20, 2012, 02:00:44 am
The fiziness is actually quite realistic. We just happen to be looking at very, very small amounts of water. Fish breathe the 'fizz' in real water.

You know what this needs? Fancy CG shaders and stuff.
Title: Re: Observations of Cellular Automata simulating particles
Post by: aeTIos on November 20, 2012, 05:47:18 am
Are you hinting at the 84C? :P
Title: Re: Observations of Cellular Automata simulating particles
Post by: Xeda112358 on November 20, 2012, 08:11:49 am
The worms were pretty simple. All I did was make a small buffer for each worm (if it was 35 units long, use a 70-byte area). Then, get the coordinates of the tail, Pxl-Off( at that coordinate, then shift all of the coordinates down 2, get the coordinate of the head and do the same rules as the fizzy effects (move down if possible, move left/right if it couldn't move down, move up if there are no other options). Here is an older screenshot:
(http://www.omnimaga.org/index.php?action=dlattach;topic=11031.0;attach=10050;image)
Title: Re: Observations of Cellular Automata simulating particles
Post by: shmibs on November 20, 2012, 10:48:22 am
that's really cool =D
maybe it could be made into a game of some sort. something like a flea circus, where the player has to set up a number of elements and try to get it to complete the course.

The fiziness is actually quite realistic. We just happen to be looking at very, very small amounts of water. Fish breathe the 'fizz' in real water.

dissolved oxygen isn't really the same thing, and that only comes in a few parts per million, anyways, so it'd be unlikely to see even one with this number of particles :P
Title: Re: Observations of Cellular Automata simulating particles
Post by: Eiyeron on November 20, 2012, 03:50:37 pm
This thread interesses me... What about 3D fluid cellular automata? This is giving me ideas with trixel rendering...
Title: Re: Observations of Cellular Automata simulating particles
Post by: shmibs on November 20, 2012, 05:00:52 pm
voxels you mean?

that would be REALLY REALLY cool =D
someone should make a 3d dust toy.
Title: Re: Observations of Cellular Automata simulating particles
Post by: willrandship on November 20, 2012, 05:30:28 pm
Well, but if we look at water on a molecular level we find many, many variable-length gaps between the molecules. Often, these are larger than the molecules themselves.

Title: Re: Observations of Cellular Automata simulating particles
Post by: ruler501 on November 20, 2012, 05:45:36 pm
voxels you mean?

that would be REALLY REALLY cool =D
someone should make a 3d dust toy.
The people from powder actually talked about that, but they decided that it would be too inefficient(If I remember correctly). It'd be really interesting but could run really slow if you put much in it
Title: Re: Observations of Cellular Automata simulating particles
Post by: willrandship on November 20, 2012, 05:50:17 pm
well, powder is written in java......:P

Minecraft is essentially a minimal CA with some real physics on top.
Title: Re: Observations of Cellular Automata simulating particles
Post by: Xeda112358 on November 20, 2012, 07:47:14 pm
Hmm, actually, the only difficulty with 3D stuff is that you would need to store the 3D image. Then, you just need a creative way to "pixel test." For example, if I wanted to use a 3D graph of z(x,y), when a particle moves "down", you would pixel test by testing if it is within a certain bound of the equation (for example, you could test between x+.05 and x-.05 and the same for the y values to see if there is a barrier in the region). Then, you need an efficient way to check for collision with particles XD Yeah, I guess that could get inefficient pretty quickly o.o
Title: Re: Observations of Cellular Automata simulating particles
Post by: aeTIos on November 21, 2012, 10:02:09 am
or just have cubes?
Title: Re: Observations of Cellular Automata simulating particles
Post by: shmibs on November 21, 2012, 10:32:03 am
xeda, i don't see why the movements, at least, couldn't be done the exact same way as they are in 2d, just using a 3d matrix?
the only thing that would really slow stuff down would be graphics.
Title: Re: Observations of Cellular Automata simulating particles
Post by: willrandship on November 21, 2012, 02:54:37 pm
Shmibs, the speed per-pixel isn't the problem. It comes more from the fact that you have an extreme number more pixels to deal with.

Example: A small box of water (60x60x60 should be enough to show it well) has 216000 pixels, versus a 2D 3600 pixels. That's 60x the work.
Title: Re: Observations of Cellular Automata simulating particles
Post by: Xeda112358 on November 21, 2012, 07:04:31 pm
Yeah, it is more an issue of memory as opposed to computing. The 3D rule would be move down if possible, or choose one of the directions on the X-Z plane if that didn't work, or move up if none of those worked. I was trying to see how it would work if you used 3D equations (I imagined using something like Graph3D, tilting the graph, and having the particles all fall out around the graph). That would be easier on memory, but much slower to handle all of the collisions.
Title: Re: Observations of Cellular Automata simulating particles
Post by: Roboman on December 02, 2012, 11:07:37 am
ATM we don't care too much at how fast it is, lets just get the memory issue sorted out so we can get a running version :D
And if it is REALLY slow... lets just take a time laps screenie XD
Title: Re: Observations of Cellular Automata simulating particles
Post by: cooliojazz on December 02, 2012, 01:21:09 pm
Actually, the other day I just happened to make a 3d Cellular Automata to test the new 3D engine i was working on =P  Here, check it out!

It takes command line arguments <mode> <size>, where mode is 0 for a 3d life-like simulation, and 1 is my fail attempt at a water simulation, and size is a number that determines how big the "field" is.  the defaults are 0 and 20, and i would recommend not going over 100.

Fly around with WASD and arrow keys, and change the simulation speed with - and =

https://docs.google.com/open?id=0B7yzzD91paU_aUpwMHhKUEtRblU
Title: Re: Observations of Cellular Automata simulating particles
Post by: nikitouzz on December 02, 2012, 04:32:02 pm
good jobs :)

can I see the source code ? :) i'm interressing because i write in french one tutorial of the cellular automate :)

thanks
Title: Re: Observations of Cellular Automata simulating particles
Post by: willrandship on December 02, 2012, 08:43:19 pm
Your program has no way to move up and down. Sideways movement is relative to the direction faced, but vertical movement is not tied in the same way. Not sure if that's intentional.
Title: Re: Observations of Cellular Automata simulating particles
Post by: cooliojazz on December 02, 2012, 09:11:15 pm
Sorry I forgot to mention it, move up and down with E and SHIFT.

Here's the relevant code
Code: [Select]
int[][][] cells = new int[size][size][size];
cells[10][10][10] = 1;
cells[10][10][11] = 1;
cells[10][10][12] = 1;
cells[11][11][10] = 1;
cells[10][11][11] = 1;
cells[10][11][12] = 1;
cells[11][4][10] = 3;
cells[11][4][11] = 3;
cells[11][4][12] = 3;
cells[12][4][10] = 3;
cells[12][4][11] = 3;
cells[12][4][12] = 3;
cells[10][4][10] = 3;
cells[10][4][11] = 3;
cells[10][4][12] = 3;
while (f.isVisible()) {
    int[][][] tempcells = new int[size][size][size];
    for (int x = 1; x < size - 1; x++) {
        for (int y = 1; y < size - 1; y++) {
            System.arraycopy(cells[x][y], 0, tempcells[x][y], 0, tempcells[x][y].length);
        }
    }
    for (int x = 1; x < size - 1; x++) {
        for (int y = 1; y < size - 1; y++) {
            for (int z = 1; z < size - 1; z++) {
                if (simtype == 0) {
                    tempcells[x][y][z] = rule3DGOL(x, y, z, sumCells(cells, x, y, z), cells);
                } else if (simtype == 1) {
                    tempcells[x][y][z] = ruleFlow(x, y, z, sumCells(cells, x, y, z), cells);
                }
            }
        }
    }
    cells = tempcells;
    DisplayList dl = new DisplayList();
    int cubes = 0;
    for (int x = 1; x < size - 1; x++) {
        for (int y = 1; y < size - 1; y++) {
            for (int z = 1; z < size - 1; z++) {
                if (cells[x][y][z] > 0) {
                    cubes++;
                    Cube c = new Cube(new Point3D(x, y, -z), new Point3D(.45, .45, .45), dl);
                    c.setMaterial(new Material(new Color(cells[x][y][z] * 50, 255 - cells[x][y][z] * 50, 0), Color.white, Color.black, 63, null), dl);
                }
            }
        }
    }
    totalc = cubes;
    displist = dl;
    try {
        Thread.sleep(10 * delay);
    } catch (Exception e) {

    }
}

static public int sumCells(int[][][] i, int x, int y, int z) {
    return  i[++x][y][z] + i[x][++y][z] + i[x][y][z + 1] + i[x][--y][z + 1] + i[x][--y][z + 1] + i[x][y][z] + i[x][y][z - 1] + i[x][++y][z - 1] + i[x][++y][z - 1] +
                         i[--x][y][z] + i[x][y][z + 1] + i[x][--y][z + 1] + i[x][--y][z + 1] + i[x][y][z] + i[x][y][z - 1] + i[x][++y][z - 1] + i[x][++y][z - 1] +
            i[--x][y][z] + i[x][y][z] + i[x][y][z + 1] + i[x][--y][z + 1] + i[x][--y][z + 1] + i[x][y][z] + i[x][y][z - 1] + i[x][++y][z - 1] + i[x][++y][z - 1];
}

static public int rule3DGOL(int x, int y, int z, int sum, int[][][] i) {
    if (sum == 2 || sum == 3 && i[x][y][z] == 1) {
        return 1;
    } else if (sum == 3 && i[x][y][z] == 0) {
        return 1;
    } else if (sum == 4 || sum == 5 && i[x][y][z] == 2) {
        return 2;
    } else if (sum == 5 && i[x][y][z] == 1) {
        return 2;
    } else if (sum == 6 || sum == 7 && i[x][y][z] == 3) {
        return 3;
    } else if (sum == 7 && i[x][y][z] == 2) {
        return 3;
    } else if (sum == 8 || sum == 9 && i[x][y][z] == 4) {
        return 4;
    } else if (sum == 9 && i[x][y][z] == 3) {
        return 4;
    } else if (sum < 2 || sum > 9 && i[x][y][z] == 1) {
        return 0;
    } else {
        return 0;
    }
}

static public int ruleFlow(int x, int y, int z, int sum, int[][][] i) {
    if (i[x][y][z] == 0 && i[x][y + 1][z] == 1) {
        return 1;
    } else if ((i[x + 1][y][z] == 1 || i[x][y][z + 1] == 1 || i[x - 1][y][z] == 1 || i[x][y][z - 1] == 1) && i[x][y - 1][z] == 3 && i[x][y][z] == 0) {
        return 1;
    } else if (((i[x + 1][y][z] == 1 && i[x + 1][y - 1][z] == 3)
            || (i[x][y][z + 1] == 1 && i[x][y - 1][z + 1] == 3)
            || (i[x - 1][y][z] == 1 && i[x - 1][y - 1][z] == 3)
            || (i[x][y][z - 1] == 1 && i[x][y - 1][z - 1] == 3))
            && i[x][y - 1][z] == 0 && i[x][y][z] == 0) {
        return 1;
    } else if (((i[x + 1][y - 1][z] == 0)
            || (i[x][y - 1][z + 1] == 0)
            || (i[x - 1][y - 1][z] == 0)
            || (i[x][y - 1][z - 1] == 0))
            && i[x][y - 1][z] == 3 && i[x][y][z] == 1) {
        return 0;
    } else if (i[x][y][z] == 1 && i[x][y + 1][z] == 0) {
        return 0;
    } else {

        return i[x][y][z];
    }
}
Title: Re: Observations of Cellular Automata simulating particles
Post by: Sorunome on December 02, 2012, 10:41:30 pm
Wow, that is looking pretty epic, great work there! :D