This is my soda can button. The program detects when a soda can is placed on the coaster-like button and simulates fizzing by trapping the bubbles at the top of the screen. Once the can is lifted, the bubbles move upwards freely to give the effect of drinking the soda.
Here’s a close up of the button. I basically just soldered two wires to the mouse left button and made it so when the can was placed, it would complete the circuit and thus press the button.
Here’s the code. I modified this Processing tutorial.
int numBalls = 700;
float spring = 0.005;
float gravity = 0.03;
float friction = -0.5;
Ball[] balls = new Ball[numBalls];
void setup()
{
size(500, 800);
noStroke();
smooth();
for (int i = 0; i < numBalls; i++) {
balls[i] = new Ball(random(width), random(height), random(5, 15), i, balls);
}
}
void draw()
{
background(85,52,19);
for (int i = 0; i < numBalls; i++) {
if (keyPressed) {
balls[i].collide();
balls[i].collect();
balls[i].display();
} else {
balls[i].collide();
balls[i].move();
balls[i].display();
}
}
}
class Ball {
float x, y;
float diameter;
float vx = 0;
float vy = 0;
int id;
Ball[] others;
Ball(float xin, float yin, float din, int idin, Ball[] oin) {
x = xin;
y = yin;
diameter = din;
id = idin;
others = oin;
}
void collide() {
for (int i = id + 1; i < numBalls; i++) {
float dx = others[i].x – x;
float dy = others[i].y – y;
float distance = sqrt(dx*dx + dy*dy);
float minDist = others[i].diameter/2 + diameter/2;
if (distance < minDist) {
float angle = atan2(dy, dx);
float targetX = x + cos(angle) * minDist;
float targetY = y + sin(angle) * minDist;
float ax = (targetX – others[i].x) * spring;
float ay = (targetY – others[i].y) * spring;
vx -= ax;
vy -= ay;
others[i].vx += ax;
others[i].vy += ay;
}
}
}
void move() {
vy -= gravity;
y = y – .25;
if (y width) {
x = width – diameter;
vx *= friction;
}
else if (x – diameter height) {
y = height – diameter/2;
vy *= friction;
}
else if (y – diameter/2 < 0) {
y = diameter/2;
vy *= friction;
}
}
void display() {
fill(255, 204);
ellipse(x, y, diameter, diameter);
}
}