From ced2249edb3a2c59f457cc19079f7a7fb72818f6 Mon Sep 17 00:00:00 2001 From: JLimoo11 Date: Thu, 12 Nov 2015 14:46:03 -0500 Subject: [PATCH 1/4] replaced variables loc and Pvectors --- BouncingWithVectors/BouncingWithVectors.pde | 42 ++++++++++++--------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..d702cde 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,23 @@ //declare variables -float x, y, velX, velY, diam; - +float diam; +//float loc.x,y +PVector loc; //replaces loc.x and y +//float vel.loc.x, vel.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.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.x = random(-5, 5); + //vel.y = random(-5, 5); + vel = new PVector(random(-5,5), random(-5,5)); + } void draw() { @@ -18,20 +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.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 062d5441213e60f1872b34ccb4fd145b45a989c4 Mon Sep 17 00:00:00 2001 From: JLimoo11 Date: Thu, 12 Nov 2015 14:50:56 -0500 Subject: [PATCH 2/4] Adding vectors --- BouncingWithVectors/BouncingWithVectors.pde | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index d702cde..439405e 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -30,7 +30,7 @@ void draw() { //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) { vel.x = -abs(vel.x); //if the ball hits the right wall, assign x velocity the negative version of itself From c431171c0b31abba807ebcd6a1be81da3322f235 Mon Sep 17 00:00:00 2001 From: JLimoo11 Date: Wed, 18 Nov 2015 13:46:44 -0500 Subject: [PATCH 3/4] ball goes through one side and ends up on the other --- Wanderer/Wanderer.pde | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..25b2c38 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 90f4426dd38e830107b2102f1f8aef7fdbc369bb Mon Sep 17 00:00:00 2001 From: JLimoo11 Date: Wed, 18 Nov 2015 14:50:24 -0500 Subject: [PATCH 4/4] Fixed has limits --- Wanderer/Wanderer.pde | 44 +++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 25b2c38..bc5d83d 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,16 +1,23 @@ //declare variables -float x, y, velX, velY, diam; +float diam; //float loc.x,y +PVector loc, acc, vel; //replaces loc.x and y +//float vel.loc.x,vely.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); + diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + //velX = random(-5, 5); + //velY = random(-5, 5); + vel = PVector.random2D(); + vel.mult(1); + acc = PVector.random2D(); + acc.mult(.1); } void draw() { @@ -18,21 +25,22 @@ void draw() { background(0); //draw ball - ellipse(x, y, diam, diam); - + ellipse(loc.x, loc.y, diam, diam); + vel.add(acc); + vel.limit(5); //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 (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