From 77559710f5933ddb9502ed42e8ea937e94e648dd Mon Sep 17 00:00:00 2001 From: JLimoo11 Date: Tue, 10 Nov 2015 13:58:16 -0500 Subject: [PATCH 1/4] Gravity Ball Code Ball has gravity --- GravityBallCode/GravityBallCode.pde | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..cf68424 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,5 +1,7 @@ //declare variables float x, y, velX, velY, diam; +float earthg; + void setup() { //set size of canvas @@ -11,6 +13,7 @@ void setup() { diam = 80; velX = random(-5, 5); velY = random(-5, 5); + earthg = .1; } void draw() { @@ -23,6 +26,7 @@ void draw() { //add velocity to position x += velX; y += velY; + velY+=earthg; //bounce ball if it hits walls if (x + diam/2 >= width) { @@ -34,5 +38,5 @@ void draw() { velY = -abs(velY); } else if (y - diam/2 <= 0) { velY = abs(velY); - } + } } \ No newline at end of file From 006fcb99b953f7ca928be967826405f67e51a49f Mon Sep 17 00:00:00 2001 From: JLimoo11 Date: Wed, 11 Nov 2015 14:00:24 -0500 Subject: [PATCH 2/4] Fixed Gravity It doesn't bounce higher anymore --- GravityBallCode/GravityBallCode.pde | 63 ++++++++++++++++------------- 1 file changed, 36 insertions(+), 27 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index cf68424..1c47c72 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,42 +1,51 @@ //declare variables -float x, y, velX, velY, diam; -float earthg; +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[]earthg = 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); - earthg = .1; + for (int i =0; i= 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 + //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 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]); + //} } - if (y + diam/2 >= height) { - velY = -abs(velY); - } else if (y - diam/2 <= 0) { - velY = abs(velY); - } } \ No newline at end of file From 9b226edd8b293693541092f226b459728f92ad99 Mon Sep 17 00:00:00 2001 From: JLimoo11 Date: Wed, 11 Nov 2015 14:50:49 -0500 Subject: [PATCH 3/4] Fixed floor Does not fall beneath floor --- GravityBallCode/GravityBallCode.pde | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 1c47c72..8724351 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -43,9 +43,10 @@ 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]); //} } + } } \ No newline at end of file From cb71132065d6bd63cf0ecf18187d34fab8e9539a Mon Sep 17 00:00:00 2001 From: JLimoo11 Date: Thu, 12 Nov 2015 13:44:27 -0500 Subject: [PATCH 4/4] added comments finalized --- GravityBallCode/GravityBallCode.pde | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 8724351..cc8ac6c 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,17 +1,17 @@ //declare variables -int count = 30; +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[]earthg = new float[count]; +float[]earthg = new float[count]; void setup() { //set size of canvas size(800, 600); - for (int i =0; i= height) { - velY[i] = -abs(velY[i]); - y[i]= height - diam[i]/2; + velY[i] = -abs(velY[i]); //if the ball hits bottom wall, reverse velocity + y[i]= height - diam[i]/2; // makes Y position above bottom //else if (y[i] - diam[i]/2 <= 0) { // velY[i] = abs(velY[i]); //}