From 59b9c0bb72f37aca7cceef2c01ec44fe80493705 Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Wed, 11 Nov 2015 14:18:25 -0500 Subject: [PATCH 1/3] Added Gravity glitched --- GravityBallCode/GravityBallCode.pde | 54 +++++++++++++++++------------ 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..214746a 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,16 +1,24 @@ //declare variables -float x, y, velX, velY, diam; - +int q = 500; +float[] y = new float [q]; +float[] velX = new float [q]; +float[] velY = new float [q]; +float[] diam = new float [q]; +float[] x = new float [q]; +float gravity = .5; 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); + + for (int i = 0; i < q; i++) { + x[i] = random(width); + y[i] = random(height); + diam[i] = random(10, 20); + velX[i] = random(-5, 6); + velY[i] = random(-5, 6); + } } void draw() { @@ -18,21 +26,21 @@ void draw() { background(0); //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 < q; i++) { + fill(random(255), random(255), 0); + x[i] += velX[i]; + velY[i] = velY[i] + gravity; + y[i] += velY[i]; + ellipse(x[i], y[i], diam[i], diam[i]); + if (x[i] + diam[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] <= 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] >= height) { + velY[i] = -abs(velY[i]); + } else if (y[i] - diam[i] <= 0) { + velY[i] = abs(velY[i]); + } } } \ No newline at end of file From ad5bd2b754a1976c88500def6c986db6eb06984b Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Wed, 11 Nov 2015 14:51:26 -0500 Subject: [PATCH 2/3] fixed glitch no longer falls through bottom. Glitches at the bottom by popping up slightly. --- GravityBallCode/GravityBallCode.pde | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 214746a..d6a6c25 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,5 +1,5 @@ //declare variables -int q = 500; +int q = 50; float[] y = new float [q]; float[] velX = new float [q]; float[] velY = new float [q]; @@ -27,7 +27,7 @@ void draw() { //draw ball for (int i = 0; i < q; i++) { - fill(random(255), random(255), 0); + fill(random(255),random(255),random(255)); x[i] += velX[i]; velY[i] = velY[i] + gravity; y[i] += velY[i]; @@ -37,10 +37,13 @@ void draw() { } else if (x[i] - diam[i] <= 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] >= height) { + if (y[i] + diam[i]/2 > height) { velY[i] = -abs(velY[i]); } else if (y[i] - diam[i] <= 0) { velY[i] = abs(velY[i]); } + if (y[i] >= height) { + y[i] = height - diam[i]/2; + } } } \ No newline at end of file From a4f1a8932133d7a6d7a842f307bd57a75607972e Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Thu, 12 Nov 2015 13:42:31 -0500 Subject: [PATCH 3/3] Got rid of roof Fixed glitch where some balls would hit the roof and then never get effected by the gravity. --- GravityBallCode/GravityBallCode.pde | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index d6a6c25..2d44705 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,5 +1,5 @@ //declare variables -int q = 50; +int q = 20; float[] y = new float [q]; float[] velX = new float [q]; float[] velY = new float [q]; @@ -15,7 +15,7 @@ void setup() { for (int i = 0; i < q; i++) { x[i] = random(width); y[i] = random(height); - diam[i] = random(10, 20); + diam[i] = random(20, 50); velX[i] = random(-5, 6); velY[i] = random(-5, 6); } @@ -27,7 +27,7 @@ void draw() { //draw ball for (int i = 0; i < q; i++) { - fill(random(255),random(255),random(255)); + fill(random(255), random(255), random(255)); x[i] += velX[i]; velY[i] = velY[i] + gravity; y[i] += velY[i]; @@ -39,8 +39,6 @@ void draw() { } if (y[i] + diam[i]/2 > height) { velY[i] = -abs(velY[i]); - } else if (y[i] - diam[i] <= 0) { - velY[i] = abs(velY[i]); } if (y[i] >= height) { y[i] = height - diam[i]/2;