diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..147f442 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,37 +1,46 @@ //declare variables -float x, y, velX, velY, diam; - +float g; +int count = 3000; +PVector[] loc = new PVector [count]; +float []diam = new float [count]; +PVector[] vel = new PVector [count]; +float []x = new float [count]; void setup() { //set size of canvas + frameRate(150); 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++) { + //initialize variables + loc[i] = new PVector(width/2, height/2); + diam[i] = random(4, 6); + vel[i] = PVector.random2D(); + g =.1; + } } void draw() { //draw background to cover previous frame background(0); + for (int i=0; i < count; i++) { + //draw ball + fill(255, 0, 0); + ellipse(loc[i].x, loc[i].y, diam[i], diam[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 (y + diam/2 >= height) { - velY = -abs(velY); - } else if (y - diam/2 <= 0) { - velY = abs(velY); + //add velocity to position + //loc.x += vel.x; + //loc.y += vel.y; + loc[i].add(vel[i]); + //bounce ball if it hits walls + 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 (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..8450a69 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,37 +1,62 @@ //declare variables -float x, y, velX, velY, diam; +int count = 15; +int colors = 0; +PVector []acc = new PVector [count]; +PVector[] loc = new PVector [count]; +float []diam = new float [count]; +PVector[] vel = new PVector [count]; +//float []x = new float [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); + size(800, 600); + for (int i=0; i < count; i++) { + //initialize variables + loc[i] = new PVector(width/2, height/2); + diam[i] = 50; + vel[i] =PVector.random2D(); + vel[i].mult(1); + acc[i] = PVector.random2D(); + acc[i].mult(.001); + background(0, 0, 0, 10); + } } void draw() { //draw background to cover previous frame - background(0); + // background(0, 0,0, 10); +fill(frameCount%255, 0, 0); +// background(0); + for (int i=0; i < count; i++) { + //draw ball + acc[i] = PVector.random2D(); + acc[i].mult(.3); + vel[i].add(acc[i]); + noStroke(); + //. fill(random(255), random(255), random(255)); + //vel[i] = PVector.random2D().mult(random(8)); + ellipse(loc[i].x, loc[i].y, diam[i], diam[i]); - //draw ball - ellipse(x, y, diam, diam); + //add velocity to position + loc[i].add(vel[i]); + vel[i].limit(3); - //add velocity to position - x += velX; - y += velY; + //wrap the ball's position - //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; + if (loc[i].x >= width) { + loc[i].x = width/2; //if the ball hits the right wall go to left wall + loc[i].y = height/2 ; + } else if (loc[i].x <= 0) { + loc[i].x = width/2; //if the ball hits the left wall go to right wall + loc[i].y = height/2 ; + } + if (loc[i].y >= height) { + loc[i].y = height/2; + loc[i].x = width/2; + } else if (loc[i].y <= 0) { + loc[i].y = height/2 ; + loc[i].x = width/2; + } } +} \ No newline at end of file