From 380849af5bfc657eee91dfb469bbe85a3bf42c59 Mon Sep 17 00:00:00 2001 From: SSJcbondalo Date: Tue, 10 Nov 2015 14:26:48 -0500 Subject: [PATCH 1/4] giving gravity to the ball --- GravityBallCode/GravityBallCode.pde | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index df2493b..512c314 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,5 +1,6 @@ //declare variables float x, y, velX, velY, diam; +float earthg; void setup() { //set size of canvas @@ -11,18 +12,23 @@ void setup() { diam = 80; velX = random(-5, 5); velY = random(-5, 5); + earthg = 0.1; } void draw() { //draw background to cover previous frame background(0); - + fill(157, 243, 255); //draw ball ellipse(x, y, diam, diam); - + fill(255); + textSize(50); + text("LEND ME YOUR ENERGY!!!", width/6, height/6); + text("~Son Goku", width/6, height/3.5); //add velocity to position x += velX; y += velY; + velY = velY += earthg; //bounce ball if it hits walls if (x + diam/2 >= width) { From c2df2c2b992a36375e40a867502609ed73436348 Mon Sep 17 00:00:00 2001 From: SSJcbondalo Date: Tue, 10 Nov 2015 14:43:43 -0500 Subject: [PATCH 2/4] adding array of gravitational balls --- GravityBallCode/GravityBallCode.pde | 59 +++++++++++++++++------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 512c314..3c61699 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -1,44 +1,53 @@ //declare variables -float x, y, velX, velY, diam; -float earthg; +int count = 35; +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 = 0.1; + for (int i = 0; i < count; i++) { + x[i] = width/2; + y[i] = height/2; + diam[i] = 80; + velX[i] = random(-5, 5); + velY[i] = random(-5, 5); + earthg[i] = 0.1; + } } void draw() { //draw background to cover previous frame background(0); - fill(157, 243, 255); - //draw ball - ellipse(x, y, diam, diam); fill(255); textSize(50); text("LEND ME YOUR ENERGY!!!", width/6, height/6); text("~Son Goku", width/6, height/3.5); - //add velocity to position - x += velX; - y += velY; - velY = velY += earthg; + for (int i = 0; i < count; i++) { + fill(157, 243, 255); + //draw ball + ellipse(x[i], y[i], diam[i], diam[i]); + //add velocity to position + x[i] += velX[i]; + y[i] += velY[i]; + velY[i] = velY[i] + earthg[i]; - //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 928f2766415ede068e135fcecdc399a203da311e Mon Sep 17 00:00:00 2001 From: SSJcbondalo Date: Wed, 11 Nov 2015 14:05:37 -0500 Subject: [PATCH 3/4] more actions when mouse and key pressed --- GravityBallCode/GravityBallCode.pde | 79 +++++++++++++++++++++-------- 1 file changed, 57 insertions(+), 22 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 3c61699..7cd037d 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -24,30 +24,65 @@ void setup() { void draw() { //draw background to cover previous frame - background(0); - fill(255); - textSize(50); - text("LEND ME YOUR ENERGY!!!", width/6, height/6); - text("~Son Goku", width/6, height/3.5); - for (int i = 0; i < count; i++) { - fill(157, 243, 255); - //draw ball - ellipse(x[i], y[i], diam[i], diam[i]); - //add velocity to position - x[i] += velX[i]; - y[i] += velY[i]; - velY[i] = velY[i] + earthg[i]; + if (!mousePressed) { + background(0); + fill(255); + textSize(50); + text("LEND ME YOUR ENERGY!!!", width/6, height/6); + text("~Son Goku", width/6, height/3.5); + for (int i = 0; i < count; i++) { + fill(157, 243, 255); + //draw ball + ellipse(x[i], y[i], diam[i], diam[i]); + //add velocity to position + x[i] += velX[i]; + y[i] += velY[i]; + velY[i] = velY[i] + earthg[i]; - //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 + //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 (mousePressed) { + int i = 0; + i++; + velX[i] = 0; + velY[i] = 0; } - 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 (keyPressed) { + for (int i = 0; i < count; i++) { + fill(255); + textSize(50); + text("LEND ME YOUR ENERGY!!!", width/6, height/6); + text("~Son Goku", width/6, height/3.5); + fill(157, 243, 255); + //draw ball + ellipse(x[i], y[i], diam[i], diam[i]); + //add velocity to position + x[i] += velX[i]; + y[i] += velY[i]; + velY[i] = velY[i] + earthg[i]; + + //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 0e6190ce6459c24781b903fa1d56f6c0c662f54c Mon Sep 17 00:00:00 2001 From: SSJcbondalo Date: Thu, 12 Nov 2015 13:44:51 -0500 Subject: [PATCH 4/4] coding comments --- GravityBallCode/GravityBallCode.pde | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/GravityBallCode/GravityBallCode.pde b/GravityBallCode/GravityBallCode.pde index 7cd037d..ecada97 100644 --- a/GravityBallCode/GravityBallCode.pde +++ b/GravityBallCode/GravityBallCode.pde @@ -24,11 +24,11 @@ void setup() { void draw() { //draw background to cover previous frame - if (!mousePressed) { + if (!mousePressed) { //if mouse is not Pressed background(0); fill(255); textSize(50); - text("LEND ME YOUR ENERGY!!!", width/6, height/6); + text("LEND ME YOUR ENERGY!!!", width/6, height/6); //write this text text("~Son Goku", width/6, height/3.5); for (int i = 0; i < count; i++) { fill(157, 243, 255); @@ -51,14 +51,14 @@ void draw() { velY[i] = abs(velY[i]); } } - if (mousePressed) { + if (mousePressed) { //if mouse is pressed, stop all the balls int i = 0; i++; velX[i] = 0; velY[i] = 0; } } - if (keyPressed) { + if (keyPressed) { //if any key is pressed, make the balls move again in the same way. (This only happens if the mouse is Pressed at the same time) for (int i = 0; i < count; i++) { fill(255); textSize(50);