Intersects() public method

public Intersects ( FRectangle frectangle ) : bool
frectangle FRectangle
return bool
Beispiel #1
0
        /// <summary>
        /// Checks if Two Entities are intersecting each other assuming the specified states, gameTime and group filter.
        /// This check is actually rather ineffecient. O(n^2) time complexity. In reality though, we would be smart about
        /// what to compare - hence the group and state filters. The number of comparasins between drawables should be kept
        /// to a minimum in order to maintain performance.
        /// </summary>
        public static bool IntersectsWith(
            Entity entity1, string entity1State, string entity1Group,
            Entity entity2, string entity2State, string entity2Group, 
            GameTime gameTime
            )
        {
            HashSet<GameDrawableInstance> entity1Instances = entity1.Drawables.GetByState(entity1State);
            HashSet<GameDrawableInstance> entity2Instances = entity2.Drawables.GetByState(entity2State);

            if (entity1Instances == null || entity2Instances == null) return false;

            foreach (GameDrawableInstance instanceForEntity1 in entity1Instances)
            {
                if (entity1Group == null || instanceForEntity1._associatedGroup == entity1Group)
                {
                    Rectangle sourceRectangleEntity1 = instanceForEntity1.GetSourceRectangle(gameTime);

                    float entity1SourceWidth = sourceRectangleEntity1.Width * entity1.ScaleX;
                    float entity1SourceHeight = sourceRectangleEntity1.Height * entity1.ScaleY;

                    FRectangle absBoundingRectEntity1 = new FRectangle(
                        entity1.Pos.X - entity1SourceWidth * instanceForEntity1.Drawable.Origin.X,
                        entity1.Pos.Y - entity1SourceHeight * instanceForEntity1.Drawable.Origin.Y,
                        entity1SourceWidth,
                        entity1SourceHeight
                        );

                    foreach (GameDrawableInstance instanceForEntity2 in entity2Instances)
                    {
                        if (entity2Group == null || instanceForEntity2._associatedGroup == entity2Group)
                        {
                            Rectangle sourceRectangleEntity2 = instanceForEntity2.GetSourceRectangle(gameTime);

                            float entity2SourceWidth = sourceRectangleEntity2.Width * entity2.ScaleX;
                            float entity2SourceHeight = sourceRectangleEntity2.Height * entity2.ScaleY;

                            FRectangle absBoundingRectEntity2 = new FRectangle(
                                entity2.Pos.X - entity2SourceWidth * instanceForEntity2.Drawable.Origin.X,
                                entity2.Pos.Y - entity2SourceHeight * instanceForEntity2.Drawable.Origin.Y,
                                entity2SourceWidth,
                                entity2SourceHeight
                                );

                            // Check if the two bounding boxes intersect
                            if (absBoundingRectEntity1.Intersects(absBoundingRectEntity2))
                                return true;
                        }
                    }
                }
            }

            // No Intersection tests passed.
            return false;
        }
Beispiel #2
0
        public static bool IntersectsWith(Entity entity, string entityState, string entityGroup, FRectangle bounds, GameTime gameTime)
        {
            HashSet<DrawableInstance> entityInstances = entity.Drawables.GetByState(entityState);

            if (entityInstances == null) return false;

            foreach (DrawableInstance instance in entityInstances)
            {
                if (entityGroup == null || instance._associatedGroup == entityGroup)
                {
                    float entitySourceWidth = instance.GetWidth(gameTime) * entity.ScaleX;
                    float entitySourceHeight = instance.GetHeight(gameTime) * entity.ScaleY;

                    FRectangle absBoundingBox = new FRectangle(
                        entity.Pos.X - entitySourceWidth * instance.Drawable.Origin.X,
                        entity.Pos.Y - entitySourceHeight * instance.Drawable.Origin.Y,
                        entitySourceWidth,
                        entitySourceHeight
                        );

                    if (bounds.Intersects(absBoundingBox))
                        return true;
                }
            }

            // No intersection tests passed.
            return false;
        }