/// <summary> /// Get a list of all the edges of the Polygon as Line2 objects. /// </summary> /// <returns>A Line2 list of all edges.</returns> public List<Line2> GetEdgesAsLines() { var list = new List<Line2>(); for (var i = 0; i < Points.Count; i++) { Vector2 p1 = Points[i]; Vector2 p2 = Points[i + 1 == Points.Count ? 0 : i + 1]; // Clever! var line = new Line2(p1, p2); list.Add(line); } return list; }
/// <summary> /// Giant function that checks for overlaps between many different types /// </summary> /// <param name="first">The first collider to test.</param> /// <param name="second">The second collider to test.</param> /// <returns>True if overlapping something</returns> internal static bool OverlapTest(Collider first, Collider second) { #region Box vs Box if (first is BoxCollider && second is BoxCollider) { if (first.Right <= second.Left) return false; if (first.Bottom <= second.Top) return false; if (first.Left >= second.Right) return false; if (first.Top >= second.Bottom) return false; return true; } #endregion #region Box vs Circle else if ((first is BoxCollider && second is CircleCollider) || (first is CircleCollider && second is BoxCollider)) { CircleCollider circle; BoxCollider box; if (first is CircleCollider) { circle = first as CircleCollider; box = second as BoxCollider; } else { circle = second as CircleCollider; box = first as BoxCollider; } //check is c center point is in the rect if (Util.InRect(circle.CenterX, circle.CenterY, box.Left, box.Top, box.Width, box.Height)) { return true; } //check to see if any corners are in the circle if (Util.DistanceRectPoint(circle.CenterX, circle.CenterY, box.Left, box.Top, box.Width, box.Height) < circle.Radius) { return true; } //check to see if any lines on the box intersect the circle Line2 boxLine; boxLine = new Line2(box.Left, box.Top, box.Right, box.Top); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true; boxLine = new Line2(box.Right, box.Top, box.Right, box.Bottom); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true; boxLine = new Line2(box.Right, box.Bottom, box.Left, box.Bottom); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true; boxLine = new Line2(box.Left, box.Bottom, box.Left, box.Top); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true; return false; } #endregion #region Box vs Grid else if ((first is BoxCollider && second is GridCollider) || (first is GridCollider && second is BoxCollider)) { BoxCollider box; GridCollider grid; if (first is BoxCollider) { box = first as BoxCollider; grid = second as GridCollider; } else { box = second as BoxCollider; grid = first as GridCollider; } if (box.Right <= grid.Left) return false; if (box.Bottom <= grid.Top) return false; if (box.Left >= grid.Right) return false; if (box.Top >= grid.Bottom) return false; //This is a quick fix and might have to be addressed later if (grid.GetRect(box.Left, box.Top, box.Right - 1, box.Bottom - 1, false)) return true; return false; } #endregion #region Circle vs Circle else if (first is CircleCollider && second is CircleCollider) { CircleCollider circle1 = first as CircleCollider, circle2 = second as CircleCollider; if (Util.Distance(circle1.CenterX, circle1.CenterY, circle2.CenterX, circle2.CenterY) < circle1.Radius + circle2.Radius) { return true; } return false; } #endregion #region Box vs Line else if ((first is BoxCollider && second is LineCollider) || (first is LineCollider && second is BoxCollider)) { BoxCollider box; LineCollider line; if (first is BoxCollider) { box = first as BoxCollider; line = second as LineCollider; } else { box = second as BoxCollider; line = first as LineCollider; } if (line.Line2.IntersectsRect(box.Left, box.Top, box.Width, box.Height)) return true; return false; } #endregion #region Line vs Line else if (first is LineCollider && second is LineCollider) { return (first as LineCollider).Line2.Intersects((second as LineCollider).Line2); } #endregion #region Line vs Grid else if ((first is LineCollider && second is GridCollider) || (first is GridCollider && second is LineCollider)) { //check any tiles along the line segment, somehow? LineCollider line; GridCollider grid; if (first is LineCollider) { line = first as LineCollider; grid = second as GridCollider; } else { line = second as LineCollider; grid = first as GridCollider; } //make a rectangle out of the line segment, check for any tiles in that rectangle //if there are tiles in there, loop through and check each one as a rectangle against the line if (grid.GetRect(line.Left, line.Top, line.Right, line.Bottom, false)) { float rectX, rectY; int gridx = grid.GridX(line.Left), gridy = grid.GridY(line.Top), gridx2 = grid.GridX(line.Right), gridy2 = grid.GridY(line.Bottom); for (int i = gridx; i <= gridx2; i++) { for (int j = gridy; j <= gridy2; j++) { if (grid.GetTile(i, j)) { rectX = i * grid.TileWidth + grid.Left; rectY = j * grid.TileHeight + grid.Top; if (Util.InRect((float)line.Line2.PointA.X, (float)line.Line2.PointA.Y, rectX, rectY, grid.TileWidth, grid.TileHeight)) { return true; } if (Util.InRect((float)line.Line2.PointB.X, (float)line.Line2.PointB.Y, rectX, rectY, grid.TileWidth, grid.TileHeight)) { return true; } if (line.Line2.IntersectsRect(rectX, rectY, grid.TileWidth, grid.TileHeight)) { return true; } } } } } return false; } #endregion #region Circle vs Grid else if ((first is CircleCollider && second is GridCollider) || (first is GridCollider && second is CircleCollider)) { //make a rectangle out of the circle, check for any tiles in that rectangle //if there are tiles, check each tile as a rectangle against the circle CircleCollider circle; GridCollider grid; if (first is CircleCollider) { circle = first as CircleCollider; grid = second as GridCollider; } else { circle = second as CircleCollider; grid = first as GridCollider; } int gridx, gridy, gridx2, gridy2; gridx = (int)(Util.SnapToGrid(circle.Left - grid.Left, grid.TileWidth) / grid.TileWidth); gridy = (int)(Util.SnapToGrid(circle.Top - grid.Top, grid.TileHeight) / grid.TileHeight); gridx2 = (int)(Util.SnapToGrid(circle.Right - grid.Left, grid.TileWidth) / grid.TileWidth); gridy2 = (int)(Util.SnapToGrid(circle.Bottom - grid.Top, grid.TileHeight) / grid.TileHeight); //if (grid.GetRect(gridx, gridy, gridx2, gridy2, false)) { if (grid.GetRect(circle.Left, circle.Top, circle.Right, circle.Bottom, false)) { float rectX, rectY; for (int i = gridx; i <= gridx2; i++) { for (int j = gridy; j <= gridy2; j++) { if (grid.GetTile(i, j)) { rectX = (i * grid.TileWidth) + grid.Left; rectY = (j * grid.TileHeight) + grid.Top; //check is c center point is in the rect if (Util.InRect(circle.CenterX, circle.CenterY, rectX, rectY, grid.TileWidth, grid.TileHeight)) { return true; } //check to see if any corners are in the circle if (Util.DistanceRectPoint(circle.CenterX, circle.CenterY, rectX, rectY, grid.TileWidth, grid.TileHeight) < circle.Radius) { //return true; } //check to see if any lines on the box intersect the circle Line2 boxLine; boxLine = new Line2(rectX, rectY, rectX + grid.TileWidth, rectY); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true; boxLine = new Line2(rectX + grid.TileWidth, rectY, rectX + grid.TileWidth, rectY + grid.TileHeight); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true; boxLine = new Line2(rectX + grid.TileWidth, rectY + grid.TileHeight, rectX, rectY + grid.TileHeight); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true; boxLine = new Line2(rectX, rectY + grid.TileHeight, rectX, rectY); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) return true; } } } } return false; } #endregion #region Grid vs Grid else if (first is GridCollider && second is GridCollider) { //loop through one grid, check tile as rectangle in second grid? //first check if grids even touch with basic box test //then check each tile on first as a rect against second's tilemap //maybe optimize by looping through the smaller (area wise) tilemap if (!Util.IntersectRectangles(first.Left, first.Top, first.Width, first.Height, second.Left, second.Top, second.Width, second.Height)) { return false; } GridCollider small, large; if ((first as GridCollider).TileArea < (second as GridCollider).TileArea) { small = first as GridCollider; large = second as GridCollider; } else { small = second as GridCollider; large = first as GridCollider; } for (int i = 0; i < small.TileColumns; i++) { for (int j = 0; j < small.TileRows; j++) { if (small.GetTile(i, j)) { //check rects float rectx, recty; rectx = i * small.TileWidth + small.Left; recty = j * small.TileHeight + small.Top; if (large.GetRect(rectx, recty, rectx + small.TileWidth, recty + small.TileHeight, false)) { return true; } } } } return false; } #endregion #region Line vs Circle else if ((first is LineCollider && second is CircleCollider) || (first is CircleCollider && second is LineCollider)) { CircleCollider circle; LineCollider line; if (first is LineCollider) { line = first as LineCollider; circle = second as CircleCollider; } else { line = second as LineCollider; circle = first as CircleCollider; } if (line.Line2.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) { return true; } return false; } #endregion #region Point vs Point else if (first is PointCollider && second is PointCollider) { if (first.Left != second.Left) return false; if (first.Top != second.Top) return false; return true; } #endregion #region Point vs Circle else if ((first is PointCollider && second is CircleCollider) || (first is CircleCollider && second is PointCollider)) { PointCollider point; CircleCollider circle; if (first is PointCollider) { point = first as PointCollider; circle = second as CircleCollider; } else { point = second as PointCollider; circle = first as CircleCollider; } if (Util.Distance(point.Left, point.Top, circle.CenterX, circle.CenterY) < circle.Radius) return true; return false; } #endregion #region Point vs Box else if ((first is PointCollider && second is BoxCollider) || (first is BoxCollider && second is PointCollider)) { PointCollider point; BoxCollider box; if (first is PointCollider) { point = first as PointCollider; box = second as BoxCollider; } else { point = second as PointCollider; box = first as BoxCollider; } if (Util.InRect(point.Left, point.Top, box.Left, box.Top, box.Width, box.Height)) return true; return false; } #endregion #region Point vs Grid else if ((first is PointCollider && second is GridCollider) || (first is GridCollider && second is PointCollider)) { PointCollider point; GridCollider grid; if (first is PointCollider) { point = first as PointCollider; grid = second as GridCollider; } else { point = second as PointCollider; grid = first as GridCollider; } int gridx, gridy; gridx = (int)Util.SnapToGrid(point.Left, grid.TileWidth); gridy = (int)Util.SnapToGrid(point.Top, grid.TileHeight); if (grid.GetTile(gridx, gridy)) return true; return false; } #endregion #region Point vs Line else if ((first is PointCollider && second is LineCollider) || (first is LineCollider && second is PointCollider)) { PointCollider point; LineCollider line; if (first is PointCollider) { point = first as PointCollider; line = second as LineCollider; } else { point = second as PointCollider; line = first as LineCollider; } //first take care of weird cases that might result in division by 0 Line2 line2 = line.Line2; if (line2.X1 == line2.X2) { if (line2.Y1 == line2.Y2) { if (point.Left == line2.X1 && point.Top == line2.Y1) { return true; } } if (point.Left == line2.X1 && point.Top >= Math.Min(line2.Y1, line2.Y2) && point.Top <= Math.Max(line2.Y1, line2.Y2)) { return true; } } if (line2.Y1 == line2.Y2) { if (point.Top == line2.Y1 && point.Left >= Math.Min(line2.X1, line2.X2) && point.Left <= Math.Max(line2.X1, line2.X2)) { return true; } } //if no special cases, this should work! if ((point.Left - line2.X1) / (line2.X2 - line2.X1) == (point.Top - line2.Y1) / (line2.Y2 - line2.Y1)) return true; return false; } #endregion #region Pixel vs Pixel else if ((first is PixelCollider && second is PixelCollider)) { //AABB test first var pixel1 = first as PixelCollider; var pixel2 = second as PixelCollider; if (first.Right <= second.Left) return false; if (first.Left >= second.Right) return false; if (first.Top >= second.Bottom) return false; if (first.Bottom <= second.Top) return false; //get intersecting area float x1 = Math.Max(first.Left, second.Left); float x2 = Math.Min(first.Right, second.Right); float y1 = Math.Max(first.Top, second.Top); float y2 = Math.Min(first.Bottom, second.Bottom); //scan both pixels and see if they collide var p1 = first as PixelCollider; var p2 = second as PixelCollider; for (var i = x1; i < x2; i++) { for (var j = y1; j < y2; j++) { var xx = (int)Math.Floor(i - first.Left); var yy = (int)Math.Floor(j - first.Top); if (p1.PixelAt(xx, yy)) { xx = (int)Math.Floor(i - second.Left); yy = (int)Math.Floor(j - second.Top); if (p2.PixelAt(xx, yy)) { return true; } } } } return false; } #endregion #region Pixel vs Box else if ((first is PixelCollider && second is BoxCollider) || (first is BoxCollider && second is PixelCollider)) { PixelCollider pixel; BoxCollider box; if (first is PixelCollider) { pixel = first as PixelCollider; box = second as BoxCollider; } else { pixel = second as PixelCollider; box = first as BoxCollider; } //AABB test if (box.Right <= pixel.Left) return false; if (box.Left >= pixel.Right) return false; if (box.Top >= pixel.Bottom) return false; if (box.Bottom <= pixel.Top) return false; //get intersecting area float x1 = Math.Max(box.Left, pixel.Left); float x2 = Math.Min(box.Right, pixel.Right); float y1 = Math.Max(box.Top, pixel.Top); float y2 = Math.Min(box.Bottom, pixel.Bottom); //scan for pixels in area for (var i = x1; i < x2; i++) { for (var j = y1; j < y2; j++) { var xx = (int)Math.Floor(i - pixel.Left); var yy = (int)Math.Floor(j - pixel.Top); if (pixel.PixelAt(xx, yy)) { return true; } } } return false; } #endregion #region Pixel vs Circle else if ((first is PixelCollider && second is CircleCollider) || (first is CircleCollider && second is PixelCollider)) { PixelCollider pixel; CircleCollider circle; if (first is PixelCollider) { pixel = first as PixelCollider; circle = second as CircleCollider; } else { pixel = second as PixelCollider; circle = first as CircleCollider; } //circle to rectangle collision first bool firstCollisionCheck = false; //check is c center point is in the rect if (Util.InRect(circle.CenterX, circle.CenterY, pixel.Left, pixel.Top, pixel.Width, pixel.Height)) { firstCollisionCheck = true; } if (!firstCollisionCheck) { //check to see if any corners are in the circle if (Util.DistanceRectPoint(circle.CenterX, circle.CenterY, pixel.Left, pixel.Top, pixel.Width, pixel.Height) < circle.Radius) { firstCollisionCheck = true; } } if (!firstCollisionCheck) { //check to see if any lines on the box intersect the circle Line2 boxLine; boxLine = new Line2(pixel.Left, pixel.Top, pixel.Right, pixel.Top); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) firstCollisionCheck = true; if (!firstCollisionCheck) { boxLine = new Line2(pixel.Right, pixel.Top, pixel.Right, pixel.Bottom); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) firstCollisionCheck = true; } if (!firstCollisionCheck) { boxLine = new Line2(pixel.Right, pixel.Bottom, pixel.Left, pixel.Bottom); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) firstCollisionCheck = true; } if (!firstCollisionCheck) { boxLine = new Line2(pixel.Left, pixel.Bottom, pixel.Left, pixel.Top); if (boxLine.IntersectCircle(new Vector2(circle.CenterX, circle.CenterY), circle.Radius)) firstCollisionCheck = true; } } if (!firstCollisionCheck) return false; //get intersecting area as if circle was rect //scan for pixels in area //only check pixels inside circle radius //okay the math for that is super hard so we're doing it the easy + dumb wy //dumb way, scan through pixels to see if one of the pixels is in the circle? for (var xx = 0; xx < pixel.Width; xx++) { for (var yy = 0; yy < pixel.Height; yy++) { var pixelx = xx + pixel.Left; var pixely = yy + pixel.Top; if (Util.Distance(pixelx, pixely, circle.CenterX, circle.CenterY) <= circle.Radius + 1) { if (pixel.PixelAt(xx, yy)) { return true; } } } } } #endregion #region Pixel vs Point else if ((first is PixelCollider && second is PointCollider) || (first is PointCollider && second is PixelCollider)) { PixelCollider pixel; PointCollider point; if (first is PixelCollider) { pixel = first as PixelCollider; point = second as PointCollider; } else { pixel = second as PixelCollider; point = first as PointCollider; } //rectangle to point first if (!Util.InRect(point.Left, point.Top, pixel.Left, pixel.Top, pixel.Width, pixel.Height)) return false; //check for pixel at point if (pixel.PixelAtRelative((int)point.Left, (int)point.Top)) { return true; } return false; } #endregion #region Pixel vs Line else if ((first is PixelCollider && second is LineCollider) || (first is LineCollider && second is PixelCollider)) { PixelCollider pixel; LineCollider line; if (first is PixelCollider) { pixel = first as PixelCollider; line = second as LineCollider; } else { pixel = second as PixelCollider; line = first as LineCollider; } //line to rectangle first if (!line.Line2.IntersectsRect(pixel.Left, pixel.Top, pixel.Width, pixel.Height)) return false; //dumb way, check all pixels for distance to line == 0 for (var xx = 0; xx < pixel.Width; xx++) { for (var yy = 0; yy < pixel.Height; yy++) { if (pixel.PixelAt(xx, yy)) { if (Util.DistanceLinePoint(xx + pixel.Left, yy + pixel.Top, line.Line2) < 0.1f) { return true; } } } } } #endregion #region Pixel vs Grid else if ((first is PixelCollider && second is GridCollider) || (first is GridCollider && second is PixelCollider)) { PixelCollider pixel; GridCollider grid; if (first is PixelCollider) { pixel = first as PixelCollider; grid = second as GridCollider; } else { pixel = second as PixelCollider; grid = first as GridCollider; } //collide rectangle into tiles first if (pixel.Right <= grid.Left) return false; if (pixel.Bottom <= grid.Top) return false; if (pixel.Left >= grid.Right) return false; if (pixel.Top >= grid.Bottom) return false; //This is a quick fix and might have to be addressed later if (!grid.GetRect(pixel.Left, pixel.Top, pixel.Right - 1, pixel.Bottom - 1, false)) return false; //go through tiles that pixel is overlapping for (var xx = 0; xx < pixel.Width; xx++) { for (var yy = 0; yy < pixel.Height; yy++) { float checkx = xx + pixel.Left; float checky = yy + pixel.Top; if (grid.GetTileAtPosition(checkx, checky)) { if (pixel.PixelAt(xx, yy)) { return true; } } } } } #endregion #region Unknown return false; #endregion }
/// <summary> /// Intersection test on another line. (http://ideone.com/PnPJgb) /// </summary> /// <param name="other">The line to test against</param> /// <returns></returns> public bool Intersects(Line2 other) { //A = X1, Y1; B = X2, Y2; C = other.X1, other.Y1; D = other.X2, other.Y2; Vector2 A = new Vector2(X1, Y1); Vector2 B = new Vector2(X2, Y2); Vector2 C = new Vector2(other.X1, other.Y1); Vector2 D = new Vector2(other.X2, other.Y2); Vector2 CmP = new Vector2(C.X - A.X, C.Y - A.Y); Vector2 r = new Vector2(B.X - A.X, B.Y - A.Y); Vector2 s = new Vector2(D.X - C.X, D.Y - C.Y); float CmPxr = (float)CmP.X * (float)r.Y - (float)CmP.Y * (float)r.X; float CmPxs = (float)CmP.X * (float)s.Y - (float)CmP.Y * (float)s.X; float rxs = (float)r.X * (float)s.Y - (float)r.Y * (float)s.X; if (CmPxr == 0f) { // Lines are collinear, and so intersect if they have any overlap return ((C.X - A.X < 0f) != (C.X - B.X < 0f)) || ((C.Y - A.Y < 0f) != (C.Y - B.Y < 0f)); } if (rxs == 0f) return false; // Lines are parallel. float rxsr = 1f / rxs; float t = CmPxs * rxsr; float u = CmPxr * rxsr; return (t >= 0f) && (t <= 1f) && (u >= 0f) && (u <= 1f); }