From d3a3aab782c2a2309efff93cedd341d7dd7b51b5 Mon Sep 17 00:00:00 2001 From: SMoy17 Date: Mon, 16 Nov 2015 11:24:31 -0500 Subject: [PATCH 1/4] Add Vectors to Ball --- BouncingWithVectors/BouncingWithVectors.pde | 38 +++++++++++++-------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..9ef0744 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,24 @@ //declare variables float x, y, velX, velY, diam; +//float x,y +PVector loc; //replaces x and y + +//float velX, velY +PVector vel; + + + 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); + loc = new PVector (width/2, height/2); + vel = PVector.random2D(); + vel.mult(10); + } void draw() { @@ -18,20 +26,20 @@ void draw() { background(0); //draw ball - ellipse(x, y, diam, diam); + ellipse(loc.x, loc.y, diam, diam); //add velocity to position - x += velX; - y += velY; + loc.add(vel); //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 (loc.x + diam/2 >= width) { + vel.x = -abs(vel.x); //if the ball hits the right wall, assign x velocity the negative version of itself + } else if (loc.x - diam/2 <= 0) { + vel.x = abs(vel.x); //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 (loc.y + diam/2 >= height) { + vel.y = -abs(vel.y); + } else if (loc.y - diam/2 <= 0) { + vel.y = abs(vel.y); } +} \ No newline at end of file From 7ba5125c091cc5143404311cbcf7ad2104b1e766 Mon Sep 17 00:00:00 2001 From: SMoy17 Date: Mon, 16 Nov 2015 14:16:55 -0500 Subject: [PATCH 2/4] Added Arrays --- BouncingWithVectors/BouncingWithVectors.pde | 46 ++++++++++----------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 9ef0744..7eee7e7 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,45 +1,41 @@ //declare variables -float x, y, velX, velY, diam; - -//float x,y -PVector loc; //replaces x and y - -//float velX, velY -PVector vel; - +int count=100; +float[] diam=new float[count]; +PVector[] loc=new PVector[count]; +PVector[] vel=new PVector[count]; void setup() { //set size of canvas size(800, 600); - +for(int i = 0; i < count; i++) { //initialize variables - diam = 80; - loc = new PVector (width/2, height/2); - vel = PVector.random2D(); - vel.mult(10); - + diam[i] = 80; + loc[i] = new PVector (width/2, height/2); + vel[i] = PVector.random2D(); +} } void draw() { //draw background to cover previous frame background(0); - +for(int i = 0; i < count; i++) { //draw ball - ellipse(loc.x, loc.y, diam, diam); + ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); //add velocity to position - loc.add(vel); + loc[i].add(vel[i]); //bounce ball if it hits walls - if (loc.x + diam/2 >= width) { - vel.x = -abs(vel.x); //if the ball hits the right wall, assign x velocity the negative version of itself - } else if (loc.x - diam/2 <= 0) { - vel.x = abs(vel.x); //if the ball hits the left wall, assign x velocity the positive version of itself + if (loc[i].x + diam[i]/2 >= width) { + vel[i].x = -abs(vel[i].x); //if the ball hits the right wall, assign x velocity the negative version of itself + } else if (loc[i].x - diam[i]/2 <= 0) { + vel[i].x = abs(vel[i].x); //if the ball hits the left wall, assign x velocity the positive version of itself } - if (loc.y + diam/2 >= height) { - vel.y = -abs(vel.y); - } else if (loc.y - diam/2 <= 0) { - vel.y = abs(vel.y); + if (loc[i].y + diam[i]/2 >= height) { + vel[i].y = -abs(vel[i].y); + } else if (loc[i].y - diam[i]/2 <= 0) { + vel[i].y = abs(vel[i].y); } +} } \ No newline at end of file From 788f1d8da43d0e15a7456ca3db3e196e3776634b Mon Sep 17 00:00:00 2001 From: SMoy17 Date: Wed, 18 Nov 2015 13:44:05 -0500 Subject: [PATCH 3/4] Fix Wanderer --- BouncingWithVectors/BouncingWithVectors.pde | 4 +++- Wanderer/Wanderer.pde | 17 +++++++++-------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 7eee7e7..465859b 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -10,9 +10,10 @@ void setup() { size(800, 600); for(int i = 0; i < count; i++) { //initialize variables - diam[i] = 80; + diam[i] = 60; loc[i] = new PVector (width/2, height/2); vel[i] = PVector.random2D(); + vel[i].mult(5); } } @@ -21,6 +22,7 @@ void draw() { background(0); for(int i = 0; i < count; i++) { //draw ball + fill(random(255),random(255),random(255)); ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); //add velocity to position diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..d83fae2 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -25,13 +25,14 @@ void draw() { y += velY; //wrap the ball's position - if (x + diam/2 >= width) { - x = -diam/2; - } else if (x - diam/2 <= 0) { - x = width + diam/2; + if (x >= width) { + x = 0; + } else if (x <= 0) { + x = width ; } - if (y + diam/2 >= height) { - y = -diam/2; - } else if (y - diam/2 <= 0) { - y = height + diam/2; + if (y >= height) { + y = 0; + } else if (y <= 0) { + y = height ; } +} \ No newline at end of file From 99a9dabc10be38e97c93e092f514fe4eb09db7f0 Mon Sep 17 00:00:00 2001 From: SMoy17 Date: Fri, 20 Nov 2015 13:30:36 -0500 Subject: [PATCH 4/4] Final --- Wanderer/Wanderer.pde | 49 ++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index d83fae2..2f1c873 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,38 +1,49 @@ //declare variables -float x, y, velX, velY, diam; +float diam; +PVector loc, vel, acc; void setup() { + background(100, 40, 30); //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); + + diam = 50; + loc = new PVector (width/2, height/2); + vel = PVector.random2D(); + vel.mult(.1); + acc = PVector.random2D(); + acc.mult(.01); } void draw() { - //draw background to cover previous frame - background(0); + //random acceleration + acc = PVector.random2D(); + acc.mult(.1); //draw ball - ellipse(x, y, diam, diam); + ellipse(loc.x, loc.y, diam, diam); //add velocity to position - x += velX; - y += velY; + loc.add(vel); + + //add acceleration to velocity + vel.add(acc); + + //limit velocity + vel.limit(5); + //wrap the ball's position - if (x >= width) { - x = 0; - } else if (x <= 0) { - x = width ; + if (loc.x >= width) { + loc.x = 0; + } else if (loc.x <= 0) { + loc.x = width ; } - if (y >= height) { - y = 0; - } else if (y <= 0) { - y = height ; + if (loc.y >= height) { + loc.y = 0; + } else if (loc.y <= 0) { + loc.y = height ; } } \ No newline at end of file