Example #1
0
        public bool CollidesWithTile(IPhysicalEntity physicalEntity)
        {
            List <Vector2I> coverTilesCoordinates = physicalEntity.CoverTiles();
            bool            colliding             = !coverTilesCoordinates.TrueForAll(x => !m_atlas.ContainsCollidingTile(x));

            return(colliding);
        }
 public bool Collides(IPhysicalEntity physicalEntity)
 {
     return(Collides(new List <IPhysicalEntity>()
     {
         physicalEntity
     }) > 0);
 }
Example #3
0
        public bool CollidesWithPhysicalEntity(IPhysicalEntity physicalEntity)
        {
            var circle = new Circle(physicalEntity.Position, 2 * MaximumGameObjectRadius);
            List <IPhysicalEntity> physicalEntities = m_objectLayer.GetPhysicalEntities(circle);

            return(physicalEntities.Any(physicalEntity.CollidesWith));
        }
        public bool CollidesWith(IPhysicalEntity physicalEntity)
        {
            if (physicalEntity == this)
            {
                return(false);
            }
            var collidesWith = Shape.CollidesWith(physicalEntity.Shape);

            return(collidesWith);
        }
        public bool CollidesWithPhysicalEntity(IPhysicalEntity physicalEntity)
        {
            if (!physicalEntity.ElasticCollision && !physicalEntity.InelasticCollision)
            {
                return(false);
            }
            var circle = new Circle(physicalEntity.Position, 2 * MaximumGameObjectRadius);
            List <IPhysicalEntity> physicalEntities = m_objectLayer.GetPhysicalEntities(circle);

            return(physicalEntities.Any(physicalEntity.CollidesWith));
        }
        public bool CollidesWithTile(IPhysicalEntity physicalEntity)
        {
            if (!physicalEntity.ElasticCollision && !physicalEntity.InelasticCollision)
            {
                return(false);
            }
            List <Vector2I> coverTilesCoordinates = physicalEntity.CoverTiles();
            bool            colliding             = !coverTilesCoordinates.TrueForAll(x => !m_atlas.ContainsCollidingTile(x));

            return(colliding);
        }
Example #7
0
        /// <summary>
        /// Main process for inserting objects to the quad trees
        /// Splits the tree if the maximum objects is exceeded
        /// </summary>
        /// <param name="_obj"> Collidable object to add to the tree </param>
        public void Insert(ICollidable _obj)
        {
            // Cast the target object to an IPhysicalEntity to access it's location and size
            IPhysicalEntity _entity = _obj as IPhysicalEntity;
            Rectangle       _rect   = new Rectangle((int)_entity.Position.X, (int)_entity.Position.Y, _entity.CurrentTextureWidth, _entity.CurrentTextureHeight);


            if (mNodes[0] != null)
            {
                // Check which subnode the object should be placed in
                int _index = GetIndex(_rect);

                // If not this one, then call the respective subnode's Insert method
                if (_index != -1)
                {
                    mNodes[_index].Insert(_obj);
                    return;
                }
            }

            // Add the object to the list
            mObjects.Add(_obj);

            // Check if this node is full
            // Create a new subnode and move objects if required
            if (mObjects.Count > mMaxObjs && mLevel < mMaxLevels)
            {
                if (mNodes[0] == null)
                {
                    Split();
                }

                int i = 0;
                while (i < mObjects.Count)
                {
                    // Cast the target object to an IPhysicalEntity to access it's location and size
                    IPhysicalEntity _ent = mObjects[i] as IPhysicalEntity;
                    Rectangle       _rec = new Rectangle((int)_ent.Position.X, (int)_ent.Position.Y, _ent.CurrentTextureWidth, _ent.CurrentTextureHeight);

                    int _index = GetIndex(_rec);
                    if (_index != -1)
                    {
                        // Add the object to its respective subnode and remove from the current node
                        mNodes[_index].Insert(mObjects[i]);
                        mObjects.Remove(mObjects[i]);
                    }
                    else
                    {
                        i++;
                    }
                }
            }
        }
