public bool Collide(ICollisionable other) { if (other is CollisionBox box) { if (Y > box.Y && Y < box.Bottom || Bottom > box.Y && Bottom < box.Bottom) //Collision on the X-Axis { if (X == box.Right || Right == box.X) { return(true); } } else if (X > box.X && X < box.Right || Right > box.X && Right < box.Right) //Collision on the Y-Axis { if (Y == box.Bottom || Bottom == box.Y) { return(true); } } } else if (other is CollisionCircle circle) { if (circle.Collide(LeftTop)) { return(true); } if (circle.Collide(RightTop)) { return(true); } if (circle.Collide(LeftBottom)) { return(true); } if (circle.Collide(RightBottom)) { return(true); } if (circle.Position.X > X && circle.Position.X < Right) { return(Collide(circle.Position + new Vector2(0, circle.Radius)) || Collide(circle.Position - new Vector2(0, circle.Radius))); } if (circle.Position.Y > Y && circle.Position.Y < Bottom) { return(Collide(circle.Position + new Vector2(circle.Radius, 0)) || Collide(circle.Position - new Vector2(circle.Radius, 0))); } return(false); } else { throw new NotSupportedException("Unknown Type " + other.GetType().Name); } return(false); }
public bool Intersects(ICollisionable other) { if (other is CollisionCircle circle) { Vector2 dif = circle.Position - Position; return(dif.LengthSquared() < Radius * Radius + circle.Radius * Radius); } else if (other is CollisionBox box) { if (box.Intersects(Position)) { return(true); } if (Intersects(box.LeftTop)) { return(true); } if (Intersects(box.RightTop)) { return(true); } if (Intersects(box.LeftBottom)) { return(true); } if (Intersects(box.RightBottom)) { return(true); } if (Position.X > box.X && Position.X < box.Right) { return(box.Intersects(Position + new Vector2(0, Radius)) || box.Intersects(Position - new Vector2(0, Radius))); } if (Position.Y > box.Y && Position.Y < box.Bottom) { return(box.Intersects(Position + new Vector2(Radius, 0)) || box.Intersects(Position - new Vector2(Radius, 0))); } return(false); } else { throw new NotImplementedException("Unknown Type: " + other.GetType().Name); } }