From dc34ca8096ef5aa85766d863be11c444d2165cfe Mon Sep 17 00:00:00 2001 From: nafahmed Date: Fri, 13 Nov 2015 13:12:35 -0500 Subject: [PATCH 1/6] switched to vectors --- BouncingWithVectors/BouncingWithVectors.pde | 33 +++++++++++---------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..3f8762a 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; //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); 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); //loc.x is the x component of loc //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 8c0b20f20568cca1cecae49728fb130b6b2a3106 Mon Sep 17 00:00:00 2001 From: nafahmed Date: Fri, 13 Nov 2015 13:23:10 -0500 Subject: [PATCH 2/6] keep velocity of ball the same with different directions --- BouncingWithVectors/BouncingWithVectors.pde | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 3f8762a..141b460 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -9,8 +9,9 @@ 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(); //random vector - speed of ball stays the same while direction changes +vel.mult(10); //multiply vector by 10 - random direction with magnitude of 10 +diam = 80; } void draw() { @@ -20,9 +21,8 @@ void draw() { //draw ball ellipse(loc.x, loc.y, diam, diam); //loc.x is the x component of loc - //add velocity to position - loc.x += vel.x; - loc.y += vel.y; + //add velocity to position using vector addition - add vel vector to loc vector + loc.add(vel); //bounce ball if it hits walls if (loc.x + diam/2 >= width) { From d20b0ff64f39a32020ca979c16a3aaf0b0bce17c Mon Sep 17 00:00:00 2001 From: nafahmed Date: Tue, 17 Nov 2015 12:41:36 -0500 Subject: [PATCH 3/6] fixed code and added PVectors --- Wanderer/Wanderer.pde | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..2bf2446 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,37 +1,39 @@ //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; + //initialize PVectors + loc = new PVector(width/2, height/2); + vel = PVector.random2D(); + vel.mult(5); + diam = 80; - velX = random(-5, 5); - velY = 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.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 4452802ca5e8eea9c1bc636c85f2f24eeeac9077 Mon Sep 17 00:00:00 2001 From: nafahmed Date: Tue, 17 Nov 2015 13:03:42 -0500 Subject: [PATCH 4/6] made ellipse wander with acceleration vector --- Wanderer/Wanderer.pde | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 2bf2446..99115a8 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,6 +1,6 @@ //declare variables float diam; -PVector loc, vel; +PVector loc, vel, acc; void setup() { //set size of canvas @@ -8,19 +8,30 @@ void setup() { //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.1); + diam = 80; } void draw() { + acc = PVector.random2D(); + //draw background to cover previous frame - background(0); + //background(0); //draw ball ellipse(loc.x, loc.y, diam, diam); + +//multiply acc by 0.1 +acc.mult(0.1); + +//add acc to vel +vel.add(acc); + +//magnitude of vel cannot be greater than 1 +vel.limit(1); //add velocity to position loc.add(vel); From 79819594d3207aa6fa07dd01f9fac05a37c5e163 Mon Sep 17 00:00:00 2001 From: nafahmed Date: Tue, 17 Nov 2015 13:24:01 -0500 Subject: [PATCH 5/6] started adding arrays --- Wanderer/Wanderer.pde | 54 +++++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 99115a8..72a4250 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,50 +1,58 @@ //declare variables -float diam; -PVector loc, vel, acc; +float[] diam = new float[count]; +PVector[] loc = new PVector[count]; +PVector[] vel = new PVector[count]; +PVector[]acc = new PVector[count]; +int count=30; void setup() { //set size of canvas size(800, 600); - +int i=0; +while(i= width) { - loc.x = 0; + if (loc.x[i] >= width) { + loc.x[i] = -loc.x; } else if (loc.x <= 0) { - loc.x = width; + loc.x = -loc.x; } - if (loc.y >= height) { - loc.y = 0; - } else if (loc.y <= 0) { - loc.y = height; + if (loc.y+diam/2 >= height) { + loc.y = -loc.y; + } else if (loc.y+diam/2 <= 0) { + loc.y = -loc.y; } + + } \ No newline at end of file From ff2f5e7935cf12bda50a1e8352363925b3b2c436 Mon Sep 17 00:00:00 2001 From: nafahmed Date: Thu, 19 Nov 2015 12:20:37 -0500 Subject: [PATCH 6/6] code works now --- Wanderer/Wanderer.pde | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index 72a4250..f410bba 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,9 +1,11 @@ //declare variables + +int count=30; float[] diam = new float[count]; PVector[] loc = new PVector[count]; PVector[] vel = new PVector[count]; PVector[]acc = new PVector[count]; -int count=30; + void setup() { //set size of canvas @@ -16,18 +18,21 @@ while(i= width) { - loc.x[i] = -loc.x; - } else if (loc.x <= 0) { - loc.x = -loc.x; + if (loc[i].x >= width) { + loc[i].x = -loc[i].x; + } else if (loc[i].x <= 0) { + loc[i].x = -loc[i].x; } - if (loc.y+diam/2 >= height) { - loc.y = -loc.y; - } else if (loc.y+diam/2 <= 0) { - loc.y = -loc.y; + if (loc[i].y+diam[i]/2 >= height) { + loc[i].y = -loc[i].y; + } else if (loc[i].y+diam[i]/2 <= 0) { + loc[i].y = -loc[i].y; } - - + + i++; +} } \ No newline at end of file