The idea is this: Set all pixels positions on screen to a random value of either 0,1,2, or 3. Set a particle “car” in the middle with an arbitrary velocity vector. On each frame, the car will move to a new position.
if the pixel is 0, increase speed slightly, but never more than maxSpeed.
If the pixel is 1, angle slightly to the right.
If the pixel is 2, decrease speed slightly, but never less than 1.
If the pixel is 3, angle slightly to the left.
Reset the pixel it’s on to a new random value so no infinite loops occur, and then update position.
I already have some code:
// Settings
final float SPEED_STEP = 0.1;
final float SPEED_MAX = 2;
final float SPEED_MIN = 1;
final float ANGLE_STEP = PI/2; //pi/2 radians, 90 degrees
final color COLOR_CAR = color(255, 0, 0); //Red car - my favorite!
final color COLOR_BACKGROUND = color(0, 0, 0); //Black background
// Car variables
float car_px;
float car_py;
float car_vx;
float car_vy;
float car_angle;
float car_speed;
// Automaton variables
int cell;
// Display settings
int[][] grid;
int col;
int pix;
int car;
// Setup
void setup() {
size(500, 500);
grid = new int[width][height];
smooth(); //I think this is what I want
// Randomize screen contents
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
grid[x][y] = round(random(0, 3));
}
}
// Place car
car_px = width / 2;
car_py = height / 2;
car_angle = quant(random(0, TWO_PI), ANGLE_STEP);
car_speed = random(SPEED_MIN, SPEED_MAX);
}
// Loop
void draw() {
background(COLOR_BACKGROUND);
// Car physics
car_speed = constrain(car_speed, SPEED_MIN, SPEED_MAX);
car_angle %= TWO_PI;
car_vx = car_speed * cos(car_angle);
car_vy = car_speed * sin(car_angle);
car_px = constrain(car_px + car_vx, 0, width);
car_py = constrain(car_py + car_vy, 0, height);
// Cellular automaton
cell = grid[(int) car_px][(int) car_py];
switch(cell) {
case 0:
car_speed += SPEED_STEP;
break;
case 1:
car_angle += ANGLE_STEP;
break;
case 2:
car_speed -= SPEED_STEP;
break;
case 3:
car_angle -= ANGLE_STEP;
break;
}
// Graphics
loadPixels();
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
pix = x + (y * width);
col = (int) map(grid[x][y], 0, 3, 0, 255);
pixels[pix] = col;
//stroke(col);
//point(x, y);
}
}
car = (int) (car_px + (car_py * width));
pixels[car] = COLOR_CAR;
//stroke(COLOR_CAR);
//point(car_px, car_py);
updatePixels();
}
// Quantization function
float quant(float x, float step) {
return round(x / step) * step;
}
It compiles without any issues, but it doesn’t quite work the way it should: