color red = color(255, 0, 0); color green = color(0, 255, 0); color blue = color(0, 0, 255); color black = color(0, 0, 0); // circle 1 float x1, y1, dx1, dy1, diam1; // circle 2 float x2, y2, dx2, dy2, diam2; // circle 3 float x3, y3, dx3, dy3, diam3; void setup() { size(500, 500); // Ball 1 x1 = 100; y1 = 100; dx1 = 2; dy1 = 4; diam1 = 50; // Ball 2 x2 = 400; y2 = 400; dx2 = 1; dy2 = -5; diam2 = 70; // Ball 3 x3 = 250; y3 = 250; dx3 = -2; dy3 = -3; diam3 = 100; } void draw() { background(black); noStroke(); // Ball 1 drawBall(x1, y1, diam1, red); x1 += dx1; y1 += dy1; // Ball 2 drawBall(x2, y2, diam2, green); x2 += dx2; y2 += dy2; // Ball 3 drawBall(x3, y3, diam3, blue); x3 += dx3; y3 += dy3; // Ball 1? dy1 = hitTop(y1, diam1, dy1); dy1 = hitBottom(y1, diam1, dy1); dx1 = hitLeft(x1, diam1, dx1); dx1 = hitRight(x1, diam1, dx1); // Ball 2? dy2 = hitTop(y2, diam2, dy2); dy2 = hitBottom(y2, diam2, dy2); dx2 = hitLeft(x2, diam2, dx2); dx2 = hitRight(x2, diam2, dx2); dy3 = hitTop(y3, diam3, dy3); dy3 = hitBottom(y3, diam3, dy3); dx3 = hitLeft(x3, diam3, dx3); dx3 = hitRight(x3, diam3, dx3); } void drawBall(float x, float y, float diam, color fillColor) { fill(fillColor); ellipse(x, y, diam, diam); } float hitTop(float y, float diam, float dy) { if (y - diam / 2 <= 0) { return -dy; } else { return dy; } } float hitBottom(float y, float diam, float dy) { if (y + diam / 2 >= 499) { return -dy; } else { return dy; } } float hitLeft(float x, float diam, float dx) { if (x - diam / 2 <= 0) { return -dx; } else { return dx; } } float hitRight(float x, float diam, float dx) { if (x + diam / 2 >= 499) { return -dx; } else { return dx; } }