///<summary> ///steering behavior constructor ///</summary> ///<param name="agent"></param> public Steering(BotEntity agent) { _bot = agent; Flags = 0; _weightSeparation = GameManager.GameManager.Instance.Parameters.SeparationWeight; _weightWander = GameManager.GameManager.Instance.Parameters.WanderWeight; _weightWallAvoidance = GameManager.GameManager.Instance.Parameters.WallAvoidanceWeight; _viewDistance = GameManager.GameManager.Instance.Parameters.ViewDistance; _wallDetectionFeelerLength = GameManager.GameManager.Instance.Parameters.WallDetectionFeelerLength; Feelers = new List<Vector2>(3); _deceleration = Decelerations.Normal; TargetAgent1 = null; TargetAgent2 = null; _wanderDistance = WANDER_DIST; _wanderJitter = WANDER_JITTER_PER_SEC; _wanderRadius = WANDER_RAD; _weightSeek = GameManager.GameManager.Instance.Parameters.SeekWeight; _weightArrive = GameManager.GameManager.Instance.Parameters.ArriveWeight; CellSpaceIsOn = true; SummingMethod = SummingMethods.Prioritized; //stuff for the wander behavior float theta = RandomUtil.RandomFloat()*(float) Math.PI*2.0f; //create a vector to a target position on the wander circle WanderTarget = new Vector2(WanderRadius*(float) Math.Cos(theta), WanderRadius*(float) Math.Sin(theta)); }
///<summary> ///This behavior is similar to seek but it attempts to arrive at the ///target with a zero velocity ///</summary> ///<param name="target"></param> ///<param name="deceleration"></param> ///<returns></returns> public Vector2 Arrive(Vector2 target, Decelerations deceleration) { Vector2 toTarget = target - Bot.Position; //calculate the distance to the target float dist = toTarget.Length(); if (dist > 0) { //because Decelerations is enumerated as an int, this value is //required to provide fine tweaking of the deceleration.. const float decelerationTweaker = 0.3f; //TODO: should be a parameter //calculate the speed required to reach the target given the //desired deceleration float speed = dist/((float) deceleration*decelerationTweaker); //make sure the velocity does not exceed the max speed = Math.Min(speed, Bot.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. Vector2 desiredVelocity = toTarget*speed/dist; return (desiredVelocity - Bot.Velocity); } return Vector2.Zero; }