Exemplo n.º 1
0
        public override bool CollidesWith(IColliderEntity otherCollider)
        {
            if (otherCollider.GetCollisionComponent().GetType() == ColliderType.BOX)
            {
                BoxCollisionComponent otherBox = otherCollider.GetCollisionComponent() as BoxCollisionComponent;

                return(Position.X <= otherBox.Position.X + otherBox.Width &&
                       Position.X + Width >= otherBox.Position.X &&
                       Position.Y <= otherBox.Position.Y + otherBox.Height &&
                       Position.Y + Height >= otherBox.Position.Y);
            }
            else if (otherCollider.GetCollisionComponent().GetType() == ColliderType.CIRCLE)
            {
                return(otherCollider.GetCollisionComponent().CollidesWith(owner));
            }
            throw new Exception("Unknown collider type");
        }
 public override bool CollidesWith(IColliderEntity otherCollider)
 {
     if (otherCollider.GetCollisionComponent().GetType() == ColliderType.CIRCLE)
     {
         //TODO: review if this fast check is needed
         CircleCollisionComponent other = otherCollider.GetCollisionComponent() as CircleCollisionComponent;
         if ((Math.Abs(Position.X - otherCollider.GetCollisionComponent().Position.X) > Config.GRID * 2 && Math.Abs(Position.Y - other.Position.Y) > Config.GRID * 2))
         {
             return(false);
         }
         maxDistance = Radius + other.Radius;
         distance    = Vector2.Distance(Position, other.Position);
         return(distance <= maxDistance);
     }
     else if (otherCollider.GetCollisionComponent().GetType() == ColliderType.BOX)
     {
         BoxCollisionComponent box = otherCollider.GetCollisionComponent() as BoxCollisionComponent;
         Vector2 closestPoint      = new Vector2(Math.Clamp(Position.X, box.Position.X, box.Position.X + box.Width), Math.Clamp(Position.Y, box.Position.Y, box.Position.Y + box.Height));
         return(Vector2.DistanceSquared(Position, closestPoint) < Radius * Radius);
     }
     throw new Exception("Unknown collider type");
 }