Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

3D Cellular Automata Problem... Failed....

$
0
0

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. I have no idea what's the problem with my codes. Could anyone help me please?

Below is the code:

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

    int generations;
    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);
      generations = 0;
      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, 3, 0.8);
          }
        }
      }
    }

    void draw () {
      background (0);
      stroke (255);

      generations++;
      rotateX(radians(45));
      rotateZ(radians(45));

      fill(255);
      cam.beginHUD();
      text("Generations: "+generations, width-150, height-20);
      cam.endHUD();

      // 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;
  float density = 0.8;
  int sz = 3;
  int [][][][] world;

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

  CA(Vec3D _loc, int _x, int _y, int _z, int _sz, float _density) {
    loc = _loc;
    x = _x;
    y = _y;
    z = _z;
    density = _density;
    sz = _sz;
    world = new int [x][y][z][2];
    for (int i = 0; i < density*x*y*z; i+=sz ) {
      for (int j = 0; j < density*x*y*z; j+=sz ) {
        for (int k = 0; k < density*x*y*z; k+=sz ) {
          world [( int ) random (x)] [( int ) random (y)] [( int ) random (z)] [0] = 1;

          //   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);
      pushMatrix ();
      translate ( cols, rows, depth );
      box ( sz );
      popMatrix ();
      strokeWeight ( 2 );
      point (loc.x, loc.y, loc.z);
    }
  }
}

Thank you.


Viewing all articles
Browse latest Browse all 2896

Trending Articles