diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..1151ba2 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,37 +1,56 @@ //declare variables -float x, y, velX, velY, diam; +int num = 15; +float [] diam = new float[num]; +PVector [] loc = new PVector[num]; +PVector [] vel = new PVector[num]; void setup() { //set size of canvas - size(800, 600); + size(800, 600, P3D); //initialize variables - x = width/2; - y = height/2; - diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + for(int i = 0; i < num; i++) { + diam[i] = 80; + loc[i] = new PVector(width/2, height/2, 0); + vel[i] = PVector.random3D(); + vel[i].mult(5); + } } void draw() { //draw background to cover previous frame background(0); - //draw ball - ellipse(x, y, diam, diam); + //draw box + pushMatrix(); + translate(width/2,height/2); + noFill(); + stroke(255); + box(width); + popMatrix(); + + for(int i = 0; i < num; i++) { + //draw ball + pushMatrix(); + stroke(255,0,0); + translate(loc[i].x,loc[i].y,loc[i].z); + sphere(diam[i]); + popMatrix(); - //add velocity to position - x += velX; - y += velY; + //add velocity to position + 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 (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 || loc[i].x - diam[i]/2 <= 0) { + vel[i].x *= -1; //if the ball hits the side, assign the x velocity the opposite version + } + if (loc[i].y + diam[i]/2 >= height || loc[i].y - diam[i]/2 <= 0) { + vel[i].y *= -1; + } + if (loc[i].z - diam[i]/2 <= -sqrt(sq(width)+sq(height))/2) { + vel[i].z *= -1; + } else { + vel[i].z -= 1; + } } +} \ No newline at end of file diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..09b3242 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,37 +1,63 @@ //declare variables -float x, y, velX, velY, diam; +PVector loc; +PVector vel = PVector.random2D(); +PVector acc; +int diam = 80; +PVector [] ripples = new PVector[100]; 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); + //initialize location + loc = new PVector(width/2, height/2); + + //assign initial ripples + for (int i = 0; i < 100; i++) { + ripples[i] = new PVector(0, 0); + } } void draw() { //draw background to cover previous frame - background(0); + background(128,196,255); + + PVector acc = PVector.random2D(); + acc.mult(0.1); //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); + + //add acceleration to velocity + vel.add(acc); + vel.limit(100); + + //reassign ripple values + for (int i = 99; i > 0; i--) { + ripples[i] = ripples[i-1]; + } + + //draw ripples + for (int i = 0; i < 50; i++) { + ripples[0] = loc; + noFill(); + strokeWeight(2); + stroke(0,255-25*i); + ellipse(ripples[i].x, ripples[i].y, diam+100*i, diam+100*i); + } //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; + } else if (loc.x < 0) { + loc.x = width; } - if (y + diam/2 >= height) { - y = -diam/2; - } else if (y - diam/2 <= 0) { - y = height + diam/2; + if (loc.y > height) { + loc.y = 0; + } else if (loc.y < 0) { + loc.y = height; } +} \ No newline at end of file