コード例 #1
0
    void wander() {
      float wanderR = 10.0f; // radius for our "wander circle"
      float wanderD = 20.0f; // distance for our "wander circle"
      wandertheta += random(-1, 1); // randomly changet wander theta

      // now we have to calculate the new location to steer towards on the wander circle
      Vector3D v = vel.copy();
      v.normalize(); // our heading
      float xoff =
          wanderD * v.x() + wanderR * cos(wandertheta); // x spot on circle based on heading
      float yoff =
          wanderD * v.y() + wanderR * sin(wandertheta); // y spot on circle based on heading
      Vector3D target = Vector3D.add(loc, new Vector3D(xoff, yoff)); // add the location
      acc.add(steer(target, false)); // steer towards it
    }
コード例 #2
0
 // a method that calculates a steering vector towards a target
 // takes a second argument, if true, it slows down as it approaches the target
 Vector3D steer(Vector3D target, boolean slowdown) {
   Vector3D steer; // the steering vector
   Vector3D desired =
       Vector3D.sub(target, loc); // a vector pointing from the location to the target
   float d = desired.magnitude(); // distance from the target is the magnitude of the vector
   // if the distance is greater than 0, calc steering (otherwise return zero vector)
   if (d > 0) {
     // normalize desired
     desired.normalize();
     // two options for magnitude (1 -- based on distance, 2 -- maxspeed)
     if ((slowdown) && (d < 100.0f)) desired.mult(maxspeed * (d / 100.0f));
     else desired.mult(maxspeed);
     // STEERING VECTOR IS DESIREDVECTOR MINUS VELOCITY
     steer = Vector3D.sub(desired, vel);
     steer.limit(maxforce); // limit to maximum steering force
   } else {
     steer = new Vector3D(0, 0);
   }
   return steer;
 }