// Variance int numBall = 20; Ball[] maru = new Ball[numBall]; int[][] iro = new int[numBall][3]; float[][] spd = new float[numBall][3]; float[][] pos = new float[numBall][3]; float dm = 20.0; void setup() { size(400, 400); //window background(0, 0, 0); //black noStroke(); //lineless for (int i = 0; i < numBall; i++) { for (int j=0; j<3; j++ ) { iro[i][j] = (int)(random(1)*255); spd[i][j] = random(-1,1); pos[i][j] = 180*random(-1,1)+200; } maru[i] = new Ball(pos[i][0], pos[i][1], dm, spd[i][0], spd[i][1], iro[i]); } } void draw(){ fill(0,0,0); //clear color rect(0, 0, width, height); for (int i = 0; i < numBall; i++) { collisionDetect(i); maru[i].move(width, height); maru[i].display(); } } void collisionDetect(int i) { for (int k = 0; k < numBall; k++ ) { if ( k != i ) { float bpx = maru[i].px - maru[k].px; float bpy = maru[i].py - maru[k].py; float dd = dist(0, 0, bpx, bpy); //distance of balls if( dd <= dm ){ maru[i].clear(); maru[k].clear(); break; } } } } class Ball { float px, py; //position float vx, vy; //velocity float dia; //diameter int[] col = {}; Ball(float x, float y, float d) { px = x; py = y; dia = d; } Ball(float x, float y, float d, int[] c) { px = x; py = y; dia = d; col = c; } Ball(float x, float y, float d, float sx, float sy, int[] c) { px = x; py = y; dia = d; vx = sx; vy = sy; col = c; } void display() { fill(col[0], col[1], col[2]); ellipse(px, py, dia, dia); } void display(int[] col) { fill(col[0], col[1], col[2]); ellipse(px, py, dia, dia); } void move(int w, int h) { int fx = 1; int fy = 1; if (px <= dia/2 || px >= w-dia/2 ) { fx *= -1; } if (py <= dia/2 || py >= h-dia/2 ) { fy *= -1; } vx *= fx; vy *= fy; px += vx; py += vy; } void clear() { px = width+10; py = height+10; } }