// a class containing all the features and // control structures of a spermie class spurmie { renderspurmie display; Position myPos; Position myVector; Position mySpeed; float sgnX = 1; float sgnY= 1; spurmie(float xPos, float yPos) { myPos = new Position(xPos,yPos); myVector = new Position(0,0); float sp = random(0.03,0.3); mySpeed = new Position(sp,sp); myPos.setEdgeOnOff(true); display = new renderspurmie(color(128,192,0)); sgnX = random(-1,1) > 0 ? 1 :-1; sgnY = random(-1,1) > 0 ? 1 :-1; } void update() { //display.eraseAt(myPos,myVector); recalcMyPos(); display.drawAt(myPos,myVector); } void recalcMyPos() { Position goalPos; if(daynight.IsDay()) { goalPos = daynight.getSunPos(); } else { float y = myPos.getY() + 0.05; float x = myPos.getX() + 0.05; goalPos = new Position(sgnX*x,sgnY*y); } float hyp = sqrt(sq( myPos.getX()-goalPos.getX()) + sq( myPos.getY()-goalPos.getY())); float adj = goalPos.getX() - myPos.getX(); float opp = goalPos.getY() - myPos.getY(); myVector.setX( adj/hyp ); myVector.setY( opp /hyp ); myPos.setX(myPos.getX() + mySpeed.getX()*myVector.getX()); myPos.setY(myPos.getY() + mySpeed.getY()*myVector.getY()); } } class renderspurmie { color myColor; int radius = 4; int tailLen = 24; renderspurmie(color c) { myColor = c; } void drawAt(Position pos, Position vect) { noStroke(); fill(myColor); ellipse(pos.getX(),pos.getY(),radius*2,radius*2); stroke(myColor); drawTail(pos,vect); //line(pos.getX()+radius,pos.getY(),pos.getX()+radius+tailLen,pos.getY()); } void eraseAt(Position pos, Position vect) { noStroke(); fill(daynight.currentSky()); ellipse(pos.getX(),pos.getY(),radius*2,radius*2); stroke(daynight.currentSky()); drawTail(pos,vect); //line(pos.getX()+radius, pos.getY(),pos.getX()+radius+tailLen,pos.getY()); } void drawTail(Position pos, Position vect) { float startX = pos.getX() - radius*vect.getX(); float startY = pos.getY() - radius*vect.getY(); float endX = pos.getX()-(radius+tailLen)*vect.getX(); float endY= pos.getY()-(radius+tailLen)*vect.getY(); line(startX,startY,endX,endY); } }