Example #8
0
        /// <summary>
        /// Create Textured Entity
        /// </summary>
        /// <typeparam name="T"> Type of IEntity to create </typeparam>
        /// <param name="_path"> Texture Path for the new entity</param>
        /// <returns> IEntity of new entity </returns>
        public IEntity newEntity <T>(string _path) where T : IEntity, new()
        {
            // Call parent to create Entity type
            IEntity _createdEntity = newEntity <T>();
            // Change the Entity to a PhysicalEntity
            IPhysicalEntity _entity = _createdEntity as IPhysicalEntity;

            //// Call the Content Manager to apply the entities texture
            //_entity.Texture = mContentManager.ApplyTexture(_path);
            setEntityVars(_entity, _path);
            // Return Created Entity
            return(_createdEntity);
        }
Example #9
0
    public static IEnumerator Do(IPhysicalEntity physical, float angle, float duration)
    {
        var   currentRotation = physical.Rotation;
        var   targetRotation  = Quaternion.Euler(0, angle, 0) * currentRotation;
        float timeCounter     = 0f;
        float speed           = duration;

        while (timeCounter < speed)
        {
            physical.Rotation = Quaternion.Lerp(currentRotation, targetRotation, timeCounter / speed);
            timeCounter      += Time.deltaTime;
            yield return(null);
        }
    }
Example #10
0
        private void SetSomePositionAround(IPhysicalEntity physicalEntity)
        {
            // search in spirals
            float a     = 0.1f;
            float tDiff = MathHelper.ToRadians(10);
            float t     = 0;

            do
            {
                t += tDiff;
                physicalEntity.Position = new Vector2(
                    a * t * (float)Math.Cos(t) + physicalEntity.Position.X,
                    a * t * (float)Math.Sin(t) + physicalEntity.Position.Y);
            } while (m_collisionChecker.Collides(physicalEntity));
        }
Example #11
0
 public override void Reactivate()
 {
     base.Reactivate();
     mAnimation.Frames        = 8;
     mAnimation.Columns       = 8;
     mMind.MySelf.Texturename = "Characters/OldMan/walkR";
     // Find the nurse object
     foreach (KeyValuePair <int, IEntity> _keyPair in mMind.MySelf.ServiceLocator.GetService <IEntity_Manager>().AllEntities)
     {
         if (_keyPair.Value.Name == "nurse")
         {
             mNurse = _keyPair.Value as IPhysicalEntity;
         }
     }
 }
Example #12
0
        private void ResolveCollisionsWithTiles(List <IPhysicalEntity> collisionGroup, List <Vector2> collisionPositions)
        {
            for (int i = 0; i < collisionGroup.Count; i++)
            {
                IPhysicalEntity physicalEntity = collisionGroup[i];
                Vector2         origPos        = physicalEntity.Position;
                physicalEntity.Position = collisionPositions[i];
                var collidesWithTile = m_collisionChecker.CollidesWithTile(physicalEntity);
                physicalEntity.Position = origPos;

                if (collidesWithTile)
                {
                    ResolveTileCollision((IForwardMovablePhysicalEntity)physicalEntity);
                }
            }
        }
Example #13
0
        public static List <Tuple <IPhysicalEntity, IPhysicalEntity> > CollidingCouples(List <IPhysicalEntity> physicalEntities)
        {
            List <Tuple <IPhysicalEntity, IPhysicalEntity> > collidingCouples = new List <Tuple <IPhysicalEntity, IPhysicalEntity> >();

            for (int i = 0; i < physicalEntities.Count - 1; i++)
            {
                IPhysicalEntity firstEntity = physicalEntities[i];
                for (int j = i + 1; j < physicalEntities.Count; j++)
                {
                    IPhysicalEntity secondEntity = physicalEntities[j];
                    if (firstEntity.CollidesWith(secondEntity))
                    {
                        collidingCouples.Add(new Tuple <IPhysicalEntity, IPhysicalEntity>(firstEntity, secondEntity));
                    }
                }
            }
            return(collidingCouples);
        }
