/** * Collision detection * * Math for artists who now need to program * ITP DriveBy, 10/2/2008 * Elie Zananiri - ez@silentlycrashing.net */ //---------------------------- // variables int staticX; int staticY; int staticSize; int mobileSize; //---------------------------- void setup() { // set up the applet size(300, 300); smooth(); frameRate(30); noCursor(); noStroke(); // set the ball params staticSize = (int)random(10, 100); mobileSize = (int)random(10, 100); staticX = (int)random(staticSize, width - staticSize); staticY = (int)random(staticSize, height - staticSize); } //---------------------------- void draw() { // clear the screen if (areColliding()) { // some shade of red background(color(random(200, 255), 0, 0)); } else { background(0); } // draw the static ball fill(#505EEA); ellipse(staticX, staticY, staticSize, staticSize); // draw the moving ball fill(#7BD14A); ellipse(mouseX, mouseY, mobileSize, mobileSize); } //---------------------------- boolean areColliding() { float currDistance = sqrt(pow(staticX - mouseX, 2) + pow(staticY - mouseY, 2)); // alternatively, use the built-in dist(...) function //float currDistance = dist(staticX, staticY, mouseX, mouseY); float minDistance = staticSize/2 + mobileSize/2; if (currDistance < minDistance) { return true; } return false; }