From 21fd283f11e9ae8e2a0e2ba487427ff9530c45f6 Mon Sep 17 00:00:00 2001 From: nli17 Date: Tue, 10 Nov 2015 13:58:37 -0500 Subject: [PATCH 1/3] gravity --- GravityBallCode/GravityBallCode.pde | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..6f7b879 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,5 +1,5 @@ //declare variables -float x, y, velX, velY, diam; +float x, y, velX, velY, diam, gravity; void setup() { //set size of canvas @@ -11,12 +11,16 @@ void setup() { diam = 80; velX = random(-5, 5); velY = random(-5, 5); + gravity= 1; } void draw() { //draw background to cover previous frame background(0); + //add gravity + velY += gravity; + //draw ball ellipse(x, y, diam, diam); @@ -24,6 +28,9 @@ void draw() { 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 From a76035ce594010fb09dd83babc3d646cd2baf075 Mon Sep 17 00:00:00 2001 From: nli17 Date: Tue, 10 Nov 2015 14:19:48 -0500 Subject: [PATCH 2/3] creating arrays --- GravityBallCode/GravityBallCode.pde | 74 ++++++++++++++++------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 6f7b879..e196acc 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,45 +1,53 @@ //declare variables -float x, y, velX, velY, diam, gravity; +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]; +float gravity; + 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); - gravity= 1; + for (int i = 0; i < count; i++) { + //initialize variables + x[i] = width/2; + y[i] = height/2; + diam[i] = 80; + velX[i] = random(-5, 5); + velY[i] = random(-5, 5); + gravity= 1; + } } void draw() { //draw background to cover previous frame background(0); - - //add gravity - velY += gravity; - - //draw ball - ellipse(x, y, diam, diam); - - //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); + for (int i=0; i= width) { + velX[i] = -abs(velX[i]); //if the ball hits the right wall, assign x velocity 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 velocity the positive version of itself + } + 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 From 8f7134ba5e0e29f3beae285cea0e0d713c61934d Mon Sep 17 00:00:00 2001 From: nli17 Date: Wed, 11 Nov 2015 14:38:51 -0500 Subject: [PATCH 3/3] gravity --- GravityBallCode/GravityBallCode.pde | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index e196acc..57a5fc7 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -18,7 +18,7 @@ void setup() { diam[i] = 80; velX[i] = random(-5, 5); velY[i] = random(-5, 5); - gravity= 1; + gravity= .5; } } @@ -31,7 +31,7 @@ void draw() { //draw ball ellipse(x[i], y[i], diam[i], diam[i]); - fill(random(100),random(100), random(100)); + fill(random(100), random(100), random(100)); noStroke(); //add velocity to position @@ -46,6 +46,7 @@ void draw() { } if (y[i] + diam[i]/2 >= height) { velY[i] = -abs(velY[i]); + y[i]=height-diam[i]/2; } else if (y[i] - diam[i]/2 <= 0) { velY[i] = abs(velY[i]); }