From 9f26d84889b85bcf5396f9935da914e068d0f9d8 Mon Sep 17 00:00:00 2001 From: twang1999 Date: Thu, 12 Nov 2015 14:38:11 -0500 Subject: [PATCH 1/7] added vectors for bouncing ball --- BouncingWithVectors/BouncingWithVectors.pde | 39 ++++++++++++--------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..e741c89 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,22 @@ //declare variables -float x, y, velX, velY, diam; +float diam; +//float x,y +PVector loc; //replaces x and y +//float velX,velY +PVector vel = new PVector(2,2); //replaces velX and velY void setup() { //set size of canvas size(800, 600); //initialize variables - x = width/2; - y = height/2; + //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); + //velX = random(-5, 5); + //velY = random(-5, 5); } void draw() { @@ -18,20 +24,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 cfabc990d077dcbb4bd58bc21e2caa84e5fdc6b5 Mon Sep 17 00:00:00 2001 From: twang1999 Date: Thu, 12 Nov 2015 14:51:33 -0500 Subject: [PATCH 2/7] vel.add --- BouncingWithVectors/BouncingWithVectors.pde | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index e741c89..4ad89e4 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -13,7 +13,8 @@ void setup() { //x = width/2; //y = height/2; loc = new PVector(width/2,height/2); - vel = new PVector(random(-5,5),random(-5,5)); + vel = PVector.random2D(); + vel.mult(5); diam = 80; //velX = random(-5, 5); //velY = random(-5, 5); @@ -27,8 +28,9 @@ void draw() { 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) { From 40cfdb54497aed58169f559d25a6fa1e2a31d53e Mon Sep 17 00:00:00 2001 From: twang1999 Date: Mon, 16 Nov 2015 14:13:03 -0500 Subject: [PATCH 3/7] added arrays --- BouncingWithVectors/BouncingWithVectors.pde | 62 +++++++++++---------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 4ad89e4..116354e 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,46 +1,50 @@ //declare variables -float diam; +int count = 15; +float [] diam = new float [count]; //float x,y -PVector loc; //replaces x and y +PVector [] loc = new PVector [count]; //replaces x and y //float velX,velY -PVector vel = new PVector(2,2); //replaces velX and velY +PVector [] vel = new PVector [count]; //replaces velX and velY 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(); - vel.mult(5); - diam = 80; - //velX = random(-5, 5); - //velY = random(-5, 5); + for (int i=0; i= 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 (loc.y + diam/2 >= height) { - vel.y = -abs(vel.y); - } else if (loc.y - diam/2 <= 0) { - vel.y = abs(vel.y); + //bounce ball if it hits walls + if (loc[i].x + diam[i]/2 >= width) { + vel[i].x = -abs(vel[i].x); //if the ball hits the right wall, assign x velocity the negative version of itself + } else if (loc[i].x - diam[i]/2 <= 0) { + vel[i].x = abs(vel[i].x); //if the ball hits the left wall, assign x velocity the positive version of itself + } + if (loc[i].y + diam[i]/2 >= height) { + vel[i].y = -abs(vel[i].y); + } else if (loc[i].y - diam[i]/2 <= 0) { + vel[i].y = abs(vel[i].y); + } } } \ No newline at end of file From 116c9711c48ba3f7c5b960ffb5c095bd222951cc Mon Sep 17 00:00:00 2001 From: twang1999 Date: Wed, 18 Nov 2015 13:43:46 -0500 Subject: [PATCH 4/7] fixed basic code --- Wanderer/Wanderer.pde | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..9e50362 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 9fbf0bcbd3300497e112890bf521d1ee62950a5f Mon Sep 17 00:00:00 2001 From: twang1999 Date: Wed, 18 Nov 2015 13:50:50 -0500 Subject: [PATCH 5/7] added vectors --- Wanderer/Wanderer.pde | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 9e50362..3824757 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,16 +1,18 @@ //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; + //x = width/2; + //y = height/2; diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + loc = new PVector(width/2,height/2); + vel = new PVector(random (-7,7),random(-7,7)); } void draw() { @@ -18,21 +20,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 f4bd43aea436f766a880e4c08115d2c187bfa2e6 Mon Sep 17 00:00:00 2001 From: twang1999 Date: Wed, 18 Nov 2015 14:47:49 -0500 Subject: [PATCH 6/7] added acceleration & color change --- Wanderer/Wanderer.pde | 50 ++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..f8aa9cf 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,37 +1,47 @@ //declare variables -float x, y, velX, velY, diam; +float diam; +PVector loc, vel, acc; void setup() { //set size of canvas size(800, 600); - + noStroke(); //initialize variables - x = width/2; - y = height/2; + //x = width/2; + //y = height/2; diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + loc = new PVector(width/2,height/2); + vel = PVector.random2D(); + vel.mult(1); + acc = PVector.random2D(); + acc.mult(.02); } void draw() { //draw background to cover previous frame - background(0); - + acc = PVector.random2D(); + acc.mult(.1); + fill(random(255),random(255),random(255)); //draw ball - ellipse(x, y, diam, diam); - + ellipse(loc.x, loc.y, diam, diam); +// add acceleration to velocity + vel.add(acc); + vel.limit(20); //add velocity to position - x += velX; - y += velY; + loc.add(vel); + //loc.x += vel.x; + //loc.y += vel.y; + //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 187900e08fc771ee6893ef731ecd46d22747a5e0 Mon Sep 17 00:00:00 2001 From: twang1999 Date: Wed, 18 Nov 2015 14:48:40 -0500 Subject: [PATCH 7/7] added acceleration --- Wanderer/Wanderer.pde | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 3824757..f8aa9cf 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,30 +1,37 @@ //declare variables float diam; -PVector loc; -PVector vel; +PVector loc, vel, acc; void setup() { //set size of canvas size(800, 600); - + noStroke(); //initialize variables //x = width/2; //y = height/2; diam = 80; loc = new PVector(width/2,height/2); - vel = new PVector(random (-7,7),random(-7,7)); + vel = PVector.random2D(); + vel.mult(1); + acc = PVector.random2D(); + acc.mult(.02); } void draw() { //draw background to cover previous frame - background(0); - + acc = PVector.random2D(); + acc.mult(.1); + fill(random(255),random(255),random(255)); //draw ball ellipse(loc.x, loc.y, diam, diam); - +// add acceleration to velocity + vel.add(acc); + vel.limit(20); //add velocity to position - loc.x += vel.x; - loc.y += vel.y; + loc.add(vel); + //loc.x += vel.x; + //loc.y += vel.y; + //wrap the ball's position if (loc.x >= width) {