Exemple #1
0
        public bool CollidesWith(CollisionBody other)
        {
            if (this.Shape == ShapeType.Circle && other.Shape == ShapeType.Circle)
            {
                CircleBody a = (CircleBody)this;
                CircleBody b = (CircleBody)other;
                return(Vector2.Distance(this.Position, other.Position) <= a.Radius + b.Radius);
            }
            else if (this.Shape == ShapeType.Circle && other.Shape == ShapeType.Rectangle)
            {
                return(RectCircCollision((CircleBody)this, (RectangleBody)other));
            }
            else if (this.Shape == ShapeType.Rectangle && other.Shape == ShapeType.Circle)
            {
                return(RectCircCollision((CircleBody)other, (RectangleBody)this));
            }
            else if (this.Shape == ShapeType.Rectangle && other.Shape == ShapeType.Rectangle)
            {
                RectangleBody a = (RectangleBody)this;
                RectangleBody b = (RectangleBody)other;

                return((a.Position.X <= b.Position.X + b.Size.X || a.Position.X + a.Size.X >= b.Position.X) &&
                       (a.Position.Y <= b.Position.Y + b.Size.Y || a.Position.Y + a.Size.Y >= b.Position.Y));
            }
            else
            {
                throw new NotImplementedException("Attempted collision with a shape that doesn't exist");
            }
        }
Exemple #2
0
        private bool RectCircCollision(CircleBody a, RectangleBody b)
        {
            float   nearestX     = Clamp(a.Position.X, b.Position.X, b.Position.X + b.Size.X);
            float   nearestY     = Clamp(a.Position.Y, b.Position.Y, b.Position.Y + b.Size.Y);
            Vector2 nearestPoint = new Vector2(nearestX, nearestY);

            return(Vector2.Distance(nearestPoint, a.Position) <= a.Radius);
        }
Exemple #3
0
        public Dictionary <uint, CollisionBody> GetPotentialCollisions(CollisionBody toCheck)
        {
            int xMin = 0;
            int xMax = 0;
            int yMin = 0;
            int yMax = 0;

            if (toCheck.Shape == CollisionBody.ShapeType.Circle)
            {
                CircleBody body = (CircleBody)toCheck;
                xMin = ((int)(Math.Max(body.Position.X - body.Radius / 2, 0)) / tileSize);
                xMax = (int)(Math.Min(body.Position.X + body.Radius / 2, maxWidth - 1)) / tileSize;
                yMin = ((int)(Math.Max(body.Position.Y - body.Radius / 2, 0)) / tileSize);
                yMax = (int)(Math.Min(body.Position.Y + body.Radius / 2, maxHeight - 1)) / tileSize;
            }
            else if (toCheck.Shape == CollisionBody.ShapeType.Rectangle)
            {
                RectangleBody body = (RectangleBody)toCheck;
                xMin = ((int)(Math.Max(body.Position.X, 0)) / tileSize);
                xMax = (int)(Math.Min(body.Position.X + body.Size.X, 49)) / tileSize;
                yMin = ((int)(Math.Max(body.Position.Y, 0)) / tileSize);
                yMax = (int)(Math.Min(body.Position.Y + body.Size.Y, 49)) / tileSize;
            }

            // Get every possible CollisionBody
            Dictionary <uint, CollisionBody> potentials = new Dictionary <uint, CollisionBody>();

            for (int y = yMin; y <= yMax; y++)
            {
                for (int x = xMin; x <= xMax; x++)
                {
                    foreach (CollisionBody c in Tiles[y * columns + x].Data.Values)
                    {
                        if (!potentials.ContainsKey(c.id))
                        {
                            potentials.Add(c.id, c);
                        }
                    }
                }
            }

            return(potentials);
        }
Exemple #4
0
        private void BounceOffBottom(Object other)
        {
            if (this.Collision.Shape == CollisionBody.ShapeType.Circle)
            {
                CircleBody    c = (CircleBody)this.Collision;
                RectangleBody r = (RectangleBody)other.Collision;

                float dist = (r.Position.Y + r.Size.Y) - (c.Position.Y - c.Radius);
                this.Collision.Position.Y += dist * 2;
                this.Velocity.Y           *= -1;
            }
            else
            {
                RectangleBody r1 = (RectangleBody)this.Collision;
                RectangleBody r2 = (RectangleBody)other.Collision;

                float dist = (r2.Position.Y + r2.Size.Y) - (r1.Position.Y);
                this.Collision.Position.Y += dist * 2;
                this.Velocity.Y           *= -1;
            }
        }
Exemple #5
0
        private void BounceOffLeft(Object other)
        {
            if (this.Collision.Shape == CollisionBody.ShapeType.Circle)
            {
                CircleBody    c = (CircleBody)this.Collision;
                RectangleBody r = (RectangleBody)other.Collision;

                float dist = (c.Position.X + c.Radius) - (r.Position.X);
                this.Collision.Position.X -= dist * 2;
                this.Velocity.X           *= -1;
            }
            else
            {
                RectangleBody r1 = (RectangleBody)this.Collision;
                RectangleBody r2 = (RectangleBody)other.Collision;

                float dist = (r1.Position.X + r1.Size.X) - (r2.Position.X);
                this.Collision.Position.X -= dist * 2;
                this.Velocity.X           *= -1;
            }
        }
