Wednesday, December 26, 2012

Classes on Processing

I wish i'd had the explanation I'll give about classes on Processing when I was learning for the first time...

I reunite the basic information you need to understand classes below:

A class is the basis of the object-oriented programming: "A class is a blueprint or prototype from which objects are created." And, for instance, an object is "a software bundle of related state and behavior. Software objects are often used to model the real-world objects that you find in everyday life". (http://docs.oracle.com/javase/tutorial/java/concepts/index.html).

"A class is a composite of fields (data) and methods (functions that are a part of the class) which may be instantiated as objects. The first letter of a class name is usually uppercase to separate it from other kinds of variables". (http://processing.org/reference/class.html)

Now, I'll show how to create a Class, and an array of objects.

cuBEEc

A cubic bee

Fist of all, you need to create the class and the constructor:

class Bee { //The first letter must be UperCase
//declare the variables we'll need, or the properties
int angle; //angle that rotates the wings
int factor=30; //a crazy variable that I used to control the frequency of the wings
float posX, posY,posZ; //variables for the position of the bee


  Bee () {  //constructor

  } 


} 
 
And then, create functions and methods
class Bee { 
int angle;
int factor=30;
float posX, posY,posZ;


  Bee () {  
 
  } 
 void update(){ //function that updates the rotation of the wings
    angle+=factor;
  if(angle>80){
    factor=factor*-1;
  }
   if(angle<0){
     factor=factor*-1;
   }
   
 }
 
 void render(float x, float y,float z){ //function that draws the bee, receiving the parameters like position in x,y, and z axis;
  
  posX=x;
  posY=y; 
  posZ=z;
   

//rectangles and triangles that creates the bee
  pushMatrix();  
  noStroke();
  translate(posX,posY+30,posZ);
  pushMatrix();
  translate(15,0);
  pushMatrix();
  rotate(radians(angle));
  fill(80,180);
  rect(0,0,-30,8);
  popMatrix();
  pushMatrix();
  rotate(radians((angle)*-1));
  rect(0,0,30,8);
  popMatrix();
  
  popMatrix();
  fill(220,220,0);
  quad(30,0,35,5,35,25,30,30);
  fill(255,255,0);
  rect(0,0,30,30);
  fill(0);
  triangle(35,10,40,15,35,20);
  triangle(8,30,13,30,13,35);
  triangle(17,30,22,30,17,35);
  fill(0);
  rect(25,0,5,30);
  rect(0,0,5,30);
  popMatrix();
 } 
} 
Now, you have to declare and initialize the class:

int numBees=1;//number of bees
 Bee [] b = new Bee[numBees]; //an array of bees

 
 void setup() {
  size(1200,700,OPENGL);//size of the screen, using OpenGl rendering for Z axis translation

  frameRate(60);//frame rate of 60 frames per second
 
//using a "for" function to initialize all the bees 
 for (int i=0;i<numBees;i++){
    b[i]= new Bee ();

 }
 
 void draw(){
background(255);//white background

//a "for" function to call the functions of the class for all the bees
    for (int i=0;i<numBees;i++){
      b[i].update();//update wings rotation
    b[i].render(width/2,height/2,300); //draw the bees at the given positions (x,y,z)
  } 
   
 } 
I used this class to create a flocking of animated bees. But if you want a simpler way to move the bees randomly like insects, you can use perlin noise...