Falling Cats


  PImage  img;
  
  int    nbrParticles = 100;
  
  float  damping = .95;       // lower values act like cats are moving through more viscous fluid
  float  gravity = .1;        // higher values make cats drop faster, negative values makes them rise
  float  drift = .5;          // amount of brownian drift
  float  maxSpin = radians(9);// fastest spin speed is 9 degrees
  
  class Particle
  {
    float vx, vy, vspin;  // velocities - added
    float x, y, spin;
  
    Particle() {
      x = random(width);
      y = random(height);
      vspin = random(-maxSpin, maxSpin);
      spin = random(TWO_PI);
      vx = 0;
      vy = 0;
    }
  
    void render() {
      pushMatrix();
        translate(x,y);
        rotate(spin);
        image(img, -img.width/2, -img.height/2);
      popMatrix();
    }
  }
  Particle[] myFlakes = new Particle[nbrParticles];
  
  void setup()
  {
      size(600,600);
      img = loadImage("/funny_cat_60.jpg");
      background(0);
      for (int i=0; i < nbrParticles; ++i) {
        myFlakes[i] = new Particle();
      }
  }
  
  void draw()
  {
      background(255);
      for (int i=0; i < nbrParticles; ++i) {
        Particle f = myFlakes[i];
        
        f.render();
  
        // application of forces to velocities
        f.vy += random(-drift,+drift);  // brownian motion
        f.vx += random(-drift,+drift);
        f.vy += gravity;                // gravity
  
        // velocity affects position
        f.x += f.vx;   // add vx to x
        f.y += f.vy;   // add vy to y
  
        // constant spin rate
        f.spin += f.vspin;
        
        // apply damping (air friction) to movement velocities
        f.vx *= damping;  // shortcut for vx = vx * damping
        f.vy *= damping;
  
        // wrap around
        if (f.y > height+50) {
          f.y = -50;
        }
        if (f.x > width+50) {
          f.x = -50;
        }
        if (f.x < -50) {
          f.x = width+50;
        }
      }
  }