1)寻找:
public function seek(target: Vector2D): void
{
var desiredVelocity: Vector2D = target.subtract(_position);
desiredVelocity.normalize();
desiredVelocity = desiredVelocity.multiply(_maxSpeed);
var force: Vector2D = desiredVelocity.subtract(_velocity);
_steeringForce = _steeringForce.add(force);
}
2)避开:
public function flee(target: Vector2D): void
{
var desiredVelocity: Vector2D = target.subtract(_position);
desiredVelocity.normalize();
desiredVelocity = desiredVelocity.multiply(_maxSpeed);
var force: Vector2D = desiredVelocity.subtract(_velocity);
_steeringForce = _steeringForce.subtract(force);
}
3)到达:
public function arrive(target: Vector2D): void
{
var desiredVelocity: Vector2D = target.subtract(_position);
desiredVelocity.normalize();
var dist: Number = _position.dist(target);
if(dist > _arrivalThreshold)
{
desiredVelocity = desiredVelocity.multiply(_maxSpeed);
}
else
{
desiredVelocity = desiredVelocity.multiply(_maxSpeed * dist /
_arrivalThreshold);
}
var force: Vector2D = desiredVelocity.subtract(_velocity);
_steeringForce = _steeringForce.add(force);
}
4)追捕:
public function pursue(target: Vehicle): void
{
var lookAheadTime: Number = position.dist(target.position) / _maxSpeed;
var predictedTarget: Vector2D =
target.position.add(target.velocity.multiply(lookAheadTime));
seek(predictedTarget);
}
5)躲避:
public function evade(target: Vehicle): void
{
var lookAheadTime: Number = position.dist(target.position) / _maxSpeed;
var predictedTarget: Vector2D =
target.position.add(target.velocity.multiply(lookAheadTime));
flee(predictedTarget);
}
6)漫游:
init (){
private var _wanderAngle: Number = 0;
private var _wanderDistance: Number = 10;
private var _wanderRadius: Number = 5;
private var _wanderRange: Number = 1;
}
public function wander(): void
{
var center: Vector2D = velocity.clone().normalize().multiply(_wanderDistance);
var offset: Vector2D = new Vector2D(0);
offset.length = _wanderRadius;
offset.angle = _wanderAngle;
_wanderAngle += Math.random() * _wanderRange - _wanderRange * .5;
var force: Vector2D = center.add(offset);
_steeringForce = _steeringForce.add(force);
}