diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..f619a74 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,37 +1,50 @@ //declare variables -float x, y, velX, velY, diam; +float diam; +int count = 30; +//float loc.x,loc.y +PVector [] loc= new PVector [count]; //replaces loc.x and loc.y + +//float vel.loc.x, vel.loc.y +PVector [] vel= new PVector [count]; //replces vel.loc.x and vel.loc.y + void setup() { //set size of canvas size(800, 600); - + for (int i = 0; i < count; i++) { //initialize variables - x = width/2; - y = height/2; + // loc.x = width/2; + //loc.y = height/2; + loc[i]= new PVector(width/2, height/2); diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + //vel.loc.x = random(-5, 5); + //vel.loc.y = random(-5, 5); + vel[i] = PVector.random2D(); +} } void draw() { //draw background to cover previous frame background(0); - + for (int i = 0; i < count; i++) { //draw ball - ellipse(x, y, diam, diam); + ellipse(loc[i].x, loc[i].y, diam, diam); //add velocity to position - x += velX; - y += velY; + //loc.x += vel.x; + //loc.y += vel.y; + loc[i].add(vel[i]); //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[i].x + diam/2 >= width) { + vel[i].x = -abs(vel[i].x); //if the ball hits the right wall, assign loc.x velocitloc.y the negative version of itself + } else if (loc[i].x - diam/2 <= 0) { + vel[i].x = abs(vel[i].x); //if the ball hits the left wall, assign loc.x velocitloc.y the positive version of itself } - if (y + diam/2 >= height) { - velY = -abs(velY); - } else if (y - diam/2 <= 0) { - velY = abs(velY); + if (loc[i].y + diam/2 >= height) { + vel[i].y = -abs(vel[i].y); + } else if (loc[i].y - diam/2 <= 0) { + vel[i].y = abs(vel[i].y); } +} +} \ No newline at end of file diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..c7a74e5 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,37 +1,48 @@ //declare variables -float x, y, velX, velY, diam; +float diam; //float loc. x,y +PVector loc, acc, vel; //replaces loc.x and loc.y void setup() { //set size of canvas size(800, 600); //initialize variables - x = width/2; - y = height/2; + loc = new PVector(width/2, height/2); + + + //x = width/2; + // y = height/2; diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + //velX = random(-5, 5); + //velY = random(-5, 5); + vel = new PVector(4, 7); + vel.mult(1); + acc = PVector.random2D(); + acc.mult(.1); } void draw() { //draw background to cover previous frame - background(0); + background(0, 135, 220); //draw ball - ellipse(x, y, diam, diam); - + fill(0); + ellipse(loc.x, loc.y, diam, diam); + vel.add(acc); + vel.limit(2); //add velocity to position - x += velX; - y += velY; + //loc.x += vel.x; + //loc.y += vel.y; + 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) { //allows ball to wander in the x direction + loc.x = 0; + } else if (loc.x <= 0) { //allows ball to wander in the x direction + 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) { //allows ball to wander in the y direction + loc.y = 0; + } else if (loc.y <= 0) { //allows ball to wander in the y direction + loc.y = height; } +} \ No newline at end of file