public int compare(Particle p1, Particle p2) { float val1 = PVector.dist(p1.pos, arbitraryPos); float val2 = PVector.dist(p2.pos, arbitraryPos); if (val1 < val2) { return -1; } if (val1 > val2) { return 1; } return 0; }
public void avoid(ArrayList obstacles) { // Make a vector that will be the position of the object // relative to the Boid rotated in the direction of boid's velocity PVector closestRotated = new PVector(sight + 1, sight + 1); float closestDistance = 99999; Obstacle avoid = null; // Let's look at each obstacle for (int i = 0; i < obstacles.size(); i++) { Obstacle o = (Obstacle) obstacles.get(i); float d = PVector.dist(loc, o.loc); PVector dir = vel.get(); dir.normalize(); PVector diff = PVector.sub(o.loc, loc); // Now we use the dot product to rotate the vector that points from boid to obstacle // Velocity is the new x-axis PVector rotated = new PVector(diff.dot(dir), diff.dot(getNormal(dir))); // Is the obstacle in our path? if (PApplet.abs(rotated.y) < (o.radius + r)) { // Is it the closest obstacle? if ((rotated.x > 0) && (rotated.x < closestRotated.x)) { closestRotated = rotated; avoid = o; } } } // Can we actually see the closest one? if (PApplet.abs(closestRotated.x) < sight) { // The desired vector should point away from the obstacle // The closer to the obstacle, the more it should steer PVector desired = new PVector(closestRotated.x, -closestRotated.y * sight / closestRotated.x); desired.normalize(); desired.mult(closestDistance); desired.limit(maxspeed); // Rotate back to the regular coordinate system rotateVector(desired, vel.heading2D()); // Draw some debugging stuff if (debug) { stroke(0); line(loc.x, loc.y, loc.x + desired.x * 10, loc.y + desired.y * 10); avoid.highlight(true); } // Apply Reynolds steering rules desired.sub(vel); desired.limit(maxforce); acc.add(desired); } }
// TODO: passar métodos de comer para métodos nas classes correspondentes!! // método para consumir comida! diferente de caçar comida!! void eat(ArrayList<Food> theFood) { for (Food f : theFood) { float d = PVector.dist(bodyCopy.loc, f.loc); if (d < 20) { // TODO: implementar métodos de parar para comer, (é uma // decisão). } } }
// se outro é predator public void beHunted(ArrayList<Creature> creatures) { for (Creature other : creatures) { float d = PVector.dist(loc, other.loc); if (((d > 0) && (d < 40)) && (other.isPredator == true && isPredator == false)) { if (other.isHunting) { fight(other); bodyCopy.fleeFromEnemy(other); } } } }
public void draw() { /*---Update---*/ world.update(); worm.update(); for (int i = 0; i < extraNum; i++) { extraWorms[i].update(); } /*---Draw--*/ // Lights lights(); /* ambientLight(128f, 128f, 128f); directionalLight(128f, 128f, 128f, 0f, 0f, -1f); lightFalloff(1f, 0f, 0f); lightSpecular(0f, 0f, 0f); */ // Camera perspective(fov, aspect, near, far); // Calculate camera position if (PVector.dist(worm.getPosition(), eye) > eyeDist) { eye = PVector.sub(eye, worm.getPosition()); eye.normalize(); eye.mult(eyeDist); eye.add(worm.getPosition()); } camera( eye.x, eye.y, eye.z, // eyeX, eyeY, eyeZ (camera position) worm.getPosition().x, worm.getPosition().y, worm.getPosition().z, // centerX, centerY, centerZ (look at) 0f, 1f, 0f); // upX, upY, upZ // Action world.draw(); worm.draw(); for (int i = 0; i < extraNum; i++) { extraWorms[i].draw(); } // Drama! }
private void calcCam(boolean is3D) { if (rotVector.mag() != 0 || panVector.mag() != 0 || camDist != prevCamDist) { prevCamDist = camDist; camRot += rotVector.x; if ((camPitch + rotVector.y) < 0.99 && (camPitch + rotVector.y) > 0.01) camPitch += rotVector.y; if (is3D) { PVector camPan = new PVector( (panVector.x * (float) Math.sin(Math.PI * 0.5 + (Math.PI * camRot))) + (panVector.y * (float) Math.sin(Math.PI * 1.0 + (Math.PI * camRot))), (panVector.y * (float) Math.cos(Math.PI * 1.0 + (Math.PI * camRot))) + (panVector.x * (float) Math.cos(Math.PI * 0.5 + (Math.PI * camRot)))); camPan.mult(0.05f * PVector.dist(camPos, camCenter)); camCenter.x += camPan.x; camCenter.y += camPan.y; camPan.x = 0; camPan.y = 0; } else { camCenter.x -= 0.05f * (camPos.z - camCenter.z) * panVector.x; camCenter.y += 0.05f * (camPos.z - camCenter.z) * panVector.y; } panVector.x = 0; panVector.y = 0; rotVector.x = 0; rotVector.y = 0; camPos.x = camCenter.x - camDist * (float) Math.sin(Math.PI * camRot) * (float) Math.sin(Math.PI * camPitch); camPos.y = camCenter.y - camDist * (float) Math.cos(Math.PI * camRot) * (float) Math.sin(Math.PI * camPitch); camPos.z = camCenter.z - camDist * (float) Math.cos(Math.PI * camPitch); } }
public void setPositions(PVector pos1, PVector pos2) { this.pos1 = pos1; this.pos2 = pos2; this.height = pos1.dist(pos2); }