• fullscreen
  • Fish.pde
  • fishfish6.pde
  • hand.pde
  • //class to describe a single fish object
    
    class Fish {
      PVector location;
      PVector velocity;
      PVector acceleration;
      float life;
      float angle;
      float angleSpeed;
      float angleFish;
    
      float mass =1;
    
      //float angle;//=0.0; //have to declare this variable because it is essentially a property of the fish 
      //dont initiate here yet..technically it is better to initiate inside constructor
      //float angleSpeed;//=random(0.1,1); 
    
    
      Fish (PVector l) {
        location = l.get();
        velocity = new PVector (random(-1, 3), random(-2, 2));
        acceleration = new PVector (0.02, -0.02);
        //float angleFish;
        angle=0;
        angleSpeed = random(0.1, 0.3);
        life = 255;
        angleFish=30;
      }
    
      void run() {
        update();
        display();
      }
    
      void applyForce(PVector hand) {
        PVector f=hand.get();
        f.div(mass);
        acceleration.add(f);
      }
    
    
    
    
    
      void update() {
        velocity.add(acceleration);
        location.add(velocity);
        //acceleration.mult(0);
        life-=0.9;
      }
    
    
      void display() {
    
        angle=angle+angleSpeed;
        float offsetHead;
        float offsetTail;
    
        offsetHead=sin(angle+PI)*3;
        offsetTail=sin(angle+PI/2)*5;
    
        //fish body outline
        fill(245, 187, 111, life);
        stroke(245, 187, 111, life);
        pushMatrix();
        translate(location.x, location.y);
    
        //float rotateAngle = atan2(-yspeed,xspeed);
    
        //!!!!!
        angleFish=atan2(velocity.y, velocity.x);
        //!!!
    
        rotate((angleFish)+HALF_PI);
        scale(0.65);
        beginShape();
        vertex((offsetTail), -10);
        bezierVertex((-30+offsetHead), -80, (30+offsetHead), -80, (offsetTail), -10);
        endShape();
        popMatrix();
      }
    
      boolean dead() {
        if (life <= 0.0) {
          return true;
        } 
        else {
          return false;
        }
      }
    }
    
    
    
    //mouse pressed will make fish swim to the left
    
    
    ArrayList<Fish> fishes; 
    
    void setup() {
      size(500,500);
      frameRate(30);
      fishes = new ArrayList<Fish> (); //create an empty array list
      }
    
    void draw() {
      background(215,248,250);
      fishes.add(new Fish(new PVector(random(width),600)));
      //add a random fish each loop through draw
      
      /*for (int i=fishes.size()-1; i>=0; i--) {
        //Fish f = fishes.get(i);
        Fish f = (Fish)fishes.get(i); //******* we specifiy type (Fish) when we pull objects
        //if we don't specify, we are using generics which means we need to put <Fish> behind ArrayList when
        //we first delcare it at the top and when we initiate the ArrayList in setup
      f.run();
      if (f.dead()) {
        fishes.remove(i);
      }
      }*/
      
      
      //using the iterator function in arraylist
      Iterator it = fishes.iterator();
      while (it.hasNext() ) {
        Fish f = (Fish)it.next();
        f.run();
        if (f.dead()) {
          it.remove();
        }
      }
    }
    
    void mousePressed() {
      for (Fish f: fishes) {
        PVector hand = new PVector (-0.1,0);
        f.applyForce(hand);
      }
    }
    
     
        
    
    
    
    
    
    /*
    class Hand {
    
    
      float G=100;
    
      PVector location;
    
      Hand (float x, float y) {
    
        location = new PVector (x, y);
      }
    
      PVector hand (Fish f) {
        PVector dir = PVector.sub(location, f.location);
        float d= dir.mag();
        dir.normalize();
        d=constrain (d, 5, 100);
        float force=G/(d*d);
        dir.mult(force);
        return dir;
      }
    }
    */
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    yin liu

    fish pond

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    mouse pressed will make the fishes swim to the left

    You need to login/register to comment.