From 68cb1a95ff52e617cfceeab890b6e7c7c3623069 Mon Sep 17 00:00:00 2001 From: rafaelcandamil Date: Thu, 12 Nov 2015 14:38:59 -0500 Subject: [PATCH 1/3] added vectors --- BouncingWithVectors/BouncingWithVectors.pde | 41 ++++++++++++--------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..fe504b5 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,22 @@ //declare variables -float x, y, velX, velY, diam; +float diam; + +PVector loc; //replaces loc.x,loc.y + +PVector vel; // replaces vel.loc.x/loc.y + void setup() { //set size of canvas size(800, 600); //initialize variables - x = width/2; - y = height/2; + // loc.x = width/2; + //loc.y = height/2; + loc = new PVector(width/2,height/2); diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + //vel.loc.y = random(-5, 5); + vel = new PVector(random(-5,5), random(-5,5)); } void draw() { @@ -18,20 +24,21 @@ 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; + //add velocitloc.y to position + loc.x += vel.x; + loc.y += vel.y; //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 loc.x velocitloc.y 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 loc.x velocitloc.y 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 e1cfcc9a847526293ea4deeba18a8d81a58f4cc3 Mon Sep 17 00:00:00 2001 From: rafaelcandamil Date: Wed, 18 Nov 2015 13:44:06 -0500 Subject: [PATCH 2/3] fixed wanderer --- BouncingWithVectors/BouncingWithVectors.pde | 57 +++++++++++++-------- Wanderer/Wanderer.pde | 17 +++--- 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index fe504b5..75e4ac9 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,22 +1,33 @@ //declare variables -float diam; +//float diam [i]; -PVector loc; //replaces loc.x,loc.y - -PVector vel; // replaces vel.loc.x/loc.y +//PVector loc; //replaces loc.x,loc.y [i] +//PVector vel; // replaces vel.loc.x/loc.y [i] +int count = 25; +float [] diam = new float[count]; +float [] y= new float[count]; +float [] loc= new float[count]; +float [] vel= new float[count]; void setup() { //set size of canvas size(800, 600); //initialize variables - // loc.x = width/2; - //loc.y = height/2; - loc = new PVector(width/2,height/2); - diam = 80; - //vel.loc.y = random(-5, 5); - vel = new PVector(random(-5,5), random(-5,5)); + // loc.x = width/2; + // loc.y [i] = height/2; + + +for (int i = 0; i < count; i++) { + loc [i] = new PVector(width/2, height/2); + vel [i] = PVector.random2D(); + diam [i] = random(30,100); + y [i] = height/2; + // vel.loc.x = random(-5, 5); + //vel.loc.y [i] = random(-5, 5); + +} } void draw() { @@ -24,21 +35,23 @@ void draw() { background(0); //draw ball - ellipse(loc.x, loc.y, diam, diam); + ellipse(loc[i].x, loc.y[i], diam [i], diam [i]); - //add velocitloc.y to position - loc.x += vel.x; - loc.y += vel.y; + //add velocitloc.y [i] to position + //loc.x += vel.x; + //loc.y [i] += vel.y [i]; + 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 loc.x velocitloc.y 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 loc.x velocitloc.y 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 loc.x velocitloc.y [i] 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 loc.x velocitloc.y [i] 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[i] + diam [i]/2 >= height) { + vel[i].y [i] = -abs(vel[i].y [i]); + } else if (loc[i].y [i] - diam [i]/2 <= 0) { + vel[i].y [i] = abs(vel[i].y [i]); } } \ No newline at end of file diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..205078c 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 264c4cb2fcef2cf14b0196bd01e54253b95c3d21 Mon Sep 17 00:00:00 2001 From: rafaelcandamil Date: Wed, 18 Nov 2015 14:43:03 -0500 Subject: [PATCH 3/3] added vectors --- Wanderer/Wanderer.pde | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 205078c..9b07c76 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,16 +1,23 @@ //declare variables -float x, y, velX, velY, diam; +//float x, y, velX, velY, diam; +float diam; + +PVector loc; + +PVector vel; void setup() { //set size of canvas size(800, 600); //initialize variables - x = width/2; - y = height/2; + //x = width/2; + //y = height/2; diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + //velX = random(-5, 5); + //velY = random(-5, 5); + loc = new PVector(width/2,height/2); + vel = new PVector(random(-5,5), random(-5,5)); } void draw() { @@ -18,21 +25,21 @@ 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.x += vel.y; + loc.y += vel.x; //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