diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..6e5df3e 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,37 +1,45 @@ //declare variables -float x, y, velX, velY, diam; +int count = 30; +float[] diam = new float[count]; +PVector[] loc = new PVector[count]; +PVector[] vel = new PVector[count]; +PVector[] gravity = 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(5,30); + vel[i] = new PVector(random(-2,2),random(-2,2)); + vel[i].mult(5); + gravity[i] = new PVector(0,1); + } } void draw() { - //draw background to cover previous frame background(0); + for (int i = 0; i < count; i++) { + //draw ball + fill(random(255),random(255),random(255)); + ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); - //draw ball - ellipse(x, y, diam, diam); + //add velocity to position + //loc.x += vel.x; + //loc.y += vel.y; + vel[i].add(gravity[i]); + loc[i].add(vel[i]); - //add velocity to position - x += velX; - y += velY; - - //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 (y + diam/2 >= height) { - velY = -abs(velY); - } else if (y - diam/2 <= 0) { - velY = abs(velY); + //bounce ball if it hits walls + if (loc[i].x + diam[i]/2 >= width) { + vel[i].x = -abs(vel[i].x); + } else if (loc[i].x - diam[i]/2 <= 0) { + vel[i].x = abs(vel[i].x); + } + 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..36388b6 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,37 +1,48 @@ //declare variables -float x, y, velX, velY, diam; +int count = 30; +float[] diam = new float[count]; +PVector[] loc = new PVector[count]; +PVector[] vel = new PVector[count]; +PVector[] a = 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 q = 0; q < count; q++) { + background(0); + //initialize variables + loc[q] = new PVector(width/2, height/2); + diam[q] = 10; + vel[q] = new PVector(0, 0); + a[q] = PVector.random2D(); + a[q].mult(.1); + } } void draw() { //draw background to cover previous frame - background(0); //draw ball - ellipse(x, y, diam, diam); + for (int q = 0; q < count; q++) { + fill(0,random(255),0); + ellipse(loc[q].x, loc[q].y, diam[q], diam[q]); + a[q] = PVector.random2D(); + a[q].mult(.1); + //add velocity to position + vel[q].add(a[q]); + loc[q].add(vel[q]); + vel[q].limit(10); - //add velocity to position - x += velX; - y += velY; - - //wrap the ball's position - if (x + diam/2 >= width) { - x = -diam/2; - } else if (x - diam/2 <= 0) { - x = width + diam/2; - } - if (y + diam/2 >= height) { - y = -diam/2; - } else if (y - diam/2 <= 0) { - y = height + diam/2; + //wrap the ball's position + if (loc[q].x >= width) { + loc[q].x = 0; + } else if (loc[q].x <= 0) { + loc[q].x = width; + } + if (loc[q].y >= height) { + loc[q].y = 0; + } else if (loc[q].y <= 0) { + loc[q].y = height; + } } +} \ No newline at end of file