public SteeringBehavior(FlyingEntity agent) { _flyingEntity = agent; TargetAgent1 = null; TargetAgent2 = null; decelerationRate = DecelerationRate.NORMAL; SummingMethod = SumMethod.WEIGHTED_AVG; _path = new Path { Looped = true }; }
/// <summary> /// This behaviour will give return a steering force that will which will direct the agent to the TargetPosition. /// Unlike seek, this behaviour will decelerate when the the agent is close to the targetPosition. /// </summary> /// <param name="TargetPos"></param> /// <param name="deceleration"></param> /// <returns>Steering Force</returns> public Vector2D Arrive(Vector2D TargetPos, DecelerationRate deceleration) { if (_flyingEntity.Pos.x == TargetPos.x && _flyingEntity.Pos.y == TargetPos.y) { return(Vector2D.Zero); } ClampPositions(ref _flyingEntity, TargetPos); Vector2D ToTarget = TargetPos - _flyingEntity.Pos; // Calculate the distance to the target. double dist = ToTarget.Length(); if (dist > 0) { /* Because Deceleration is enumerated as an int, this value is required * to provide fine tweaking of the deceleration. */ const double DecelerationTweaker = 2.0; /* Calculate the speed required to reach the target given the desired * deceleration. */ double speed = dist / ((double)deceleration * DecelerationTweaker); // Make sure the velocity does not exceed the max. speed = Math.Min(speed, _flyingEntity.MaxSpeed); /* From here proceed just like Seek except we don't need to normalize * the ToTarget vector because we have already gone to the trouble * of calculating its length: dist. */ Vector2D DesiredVelocity = ToTarget * speed / dist; return(DesiredVelocity - _flyingEntity.Velocity); } return(new Vector2D(0, 0)); }