Processing – Object Oriented Programming Box Boids
Posted By: Jordan ParsonsWith all my recent free time (I’m on break) I’ve been really going at processing. Here is another batch of code I wrote. This one works as an applet, so you can see it in action, http://www.jordanparsons.com/processing/oop_boxes/. Its a simple series of boxes with small circles in them that function as individual “boids” or entities that move a set speeds and bounce off the walls of the image. You can click to add another boid as well. Hit the jump for the source and more info.

This is my first implementation of OOP in processing so its a bit fuzzy. My end goal is to write my own flocking script because that entails a lot of work with vectors which are currently driving me nuts. These little guys don’t rely on vectors for movements, its a simple X and Y displacement. Its a neat little demo with more to come. Again any ideas, thoughts, tips or what ever is welcome.
Source:
//-------------------------------
//OOP Boxes
//Creative Commons Attribution-Share Alike 3.0
//www.jordanparsons.com
//--------------------------------
traffic traffic;
void setup(){
size(640,300);
background(255);
int num = 1;
traffic = new traffic();
for (int i = 0; i < 75; i++) {
switch(num){
case 1:
traffic.addCar(new Car(25,random(0,5),-random(0,5)));
break;
case 2:
traffic.addCar(new Car(150,-random(0,5),random(0,5)));
break;
case 3:
traffic.addCar(new Car(100,-random(0,5),-random(0,5)));
break;
case 4:
traffic.addCar(new Car(75,random(0,5),random(0,5)));
break;
}
if(num==1){
num=2;
}
else if(num==2){
num=3;
}
else if(num==3){
num=4;
}
else if(num==4){
num=1;
}
}
}
void draw(){
background(255);
traffic.run();
}
void mousePressed(){
traffic.addCar(new Car(0,random(0,5),random(0,5)));
}
Cars Class:
//car class creates and controls the boxes via calls from traffic
class Car{
color c;
float xpos;
float ypos;
float xspeed;
float yspeed;
float changeInt;
Car(color cIn, float speedIn, float chIn){
c = color(255);
xpos = width/2;
ypos = height/2;
xspeed = 1;
c = cIn;
xspeed = speedIn;
yspeed = chIn;
}
void display(){
smooth();
noStroke();
rectMode(CENTER);
fill(c);
rect(xpos, ypos, 20, 10);
fill(0);
ellipse(xpos,ypos,1,1);
}
void drive(){
xpos = xpos + xspeed;
ypos = ypos + yspeed;
if(xpos > width){
xspeed = xspeed*(-1);
}
if(xpos < 0){
xspeed = xspeed*(-1);
}
if(ypos < 0){
yspeed = yspeed*(-1);
}
if(ypos > height){
yspeed = yspeed*(-1);
}
}
float getX(){
return xpos;
}
float getY(){
return ypos;
}
}
Traffic Class:
//List of the cars
class traffic{
ArrayList cars;
traffic(){
cars = new ArrayList();
}
void run(){
for (int i = 0; i < cars.size(); i++){
Car c = (Car) cars.get(i);
c.drive();
c.display();
}
}
void addCar(Car c){
cars.add(c);
}
}
1 Comment »
RSS feed for comments on this post. TrackBack URL





March 12, 2009 @ 3:05 am
[...] pretty cool examples. I really suggest trying to write something like this. After writing my lastbit-o-code this made me see how bloated thing can get. Especially when I think about the php I’ve had to [...]