/** * Welcome to Skanaar microBilliard

* * Click, drag and release on a ball to give it a push.
* numerical keys 1-4 respawns different number of balls **/ Table game; //-------------- void setup(){ size(210,330); smooth(); game = new Table(6,5,200,300); game.startGame(); } //-------------- void draw(){ background(32); game.update(); game.visualize(); } //-------------- void keyPressed(){ println( game.kineticEnergy() ); } //-------------- void mousePressed(){ game.mousePressed(mouseX-game.x,mouseY-game.y); } //-------------- void mouseReleased(){ game.mouseReleased(mouseX-game.x,mouseY-game.y); } //============================= class Table{ float drag = 0.985; float elasticity = 0.9; float wallElasticity = 0.8; float pushFactor = 0.05; float maxPush = 10; color[] ballColors = new color[]{color(192),color(192,64,32),color(64,192,0)}; Ball[] balls; Ball selectedBall; int x,y,width,height; //-------------- Table(int x, int y, int w, int h){ this.x = x; this.y = y; width = w; height = h; } //-------------- void startGame(){ buildBalls(8); } //-------------- void buildBalls(int count){ balls = new Ball[2*count+1]; for(int i=0;i maxPush ){ px = maxPush*px/push; py = maxPush*py/push; } selectedBall.push(px,py); } selectedBall = null; } }