diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..ad5d2e2 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,37 +1,55 @@ -//declare variables -float x, y, velX, velY, diam; +int woof = 30; +float[] diam = new float[woof]; +PVector[] loc = new PVector[woof]; +PVector[] vel = new PVector[woof]; +PVector[] grav = new PVector[woof]; +PVector[] fric = new PVector[woof]; +PVector[] finpos = new PVector[woof]; -void setup() { - //set size of canvas +void setup() +{ 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 < woof; i++) + { + diam[i] = 80; + loc[i] = new PVector (random(width),random(height)); + vel[i] = new PVector (random(-5,5),random(-5,5)); + grav[i] = new PVector (0,0.4); + fric[i] = new PVector (0.95,0); + finpos[i] = new PVector (loc[i].y, height-diam[i]/2); + } } -void draw() { - //draw background to cover previous frame +void draw() +{ background(0); + for(int i = 0;i < woof; i++) +{ + noStroke(); + fill(random(255),random(255),random(255)); + ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); + + loc[i].add(vel[i]); - //draw ball - ellipse(x, y, diam, diam); - - //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 (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 (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); } + if (loc[i].y - diam[i]/2 > 0) + { + vel[i].add(grav[i]); + } + if (loc[i].y + diam[i]/2 > height) + { + loc[i] = finpos[i]; + } +} +} \ No newline at end of file diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..02ace5b 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,37 +1,47 @@ -//declare variables -float x, y, velX, velY, diam; +int woof = 40; +float[] diam = new float[woof]; +PVector[] loc = new PVector[woof]; +PVector[] vel = new PVector[woof]; +PVector[] accel = new PVector[woof]; 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); + background(0); +for(int i = 0;i < woof; i++) + { + diam[i] = 5; + loc[i] = new PVector (width/2,height/2); + vel[i] = PVector.random2D(); + vel[i].mult(0); + accel[i] = PVector.random2D(); + accel[i].mult(0.1); + } } void draw() { - //draw background to cover previous frame - background(0); + for(int i = 0;i < woof; i++) +{ + strokeWeight(1); + stroke(random(255),random(255),random(255)); + fill(0); + ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); + + accel[i] = PVector.random2D(); + accel[i].mult(0.1); + + vel[i].add(accel[i]); + vel[i].limit(5); + loc[i].add(vel[i]); - //draw ball - ellipse(x, y, diam, diam); - - //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 (loc[i].x >= width) { + loc[i].x = 0; + } else if (loc[i].x <= 0) { + loc[i].x = width; } - if (y + diam/2 >= height) { - y = -diam/2; - } else if (y - diam/2 <= 0) { - y = height + diam/2; + if (loc[i].y >= height) { + loc[i].y = 0; + } else if (loc[i].y <= 0) { + loc[i].y = height; } +} +} \ No newline at end of file