
Please update your flash player
Inspired by Conway’s game of life (to which I was a late comer but now a devotee!), I decided to program a 2 dimensional Cellular Automaton in Actionscript 3.
I had been reading up on complexity theory for a recent project and the Cellular Automata just kept on rearing its head. Of course it displays the fundamental qualities of complexity; starting with an object following a set of simple rules and then creating multiple instances to form complex and unpredictable patterns.
In terms of visualisation, the Cellular Automata is interesting because it has potential to be used for many applications. Complexity Theory and Systems Theory are used to simulate various growth patterns such as that of a city or population. My personal ideas are of course much simpler, one being a 3 dimensional version which uses Papervision 3D; and, rather than switching cells on or off, would incrementally grow or shrink shapes in 3D space to create landscapes or cityscapes whose growth is determined by the development of the Automata.
This particular Actionscript 3 version is a fairly direct port of the 2 dimensional ‘Game of Life’ permutation, but is fun to play with none-the-less.
The rules are as follows:
Each cell can be either empty, new, alive or dead. As the grid updates, each cell observes its 8 neighbouring cells (or less if it is at a corner or edge) and changes it’s state depending on the state of said neighbouring cells.
- If less than 2 neighbours are alive, the cell dies (loneliness)
- If more than 3 neighbours are alive, the cell dies (overcrowding)
- If a living cell has 2 or 3 living neighbours it continues to live
- If a dead cell has 3 living neighbours it comes to life
There are many initial shapes which either form patterns of perpetual growth or at least interesting shapes as the system updates; some simply die out. Have a play and see if you can find shapes, or combinations of shapes which do interesting things. It’s quite addictive!
I have added a few presets to illustrate how certain groupings of cells that are alive when the system starts can behave in the grid as the automaton runs, and also a randomise button which will create a random grid of empty and alive cells and can form some interesting patterns as the system iterates and emergence begins to kick in.
The source code is of course available, so have a play with that and see what happens when you change the rules or create bigger grids.
A quick note about the Actionscript.
I have created a CAGrid class for building Cellular Automata, which can be instantiated at any size and with any amount of rows and colums.
/** * A grid of cells used to produce a 2 dimensional cellular automata * * @param gridSize The dimensions in pixels of the grid * @param segments The number of cells in each row or column * @param cellPad The padding (or spacing) inside each cell */ public function CAGrid( gridSize:int, segments:int, cellPad:int )
The CAGrid class contains methods for drawing the grid, resetting the grid, updating the system, randomising cells, saving configurations and loading presets, and is automatically redrawn when the size or rows / columns are changed during runtime using the size and segments setters respectively.
The Cell class contains a simple setter for status, and some constants for easy reference to the cell’s state. In this example the cell is drawn by choosing a colour which corresponds to the state of the cell, but a movieclip with labelled frames (for example) could just as easily be used to create more visually interesting results.
Oh, and included are some custom skinned CS3 components (button and comboBox) which look a bit like the Vista skin, so maybe those are goodies in themselves!
12 Responses to 2D Cellular Automata
Alexander Kirkley
May 11th 2008
2:27 am
I love this. It is possible to create something very beautiful with it. It is certainly a very addictive ‘game’. It would be even better it was possible to save some of the patterns in order to come up with increasingly more complex systems. Colour might be an interesting addition/complication. Would it be possible to allow greater complexity?
Soulwire
May 15th 2008
3:39 pm
Hey Alex, how’s it going? You can actually save patterns if you use the source code. I suppose when I have time I could integrate this with my database and have you guys save your favourites. Maybe a session based saving would be worthy though, if I have 5 minutes I’ll update it ;)
Colour would also be good, maybe an image behind influencing movement and colour?? Good stuff!
John Irving
Aug 15th 2008
8:31 am
Hi Soulwire,
this is another great example of your coding prowess!!!
I’ve been looking at a lot of other code you have on this site and would love to be able to apply some modification to this code so that the SoundMixer.computeSpectrum function could adjust how the cells interact with each other. I noticed that after a while, the cells form a stable environment and no longer come back to life or die. Maybe the SoundMixer.computeSpectrum function could be used in some way to ensure that the cells continue for as long as the music does. I think i’ll try mucking about with the rules for it a bit and see what i can come up with.
I’m trying to get my website up and running. (i produce music and wanted visualisations to go with it) You have so many pieces of AS3 code here, it’s a great resource for this. When it’s all up and running i’ll send you a link and i’ll also credit you and link to soulwire.co.uk on the site.
Cheers
Soulwire
Aug 15th 2008
8:53 am
Hey John,
Yeah, I’d love to see what you come up with! Regarding the CA, I suppose if the grid was 16 x 16 and each cell represented one of the 256 frequencies in each channel of the spectrum, you could, perhaps on the beat, reset the pattern so that all cells with an amplitude above a certain threshold lit up, then just letting the CA run until the next beat or interval would create some cool patterns. Also, at moments of high energy the grid would light up - I think that would look great.
Good luck with your work, I’ll look forward to seeing your site and hearing the music you produce :)
Dominic
Sep 16th 2008
7:52 pm
very nice!
i:DAT 204 | Organism Project « i-DAT 08/9 Web_Log
Oct 8th 2008
11:08 am
[...] http://blog.soulwire.co.uk/flash/actionscript-3/2d-cellular-automata/ [...]
Inspiration « [i/:DAT_204]
Oct 9th 2008
4:07 pm
[...] http://blog.soulwire.co.uk/flash/actionscript-3/2d-cellular-automata/#more-69 [...]
Dirk
Oct 25th 2008
1:15 pm
Hi, just stumbled across this interesting article. Maybe you want to have a look at our company-logo wich also consists of a conway automaton. I build this some years ago, should be time for a port to as 3 (*sigh*)…
Cheers, Dirk
Mugga
Mar 27th 2009
10:22 am
I play with this for some time!!
Peter Bird
Apr 5th 2009
7:36 am
It’s really interesting!
Anthony
Apr 7th 2009
5:24 pm
I Love Each and evrey bit of your code -> coding skills, Put me in you Fan List :D.
AS3 Boid Flocking Simulation using Steering Behaviors - AI Flash Player 10
May 5th 2009
3:12 pm
[...] and the notion that you can create complex systems from relatively simple components. Much like the Cellular Automata, you can program an agent to have only a limited set of abilities and awareness, define a set rules [...]
Leave a Reply