diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..c66204c 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,20 @@ //declare variables -float x, y, velX, velY, diam; +float diam; + +PVector loc; +PVector vel; 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 = PVector.random2D(); + vel.mult(10); diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + } void draw() { @@ -18,20 +22,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 diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..ed06245 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,37 +1,64 @@ //declare variables -float x, y, velX, velY, diam; + +int count = 10; +float[] diam = new float[count]; +PVector[] loc = new PVector[count]; +PVector[] vel = new PVector[count]; +PVector[] acc = new PVector[count]; void setup() { //set size of canvas size(800, 600); - + + for(int i = 0; i < count; i++){ //initialize variables - x = width/2; - y = height/2; - diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + loc[i] = new PVector(width/2, height/2); + vel[i] = new PVector(0, 0); + acc[i] = PVector.random2D(); + acc[i].mult(.1); + + diam[i] = 80; + + } + noStroke(); } void draw() { + //draw background to cover previous frame background(0); + + for(int i = 0; i < count; i++){ + + acc[i] = PVector.random2D(); + acc[i].mult(.1); + vel[i].limit(1); + + + //draw ball - ellipse(x, y, diam, diam); + ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); + + //add acceleration to velocity + vel[i].add(acc[i]); //add velocity to position - x += velX; - y += velY; + loc[i].x += vel[i].x; + loc[i].y += vel[i].y; //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[i].x >= width) { + loc[i].x = 0; + } else if (loc[i].x <= 0) { + loc[i].x = width ; + } + if (loc[i].y >= height) { + loc[i].y = 0; + } else if (loc[i].y <= 0) { + loc[i].y = height; } - if (y + diam/2 >= height) { - y = -diam/2; - } else if (y - diam/2 <= 0) { - y = height + diam/2; + } + +} \ No newline at end of file