Page 1 of 1

3d Cellular Automata

Posted: March 12th, 2016, 11:46 pm
by TwoPeas
Hi. I am very new to processing, and currently I am doing some exploration on 3D Cellular Automata. Basically, I followed the tutorial of Cellular Automata by Jose Sanchez ( in 2d) and I am trying to edit the codes into 3d. The first intention is to make the cell grows in the z-axis. I tried a few times, and I did not manage to do it. So in the end I chose to make the whole thing as a box, and have the activity of cells inside the box. But I failed. Could anyone help me please?

if there's a way to iterate everything in the z-axis, it would be the best! ( as that was the first intention )

Below is the code:

Code: Select all

import peasy.*;
import toxi.geom.*;
PeasyCam cam; 

int cols = 50; // population
int rows = 50;
int depth = 50;


CA [][][] grid = new CA [cols][rows][depth]; //declare two dimmensional array

void setup () {
  size(1280, 720, P3D);
  frameRate (12);

  cam = new PeasyCam (this, 100);

  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      for ( int k = 0; k < depth; k++) {

        Vec3D ptLoc = new Vec3D ( i* 10, j * 10, k*10 );
        grid [i][j][k] = new CA (ptLoc, i, j, k);
      }
    }
  }
}

void draw () {
  background (0);
  stroke (255);
  fill (255, 60);
  // rect (0,0,600,600);

  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      for ( int k = 0; k < depth; k++) {

        grid[i][j][k].run();
      }
    }
  }


  for (int i = 0; i < cols; i++) {
    for (int j = 0; j < rows; j++) {
      for ( int k = 0; k < depth; k++) {
        grid[i][j][k].updateType();
        grid[i][j][k].loc.z = grid[i][j][k].loc.z  ;
      }
    }
  }
}


////////////////////////////////////////////////////CLASS CA
class CA {
  Vec3D loc;
  int x;
  int y;
  int z;

  //specify your neighbour
  int type = 0;
  int futType = 100;

  CA(Vec3D _loc, int _x, int _y, int _z) {
    loc = _loc;
    x = _x;
    y = _y;
    z = _z;

    float rnd = random (100);
    if (rnd < 50) {

      type = 1;
    }
  }

  void run() {
    display();
    evo1N();
  }

  void updateType() {
    type = futType;
  }

  void evo1N() {

    int count = 0;
    if ( grid [(x+cols-1)%cols] [(y+rows-1)%rows ]  [(z+depth+1)  %depth].type == 1) count ++;
    if ( grid [(x+cols)%cols]   [(y+rows-1)%rows]   [(z+depth-1)  %depth].type == 1) count ++;
    if ( grid [(x+cols+1)%cols] [(y+rows-1)%rows]   [(z+depth)  %depth].type == 1) count ++;
    if ( grid [(x+cols-1)%cols] [(y+rows)%rows]     [(z+depth-1)    %depth].type == 1) count ++;
    if ( grid [(x+cols+1)%cols] [(y+rows)%rows]     [(z+depth)    %depth].type == 1) count ++;
    if ( grid [(x+cols-1)%cols] [(y+rows+1)%rows]   [(z+depth)  %depth].type == 1) count ++;
    if ( grid [(x+cols)%cols]   [(y+rows+1)%rows]   [(z+depth+1)  %depth].type == 1) count ++;
    if ( grid [(x+cols+1)%cols] [(y+rows+1)%rows]   [(z+depth+1)  %depth].type == 1) count ++;

    //if i have too many neighbours around me, i die out of the over population
    if (type == 1 && count < 2) {
      futType = 0;
    }

    if (type == 1 && count <=3 && count >=2) {
      futType = 1;
    }

    if (type ==1 && count > 3) {
      futType = 0;
    }

    if (type == 0 && count == 3) {
      futType = 1;
    }
  }

  void display () {

    if (type == 1) {
      stroke (255);
      strokeWeight ( 5 );
      point (loc.x, loc.y, loc.z);
      
    }
    
  }
}
Thank you.

Re: 3d Cellular Automata

Posted: March 13th, 2016, 12:05 am
by drc
TwoPeas wrote: Below is the code:
Please put into

Code: Select all

[ /code]