Nature of Code (HW3)

Hot Air Ballons (Nature of Code) from Colin Narver on Vimeo.

In this homework assignment, I created an array of hot air ballons and brought in the additional force of wind as well as simple harmonic motion (via oscillation).

Mover[] movers;
PImage img;

//PVector current;

void setup() {
  size(600, 800, P2D);
  PShape balloon = loadShape("balloon.svg");
  movers = new Mover[10];
  for (int i = 0; i < movers.length; i++) {
    movers[i] = new Mover(balloon, random(width), random(height));
  }

  img = loadImage("clouds.jpeg");
}

void draw() {
  background(255);
  image(img, 0, 0);

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

  for (int i= 0; i<movers.length; i++) {

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

    movers[i].applyForce(wind);

    PVector r = PVector.random2D();
    r.mult(0.05);
    movers[i].applyForce(r);

    movers[i].run();
  }
}

class Mover {

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

  float x;
  float y;

  float angle = 0;
  float aVelocity = random (0.01, 0.05);

  //constructor
  Mover(PShape _s, float _x, float _y) {
    s = _s;
    x = _x;
    y = _y;

    location= new PVector (_x, _y);
    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() {
    angle += aVelocity;
    velocity.add(acceleration);
    velocity.limit(5);
    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);
    float r = map(cos(angle),-1,1,100,190);
    shape(s, location.x, location.y, r, 125);  //shape(s, location.x, location.y, 125, 125);
  }

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

  void checkEdges() {
    float buffer = 150;
    if (location.x> width+buffer) {
      location.x=-buffer;
    } 
    else if (location.x< -buffer) {
      location.x=width+buffer;
    }

    if (location.y> height+buffer) {
      location.y=-buffer;
    } 
    else if (location.y<-buffer) {
      location.y=height+buffer;
    }
  }
}