From 99b03aa85b34695f5f45a8f876836501093a4bf2 Mon Sep 17 00:00:00 2001 From: jessgannon13 Date: Thu, 12 Nov 2015 11:49:48 -0500 Subject: [PATCH 1/3] create arrays --- GravityBallCode/GravityBallCode.pde | 61 +++++++++++++++++++---------- 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..f593b9a 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,38 +1,57 @@ //declare variables -float x, y, velX, velY, diam; +int count=450; +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]; 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 < count; i++) { + x[i] = width/2; + y[i] = height/2; + diam[i] = random(50); + velx[i] = random(-5, 5); + velY[i] = random(-5, 5); + } } - void draw() { //draw background to cover previous frame background(0); //draw ball - ellipse(x, y, diam, diam); + 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); + //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[0] 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[0] 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 (x[i] + diam[i] >= width) { + velx[i] = -abs(velx[i]); //if the ball hits the right wall, assign x[0] 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[0] 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 28b4b94f27f2eeacfaf85fffa4aac154c5846ab5 Mon Sep 17 00:00:00 2001 From: jessgannon13 Date: Thu, 12 Nov 2015 13:31:42 -0500 Subject: [PATCH 2/3] changed to 30 balls --- GravityBallCode/GravityBallCode.pde | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index f593b9a..a1f8ecb 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,5 +1,5 @@ //declare variables -int count=450; +int count=30; float [] x = new float [count]; float [] y = new float [count]; @@ -14,7 +14,7 @@ void setup() { for (int i=0; i < count; i++) { x[i] = width/2; y[i] = height/2; - diam[i] = random(50); + diam[i] = 50; velx[i] = random(-5, 5); velY[i] = random(-5, 5); } @@ -25,7 +25,6 @@ void draw() { //draw ball for (int i=0; i Date: Thu, 12 Nov 2015 13:35:30 -0500 Subject: [PATCH 3/3] added gravity variable (acceleration) --- GravityBallCode/GravityBallCode.pde | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index a1f8ecb..a9d2908 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -6,6 +6,7 @@ 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); @@ -14,9 +15,10 @@ void setup() { for (int i=0; i < count; i++) { x[i] = width/2; y[i] = height/2; - diam[i] = 50; + diam[i] = random(50); velx[i] = random(-5, 5); velY[i] = random(-5, 5); + grav[i] = .2; } } void draw() { @@ -25,6 +27,8 @@ void draw() { //draw ball for (int i=0; i