From 8a77e9cf710c2e11d5dae0d31af619868d7701c5 Mon Sep 17 00:00:00 2001 From: SMoy17 Date: Tue, 10 Nov 2015 13:59:29 -0500 Subject: [PATCH 1/3] One Ball w/Gravity --- GravityBallCode/GravityBallCode.pde | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..c384761 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,18 +11,22 @@ void setup() { diam = 80; velX = random(-5, 5); velY = random(-5, 5); + gravity = .1; } void draw() { //draw background to cover previous frame background(0); + 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) { From 54086b49814a177081cde5cbb1890e5893fa053a Mon Sep 17 00:00:00 2001 From: SMoy17 Date: Tue, 10 Nov 2015 14:14:05 -0500 Subject: [PATCH 2/3] Many Balls w/Gravity --- GravityBallCode/GravityBallCode.pde | 54 +++++++++++++++++++---------- 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index c384761..1dd4c51 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,42 +1,60 @@ //declare variables -float x, y, velX, velY, diam, gravity; +int count = 30; + + +//declare and initialize arrays +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; +int i; void setup() { //set size of canvas size(800, 600); +for (int i = 0; i < count; i++) { //initialize variables - x = width/2; - y = height/2; - diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + 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); - velY += gravity; + //draw ball - ellipse(x, y, diam, diam); + for (i = 0; i < count; i++) { + + velY[i] += gravity; + ellipse(x[i], y[i], diam[i], diam[i]); + //add velocity to position - x += velX; - y += velY; + x[i] += velX[i]; + y[i] += velY[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 (x[i] + diam[i]/2 >= 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 + diam/2 >= height) { - velY = -abs(velY); - } else if (y - diam/2 <= 0) { - velY = abs(velY); + 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 ba42602908685512e844e65ff70207e0870ab432 Mon Sep 17 00:00:00 2001 From: SMoy17 Date: Thu, 12 Nov 2015 13:42:04 -0500 Subject: [PATCH 3/3] Doesn't Sink --- GravityBallCode/GravityBallCode.pde | 1 + 1 file changed, 1 insertion(+) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 1dd4c51..c04248b 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -52,6 +52,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]); }