From ce5c98c18edb2b3efbf032d3ee698bac864ee838 Mon Sep 17 00:00:00 2001 From: MyiaS Date: Fri, 13 Nov 2015 13:13:02 -0500 Subject: [PATCH 1/5] Switched to Vectors --- BouncingWithVectors/BouncingWithVectors.pde | 36 ++++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..c6b57d2 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,19 @@ //declare variables -float x, y, velX, velY, diam; +float diam; + +PVector loc; //declare loc, replaces x and y +PVector vel; //declare vel 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= new PVector(random(-5,5),random(-5,5)); diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); } void draw() { @@ -18,20 +21,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 34374dac1a36f783eb0029a160db14f06b234a30 Mon Sep 17 00:00:00 2001 From: MyiaS Date: Fri, 13 Nov 2015 13:19:18 -0500 Subject: [PATCH 2/5] .add --- BouncingWithVectors/BouncingWithVectors.pde | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index c6b57d2..b91a512 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -24,8 +24,7 @@ void draw() { 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 ede0215e375e7b5a0c27da459875a4e7d31a8408 Mon Sep 17 00:00:00 2001 From: MyiaS Date: Fri, 13 Nov 2015 13:22:57 -0500 Subject: [PATCH 3/5] used random2d and .mult --- BouncingWithVectors/BouncingWithVectors.pde | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index b91a512..1ab4eb1 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -12,7 +12,8 @@ void setup() { //initialize variables loc = new PVector(width/2,height/2); - vel= new PVector(random(-5,5),random(-5,5)); + vel= PVector.random2D(); + vel.mult(10); diam = 80; } From 96aa3d38e9cf244e59b1e32e506f569a936eb0f0 Mon Sep 17 00:00:00 2001 From: MyiaS Date: Tue, 17 Nov 2015 12:45:30 -0500 Subject: [PATCH 4/5] wanderer pvectors --- Wanderer/Wanderer.pde | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..72662ee 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, vel; 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); + //initialize PVectors + loc = new PVector(width/2, height/2); + vel = PVector.random2D(); + vel.mult(5); + diam=80; + } void draw() { @@ -18,20 +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 + 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 0ccfab6400cabfa3ff0a12fd0049a5c4bca95590 Mon Sep 17 00:00:00 2001 From: MyiaS Date: Tue, 17 Nov 2015 13:16:08 -0500 Subject: [PATCH 5/5] Acceleration to wandering ball --- Wanderer/Wanderer.pde | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 72662ee..865d3a1 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,26 +1,39 @@ //declare variables float diam; -PVector loc, vel; +PVector loc, vel, acc; void setup() { //set size of canvas - size(800, 600); + size(1500, 800); + background(0); + colorMode( HSB, 360,100,100,100); + //initialize PVectors loc = new PVector(width/2, height/2); - vel = PVector.random2D(); - vel.mult(5); - diam=80; + vel = new PVector(0,0); + acc=PVector.random2D(); + //acc.mult(0.0001); + + diam=80; + //noStroke(); } void draw() { + frameRate(1000); + fill(frameCount%360,60,100); + acc=PVector.random2D(); //draw background to cover previous frame - background(0); + //draw ball ellipse(loc.x, loc.y, diam, diam); +// add acc to vel +vel.add(acc); +vel.limit(4); + //add velocity to position loc.add(vel);