Loading...
Logo
Processing Forum
Hi all
This is my first step towards a Boid code. I wrote this code to learn PVector, vector manipulation and OOP (Class). This sketch creates a system with two elements (balls) that try to catch up to the mouse pointer. These elements bounce off the walls and have different accelerations. Red ball's acceleration is twice that of the blue ball. Both have a maximum speed limitation. Both balls start at random locations. The user can assign the acceleration and color of the balls while creating the Object. I have placed as many comments as time allowed (it is Friday past midnight).

Next step would be to make these elements interact with each other while chasing the target which would make it a Boid code.

Took help from PVector and Objects tutorial here @ processing.org
Have fun with the code and the animation. Feel free to comment.

Bye,
pf

Code
Copy code
  1. // Declare Objects
    Boid Boid1;
    Boid Boid2;

    void setup() {
      size(500,500);
      background(2);
      Boid1 = new Boid(new PVector(random(width),random(height)), new PVector(0,0), 1.0, color(255,0,0));
      Boid2 = new Boid(new PVector(random(width),random(height)), new PVector(0,0), 0.5, color(0,0,255));
    }

    void draw() {
      noStroke();
      fill(255,10);
      rect(0,0,width,height);
      Boid1.move();
      Boid1.display();
      Boid2.move();
      Boid2.display();
    }

    class Boid {
      PVector pos;
      PVector vel;
      float acc;
      color colour;
      float maxspeed;
      PVector accelerate;
     
    Boid(PVector position, PVector velocity, float acceleration, color paint) {
      pos = position;
      vel = velocity;
      acc = acceleration;
      maxspeed = 10;
      colour = paint;
    }

    void move() {
    // Determine Mouse location 
      PVector mouse = new PVector(mouseX,mouseY);
    // Determine which direction to go (accelerate) 
      PVector direction = PVector.sub(mouse,pos);
    // Determine how close to target
      float separation = direction.mag();
      direction.normalize(); // Normalize to get unit vector
      direction.mult(acc); // Calculate actual acceleration
      accelerate = direction;

      vel.add(accelerate); // Calculate velocity
      vel.limit(maxspeed); // Limit speed
     
      if (separation < 20) { // If Boid close to target then stop
        vel.x = 0;
        vel.y = 0;
      }
     
      pos.add(vel); // Calculate position
     
      // Limit movement within window and bounce at window edge
      if ((pos.x < 0) || (pos.x > width)) {
        vel.x = vel.x * -1;
      }
      if ((pos.y < 0) || (pos.y > height)) {
        vel.y = vel.y * -1;
      } 
    }

    void display() {
      stroke(0);
      fill(colour);
      ellipse(pos.x,pos.y,10,10);
    }
    }

Replies(2)

I like this code  a lot. Can I use it for a 2d/3d fish game I am writing with a friend ?
Sure go ahead. Glad you liked it Just acknowledge me in your work. You can use this link http://arunwaves.wordpress.com/2010/06/19/building-a-boid-system-with-processing-sketch/
or
http://www.openprocessing.org/visuals/?visualID=10494

And do share your results.

Bye,
pf