class Creature { //** THIS STUFF IS COMING FROM VECTOR3D CLASS**// Vector3D loc; Vector3D vel; Vector3D acc; float r; //**FITNESS AND DNA**// float fitness; DNA genes; boolean stopped; // understand whether we are stucked. int finish; // what was my finish place //CONSTRUCTOR Creature(Vector3D l, DNA dna, int f) { acc = new Vector3D(0.0,0.0,0.0); // acceleration vel = new Vector3D(0.0,0.0,0.0); // velocity loc = l.copy(); r = 10.0f; genes = dna; stopped = false; finish = f; } // **** FITNESS FUNCTION **** // // distance = distance from target // finish = what order did I finish // f(distance, finish) = (1.0f / finish^1.5)*(1.0f / distance^6); // a lower finish is rewarded (exponentially) and/or shorter distance to target void calcFitness() { float d = v3d.distance(loc,target); if(d < diam /2) { d = 1.0f; } // reward finishing faster and getting closer fitness = (1.0f / pow(finish, 1.5f)) * (1.0f / (pow(d,6))); } void setFinish(int f) { finish = f; } // run in relation to all the obstacle // if I am stuck don't bother updating or checking for intersection void run(ArrayList o) { if(!stopped) { update(); // if I hit an edge or an obstacle if ((borders())) { stopped = true; } } //draw me render(); } // EDGES FUNCTION boolean borders() { if((loc.x() < 0) || (loc.y() < 0) || (loc.y() > width) || (loc.y() > height)) { return true; } else { return false; } } // DID I MAKE IT TO THE TARGET** // boolean finished() { float d = v3d.distance(loc,target); if(d< diam/2) { stopped = true; return true; } return false; } void update() { if(!finished()) { // where are we? Our location will tell us what steering vector to look up in our DNA int x = (int) loc.x()/gridscale; int y = (int) loc.y()/gridscale; x = constrain(x,0,width/gridscale-1); // make sure we are not off the edge y = constrain(y,0,height/gridscale-1); //Get the steering vector out of our genes in the right spot acc.add(genes.getGene(x+y*width/gridscale)); // this is the stuff from vector3D acc.mult(maxforce); vel.add(acc); vel.limit(maxspeed); loc.add(vel); acc.setXYZ(0,0,0); } } void render() { //draws a triangle rotated in the direction of velocity float theta = vel.heading2D() + radians(180); if(stopped) fill(255,25); else fill(255); noStroke(); pushMatrix(); translate(loc.x(),loc.y()); rotateZ(theta); fill(255,0,0,1); beginShape(TRIANGLES); vertex(0, -r*0.1); vertex(-r, r*0.1); vertex(r, r*0.1); endShape(); popMatrix(); } float getFitness() { return fitness; } DNA getGenes() { return genes; } boolean stopped() { return stopped; } }