From 4096d4e4bea5682aa19a5a892d56370867705189 Mon Sep 17 00:00:00 2001 From: twang1999 Date: Tue, 10 Nov 2015 13:56:26 -0500 Subject: [PATCH 1/4] add gravity/acceleration to the ball --- GravityBallCode/GravityBallCode.pde | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..029f0d9 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, grav; void setup() { //set size of canvas @@ -11,6 +11,7 @@ void setup() { diam = 80; velX = random(-5, 5); velY = random(-5, 5); + grav=.1; } void draw() { @@ -23,6 +24,7 @@ void draw() { //add velocity to position x += velX; y += velY; + velY += grav; //bounce ball if it hits walls if (x + diam/2 >= width) { From 9db8180e051b586ff5c8824b2b958b22c41193b5 Mon Sep 17 00:00:00 2001 From: twang1999 Date: Tue, 10 Nov 2015 14:21:42 -0500 Subject: [PATCH 2/4] adding 30 balls and changed color of balls --- GravityBallCode/GravityBallCode.pde | 55 +++++++++++++++++------------ 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 029f0d9..be9d216 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,17 +1,25 @@ //declare variables -float x, y, velX, velY, diam, grav; +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 []grav=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); - grav=.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 - } - if (y + diam/2 >= height) { - velY = -abs(velY); - } else if (y - diam/2 <= 0) { - velY = abs(velY); + 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]); + } } } \ No newline at end of file From badb7d9b1e154ca258e30d60d1a11404ce0e59fb Mon Sep 17 00:00:00 2001 From: twang1999 Date: Wed, 11 Nov 2015 14:36:10 -0500 Subject: [PATCH 3/4] ensure that balls do not bounce below screen --- GravityBallCode/GravityBallCode.pde | 1 + 1 file changed, 1 insertion(+) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index be9d216..d6c1f2a 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -44,6 +44,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]); } From 3911ba7712ed267bc08be397f4b20a6fae2a774d Mon Sep 17 00:00:00 2001 From: twang1999 Date: Wed, 11 Nov 2015 14:39:59 -0500 Subject: [PATCH 4/4] ensure that ball does not bounce off screen --- GravityBallCode/GravityBallCode.pde | 1 + 1 file changed, 1 insertion(+) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index be9d216..d6c1f2a 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -44,6 +44,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]); }