From 7f319b1ce2ad2258bd6279042fcc3ecd7e1c0b3a Mon Sep 17 00:00:00 2001 From: stoussaint1 Date: Tue, 10 Nov 2015 14:00:25 -0500 Subject: [PATCH] updated gravity balls --- GravityBallCode/GravityBallCode.pde | 62 ++++++++++++++++------------- 1 file changed, 35 insertions(+), 27 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..5883392 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,38 +1,46 @@ -//declare variables -float x, y, velX, velY, diam; +int count = 30; + + + +float[] x = new float[count]; +float[] y = new float[count]; +float[] velx = new float[count]; +float[] vely = new float[count]; +float[] diam = new float[count]; void setup() { - //set size of canvas - size(800, 600); - - //initialize variables - x = width/2; - y = height/2; - diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + size(600, 600); + + + for (int i = 0; i < count; i++) { + x[i] = random(width); + y[i] = random(height); + diam[i] = 10 + 10*i; + velx[i] = random(-5, 5); + vely[i] = random(-5, 5); + } } void draw() { - //draw background to cover previous frame + background(0); - //draw ball - ellipse(x, y, diam, diam); + for (int i = 0; i < count; i++) { + fill(random(255), random(255), random(255)); + ellipse(x[i], y[i], diam[i], diam[i]); + x[i] += velx[i]; + y[i] += vely[i]; - //add velocity to position - x += velX; - y += velY; - //bounce ball if it hits walls - if (x + diam/2 >= width) { - velX = -abs(velX); //if the ball hits the right wall, assign x velocity the negative version of itself - } else if (x - diam/2 <= 0) { - velX = abs(velX); //if the ball hits the left wall, assign x velocity the positive version of itself - } - if (y + diam/2 >= height) { - velY = -abs(velY); - } else if (y - diam/2 <= 0) { - velY = abs(velY); + if (x[i] + diam[i]/2 >= width) { + velx[i] = -abs(velx[i]); + } else if (x[i] - diam[i]/2 <= 0) { + velx[i] = abs(velx[i]); + } + if (y[i] + diam[i]/2 >= height) { + vely[i] = -abs(vely[i]); + } else if (y[i] - diam[i]/2 <= 0) { + vely[i] = abs(vely[i]); + } } } \ No newline at end of file