Example #14
0
        public List <IGameObject> GetGameObjects(RectangleF rectangle)
        {
            List <IGameObject> list = new List <IGameObject>();

            foreach (IGameObject o in GameObjects)
            {
                var             gameObject     = (GameObject)o;
                IPhysicalEntity physicalEntity = gameObject.PhysicalEntity;
                RectangleF      r;
                RectangleF      cover = physicalEntity.CoverRectangle();
                RectangleF.Intersect(ref cover, ref rectangle, out r);
                if (r.Size.Length() > 0f)
                {
                    list.Add(gameObject);
                }
            }

            return(list);
        }
Example #15
0
        public override void Resolve(GameActorPosition target, IAtlas atlas)
        {
            ICanPickGameObject picker    = Sender as ICanPickGameObject;
            ICharacter         character = Sender as ICharacter;

            if (picker == null || character == null)
            {
                return;
            }
            Vector2 positionInFrontOf = target.Position;

            // solving case when positions where character should place tile collides with character's position
            if (target.Actor is Tile)
            {
                Tile            tile               = (target.Actor as Tile);
                IPhysicalEntity physicalEntity     = tile.GetPhysicalEntity(new Vector2I(positionInFrontOf));
                bool            collidesWithSource = physicalEntity.CollidesWith(character.PhysicalEntity);
                if (collidesWithSource)
                {
                    /*  <- change to //* to switch
                     * // to the center of current tile
                     * character.Position = Vector2.Floor(character.Position) + Vector2.One/2;
                     *
                     * /*/
                    // back WRT his direction
                    do
                    {
                        character.Position = Physics.Utils.Move(character.Position, character.Direction, -0.01f);
                    } while (physicalEntity.CollidesWith(character.PhysicalEntity));
                    // */
                }
            }

            GameActorPosition toLayDown = new GameActorPosition(target.Actor, positionInFrontOf, target.Layer);

            bool added = atlas.Add(toLayDown, true);

            if (added)
            {
                picker.RemoveFromInventory();
            }
        }
Example #16
0
        /// <summary>
        /// Main Method for accessing the quadtree process
        /// </summary>
        /// <param name="_objects"> List of objects, either from the Collision Manager as new, or from the previous layer </param>
        /// <param name="_target"> Target object to check against </param>
        /// <returns> List of all objects in the same tree as the specified target object </returns>
        public List <ICollidable> Retrieve(List <ICollidable> _objects, ICollidable _target)
        {
            // Cast the target object to an IPhysicalEntity to access it's location and size
            IPhysicalEntity _entity = _target as IPhysicalEntity;
            Rectangle       _rect   = new Rectangle((int)_entity.Position.X, (int)_entity.Position.Y, _entity.CurrentTextureWidth, _entity.CurrentTextureHeight);

            // Calculate which quadtree the target obejct is in
            int _index = GetIndex(_rect);

            // If the target object is in a subtree, pass the detection off to that subtree
            if (_index != -1 && mNodes[0] != null)
            {
                mNodes[_index].Retrieve(_objects, _target);
            }

            // Add all of this subtree's objects to the list
            _objects.AddRange(mObjects);

            // Return list to the next layer up/Collision manager
            return(_objects);
        }
