Beispiel #1
0
 public void act(double time) {
   // our default is to move forward in a straight line
   double forwardSpeed = 0.024; // 24mm per second straight ahead
   double lateralSpeed = 0.0; // Ants *can* move laterally, but ours won't for now
   double turningSpeed = 0.0; // no rotational velocity by default
   // get a vector towards the nearest thing so we can avoid it
   MutableDouble2D ant = new MutableDouble2D();
   boolean sawAnt = antBody.getNearestSameTypeVec(ant);
   MutableDouble2D wall = new MutableDouble2D();
   boolean sawWall = antBody.getNearestObstacleVec(wall);
   MutableDouble2D avoidPoint = null;
   if (!sawWall && !sawAnt) {
     avoidPoint = null;
   } else if (!sawAnt) {
     avoidPoint = wall;
   } else if (!sawWall) {
     avoidPoint = ant;
   } else {
     avoidPoint = (ant.lengthSq() < wall.lengthSq()) ? ant : wall;
   }
   if (avoidPoint != null) {
     if (avoidPoint.y > 0) turningSpeed = -40.0 * (Math.PI / 180.0);
     else turningSpeed = 40.0 * (Math.PI / 180.0);
   }
   antBody.setDesiredVelocity(forwardSpeed, lateralSpeed, turningSpeed);
 }
Beispiel #2
0
  public void step(SimState state) {
    Students students = (Students) state;
    Continuous2D yard = students.yard;

    Double2D me = students.yard.getObjectLocation(this);

    MutableDouble2D sumForces = new MutableDouble2D();

    // add in a vector to the "teacher" -- the center of the yard, so we don't go too far away
    sumForces.addIn(
        new Double2D(
            (yard.width * 0.5 - me.x) * students.forceToSchoolMultiplier,
            (yard.height * 0.5 - me.y) * students.forceToSchoolMultiplier));

    // add a bit of randomness
    sumForces.addIn(
        new Double2D(
            students.randomMultiplier * (students.random.nextDouble() * 1.0 - 0.5),
            students.randomMultiplier * (students.random.nextDouble() * 1.0 - 0.5)));

    sumForces.addIn(me);

    students.yard.setObjectLocation(this, new Double2D(sumForces));
  }