Exemple #6
0
 public Brick(Game1 game, CollisionBody collision, int health, int spriteNum, int crackedNum, PowerUpType powerUpType) : base(game, collision)
 {
     Game             = game;
     Collision        = collision;
     Collision.Parent = this;
     Health           = health;
     //Texture = null;
     //CrackedTexture = null;
     //TextureColor = Color.White;
     Velocity              = new Vector2(0f);
     Acceleration          = new Vector2(0f);
     this.SpriteNum        = spriteNum;
     this.CrackedSpriteNum = crackedNum;
     if (powerUpType == PowerUpType.None)
     {
         Powerup = null;
     }
     else
     {
         CircleBody pCollision = new CircleBody(new Vector2(collision.Position.X + collision.Region().Width / 2, collision.Position.Y + collision.Region().Height / 2), Powerup.Size);
         Powerup = new Powerup(game, pCollision, powerUpType);
     }
 }
Exemple #7
0
        private List <GridTile> GetCoveredTiles(CollisionBody toAdd)
        {
            int xMin = 0;
            int xMax = 0;
            int yMin = 0;
            int yMax = 0;

            if (toAdd.Shape == CollisionBody.ShapeType.Circle)
            {
                CircleBody body = (CircleBody)toAdd;
                xMin = ((int)(Math.Max(body.Position.X - body.Radius / 2, 0)) / tileSize);
                xMax = (int)(Math.Min(body.Position.X + body.Radius / 2, maxWidth - 1)) / tileSize;
                yMin = ((int)(Math.Max(body.Position.Y - body.Radius / 2, 0)) / tileSize);
                yMax = (int)(Math.Min(body.Position.Y + body.Radius / 2, maxHeight - 1)) / tileSize;
            }
            else if (toAdd.Shape == CollisionBody.ShapeType.Rectangle)
            {
                RectangleBody body = (RectangleBody)toAdd;
                xMin = ((int)(Math.Max(body.Position.X, 0)) / tileSize);
                xMax = (int)(Math.Min(body.Position.X + body.Size.X, maxWidth - 1)) / tileSize;
                yMin = ((int)(Math.Max(body.Position.Y, 0)) / tileSize);
                yMax = (int)(Math.Min(body.Position.Y + body.Size.Y, maxWidth - 1)) / tileSize;
            }

            List <GridTile> toReturn = new List <GridTile>();

            for (int y = yMin; y <= yMax; y++)
            {
                for (int x = xMin; x <= xMax; x++)
                {
                    toReturn.Add(Tiles[y * columns + x]);
                }
            }

            return(toReturn);
        }
Exemple #8
0
        public void BounceOff(Object other)
        {
            Vector2 velocityToCheck = this.Velocity - other.Velocity;

            if (this.Collision.Shape == CollisionBody.ShapeType.Circle && other.Collision.Shape == CollisionBody.ShapeType.Circle)
            {
                // Circle on Circle
            }
            else if (this.Collision.Shape == CollisionBody.ShapeType.Circle && other.Collision.Shape == CollisionBody.ShapeType.Rectangle)
            {
                CircleBody    c = (CircleBody)this.Collision;
                RectangleBody r = (RectangleBody)other.Collision;
                if (velocityToCheck.X >= 0)
                {
                    // Moving right
                    if (velocityToCheck.Y >= 0)
                    {
                        // Moving right-down
                        if (r.Position.X - c.Position.X <= r.Position.Y - c.Position.Y)
                        {
                            this.BounceOffTop(other);
                        }
                        else
                        {
                            this.BounceOffLeft(other);
                        }
                    }
                    else
                    {
                        // Moving right-up
                        if (-1 * (r.Position.X - c.Position.X) <= (r.Position.Y + r.Size.Y - c.Position.Y))
                        {
                            this.BounceOffLeft(other);
                        }
                        else
                        {
                            this.BounceOffBottom(other);
                        }
                    }
                }
                else
                {
                    // Moving left
                    if (velocityToCheck.Y >= 0)
                    {
                        // Moving left-down
                        if (r.Position.X + r.Size.X - c.Position.X <= -1 * (r.Position.Y - c.Position.Y))
                        {
                            this.BounceOffRight(other);
                        }
                        else
                        {
                            this.BounceOffTop(other);
                        }
                    }
                    else
                    {
                        // Moving left-up
                        if (r.Position.X + r.Size.X - c.Position.X <= r.Position.Y + r.Size.Y - c.Position.Y)
                        {
                            this.BounceOffRight(other);
                        }
                        else
                        {
                            this.BounceOffBottom(other);
                        }
                    }
                }
            }
        }