/// <summary> /// Given an entity and a container of nearby /// entities, this function checks to see if there is an overlap between /// entities. If there is, then the entities are moved away from each /// other /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="others"></param> public static void EnforceNonPenetrationContraint <T>(BaseGameEntity entity, List <T> others) where T : BaseGameEntity { //iterate through all entities checking for any overlap of bounding //radii for (int entityIndex = 0; entityIndex < others.Count; entityIndex++) { //make sure we don't check against this entity if (others[entityIndex] == entity) { continue; } //calculate the distance between the positions of the entities Vector2D ToEntity = entity.Position - others[entityIndex].Position; double DistFromEachOther = ToEntity.Length; //if this distance is smaller than the sum of their radii then this //entity must be moved away in the direction parallel to the //ToEntity vector double AmountOfOverLap = others[entityIndex].BoundingRadius + entity.BoundingRadius - DistFromEachOther; if (AmountOfOverLap >= 0) { //move the entity a distance away equivalent to the amount of overlap. entity.Position += ((ToEntity / DistFromEachOther) * AmountOfOverLap); } }//next entity }
public BaseGameEntity FindEntity(int id) { BaseGameEntity entity = null; if (_entityMap.ContainsKey(id)) { entity = _entityMap[id]; } return(entity); }
/// <summary> /// tests to see if an entity is overlapping any of a number of entities /// stored in a std container /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="others"></param> /// <param name="MinDistBetweenObstacles"></param> /// <returns></returns> public static bool Overlapped <T>(BaseGameEntity entity, List <T> others, double MinDistBetweenObstacles) where T : BaseGameEntity { foreach (BaseGameEntity otherEntity in others) { if (Geometry.TwoCirclesOverlapped(entity.Position, entity.BoundingRadius + MinDistBetweenObstacles, otherEntity.Position, otherEntity.BoundingRadius)) { return(true); } } return(false); }
public MovingEntity(Vector2D position, double radius, Vector2D velocity, double maxSpeed, Vector2D heading, double mass, Vector2D scale, double turnRate, double maxForce) : base(BaseGameEntity.GetNextId()) { Position = position; BoundingRadius = radius; Velocity = velocity; Scale = scale; _heading = heading; _maxSpeed = maxSpeed; _mass = mass; _maxTurnRate = turnRate; _side = _heading.Perp; _maxForce = maxForce; }
//---------------------------- DispatchMsg --------------------------- // // given a message, a receiver, a sender and any time delay, this function // routes the message to the correct agent (if no delay) or stores // in the message queue to be dispatched at the correct time //------------------------------------------------------------------------ public void DispatchMsg(TimeSpan delay, int sender, int receiver, int messagecode, object additionalInfo) { //get a pointer to the receiver BaseGameEntity receiverEntity = EntityManager.Instance.FindEntity(receiver); //make sure the receiver is valid if (receiverEntity == null) { //#ifdef SHOW_MESSAGING_INFO //debug_con << "\nWarning! No Receiver with ID of " << receiver << " found" << ""; //#endif } else { //create the telegram Telegram telegram = new Telegram(sender, receiver, messagecode, DateTime.Now, additionalInfo); //if there is no delay, route telegram immediately if (delay.TotalMilliseconds <= Telegram.SmallestDelay) { //#ifdef SHOW_MESSAGING_INFO //debug_con << "\nTelegram dispatched at time: " << TickCounter->GetCurrentFrame() // << " by " << sender << " for " << receiver // << ". Msg is " << msg << ""; //#endif //send the telegram to the recipient discharge(receiverEntity, telegram); } //else calculate the time when the telegram should be dispatched else { telegram.DispatchTime = DateTime.Now + delay; _priorityQueue.Enqueue(telegram, -delay.TotalMilliseconds); //#ifdef SHOW_MESSAGING_INFO //debug_con << "\nDelayed telegram from " << sender << " recorded at time " // << TickCounter->GetCurrentFrame() << " for " << receiver // << ". Msg is " << msg << ""; //#endif } } }
/// <summary> /// tags any entities contained in a std container that are within the /// radius of the single entity parameter /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="others"></param> /// <param name="radius"></param> public static void TagNeighbors <T>(BaseGameEntity entity, List <T> others, double radius) where T : BaseGameEntity { foreach (BaseGameEntity baseObject in others) { //first clear any current tag baseObject.BooleanTag = false; //work in distance squared to avoid sqrts Vector2D to = baseObject.Position - entity.Position; //the bounding radius of the other is taken into account by adding it //to the range double range = radius + baseObject.BoundingRadius; //if entity within range, tag for further consideration if ((baseObject != entity) && (to.LengthSquared < range * range)) { baseObject.BooleanTag = true; } } }
public void DispatchDelayedMessages() { //now peek at the queue to see if any telegrams need dispatching. //remove all telegrams from the front of the queue that have gone //past their sell by date while (_priorityQueue.Count > 0 && _priorityQueue.Peek().Value.DispatchTime >= DateTime.Now && _priorityQueue.Peek().Value.DispatchTime != DateTime.MinValue) { //read the telegram from the front of the queue Telegram telegram = _priorityQueue.Dequeue().Value; //find the recipient BaseGameEntity receiver = EntityManager.Instance.FindEntity(telegram.Receiver); //#ifdef SHOW_MESSAGING_INFO //debug_con << "\nQueued telegram ready for dispatch: Sent to " // << pReceiver->ID() << ". Msg is "<< telegram.Msg << ""; //#endif //send the telegram to the recipient discharge(receiver, telegram); } }
/// <summary> /// tests to see if an entity is overlapping any of a number of entities /// stored in a std container /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="entity"></param> /// <param name="conOb"></param> /// <returns></returns> public static bool Overlapped <T>(BaseGameEntity entity, List <T> conOb) where T : BaseGameEntity { return(Overlapped(entity, conOb, 40.0)); }
public void RegisterEntity(BaseGameEntity entity) { _entityMap.Add(entity.ObjectId, entity); }
//this method is utilized by DispatchMsg or DispatchDelayedMessages. //This method calls the message handling member function of the receiving //entity, pReceiver, with the newly created telegram private void discharge(BaseGameEntity receiver, Telegram telegram) { receiver.HandleMessage(telegram); }