From 75a03e6fd9b89b16994fe9a2ff22f509f1b154fb Mon Sep 17 00:00:00 2001 From: smakkar Date: Thu, 12 Nov 2015 14:39:50 -0500 Subject: [PATCH 1/7] Added vel and loc vectors --- BouncingWithVectors/BouncingWithVectors.pde | 44 +++++++++++++-------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..bc4b01e 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,25 @@ //declare variables -float x, y, velX, velY, diam; +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 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.x = random(-5, 5); + //vel.loc.y = random(-5, 5); + vel = new PVector (random(-5, 5), random (-5, 5)); } void draw() { @@ -18,20 +27,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 fd1a6c1128275238be097245ce1a1cf398a089ad Mon Sep 17 00:00:00 2001 From: smakkar Date: Thu, 12 Nov 2015 14:51:34 -0500 Subject: [PATCH 2/7] Vector Add --- BouncingWithVectors/BouncingWithVectors.pde | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index bc4b01e..806e3a0 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -19,7 +19,7 @@ void setup() { diam = 80; //vel.loc.x = random(-5, 5); //vel.loc.y = random(-5, 5); - vel = new PVector (random(-5, 5), random (-5, 5)); + vel = PVector.random2D(); } void draw() { @@ -29,9 +29,10 @@ void draw() { //draw ball ellipse(loc.x, loc.y, diam, diam); - //add velocitloc.y to position - loc.x += vel.x; - loc.y += vel.y; + //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 5555575fac613bef0d87af4a8e55d00cf63771d9 Mon Sep 17 00:00:00 2001 From: smakkar Date: Thu, 12 Nov 2015 14:52:14 -0500 Subject: [PATCH 3/7] Vector Add again --- BouncingWithVectors/BouncingWithVectors.pde | 1 + 1 file changed, 1 insertion(+) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 806e3a0..23bd8d7 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -20,6 +20,7 @@ void setup() { //vel.loc.x = random(-5, 5); //vel.loc.y = random(-5, 5); vel = PVector.random2D(); + vel.mult(5); } void draw() { From 1f16b290613af1a76b86b26a661b1ece14943533 Mon Sep 17 00:00:00 2001 From: smakkar Date: Mon, 16 Nov 2015 14:48:33 -0500 Subject: [PATCH 4/7] Added More Vectors w/Arrays --- BouncingWithVectors/BouncingWithVectors.pde | 64 +++++++++++---------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 23bd8d7..6f2d203 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,49 +1,51 @@ //declare variables +int count=20; float diam; //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); - - //initialize variables - // loc.x = width/2; - //loc.y = height/2; - loc= 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(5); + for (int i=0; i= 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.y + diam/2 >= height) { - vel.y = -abs(vel.y); - } else if (loc.y - diam/2 <= 0) { - vel.y = abs(vel.y); + for (int i = 0; i < count; i++) { + //draw ball + ellipse(loc[i].x, loc[i].y, diam, diam); + + //add velocity to position + //loc[i].x += vel[i].x; + //loc[i].y += vel[i].y; + loc[i].add(vel[i]); + + //bounce ball if it hits walls + 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[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 0959f8714451fed0aa6ff1a6d30dd488b53f1bce Mon Sep 17 00:00:00 2001 From: smakkar Date: Wed, 18 Nov 2015 13:44:36 -0500 Subject: [PATCH 5/7] Fixed the Code --- Wanderer/Wanderer.pde | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..37cc67f 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 1399f6e71b43b0250116d518b9f526ed6d45dca4 Mon Sep 17 00:00:00 2001 From: smakkar Date: Wed, 18 Nov 2015 14:05:46 -0500 Subject: [PATCH 6/7] Added Vectors --- Wanderer/Wanderer.pde | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 37cc67f..2862832 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,16 +1,21 @@ //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); - + loc=new PVector(width/2, height/2); //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); + vel = PVector.random2D(); } void draw() { @@ -18,21 +23,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; //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 From a990f07b4a31cca72e5ea861b48750bf8c1dc279 Mon Sep 17 00:00:00 2001 From: smakkar Date: Wed, 18 Nov 2015 14:50:40 -0500 Subject: [PATCH 7/7] Added Limit and Different Directions and Colors --- Wanderer/Wanderer.pde | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 2862832..033529d 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,11 +1,12 @@ //declare variables - -//float x, y, velX, velY, diam; float diam; PVector loc; PVector vel; +PVector acc; void setup() { + colorMode(HSB, 360, 100, 100, 100); + noStroke(); //set size of canvas size(800, 600); loc=new PVector(width/2, height/2); @@ -13,17 +14,25 @@ void setup() { //x = width/2; //y = height/2; diam = 80; - //velX = random(-5, 5); - //velY = random(-5, 5); + //define velocity vel = PVector.random2D(); + vel.mult(1); + //define acceleration + acc= PVector.random2D(); + acc.mult(.09); } void draw() { //draw background to cover previous frame - background(0); + background(255); //draw ball + fill(frameCount%360, 70, 80); ellipse(loc.x, loc.y, diam, diam); + vel.add(acc); //add acceleration to velocity + vel.limit(30); //limit max velocity of ball + acc= PVector.random2D(); + acc.mult(.09); //add velocity to position loc.x += vel.x;