//CSC 123 Lab 9 - fillin the collide function //This program is an animation of severals balls bouncing around - //Students need to modify the code to provide a function to test if any two balls are //colliding and if they are to reverse the direction of one of teh balls, so they //'bounce' off one another //*** In its current form this code will not compile until you write the necessary function //*** To start, find the "TODO" comment in this program and fill it in int numD; PVector v[]; float curX[]; float curY[]; float t; color BC[]; int rad; void setup() { size(500, 500); smooth(); numD = 10; t = 3.5; rad = 45; v = new PVector[numD]; curX = new float[numD]; curY = new float[numD]; BC = new color[numD]; //set balls directions for (int i =0; i < numD; i++) { v[i] = new PVector(random(1,4), random(-4, 4)); BC[i] = color(random(0, 255), random(0, 10), random(0, 255)); curX[i] = random(0, 500); curY[i] = random(0, 500); } frameRate(10); //Test to make sure that no two balls start stuck together for (int i=0; i < numD; i++) { //test this ball against all other balls, except this one for (int j=0; j< numD; j++) { // only test against other balls if (j != i) { //test if these balls are colliding - if so, reverse the 'j' ball if (collide(curX[i], curY[i], rad/2, curX[j], curY[j], rad/2)) { curX[i] += 45; } } } } } //TODO, write the collide function!!! - note that two balls are colliding if the distance between the //two balls ceneters is less than the sum of their two radii //you may also want to write another helper function (like one to compute the distance of two points) //function header: //function to test if two speheres are colliding //input: center of a circle and its radius, center of the other circle and its radius (6 floats) //output: true or false (true if colliding, false otherwise) //example: collide(0, 0, 2, 1, 1, 2) -> true void draw() { float tmp; background(0); //First test for collisions for (int i=0; i < numD; i++) { //test this ball against all other balls, except this one for (int j=0; j< numD; j++) { // only test against other balls if (j != i) { //test if these balls will collide int the next frame - if so, reverse the 'j' ball if (collide(curX[i] + v[i].x*t, curY[i] + v[i].y*t, rad/2, curX[j], curY[j], rad/2)) { tmp = v[i].x; v[i].x = v[i].y; v[i].y = -tmp; } } } } //now draw all the balls and update their direction for (int i=0; i < numD; i++) { fill(BC[i]); ellipse(curX[i], curY[i], rad, rad); curX[i] = curX[i] + v[i].x*t; curY[i] = curY[i] + v[i].y*t; //edge conditions if (curX[i] > width || curX[i] < 0) { v[i].x = -v[i].x; } if (curY[i] < 0 || curY[i] > height) { v[i].y = -v[i].y; } } }