Created
November 17, 2015 16:29
-
-
Save claytical/77b63f61e5af835f03cf to your computer and use it in GitHub Desktop.
Simple Circle Collision
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var balls = []; | |
| function setup() { | |
| createCanvas(windowWidth,windowHeight); | |
| x = width/2; | |
| y = height/2; | |
| } | |
| function draw() { | |
| background(255,255,255); | |
| for (var i = 0; i < balls.length; i++) { | |
| balls[i].display(); | |
| } | |
| //check collision | |
| for (var i = 0; i < balls.length; i++) { | |
| for (var j = i + 1; j < balls.length; j++) { | |
| balls[i].hitCheck(balls[j]); | |
| } | |
| } | |
| } | |
| function mousePressed() { | |
| balls.push(new Ball()); | |
| } | |
| var Ball = function() { | |
| this.x = mouseX; | |
| this.y = mouseY; | |
| this.speedX = random(-1,1); | |
| this.diameter = random(20,50); | |
| } | |
| Ball.prototype.display = function() { | |
| ellipse(this.x, this.y, this.diameter, this.diameter); | |
| this.x += this.speedX; | |
| } | |
| Ball.prototype.hitCheck = function(otherBall) { | |
| if (dist(this.x, this.y, otherBall.x, otherBall.y) < this.diameter) { | |
| this.speedX *= -1; | |
| otherBall.speedX *= -1; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment