From 491cf59b6943fd754fcd2b45d87986e9f510d794 Mon Sep 17 00:00:00 2001 From: Smore98 Date: Fri, 13 Nov 2015 13:12:55 -0500 Subject: [PATCH 1/4] Created PVectors --- BouncingWithVectors/BouncingWithVectors.pde | 35 +++++++++++---------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..e057879 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,17 @@ //declare variables -float x, y, velX, velY, diam; +float diam; + +PVector loc; //declared loc, replaces x & y +PVector vel; //delcared vel, replaced velx & vel y void setup() { //set size of canvas size(800, 600); //initialize variables - x = width/2; - y = height/2; + loc = new PVector(width/2, height/2); + vel = new PVector(random(-5,5), random(-5,5)); diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); } void draw() { @@ -18,20 +19,22 @@ 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; //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 a562a2675c5c9de5c438cb25b2ddb1a919bf59b6 Mon Sep 17 00:00:00 2001 From: Smore98 Date: Fri, 13 Nov 2015 13:22:55 -0500 Subject: [PATCH 2/4] Altered Velocity --- BouncingWithVectors/BouncingWithVectors.pde | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index e057879..46e421c 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -10,7 +10,8 @@ void setup() { //initialize variables loc = new PVector(width/2, height/2); - vel = new PVector(random(-5,5), random(-5,5)); + vel = PVector.random2D(); + vel.mult(10); diam = 80; } @@ -22,8 +23,7 @@ void draw() { ellipse(loc.x, loc.y, diam, diam); //add velocity to position - loc.x += vel.x; - loc.y += vel.y; + loc.add(vel); //bounce ball if it hits walls if (loc.x + diam/2 >= width) { From b090ff6a8c98672d21534d9f586f0f91e1a6423f Mon Sep 17 00:00:00 2001 From: Smore98 Date: Tue, 17 Nov 2015 12:29:11 -0500 Subject: [PATCH 3/4] Added PVectors --- Wanderer/Wanderer.pde | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..9ee74a5 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,16 +1,17 @@ //declare variables -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; + loc = new PVector(width/2, height/2); + vel = PVector.random2D(); diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); } void draw() { @@ -18,20 +19,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.add(vel); + //wrap the ball's position - if (x + diam/2 >= width) { - x = -diam/2; - } else if (x - diam/2 <= 0) { - x = width + diam/2; + if (loc.x >= width) { + loc.x = 0; + } else if (loc.x <= 0) { + loc.x = width; } - if (y + diam/2 >= height) { - y = -diam/2; - } else if (y - diam/2 <= 0) { - y = height + diam/2; + if (loc.y >= height) { + loc.y = 0; + } else if (loc.y <= 0) { + loc.y = height; } +} \ No newline at end of file From 066149394123ec78c358f91a6e178fe372fda1a2 Mon Sep 17 00:00:00 2001 From: Smore98 Date: Tue, 17 Nov 2015 13:23:48 -0500 Subject: [PATCH 4/4] Added Wandering Ball and Extras --- Wanderer/Wanderer.pde | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 9ee74a5..8f49b5b 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,8 +1,9 @@ //declare variables float diam; -PVector loc; -PVector vel; +PVector loc; //declared loc +PVector vel; //declared vel +PVector acc; //declared acc; acceleration void setup() { //set size of canvas @@ -11,18 +12,27 @@ void setup() { //initialize variables loc = new PVector(width/2, height/2); vel = PVector.random2D(); + vel.mult(0); diam = 80; } void draw() { //draw background to cover previous frame + acc = PVector.random2D(); //initialize acceleration + acc.mult(.5); //gradually increase velocity with acceleration background(0); //draw ball ellipse(loc.x, loc.y, diam, diam); + + //add acceleration to velocity + vel.add(acc); //add velocity to position loc.add(vel); + + //Limit velocity + vel.limit(5); //wrap the ball's position @@ -36,4 +46,17 @@ void draw() { } else if (loc.y <= 0) { loc.y = height; } + if(loc.x >= width/2);{ + fill(0,0,255); //blue on right side + } + if(loc.x <= width/2){ + fill(255,0,0); //red on left side + } + if(loc.y <= height/2){ + text("Up is Yup", width/2, 450); + } + if(loc.y >= height/2){ + text("Down is Bound", width/2, 150); + } + } \ No newline at end of file