From 8321dc64b230329ab2443c294e70b100f4270577 Mon Sep 17 00:00:00 2001 From: elugtu Date: Fri, 13 Nov 2015 13:10:36 -0500 Subject: [PATCH 1/7] added vectors PVector loc; PVector vel; //both initialized --- BouncingWithVectors/BouncingWithVectors.pde | 38 ++++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..4c5d8ea 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,20 @@ //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 = new PVector(random(-5, 5), random(-5, 5)); + diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + } void draw() { @@ -18,20 +22,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 caee4958ec162db30404cbb28e2132404454466e Mon Sep 17 00:00:00 2001 From: elugtu Date: Fri, 13 Nov 2015 13:23:40 -0500 Subject: [PATCH 2/7] unit vectors dir random mult 10 --- BouncingWithVectors/BouncingWithVectors.pde | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 4c5d8ea..c66204c 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -11,8 +11,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; } @@ -25,8 +25,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 23b6161fbc36098fbfb2e79c9f9c78f433e56aca Mon Sep 17 00:00:00 2001 From: elugtu Date: Tue, 17 Nov 2015 12:22:31 -0500 Subject: [PATCH 3/7] fixed typos --- Wanderer/Wanderer.pde | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..6c77407 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -25,13 +25,15 @@ 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 ed954bd07efe21be95e019aaf8577425b28d82e6 Mon Sep 17 00:00:00 2001 From: elugtu Date: Tue, 17 Nov 2015 12:28:50 -0500 Subject: [PATCH 4/7] added PVectors --- Wanderer/Wanderer.pde | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 6c77407..b19a916 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; +PVector acc; 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,22 +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; //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 561c70069864c1b5461a49065286cb41e65f4c19 Mon Sep 17 00:00:00 2001 From: elugtu Date: Tue, 17 Nov 2015 13:03:20 -0500 Subject: [PATCH 5/7] basic wanderer code --- Wanderer/Wanderer.pde | 1 + 1 file changed, 1 insertion(+) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b19a916..5894a52 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -11,6 +11,7 @@ void setup() { //initialize variables loc = new PVector(width/2, height/2); vel = new PVector(random(-5, 5), random(-5, 5)); + acc = new PVector(1, 1); diam = 80; } From 9e5a0316c77e4ef7befb2aad536848dc52b3f04f Mon Sep 17 00:00:00 2001 From: elugtu Date: Tue, 17 Nov 2015 13:13:45 -0500 Subject: [PATCH 6/7] added arrays vectors and arrays baby!!! --- Wanderer/Wanderer.pde | 60 ++++++++++++++++++++++++++++--------------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 5894a52..3d543be 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,41 +1,59 @@ //declare variables -float diam; -PVector loc; -PVector vel; -PVector acc; +int count = 3; +float[] diam = new float[count]; +PVector[] loc = new PVector[count]; +PVector[] vel = new PVector[count]; +PVector[] acc = new PVector[count]; void setup() { //set size of canvas size(800, 600); - + + for(int i = 0; i < count; i++){ //initialize variables - loc = new PVector(width/2, height/2); - vel = new PVector(random(-5, 5), random(-5, 5)); - acc = new PVector(1, 1); - diam = 80; + loc[i] = new PVector(width/2, height/2); + vel[i] = new PVector(0, 0); + acc[i] = PVector.random2D(); + acc[i].mult(.1); + + diam[i] = 80; + + } } void draw() { + for(int i = 0; i < count; i++){ + + acc[i] = PVector.random2D(); + acc[i].mult(.1); + vel[i].limit(1); + //draw background to cover previous frame - background(0); + //background(0); + //draw ball - ellipse(loc.x, loc.y, diam, diam); + ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); + + //add acceleration to velocity + vel[i].add(acc[i]); //add velocity to position - loc.x += vel.x; - loc.y += vel.y; + loc[i].x += vel[i].x; + loc[i].y += vel[i].y; //wrap the ball's position - if (loc.x >= width) { - loc.x = 0; - } else if (loc.x <= 0) { - loc.x = width ; + if (loc[i].x >= width) { + loc[i].x = 0; + } else if (loc[i].x <= 0) { + loc[i].x = width ; } - if (loc.y >= height) { - loc.y = 0; - } else if (loc.y <= 0) { - loc.y = height; + if (loc[i].y >= height) { + loc[i].y = 0; + } else if (loc[i].y <= 0) { + loc[i].y = height; + } + } } \ No newline at end of file From e091b1803409c99a043b1d0c0441927a700b8a6d Mon Sep 17 00:00:00 2001 From: elugtu Date: Tue, 17 Nov 2015 13:19:48 -0500 Subject: [PATCH 7/7] added small changes --- Wanderer/Wanderer.pde | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 3d543be..ed06245 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,5 +1,6 @@ //declare variables -int count = 3; + +int count = 10; float[] diam = new float[count]; PVector[] loc = new PVector[count]; PVector[] vel = new PVector[count]; @@ -19,17 +20,21 @@ void setup() { diam[i] = 80; } + noStroke(); } void draw() { + + //draw background to cover previous frame + background(0); + for(int i = 0; i < count; i++){ acc[i] = PVector.random2D(); acc[i].mult(.1); vel[i].limit(1); - //draw background to cover previous frame - //background(0); + //draw ball