Example #17
0
        private void SetSomePositionAround(IPhysicalEntity physicalEntity)
        {
            Vector2 originalPosition = physicalEntity.Position;
            // search in spirals
            const float a     = 0.001f;
            const float tDiff = MathHelper.Pi / 180; // one degree step
            float       t     = 0;

            do
            {
                t += tDiff;
                physicalEntity.Position = new Vector2(
                    a * t * (float)Math.Cos(t) + physicalEntity.Position.X,
                    a * t * (float)Math.Sin(t) + physicalEntity.Position.Y);
            } while (m_collisionChecker.Collides(physicalEntity) && t < 1000);

            if (t >= 1000)
            {
                physicalEntity.Position = originalPosition;
                Log.Instance.Warn(GetType().Name + " cannot place one of objects on position " + physicalEntity.Position + ".");
            }
        }
 public bool Collides(IPhysicalEntity physicalEntity)
 {
     return Collides(new List<IPhysicalEntity>() {physicalEntity}) > 0;
 }
 public List <IPhysicalEntity> CollisionThreat(IPhysicalEntity targetEntity, List <IPhysicalEntity> physicalEntities, float eps = 0)
 {
     return(new List <IPhysicalEntity>());
 }
 public bool CollidesWithTile(IPhysicalEntity physicalEntity)
 {
     return(physicalEntity.Position.X < 0);
 }
 public bool CollidesWithPhysicalEntity(IPhysicalEntity physicalEntity)
 {
     throw new NotImplementedException();
 }
 public bool CollidesWithTile(IPhysicalEntity physicalEntity, float eps)
 {
     return(physicalEntity.Position.X - eps < 0);
 }
Example #23
0
 public bool CollidesWith(IPhysicalEntity physicalEntity)
 {
     return(Shape.CollidesWith(physicalEntity.Shape));
 }
 public bool CollidesWithPhysicalEntity(IPhysicalEntity physicalEntity)
 {
     throw new NotImplementedException();
 }
Example #25
0
 internal PhysicsObject(IPhysicalEntity handle)
 {
     NativeHandle = handle;
 }
Example #26
0
 /// <summary>
 /// Set Core Entity Variables for Physical Entities
 /// </summary>
 /// <param name="_entity">Entity to set up</param>
 /// <param name="_path">Path to Entities Texture</param>
 private void setEntityVars(IPhysicalEntity _entity, string _path)
 {
     // Set the entities Texture Path
     _entity.setVars(_path, mContentManager);
 }
 public bool CollidesWithTile(IPhysicalEntity physicalEntity)
 {
     return physicalEntity.Position.X < 0;
 }
 private void SetSomePositionAround(IPhysicalEntity physicalEntity)
 {
     // search in spirals
     float a = 0.1f;
     float tDiff = MathHelper.ToRadians(10);
     float t = 0;
     do
     {
         t += tDiff;
         physicalEntity.Position = new Vector2(
             a*t*(float) Math.Cos(t) + physicalEntity.Position.X,
             a*t*(float) Math.Sin(t) + physicalEntity.Position.Y);
     } while (m_collisionChecker.Collides(physicalEntity));
 }
 public bool CollidesWith(IPhysicalEntity physicalEntity)
 {
     return Shape.CollidesWith(physicalEntity.Shape);
 }
 public List<IPhysicalEntity> CollisionThreat(IPhysicalEntity targetEntity, List<IPhysicalEntity> physicalEntities, float eps = 0)
 {
     return new List<IPhysicalEntity>();
 }
Example #31
0
 public bool CollidesWithTile(IPhysicalEntity physicalEntity)
 {
     List<Vector2I> coverTilesCoordinates = physicalEntity.CoverTiles();
     bool colliding = !coverTilesCoordinates.TrueForAll(x => !m_atlas.ContainsCollidingTile(x));
     return colliding;
 }
