From 44018d7d8db3444c66bcfbe7f37fafb5ba292bb8 Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Thu, 12 Nov 2015 14:46:05 -0500 Subject: [PATCH 1/8] Added Vectors, got rid of other variables --- BouncingWithVectors/BouncingWithVectors.pde | 36 ++++++++++----------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..3413e8f 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,37 +1,37 @@ //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 = new PVector(random(-5, 5), random(-5, -5)); } void draw() { - //draw background to cover previous frame 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); + } else if (loc.x - diam/2 <= 0) { + vel.x = abs(vel.x); } - 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 0a2a0263e78850c2c22ed7e563645c9e9471a208 Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Thu, 12 Nov 2015 14:52:16 -0500 Subject: [PATCH 2/8] added vectors --- BouncingWithVectors/BouncingWithVectors.pde | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 3413e8f..19bc67b 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -10,7 +10,8 @@ void setup() { loc = new PVector(width/2, height/2); diam = 80; - vel = new PVector(random(-5, 5), random(-5, -5)); + vel = PVector.random2D(); + vel.mult(5); } void draw() { @@ -20,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 30bf61b5d396693810ebe1f714baac8cddd0d2dc Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Mon, 16 Nov 2015 14:36:50 -0500 Subject: [PATCH 3/8] Created 30 balls rather than 1 Created an array to create 30 balls. --- BouncingWithVectors/BouncingWithVectors.pde | 55 +++++++++++---------- 1 file changed, 29 insertions(+), 26 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 19bc67b..bab3a7e 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,39 +1,42 @@ //declare variables -float diam; -PVector loc; -PVector vel; - +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); - - loc = new PVector(width/2, height/2); - diam = 80; - vel = PVector.random2D(); - vel.mult(5); + for (int i = 0; i < count; i++) { + loc[i] = new PVector(width/2, height/2); + diam[i] = 80; + vel[i] = new PVector(random(-2,2),random(-2,2)); + vel[i].mult(5); + } } void draw() { background(0); + for (int i = 0; i < count; i++) { + //draw ball + fill(random(255),random(255),random(255)); + ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); - //draw ball - ellipse(loc.x, loc.y, diam, diam); + //add velocity to position + //loc.x += vel.x; + //loc.y += vel.y; + loc[i].add(vel[i]); - //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); - } else if (loc.x - diam/2 <= 0) { - vel.x = abs(vel.x); - } - 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); + } else if (loc[i].x - diam[i]/2 <= 0) { + vel[i].x = abs(vel[i].x); + } + 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 b4576a7d8494185bdf384de14d26ff19d7b0daa3 Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Mon, 16 Nov 2015 14:51:17 -0500 Subject: [PATCH 4/8] Added Gravity Now contains working gravity vector. BUG: Balls sink below the floor as the velocity vector continues to increase by the gravity vector. --- BouncingWithVectors/BouncingWithVectors.pde | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index bab3a7e..6e5df3e 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -3,15 +3,17 @@ int count = 30; float[] diam = new float[count]; PVector[] loc = new PVector[count]; PVector[] vel = new PVector[count]; +PVector[] gravity = new PVector[count]; void setup() { //set size of canvas size(800, 600); for (int i = 0; i < count; i++) { loc[i] = new PVector(width/2, height/2); - diam[i] = 80; + diam[i] = random(5,30); vel[i] = new PVector(random(-2,2),random(-2,2)); vel[i].mult(5); + gravity[i] = new PVector(0,1); } } @@ -25,6 +27,7 @@ void draw() { //add velocity to position //loc.x += vel.x; //loc.y += vel.y; + vel[i].add(gravity[i]); loc[i].add(vel[i]); //bounce ball if it hits walls From 90cea3ac4af4f4c0dde5aed8f4f993adfccfb13b Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Wed, 18 Nov 2015 13:44:39 -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 fcc36d4c32913f1ec4041b8d9f74f80e0a4151c6 Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Wed, 18 Nov 2015 14:11:06 -0500 Subject: [PATCH 6/8] Added Vectors --- Wanderer/Wanderer.pde | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 9e50362..f1f838a 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 = new PVector(2,2); + 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 e25cfa8e9e78ac189030ddcfd169d5973bfcdd45 Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Wed, 18 Nov 2015 14:50:35 -0500 Subject: [PATCH 7/8] It wanders --- Wanderer/Wanderer.pde | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index f1f838a..eef31ee 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -2,16 +2,18 @@ float diam; PVector loc; PVector vel; +PVector a; void setup() { //set size of canvas size(800, 600); //initialize variables - loc = new PVector(width/2,height/2); - diam = 80; - vel = new PVector(2,2); - vel.mult(5); + loc = new PVector(width/2, height/2); + diam = 40; + vel = new PVector(0, 0); + a = PVector.random2D(); + a.mult(.1); } void draw() { @@ -19,14 +21,18 @@ void draw() { background(0); //draw ball + fill(random(255),random(255),random(255)); ellipse(loc.x, loc.y, diam, diam); - + a = PVector.random2D(); + a.mult(.1); //add velocity to position + vel.add(a); loc.add(vel); + vel.limit(10); //wrap the ball's position if (loc.x >= width) { - loc.x = 0; + loc.x = 0; } else if (loc.x <= 0) { loc.x = width; } From af3ceee6a24cc818f3749b535c95ebbae45a5de8 Mon Sep 17 00:00:00 2001 From: Agersh88 Date: Fri, 20 Nov 2015 13:53:52 -0500 Subject: [PATCH 8/8] Made an array...looks really cool --- Wanderer/Wanderer.pde | 64 +++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index eef31ee..36388b6 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,44 +1,48 @@ //declare variables -float diam; -PVector loc; -PVector vel; -PVector a; +int count = 30; +float[] diam = new float[count]; +PVector[] loc = new PVector[count]; +PVector[] vel = new PVector[count]; +PVector[] a = new PVector[count]; void setup() { //set size of canvas size(800, 600); - - //initialize variables - loc = new PVector(width/2, height/2); - diam = 40; - vel = new PVector(0, 0); - a = PVector.random2D(); - a.mult(.1); + for (int q = 0; q < count; q++) { + background(0); + //initialize variables + loc[q] = new PVector(width/2, height/2); + diam[q] = 10; + vel[q] = new PVector(0, 0); + a[q] = PVector.random2D(); + a[q].mult(.1); + } } void draw() { //draw background to cover previous frame - background(0); //draw ball - fill(random(255),random(255),random(255)); - ellipse(loc.x, loc.y, diam, diam); - a = PVector.random2D(); - a.mult(.1); - //add velocity to position - vel.add(a); - loc.add(vel); - vel.limit(10); + for (int q = 0; q < count; q++) { + fill(0,random(255),0); + ellipse(loc[q].x, loc[q].y, diam[q], diam[q]); + a[q] = PVector.random2D(); + a[q].mult(.1); + //add velocity to position + vel[q].add(a[q]); + loc[q].add(vel[q]); + vel[q].limit(10); - //wrap the ball's position - if (loc.x >= width) { - loc.x = 0; - } else if (loc.x <= 0) { - loc.x = width; - } - if (loc.y >= height) { - loc.y = 0; - } else if (loc.y <= 0) { - loc.y = height; + //wrap the ball's position + if (loc[q].x >= width) { + loc[q].x = 0; + } else if (loc[q].x <= 0) { + loc[q].x = width; + } + if (loc[q].y >= height) { + loc[q].y = 0; + } else if (loc[q].y <= 0) { + loc[q].y = height; + } } } \ No newline at end of file