From 8d117a08d25e041567aa86b2b72b77c0a01eca25 Mon Sep 17 00:00:00 2001 From: smakkar Date: Tue, 10 Nov 2015 14:09:20 -0500 Subject: [PATCH 1/3] Added Gravity --- GravityBallCode/GravityBallCode.pde | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..a35b8e0 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,16 +1,17 @@ //declare variables float x, y, velX, velY, diam; +float g; void setup() { //set size of canvas size(800, 600); //initialize variables + g=0.1; x = width/2; y = height/2; diam = 80; velX = random(-5, 5); - velY = random(-5, 5); } void draw() { @@ -23,6 +24,7 @@ void draw() { //add velocity to position x += velX; y += velY; + velY = velY+g; //bounce ball if it hits walls if (x + diam/2 >= width) { From bd11d3e576acd1b9988f39a018e7282ea68472ad Mon Sep 17 00:00:00 2001 From: smakkar Date: Tue, 10 Nov 2015 14:45:20 -0500 Subject: [PATCH 2/3] 30 Balls with Gravity --- GravityBallCode/GravityBallCode.pde | 59 +++++++++++++++++------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index a35b8e0..cebebf0 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,40 +1,49 @@ //declare variables -float x, y, velX, velY, diam; -float g; +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[] g= new float[count]; + void setup() { //set size of canvas size(800, 600); - - //initialize variables - g=0.1; - x = width/2; - y = height/2; - diam = 80; - velX = random(-5, 5); + for (int i =0; i < count; i++) { + //initialize variables + g[i] =0.1; + x[i] = width/2; + y[i] = height/2; + diam[i] = 80; + velX[i] = random(-5, 5); + } } void draw() { //draw background to cover previous frame background(0); + for (int i = 0; i < count; i++) { + velY[i] = velY[i] +g[i] ; + //draw ball + ellipse(x[i], y[i], diam[i], diam[i] ); - //draw ball - ellipse(x, y, diam, diam); + //add velocity to position + x[i] += velX[i] ; + y[i] += velY[i] ; - //add velocity to position - x += velX; - y += velY; - velY = velY+g; - //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); + //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] ); + } } } \ No newline at end of file From 6b24627b67b1f59910a36248874bd671642dd873 Mon Sep 17 00:00:00 2001 From: smakkar Date: Wed, 11 Nov 2015 14:05:59 -0500 Subject: [PATCH 3/3] Fixed Code up a little --- GravityBallCode/GravityBallCode.pde | 1 + 1 file changed, 1 insertion(+) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index cebebf0..91bc085 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -42,6 +42,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] ); }