From f251a9c485ca28c18eca522d2c88f520181d00ca Mon Sep 17 00:00:00 2001 From: sirfrankster Date: Thu, 12 Nov 2015 14:46:01 -0500 Subject: [PATCH 1/6] Adding loc.x and loc.y --- BouncingWithVectors/BouncingWithVectors.pde | 45 ++++++++++++--------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..f9a8f35 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,37 +1,44 @@ //declare variables -float x, y, velX, velY, diam; - +float diam; //declaring variables +PVector loc; //replaces loc.x and loc.y +//float velX, velY +PVector vel = new PVector(2,2); void setup() { //set size of canvas size(800, 600); - //initialize variables - x = width/2; - y = height/2; + //initialize the variables + //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); + //velx = random(-5,5); + //vely = random(-5,5); + vel = new PVector(random(-5,5), random(-5,5)); + } void draw() { //draw background to cover previous frame 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.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 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 loc.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 bf2df7801c0c03b294a7b580c7e97ce6fbde91b8 Mon Sep 17 00:00:00 2001 From: sirfrankster Date: Mon, 16 Nov 2015 14:50:00 -0500 Subject: [PATCH 2/6] Faster ball with random 2-D --- BouncingWithVectors/BouncingWithVectors.pde | 41 ++++++++++++--------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index f9a8f35..87ca1a3 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,40 +1,45 @@ //declare variables -float diam; //declaring variables -PVector loc; //replaces loc.x and loc.y -//float velX, velY -PVector vel = new PVector(2,2); +float diam; + +//float loc.x,loc.y +PVector loc; //replaces loc.x and loc.y + +//float vel.loc.x, vel.loc.y +PVector vel; //replces vel.loc.x and vel.loc.y + + void setup() { //set size of canvas size(800, 600); - //initialize the variables - //loc.x = width/2 - //loc.y = height/2 - loc = new PVector(width/2,height/2); - + //initialize variables + // 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 = new PVector(random(-5,5), random(-5,5)); - + //vel.loc.x = random(-5, 5); + //vel.loc.y = random(-5, 5); + vel = PVector.random2D(); + vel.mult(70); } void draw() { //draw background to cover previous frame background(0); - + //draw ball ellipse(loc.x, loc.y, diam, diam); //add velocity to position - loc.x += vel.x; - loc.y += vel.y; + //loc.x += vel.x; + //loc.y += vel.y; + loc.add(vel); //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 velocity the negative version of itself + 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 velocity the positive version of itself + vel.x = abs(vel.x); //if the ball hits the left wall, assign loc.x velocitloc.y the positive version of itself } if (loc.y + diam/2 >= height) { vel.y = -abs(vel.y); From c4b3a0b65caeb6ad24cece938afd28f8142d49a8 Mon Sep 17 00:00:00 2001 From: sirfrankster Date: Wed, 18 Nov 2015 13:49:45 -0500 Subject: [PATCH 3/6] Fixed Wanderer statements, ball moves in random directions --- Wanderer/Wanderer.pde | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..1e241d4 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 f3a9d6414256b3990949cdb5a84d3f50170d3542 Mon Sep 17 00:00:00 2001 From: sirfrankster Date: Wed, 18 Nov 2015 14:01:39 -0500 Subject: [PATCH 4/6] Array W/ vectors --- BouncingWithVectors/BouncingWithVectors.pde | 37 +++++++++++---------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 87ca1a3..f619a74 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,49 +1,50 @@ //declare variables float diam; - +int count = 30; //float loc.x,loc.y -PVector loc; //replaces loc.x and loc.y +PVector [] loc= new PVector [count]; //replaces loc.x and loc.y //float vel.loc.x, vel.loc.y -PVector vel; //replces vel.loc.x and vel.loc.y +PVector [] vel= new PVector [count]; //replces vel.loc.x and vel.loc.y void setup() { //set size of canvas size(800, 600); - + for (int i = 0; i < count; i++) { //initialize variables // loc.x = width/2; //loc.y = height/2; - loc= new PVector(width/2, height/2); + loc[i]= new PVector(width/2, height/2); diam = 80; //vel.loc.x = random(-5, 5); //vel.loc.y = random(-5, 5); - vel = PVector.random2D(); - vel.mult(70); + 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, diam); //add velocity to position //loc.x += vel.x; //loc.y += vel.y; - 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 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/2 >= width) { + vel[i].x = -abs(vel[i].x); //if the ball hits the right wall, assign loc.x velocitloc.y the negative version of itself + } else if (loc[i].x - diam/2 <= 0) { + vel[i].x = abs(vel[i].x); //if the ball hits the left wall, assign loc.x velocitloc.y 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/2 >= height) { + vel[i].y = -abs(vel[i].y); + } else if (loc[i].y - diam/2 <= 0) { + vel[i].y = abs(vel[i].y); } +} } \ No newline at end of file From a1e5ab0c31dd53a64326f66fbf6b85bc64a85f99 Mon Sep 17 00:00:00 2001 From: sirfrankster Date: Wed, 18 Nov 2015 14:12:21 -0500 Subject: [PATCH 5/6] Wandering ball --- Wanderer/Wanderer.pde | 44 ++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 1e241d4..222b070 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,16 +1,20 @@ //declare variables -float x, y, velX, velY, diam; +float diam; //float loc. x,y +PVector loc; //replaces loc.x and loc.y +PVector vel = new PVector(2, 2); void setup() { //set size of canvas size(800, 600); //initialize variables - x = width/2; - y = height/2; + loc= new PVector(width/2, 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); + vel = PVector.random2D(); } void draw() { @@ -18,21 +22,23 @@ 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.x; + //loc.y += vel.y; + loc.add(vel); - //wrap the ball's position - if (x>= width) { - x = 0; - } else if (x<= 0) { - x = width; - } - if (y>= height) { - y = 0; - } else if (y <= 0) { - y = height; - } +if (loc.x >= width) { //allows ball to wander in the x direction + loc.x = 0; +} +else if (loc.x <= 0) { //allows ball to wander in the x direction + loc.x = width; +} +if (loc.y >=height) { //allows ball to wander in the y direction + loc.y = 0; +} +else if (loc.y <= 0) { //allows ball to wander in the y direction + loc.y = height; +} } \ No newline at end of file From c05b7f96e16500a4b230c37387a53caaf437bf71 Mon Sep 17 00:00:00 2001 From: sirfrankster Date: Wed, 18 Nov 2015 14:49:49 -0500 Subject: [PATCH 6/6] Adding vel.limit, vel.add, vel.mult, acc.mult Wanderer --- Wanderer/Wanderer.pde | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 222b070..c7a74e5 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,44 +1,48 @@ //declare variables float diam; //float loc. x,y -PVector loc; //replaces loc.x and loc.y +PVector loc, acc, vel; //replaces loc.x and loc.y -PVector vel = new PVector(2, 2); void setup() { //set size of canvas size(800, 600); //initialize variables - loc= new PVector(width/2, height/2); + loc = new PVector(width/2, height/2); + + //x = width/2; // y = height/2; diam = 80; //velX = random(-5, 5); //velY = random(-5, 5); - vel = PVector.random2D(); + vel = new PVector(4, 7); + vel.mult(1); + acc = PVector.random2D(); + acc.mult(.1); } void draw() { //draw background to cover previous frame - background(0); + background(0, 135, 220); //draw ball + fill(0); ellipse(loc.x, loc.y, diam, diam); - + vel.add(acc); + vel.limit(2); //add velocity to position //loc.x += vel.x; //loc.y += vel.y; loc.add(vel); -if (loc.x >= width) { //allows ball to wander in the x direction + if (loc.x >= width) { //allows ball to wander in the x direction loc.x = 0; -} -else if (loc.x <= 0) { //allows ball to wander in the x direction + } else if (loc.x <= 0) { //allows ball to wander in the x direction loc.x = width; -} -if (loc.y >=height) { //allows ball to wander in the y direction - loc.y = 0; -} -else if (loc.y <= 0) { //allows ball to wander in the y direction - loc.y = height; -} + } + if (loc.y >=height) { //allows ball to wander in the y direction + loc.y = 0; + } else if (loc.y <= 0) { //allows ball to wander in the y direction + loc.y = height; + } } \ No newline at end of file