Example #32
0
        /// <summary>
        /// Collision Detection - Setup subroutine
        /// First compile a list of all normalized Normals of each object to test
        /// Runs through all entities in the list
        /// </summary>
        public void CollisionDetec()
        {
            mAbort = false;
            // Update the quad tree to line up with the camera's current position
            mQuadtree.UpdatePosition(new Rectangle(Global.Camera.TLPosition.ToPoint(), new Point(Global.Camera.ViewPortWidth, Global.Camera.ViewPortHeight)));
            // Iterate through all the current Entities
            foreach (KeyValuePair <int, IEntity> _keypair in mEntityManager.AllEntities)
            {
                // try cast the current Dictionary entry as a collidable
                var asInterface = _keypair.Value as ICollidable;
                // If successful, add it to the local list
                if (asInterface != null && _keypair.Value.GetState())
                {
                    mCollidables.Add(asInterface.UID, asInterface);
                    // Add to the quadtree
                    mQuadtree.Insert(asInterface);
                }

                // If needed uncomment to store the player seperately
                //else
                //{
                //    var asInterface = _keypair.Value as IPlayer;
                //    if (asInterface != null)
                //        mPlayer = asInterface
                //}
            }

            // Iterate through all objects in the collidable list
            foreach (KeyValuePair <int, ICollidable> _keypair in mCollidables)
            {
                // Initialize a second list
                List <ICollidable> _nearbyObjs = new List <ICollidable>();

                // Broad phase collision detection - Quadtree
                // Get all the objects in the same quadtree as this object
                _nearbyObjs = mQuadtree.Retrieve(_nearbyObjs, _keypair.Value);

                foreach (ICollidable _obj in _nearbyObjs)
                {
                    ICollidable objA = _keypair.Value;
                    ICollidable objB = _obj;
                    // Check not colliding with self
                    if (objA != objB)
                    {
                        // Mid phase collision detection - Intersecting rectangles
                        if (objA.Hitbox.Intersects(objB.Hitbox))
                        {
                            // Narrow phase collision detection - SAT
                            ObjectABNormals.Clear();
                            foreach (Vector2 vec in objA.UpdateCollisionMesh())
                            {
                                ObjectABNormals.Add(vec);
                            }
                            //foreach (Vector2 vec in objB.RectangleNormalize)
                            foreach (Vector2 vec in objB.UpdateCollisionMesh())
                            {
                                ObjectABNormals.Add(vec);
                            }
                            // debug if statement
                            IPhysicalEntity objAEnt = objA as IPhysicalEntity;
                            IPhysicalEntity objBEnt = objB as IPhysicalEntity;
                            if (objAEnt.Texturename == "Characters/Player/standR" && objBEnt.Texturename == "floor")
                            {
                                //breakpoint
                            }
                            if (objBEnt.Texturename == "Characters/Player/standR" && objAEnt.Texturename == "floor")
                            {
                                //breakpoint
                            }
                            // Call the main collision test method, pass the reference to the objects Vertices lists
                            // If collision returns true, call the MTV calculation, pass Object A as the target
                            if (CalculateDot(objA.RectangleVertices, objB.RectangleVertices))
                            {
                                DetermineCollisionType(objA, objB);
                            }
                            // if an object has triggered a level reset then abort the current collision detetion
                            if (mAbort)
                            {
                                break;
                            }
                        }
                    }
                }
                if (mAbort)
                {
                    break;
                }
            }
            // Clear all Collidables and Quadtree for next update
            mCollidables.Clear();
            mQuadtree.Clear();
        }
Example #33
0
 public bool CollidesWith(IPhysicalEntity physicalEntity)
 {
     if (physicalEntity == this)
     {
         return false;
     }
     var collidesWith = Shape.CollidesWith(physicalEntity.Shape);
     return collidesWith;
 }
Example #34
0
 public bool Collides(IPhysicalEntity physicalEntity)
 {
     return CollidesWithTile(physicalEntity) || CollidesWithPhysicalEntity(physicalEntity);
 }
Example #35
0
 public bool Collides(IPhysicalEntity physicalEntity)
 {
     return(CollidesWithTile(physicalEntity) || CollidesWithPhysicalEntity(physicalEntity));
 }
Example #36
0
 public bool CollidesWithPhysicalEntity(IPhysicalEntity physicalEntity)
 {
     var circle = new Circle(physicalEntity.Position, 2 * MaximumGameObjectRadius);
     List<IPhysicalEntity> physicalEntities = m_objectLayer.GetPhysicalEntities(circle);
     return physicalEntities.Any(physicalEntity.CollidesWith);
 }
 public bool CollidesWithTile(IPhysicalEntity physicalEntity, float eps)
 {
     return physicalEntity.Position.X - eps < 0;
 }