/********************************* * Balls can be thrown about and * softly bounce off one another **********************************/ // Physical variables to play with var acceleration = 0.05; // Force of throw var drag = 0.975; // Air resistance var friction = 0.85; // Wall bounciness var gravity = 0.5; // Force due to gravity var numBalls = 5; // Number of balls var ballX = 0; // where balls will come from var ballY = 0; // Ball object with a position, speed and colour var Ball = function(x, y, r, c){ this.x = x; this.y = y; this.r = r; this.c = c; this.dx = (mouseX - ballX)/30*1.5; this.dy = (mouseY - ballY)/33*1.5; // Move ball based on its velocity this.move = function(){ this.dx *= drag; this.dy *= drag; this.x += this.dx; this.y += this.dy; }; // Bounce off walls this.bounce = function(){ if (this.x < this.r) { this.x = this.r; this.dx *= -friction; } if (this.x > 400 - this.r){ this.x = 400 - this.r; this.dx *= -friction; } if (this.y < this.r) { this.y = this.r; this.dy *= -friction; } if (this.y > 400 - this.r){ this.y = 400 - this.r; this.dy *= -friction; } }; // Test whether mouse is over ball this.selected = function() { if (abs(mouseX - this.x) < this.r && abs(mouseY - this.y) < this.r) { return true; } }; this.collide = function(that) { var dx = this.x - that.x; var dy = this.y - that.y; var dr = this.r + that.r; if (dx * dx + dy * dy < dr * dr){ var theta = atan2(dy, dx); var force = (dr - sqrt(dx*dx + dy*dy)); this.dx += force * cos(theta); that.dx -= force * cos(theta); this.dy += force * sin(theta); that.dy -= force * sin(theta); } }; }; var drawCannon = function() { var rotateValue = atan(mouseY/mouseX); rotate(rotateValue); fill(255, 0, 0, 50); ellipse(0, 0, 114, 27); rotate(-rotateValue); }; var balls = []; for (var b=0; b