From f6965a2ce164ebb95d77111996a04e2490ba3dca Mon Sep 17 00:00:00 2001 From: rafaelcandamil Date: Wed, 11 Nov 2015 14:46:13 -0500 Subject: [PATCH 1/2] added gravity effect --- GravityBallCode/GravityBallCode.pde | 59 +++++++++++++++++------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..490013d 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,38 +1,47 @@ //declare variables -float x, y, velX, velY, diam; +int count = 30; // sets count +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]; +float [] gravity = new float[count]; //defines variables void setup() { - //set size of canvas - size(800, 600); + + size(800, 600); //set size of canvas - //initialize variables - x = width/2; - y = height/2; - diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + for (int i = 0; i < count; i++) { + //initialize variables + x [i] = width/2; + y [i] = height/2; + diam [i] = 50; + velx [i] = random(3, 7); + vely [i] = random(3, 7); + gravity [i] = .5; //sets speed of acceleration + } } void draw() { //draw background to cover previous frame background(0); + for (int i = 0; i < count; i++) { + vely [i] += gravity [i]; //adds gravity to accelaration - //draw ball - ellipse(x, y, diam, diam); - - //add velocity to position - x += velX; - y += velY; + //draw ball + x [i] += velx [i] ; + y [i] += vely [i] ; + ellipse(x [i], y [i], diam [i], diam [i]); - //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); + //bounce ball if it hits walls + if (x [i] + diam [i]/2 >= width) { + velx [i] = -abs(velx [i]); //if the ball hits the right wall, assign x [i] velocity [i] the negative version of itself + } else if (x [i] - diam [i]/2 <= 0) { + velx [i] = abs(velx [i]); //if the ball hits the left wall, assign x [i] velocity [i] the positive version of itself + } + if (y [i] + diam [i]/2 >= height) { //if ball hits bottom wall, reverse + vely [i] = -abs(vely [i]); + y [i] = height - diam[i]/2 ; + } } } \ No newline at end of file From 50c2f45825927152a859411c92d0df0bb65c45d0 Mon Sep 17 00:00:00 2001 From: rafaelcandamil Date: Thu, 12 Nov 2015 13:42:12 -0500 Subject: [PATCH 2/2] final version --- GravityBallCode/GravityBallCode.pde | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 490013d..f2be116 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -17,7 +17,7 @@ void setup() { y [i] = height/2; diam [i] = 50; velx [i] = random(3, 7); - vely [i] = random(3, 7); + vely [i] = random(3, 15); gravity [i] = .5; //sets speed of acceleration } }