diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..e49d517 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,37 +1,46 @@ //declare variables -float x, y, velX, velY, diam; +int count = 100; +float[] diam = new float[count]; +PVector[] loc = new PVector[count]; +PVector[] vel = new PVector[count]; 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); + for (int i = 0; i < count; i++) { + loc[i] = new PVector(width/2, height/2); + diam[i] = random(20,60); + vel[i] = PVector.random2D(); + vel[i].mult(15); + } } void draw() { //draw background to cover previous frame - background(0); + background(255,0,0); + for (int i = 0; i < count; i++) { //draw ball - ellipse(x, y, diam, diam); + fill(100,200,100); + ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); //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[i]/2 >= width) { + vel[i].x = -abs(vel[i].x); //if the ball hits the right wall, assign x velocity the negative version of itself + } else if (loc[i].x - diam[i]/2 <= 0) { + vel[i].x = abs(vel[i].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[i].y + diam[i]/2 >= height) { + vel[i].y = -abs(vel[i].y); + } else if (loc[i].y - diam[i]/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..79bb3cc 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,37 +1,53 @@ //declare variables -float x, y, velX, velY, diam; +float diam; +PVector loc; +PVector vel; +PVector acc; void setup() { //set size of canvas size(800, 600); + background(0); //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 = PVector.random2D(); + vel.mult(0); + acc = PVector.random2D(); + acc.mult(5); + fill(random(200, 255), random(200, 255), random(200, 255)); } void draw() { //draw background to cover previous frame - background(0); - //draw ball - ellipse(x, y, diam, diam); + //draw ball; + ellipse(loc.x, loc.y, diam, diam); + acc = PVector.random2D(); + acc.mult(2); //add velocity to position - x += velX; - y += velY; + loc.add(vel); + vel.add(acc); + vel.limit(10); //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; + fill(random(255), random(255), random(255)); + } else if (loc.x <= 0) { + loc.x = width; + fill(random(255), random(255), random(255)); + } + if (loc.y >= height) { + loc.y = 0; + fill(random(255), random(255), random(255)); + } else if (loc.y <= 0) { + loc.y = height; + fill(random(255), random(255), random(255)); } - if (y + diam/2 >= height) { - y = -diam/2; - } else if (y - diam/2 <= 0) { - y = height + diam/2; + if (mousePressed) { + vel.limit(1.5); //velocity will decrease if mouse if pressed } +} \ No newline at end of file