// 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; }