2D Cellular Automata

Inspired by Conway’s game of life, 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!

Download: 2D Cellular Automata
Posted on 06 May 2008
6 Trackbacks

Warning: file_get_contents(http://search.twitter.com/search.atom?q=from:soulwire&rpp=1) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 410 Gone in /home/soulwire/webapps/soulwire_blog/wp-content/themes/soulwire/functions.php on line 203


17 Responses to 2D Cellular Automata

Leave a Reply

Pingbacks / Trackbacks

  1. 8 years ago AS3 Boid Flocking Simulation using Steering Behaviors - AI Flash Player 10

    [...] 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 [...]

  2. 6 years ago » Kanye’s Game of Life Frankfurt School of Pokémon

    [...] to this person for sharing their [...]

  1. Alexander Kirkley 9 years ago

    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?

    Reply to this comment

  2. Soulwire 9 years ago

    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!

    Reply to this comment

  3. John Irving 8 years ago

    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.


    Reply to this comment

  4. Soulwire 8 years ago

    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 :)

    Reply to this comment

  5. Dominic 8 years ago
  6. Dirk 8 years ago

    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

    Reply to this comment

  7. Mugga 8 years ago

    I play with this for some time!!

    Reply to this comment

  8. Peter Bird 8 years ago

    It’s really interesting!

    Reply to this comment

  9. Anthony 8 years ago

    I Love Each and evrey bit of your code -> coding skills, Put me in you Fan List :D.

    Reply to this comment

  10. Cameron Incarnato 7 years ago

    Lol, wirklich gut. Komme jetzt

    Reply to this comment

  11. Cameron Incarnato 7 years ago

    Lol, wirklich gut. Komme jetzt

    Reply to this comment

  12. mike 7 years ago

    Hi there, nice work!

    I created a similar kind game of life a few months ago.

    I never could really complete the project but pushed it pretty far.

    Check at a demo on my blog and download the full source code if it’s of some interest for you.


    Hope you enjoy!


    Reply to this comment