Simulation Engine

This is the outline of a simulation engine in Processing.

It consists of a class “Agent” which is the character/object of the simulation. It is broken up with a method for its Appearance, Movement and Behaviour, as described in the slides. You can fill in whatever code you like for the behavior and appearance modules.

Below is a simple sketch that uses this class.

 
 
class Agent
{
   PVector position;
   PVector velocity;
   float max_speed;
   float max_steer;
 
   Agent(float px, float py, float vx, float vy)
  {
     max_speed = 2;
     max_steer = 1;
     position = new PVector(px,py);
     velocity = new PVector(vx,vy);
  } 
 
  void bounceOffWalls()
  {
    // fill in code here
  }
 
  void seekMouse()
  {
     // fill in code here
  }  
 
  void behaviour()
  {
     seekMouse();
     bounceOffWalls(); 
  }
 
  void movement()
  {
    position.add(velocity);
  }
 
  void appearance()
  {
     pushMatrix();
     translate(position.x, position.y);
 
     // fill in whatever drawing code you want
 
     popMatrix();
  }
 
  void draw()
  {
    behaviour();
    movement();
    appearance();
  }
 
}
 
Agent agents[];
 
void setup()
{
  size(600, 400);
 
  agents = new Agent[20];
  for (int i = 0; i < agents.length; i++)
  {
     agents[i] = new Agent(random(0, width), random(0, height), random(-2,2), random(-2,2));
  } 
}
 
void draw()
{
    background(255); 
    for (int i = 0; i < agents.length; i++)
    {
       agents[i].draw();
    } 
}
Create PDF    Send article as PDF to