Nature of Code HW#2 (Vectors and Forces)

NC 2 HW from Colin Narver on Vimeo.

This week's nature of code HW has me playing with the forces of gravity and antigravity simulated through a hot air balloon. Gravity is imposed upon the balloon with the key press of G and a mouse click imposes antigravity.  This may or may not be the environment I use to experiment with more forces in future projects. I like the idea of potentially having several hot air ballons being blown around by gusts of wind and each balloon having to navigate its course through large groups of flocking birds.

//Colin Narver
//Nature of Code
//HW#2

Mover mover;

//PVector current;

void setup() {
  size(600, 800, P2D);
  mover = new Mover();

}

void draw() {
  background(255);

  PVector gravity= new PVector (0, .015);
  PVector antiGravity = new PVector (0, -.015);

  if (mousePressed) {            //click the mouse to use antiGravity 
    println("MOUSE PRESSED");
    mover.applyForce(antiGravity);
  }
  if (keyPressed) {      //press the key "g" to use gravity
    if (key == 'g') {
      mover.applyForce(gravity);
    }
  }

  mover.run();
}

class Mover {

  PVector location;
  PVector velocity; //speed
  PVector acceleration; //the rate at which that speed is being applied 
  PVector gravity;
  PShape s;  
  PImage img;

  //constructor
  Mover() {
      s = loadShape("balloon.svg");
      img = loadImage("clouds.jpeg");

    location= new PVector (width/2-50, height/2-50);
    velocity= new PVector (0, 0);      //ball starts from a stop
    acceleration= new PVector (0, 0);  //ball accelerates downward as Y values increases perpetually
    //antiGravity= new PVector (0, -.01);
  }

  //Functions
  void run() {
  update();
  display();
  checkEdges();

  println("Acc: " + acceleration);
  println("Vel: " + velocity);
  println("Loc: " + location);
}

void update() {

    velocity.add(acceleration);
    location.add(velocity);  //velocity changes by acceleration
    acceleration.mult(0);  //if acceleration is steady on, velocity will continue to ramp up  and location will fly 

  }

  void display() {
//    stroke(255, 0, 0);
//    fill(0, 0, 255);
    image(img, 0,0);     //clouds
    shape(s, location.x, location.y, 125, 125);     //hot air balloon
  }

  void applyForce(PVector forceBeingApplied) {    //the constructor of the function 
    acceleration.add(forceBeingApplied);
  }

  void checkEdges() {
    if (location.x> width) {
      location.x=0;
    } 
    else if (location.x< 0) {
      location.x=width;
    }

    if (location.y> height) {
      location.y=0;
    } 
    else if (location.y<0) {
      location.y=height;
    }
  }
}