xxxxxxxxxx
/*
Game of Life 2 - conway's game of life mapped to a 3d object
Rules from wikipedia: https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
Article disusssing artificial life and conway's game of life:
http://makeyourownalgorithmicart.blogspot.co.uk/2018/05/artificial-life-and-conways-game-of-life.html
http://makeyourownalgorithmicart.blogspot.co.uk/2018/05/artificial-life-and-conways-game-of.html
*/
var img;
var counter = 0;
// create cells
var number_of_rows = 200;
var number_of_columns = 50;
var grid_array = [];
// cell size on screen
cell_size = 10;
function setup() {
createCanvas(800, 600, WEBGL);
// slow down the calling of draw() so we can see the patterns evolve
// create image for texture
img = createImage(number_of_rows, number_of_columns);
img.loadPixels();
// populate grid randomly
for (var j = 0; j < number_of_rows; j += 1) {
for (var i = 0; i < number_of_columns; i += 1) {
// convert (i,j) to position in array
var index = i + (j * number_of_columns);
// set cell value to either 0 or 1, but mostly 0
grid_array[index] = 0;
if (random() < 0.3) {
grid_array[index] = 1;
}
// test shapes
//
//grid_array[1 + (3*number_of_columns)] = 1;
//grid_array[2 + (3*number_of_columns)] = 1;
//grid_array[3 + (3*number_of_columns)] = 1;
//grid_array[2 + (2*number_of_columns)] = 1;
}
}
}
function draw() {
// draw grid into image texture
for (var j = 0; j < number_of_rows; j += 1) {
for (var i = 0; i < number_of_columns; i += 1) {
// convert (i,j) to position in array
var index = i + (j * number_of_columns);
// get current value and draw cell
var cell_value = grid_array[index];
var col = lerpColor(color('red'), color('black'), cell_value);
img.set(j,i, col);
}
}
img.updatePixels();
// draw 3d object
background('white');
push();
rotateZ(counter);
rotateX(counter);
rotateY(counter);
texture(img);
torus(200, 50, 48, 32);
pop();
counter += 0.01;
// initialise new temporary array
var grid_array_2 = [];
for (var j = 0; j < number_of_rows; j += 1) {
for (var i = 0; i < number_of_columns; i += 1) {
// convert (i,j) to position in array
var index = i + (j * number_of_columns);
grid_array_2[index] = 0;
}
}
// update grid
for (var j = 0; j < number_of_rows; j += 1) {
for (var i = 0; i < number_of_columns; i += 1) {
// get number of neighbours
var neighbours = 0;
// look at immediate neighbourhood
for (y = -1; y <= 1; y +=1 ){
for (x = -1; x <= 1; x +=1 ){
// wrap coordinates around
var wrapped_i = (i + x + number_of_rows) % number_of_rows;
var wrapped_j = (j + y + number_of_columns) % number_of_columns;
// don't include current cell, only neighbours
if (!(x == 0 && y == 0)) {
// convert (i,j) to position in array
var index = (wrapped_i) + (wrapped_j * number_of_columns);
neighbours += grid_array[index];
}
}
}
// apply rules
// convert (i,j) to position in array
var index = i + (j * number_of_columns);
// Rule 1. Any live cell with fewer than two live neighbors dies, as if caused by under population.
if (grid_array[index] == 1 && neighbours < 2) {
grid_array_2[index] = 0;
}
// Rule 2. Any live cell with two or three live neighbors lives on to the next generation.
if (grid_array[index] == 1 && (neighbours == 2 || neighbours == 3)) {
grid_array_2[index] = 1;
}
// Rule 3. Any live cell with more than three live neighbors dies, as if by overpopulation.
if (grid_array[index] == 1 && neighbours > 3) {
grid_array_2[index] = 0;
}
// Rule 4. Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
if (grid_array[index] == 0 && neighbours == 3) {
grid_array_2[index] = 1;
}
}
}
// point grid_array to new array, old one should be garbage collected
grid_array = grid_array_2;
}