/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.uz3P5vm4N5i/rev.26
*
* authors:
* GoToLoop
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/**
* Ball in the Chamber (v2.04)
* by Rareware0192 (2015-May-06)
* Mod GoToLoop (2015-May-07)
*
* Forum.Processing.org/two/discussion/10680/collision-colors
* Studio.ProcessingTogether.com/sp/pad/export/ro.9qPrLGrYGkr2o
* Bl.ocks.org/GoSubRoutine/d0b7d3058d84970e83cf8685f8e69777
*/
static final int BALLS = 4;
final Ball[] balls = new Ball[BALLS];
static final int CHAMBERS = 8;
final Chamber[] chambers = new Chamber[CHAMBERS];
void setup() {
size(640, 440);
smooth(3);
frameRate(60);
ellipseMode(CENTER);
rectMode(CORNER);
strokeWeight(Ball.BOLD);
stroke(Ball.STROKE);
balls[0] = new Ball(50, 50, 4, 2);
balls[1] = new Ball(50, 80, 3, 5);
balls[2] = new Ball(100, 150, 4, 5);
balls[3] = new Ball(300, 300, 6, 2);
chambers[0] = new Chamber(000, 000, #FF0000); // red
chambers[1] = new Chamber(600, 000, #00FF00); // green
chambers[2] = new Chamber(000, 400, #0000FF); // blue
chambers[3] = new Chamber(600, 400, #FF00FF); // pink
chambers[4] = new Chamber(300, 000, #FFFF00); // yellow
chambers[5] = new Chamber(300, 400, #00FFFF); // cyan
chambers[6] = new Chamber(000, 200, #FA9600); // orange
chambers[7] = new Chamber(600, 200, #A100FA); // purple
}
void draw() {
background(0350);
for (final Ball b : balls) {
for (final Chamber c : chambers) if (b.colliding(c)) {
b.c = c.c;
break;
}
b.script();
}
for (final Chamber c : chambers) c.display();
}
static final int sq(final int n) {
return n*n;
}
class Ball {
static final short DIM = 25, RAD = DIM >> 1;
static final short BOLD = 2;
static final color STROKE = 0;
int x, y, vx, vy;
color c = -1;
Ball(int x$, int y$, int vx$, int vy$) {
x = x$;
y = y$;
vx = vx$;
vy = vy$;
}
void script() {
update();
display();
}
void update() {
if ((x += vx) > width - RAD | x < RAD) vx *= -1;
if ((y += vy) > height - RAD | y < RAD) vy *= -1;
}
void display() {
fill(c);
ellipse(x, y, DIM, DIM);
}
boolean colliding(final Chamber c) {
return sq(c.x + Chamber.RAD - x) + sq(c.y + Chamber.RAD - y)
< sq(Chamber.RAD + RAD);
}
}
class Chamber {
static final short DIM = 40, RAD = DIM >> 1;
static final short BOLD = 2;
static final color STROKE = 0;
final int x, y;
final color c;
Chamber(int x$, int y$, color c$) {
x = x$;
y = y$;
c = c$;
}
void display() {
fill(c);
rect(x, y, DIM, DIM);
}
}