From 4d37829dea1468092957adae586edf688190b950 Mon Sep 17 00:00:00 2001 From: jwong2 Date: Fri, 13 Nov 2015 12:53:09 -0500 Subject: [PATCH 1/6] Now works with PVector --- BouncingWithVectors/BouncingWithVectors.pde | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..d9cbd53 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,5 +1,7 @@ //declare variables -float x, y, velX, velY, diam; +float diam; +PVector loc; +PVector vel; void setup() { //set size of canvas @@ -35,3 +37,5 @@ void draw() { } else if (y - diam/2 <= 0) { velY = abs(velY); } + +} \ No newline at end of file From dc842fccca13ba08bb4aabd6eae941977d99e8e9 Mon Sep 17 00:00:00 2001 From: jwong2 Date: Fri, 13 Nov 2015 13:22:30 -0500 Subject: [PATCH 2/6] Vector Multiplication --- BouncingWithVectors/BouncingWithVectors.pde | 29 ++++++++++----------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index d9cbd53..4685053 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -8,11 +8,11 @@ void setup() { 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); + vel = PVector.random2D(); + vel.mult(5); } void draw() { @@ -20,22 +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.add(vel); //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 72e4d0ce65d78a516d6bb9239d42029f687848ca Mon Sep 17 00:00:00 2001 From: jwong2 Date: Tue, 17 Nov 2015 12:20:41 -0500 Subject: [PATCH 3/6] Wrapping Wanderer now works --- 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 5fbbf689cfb02ea5835e67f024fcef36e1c76151 Mon Sep 17 00:00:00 2001 From: jwong2 Date: Tue, 17 Nov 2015 12:31:07 -0500 Subject: [PATCH 4/6] added PVectors --- Wanderer/Wanderer.pde | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 9e50362..5487fe4 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,16 +1,16 @@ //declare variables -float x, y, velX, velY, diam; +float diam; +PVector loc, vel, accel; 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); + loc = new PVector(width/2,height/2); + vel = new PVector(1,1); + accel = new PVector(0,0); } void draw() { @@ -18,21 +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; //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 682d7d9316c7e73a6b4e4e743636586bfba86540 Mon Sep 17 00:00:00 2001 From: jwong2 Date: Tue, 17 Nov 2015 12:36:06 -0500 Subject: [PATCH 5/6] Added acceleration --- Wanderer/Wanderer.pde | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 5487fe4..499de85 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -9,8 +9,8 @@ void setup() { //initialize variables diam = 80; loc = new PVector(width/2,height/2); - vel = new PVector(1,1); - accel = new PVector(0,0); + vel = new PVector(10,3); + accel = new PVector(-0.001,0.01); } void draw() { @@ -23,6 +23,10 @@ void draw() { //add velocity to position loc.x += vel.x; loc.y += vel.y; + + //add acceleration to velocity + vel.x += accel.x; + vel.y += accel.y; //wrap the ball's position if (loc.x >= width) { From bf149cbaaf371a7e870b26d9d7a6ed1bffc317a9 Mon Sep 17 00:00:00 2001 From: jwong2 Date: Tue, 17 Nov 2015 13:23:14 -0500 Subject: [PATCH 6/6] Working Wanderer --- Wanderer/Wanderer.pde | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 499de85..bccfbf3 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,5 +1,5 @@ //declare variables -float diam; +float diam, diamChange; PVector loc, vel, accel; void setup() { @@ -8,25 +8,49 @@ void setup() { //initialize variables diam = 80; - loc = new PVector(width/2,height/2); - vel = new PVector(10,3); - accel = new PVector(-0.001,0.01); + loc = new PVector(width/2,height/2); //makes a new PVector class + vel = new PVector(1,0.5); + accel = PVector.random2D(); // calling random2D() method on class PVector returns a PVector + accel.mult(3); } void draw() { + accel = PVector.random2D(); + accel.mult(2); //draw background to cover previous frame background(0); //draw ball + fill(255); ellipse(loc.x, loc.y, diam, diam); - + + //add some text telling it's an electron + textAlign(CENTER); + textSize(32); + fill(0); + text("e-", loc.x, loc.y); + //add velocity to position - loc.x += vel.x; - loc.y += vel.y; + loc.add(vel); //add acceleration to velocity - vel.x += accel.x; - vel.y += accel.y; + vel.add(accel); + vel.limit(10); //so it doesn't go too fast + //println(sqrt(pow(vel.x,2)+pow(vel.y,2))); maximum vel is 10 + + //change the size of the radius + //check to make sure diam is not less than 0 and not more than 100 + if(diam <= 0) { + diamChange = random(0,10); + } else if(diam > 100) { + diamChange = random(-10,0); + } else{ + diamChange = random(-10,10); + } + + diamChange = random(-5,5); + diam += diamChange; + //wrap the ball's position if (loc.x >= width) {