diff --git a/BouncingWithVectors/BouncingWithVectors.pde b/BouncingWithVectors/BouncingWithVectors.pde index 2683b50..4685053 100644 --- a/BouncingWithVectors/BouncingWithVectors.pde +++ b/BouncingWithVectors/BouncingWithVectors.pde @@ -1,16 +1,18 @@ //declare variables -float x, y, velX, velY, diam; +float diam; +PVector loc; +PVector vel; void setup() { //set size of canvas size(800, 600); //initialize variables - x = width/2; - y = height/2; + loc = new PVector(width/2, height/2); + vel = new PVector(random(-5,5), random(-5, 5)); diam = 80; - velX = random(-5, 5); - velY = random(-5, 5); + vel = PVector.random2D(); + vel.mult(5); } void draw() { @@ -18,20 +20,21 @@ void draw() { background(0); //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); //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.x + diam/2 >= width) { + vel.x = -abs(vel.x); //if the ball hits the right wall, assign x velocity the negative version of itself + } else if (loc.x - diam/2 <= 0) { + vel.x = abs(vel.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.y + diam/2 >= height) { + vel.y = -abs(vel.y); + } else if (loc.y - diam/2 <= 0) { + vel.y = abs(vel.y); } + +} \ No newline at end of file diff --git a/Wanderer/Wanderer.pde b/Wanderer/Wanderer.pde index b6ce247..bccfbf3 100644 --- a/Wanderer/Wanderer.pde +++ b/Wanderer/Wanderer.pde @@ -1,37 +1,66 @@ //declare variables -float x, y, velX, velY, diam; +float diam, diamChange; +PVector loc, vel, accel; 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); + loc = new PVector(width/2,height/2); //makes a new PVector class + vel = new PVector(1,0.5); + accel = PVector.random2D(); // calling random2D() method on class PVector returns a PVector + accel.mult(3); } void draw() { + accel = PVector.random2D(); + accel.mult(2); //draw background to cover previous frame background(0); //draw ball - ellipse(x, y, diam, diam); - + fill(255); + ellipse(loc.x, loc.y, diam, diam); + + //add some text telling it's an electron + textAlign(CENTER); + textSize(32); + fill(0); + text("e-", loc.x, loc.y); + //add velocity to position - x += velX; - y += velY; + loc.add(vel); + + //add acceleration to velocity + vel.add(accel); + vel.limit(10); //so it doesn't go too fast + //println(sqrt(pow(vel.x,2)+pow(vel.y,2))); maximum vel is 10 + + //change the size of the radius + //check to make sure diam is not less than 0 and not more than 100 + if(diam <= 0) { + diamChange = random(0,10); + } else if(diam > 100) { + diamChange = random(-10,0); + } else{ + diamChange = random(-10,10); + } + + diamChange = random(-5,5); + diam += diamChange; + //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