From a5d6e1063bff1756679c29c73c57f8b1e411eb45 Mon Sep 17 00:00:00 2001 From: penguintyler Date: Thu, 12 Nov 2015 14:43:26 -0500 Subject: [PATCH 1/8] Added vectors --- BouncingWithVectors/BouncingWithVectors.pde | 33 +++++++++++---------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..0814d53 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,16 @@ //declare variables -float x, y, velX, velY, diam; +float diam; +PVector loc; +PVector vel = new PVector(2, 2); 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); + vel = new PVector(random(-5, 5), random(-5, 5)); } void draw() { @@ -18,20 +18,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 7ff496d4efb1ca994aa6cfbfdbe9a00ae13d8424 Mon Sep 17 00:00:00 2001 From: penguintyler Date: Thu, 12 Nov 2015 14:51:13 -0500 Subject: [PATCH 2/8] Lines changed --- BouncingWithVectors/BouncingWithVectors.pde | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 0814d53..447a47d 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -10,7 +10,7 @@ void setup() { //initialize variables loc = new PVector(width/2, height/2); diam = 80; - vel = new PVector(random(-5, 5), random(-5, 5)); + vel = PVector.random2D(); } void draw() { @@ -21,8 +21,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 f9a10bd4280caca38bfaa62266ddd5c6f2761271 Mon Sep 17 00:00:00 2001 From: penguintyler Date: Mon, 16 Nov 2015 14:33:49 -0500 Subject: [PATCH 3/8] Added more balls --- BouncingWithVectors/BouncingWithVectors.pde | 38 ++++++++++++--------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 447a47d..d7b63b1 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,39 +1,45 @@ //declare variables -float diam; -PVector loc; -PVector vel = new PVector(2, 2); +int count = 30; +float[] diam = new float[count]; +PVector[] loc = new PVector[count]; +PVector[] vel = new PVector[count]; void setup() { //set size of canvas size(800, 600); //initialize variables - loc = new PVector(width/2, height/2); - diam = 80; - vel = PVector.random2D(); + for (int i = 0; i < count; i++) { + loc[i] = new PVector(width/2, height/2); + diam[i] = 80; + vel[i] = PVector.random2D(); + vel[i].mult(5); + } } void draw() { //draw background to cover previous frame background(0); + for (int i = 0; i < count; i++) { //draw ball - ellipse(loc.x, loc.y, diam, diam); + ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); //add velocity to position //loc.x += vel.x; //loc.y += vel.y; - loc.add(vel); + loc[i].add(vel[i]); //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 - } 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[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.y + diam/2 >= height) { - vel.y = -abs(vel.y); - } else if (loc.y - diam/2 <= 0) { - vel.y = abs(vel.y); + 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 2a2a79113b9d654cca79774b53a1e787fe737ac1 Mon Sep 17 00:00:00 2001 From: penguintyler Date: Mon, 16 Nov 2015 14:40:36 -0500 Subject: [PATCH 4/8] Added speed and colors --- BouncingWithVectors/BouncingWithVectors.pde | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index d7b63b1..e49d517 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,5 +1,5 @@ //declare variables -int count = 30; +int count = 100; float[] diam = new float[count]; PVector[] loc = new PVector[count]; PVector[] vel = new PVector[count]; @@ -11,18 +11,19 @@ void setup() { //initialize variables for (int i = 0; i < count; i++) { loc[i] = new PVector(width/2, height/2); - diam[i] = 80; + diam[i] = random(20,60); vel[i] = PVector.random2D(); - vel[i].mult(5); + vel[i].mult(15); } } void draw() { //draw background to cover previous frame - background(0); + background(255,0,0); for (int i = 0; i < count; i++) { //draw ball + fill(100,200,100); ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); //add velocity to position From c8be9243e0b00843945bddd84763b2e897c92b8e Mon Sep 17 00:00:00 2001 From: penguintyler Date: Wed, 18 Nov 2015 13:46:22 -0500 Subject: [PATCH 5/8] Fixed wanderer --- 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 8f197f7d4678e13613ddf9655052f7648b7a8a82 Mon Sep 17 00:00:00 2001 From: penguintyler Date: Wed, 18 Nov 2015 14:02:25 -0500 Subject: [PATCH 6/8] added pvectors to wanderer --- Wanderer/Wanderer.pde | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 9e50362..d8d0bd1 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); diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + vel = PVector.random2D(); + vel.mult(5); } void draw() { @@ -18,21 +19,20 @@ 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 >= 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 c777378ca227b13324cb0ce0e6a7cef03635e40a Mon Sep 17 00:00:00 2001 From: penguintyler Date: Wed, 18 Nov 2015 14:51:24 -0500 Subject: [PATCH 7/8] It wandered and colors --- Wanderer/Wanderer.pde | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index d8d0bd1..a764cfd 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -2,37 +2,49 @@ float diam; PVector loc; PVector vel; +PVector acc; void setup() { //set size of canvas size(800, 600); + background(0); //initialize variables - loc = new PVector(width/2,height/2); + loc = new PVector(width/2, height/2); diam = 80; vel = PVector.random2D(); - vel.mult(5); + vel.mult(0); + acc = PVector.random2D(); + acc.mult(5); + fill(random(255),random(255),random(255)); } void draw() { //draw background to cover previous frame - background(0); - //draw ball - ellipse(loc.x, loc.y, diam, diam); + //draw ball; + ellipse(loc.x, loc.y, diam, diam); + acc = PVector.random2D(); + acc.mult(5); //add velocity to position loc.add(vel); + vel.add(acc); + vel.limit(5); //wrap the ball's position if (loc.x >= width) { - loc.x = 0; + loc.x = 0; + fill(random(255), random(255), random(255)); } else if (loc.x <= 0) { loc.x = width; + fill(random(255), random(255), random(255)); } if (loc.y >= height) { loc.y = 0; + fill(random(255), random(255), random(255)); } else if (loc.y <= 0) { loc.y = height; + fill(random(255), random(255), random(255)); } } \ No newline at end of file From d972239b952307b5e9c74f8889a935d6c42ab81c Mon Sep 17 00:00:00 2001 From: penguintyler Date: Fri, 20 Nov 2015 13:46:56 -0500 Subject: [PATCH 8/8] Added mouse press --- Wanderer/Wanderer.pde | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index a764cfd..79bb3cc 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -16,7 +16,7 @@ void setup() { vel.mult(0); acc = PVector.random2D(); acc.mult(5); - fill(random(255),random(255),random(255)); + fill(random(200, 255), random(200, 255), random(200, 255)); } void draw() { @@ -26,11 +26,11 @@ void draw() { //draw ball; ellipse(loc.x, loc.y, diam, diam); acc = PVector.random2D(); - acc.mult(5); + acc.mult(2); //add velocity to position loc.add(vel); vel.add(acc); - vel.limit(5); + vel.limit(10); //wrap the ball's position if (loc.x >= width) { @@ -47,4 +47,7 @@ void draw() { loc.y = height; fill(random(255), random(255), random(255)); } + if (mousePressed) { + vel.limit(1.5); //velocity will decrease if mouse if pressed + } } \ No newline at end of file