From 90694008713a7a9ddd9cecc949dc1d69ef659cb5 Mon Sep 17 00:00:00 2001 From: ewang803 Date: Fri, 13 Nov 2015 13:13:03 -0500 Subject: [PATCH 1/4] Added vectors Updated with vectors --- BouncingWithVectors/BouncingWithVectors.pde | 41 +++++++++++---------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..cb00821 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,18 @@ //declare variables -float x, y, velX, velY, diam; +float diam; + +PVector loc; //declare loc (x & y) +PVector vel; //declare vel (velX & velY) void setup() { //set size of canvas - size(800, 600); + size(800, 600, P3D); + - //initialize variables - x = width/2; - y = height/2; - diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + //initialize vectors : constructor + loc = new PVector (width/2, height/2); + vel = new PVector (random(-5,5), random(-5,5)); + diam = 50; } void draw() { @@ -18,20 +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; //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 6eb84b91688fb75f222ef6107afd045f79ac3db4 Mon Sep 17 00:00:00 2001 From: ewang803 Date: Fri, 13 Nov 2015 13:24:05 -0500 Subject: [PATCH 2/4] Acceleration --- BouncingWithVectors/BouncingWithVectors.pde | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index cb00821..0c62e9c 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,30 +1,32 @@ //declare variables float diam; + PVector loc; //declare loc (x & y) PVector vel; //declare vel (velX & velY) void setup() { //set size of canvas size(800, 600, P3D); - + //initialize vectors : constructor loc = new PVector (width/2, height/2); - vel = new PVector (random(-5,5), random(-5,5)); - diam = 50; + vel = PVector.random2D(); + vel.mult(10); + diam = 100; + } void draw() { //draw background to cover previous frame background(0); - + //draw ball 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 83756cd8e5032979a6ecfd40139f9ee4afaef206 Mon Sep 17 00:00:00 2001 From: ewang803 Date: Tue, 17 Nov 2015 12:24:40 -0500 Subject: [PATCH 3/4] Edits correction to 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 2991188f0d4157cf15786f0385d7f2a95e64f8a0 Mon Sep 17 00:00:00 2001 From: ewang803 Date: Tue, 17 Nov 2015 13:24:52 -0500 Subject: [PATCH 4/4] Arrays and color --- Wanderer/Wanderer.pde | 61 ++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 9e50362..0c090fe 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,38 +1,51 @@ //declare variables -float x, y, velX, velY, diam; +int diam = 50; +int count = 50; +PVector []loc = new PVector [count]; +PVector []vel = new PVector [count]; +PVector []acc = new PVector [count]; void setup() { //set size of canvas size(800, 600); - //initialize variables - x = width/2; - y = height/2; - diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + for (int i = 0; i < count; i++) { + loc[i] = new PVector (width/2, height/2); + vel[i] = new PVector (0, 0); + acc[i] = PVector.random2D(); + acc[i].mult(.5); + } } void draw() { - //draw background to cover previous frame - background(0); - //draw ball - ellipse(x, y, diam, diam); + for (int i = 0; i < count; i++) { + //draw ball + fill(0,0,0, 30); + stroke(255); + ellipse(loc[i].x, loc[i].y, diam, diam); - //add velocity to position - x += velX; - y += velY; + //add velocity, accel + acc[i] = PVector.random2D(); + acc[i].mult(.5); + vel[i].limit(3); - //wrap the ball's position - if (x >= width) { - x = 0; - } else if (x <= 0) { - x = width; - } - if (y >= height) { - y = 0; - } else if (y <= 0) { - y = height; + //draw background to cover previous frame + + //add velocity to position + loc[i].add(vel[i]); + vel[i].add(acc[i]); + + //wrap the ball's position + if (loc[i].x >= width) { + loc[i].x = 0; //if the ball hits the right wall, assign x velocity the negative version of itself + } else if (loc[i].x <= 0) { + loc[i].x = width; //if the ball hits the left wall, assign x velocity the positive version of itself + } + 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