public static IEnumerable <Vector2f> GetPoints(this ConvexShape shape) { for (uint i = 0; i < shape.GetPointCount(); i++) { yield return(shape.GetPoint(i)); } }
private static void Append(ref ConvexShape s, Vector2i point) { var count = s.GetPointCount(); s.SetPointCount(count + 1); s.SetPoint(count, (Vector2f)point); }
public Vector2[] GetPoints() { Vector2[] points = new Vector2[shape.GetPointCount()]; for (uint i = 0; i < points.Length; i++) { points[i] = (Vector2)shape.GetPoint(i); } return(points); }
public static bool Contain(this ConvexShape convexShape, Vector2f point) { var shapePointCount = convexShape.GetPointCount(); var count = 0; for (uint i = 0, j = shapePointCount - 1; i < shapePointCount; j = i++) { var current = convexShape.GetPoint(i); var last = convexShape.GetPoint(j); if (IsBetween(current, last, point) && (point.X > (last.X - current.X) * (point.Y - current.Y) / (last.Y - current.Y) + current.X)) { count++; } } return(count % 2 == 1); }
public static void ProjectPolygon(Vector2f axis, ConvexShape polygon, ref float min, ref float max) { // To project a point on an axis use the dot product float dotProduct = Trig.DotProduct(axis, polygon.Transform.TransformPoint(polygon.GetPoint(0))); min = dotProduct; max = dotProduct; for (uint i = 0; i < polygon.GetPointCount(); i++) { dotProduct = Trig.DotProduct(polygon.Transform.TransformPoint(polygon.GetPoint(i)), axis); if (dotProduct < min) { min = dotProduct; } else { if (dotProduct > max) { max = dotProduct; } } } }
public static List <ConvexShape> LoadFromPoly(out CorrectPath path, string title) { const int rectsize = 60; const int wallSize = 45; ConvexShape pathShape = LoadObjects(title)[0]; List <ConvexShape> pathList = new List <ConvexShape>(); List <ConvexShape> mapList = new List <ConvexShape>(); for (uint i = 0; i < pathShape.GetPointCount(); i++) { Vector2f point1 = pathShape.GetPoint(i) + pathShape.Position; Vector2f point2 = pathShape.GetPoint((i + 1) % pathShape.GetPointCount()) + pathShape.Position; float angle = Trig.FindAngleToTarget(point1, point2); ConvexShape myShape = new ConvexShape(4); List <Vector2f> pathPoint = new List <Vector2f>(); pathPoint.Add(point1 + Trig.MoveDist(angle + 90, rectsize)); pathPoint.Add(point1 + Trig.MoveDist(angle - 90, rectsize)); pathPoint.Add(point2 + Trig.MoveDist(angle - 90, rectsize)); pathPoint.Add(point2 + Trig.MoveDist(angle + 90, rectsize)); Vector2f min = pathPoint[0]; foreach (Vector2f point in pathPoint) { if (point.X < min.X) { min.X = point.X; } if (point.Y < min.Y) { min.Y = point.Y; } } for (int j = 0; j < pathPoint.Count; j++) { pathPoint[j] -= min; } myShape.Position = min; for (int j = 0; j < pathPoint.Count; j++) { myShape.SetPoint((uint)j, pathPoint[j]); } pathList.Add(myShape); } pathList = new Sempoolfi(pathList).Run(); for (int i = 0; i < pathList.Count; i++) { int index = (i + 1) % pathList.Count; Vector2f point1 = pathList[i].GetPoint(3) + pathList[i].Position; Vector2f point2 = pathList[i].GetPoint(0) + pathList[i].Position; Vector2f point3 = point1; ConvexShape shape1 = new ConvexShape(2); Vector2f min = point1; if (point2.X < min.X) { min.X = point2.X; } if (point2.Y < min.Y) { min.Y = point2.Y; } if (point3.X < min.X) { min.X = point3.X; } if (point3.Y < min.Y) { min.Y = point3.Y; } point1 -= min; point2 -= min; point3 -= min; shape1.Position = min; shape1.SetPoint(0, point1); shape1.SetPoint(1, point2); //shape1.SetPoint(2, point3); shape1.FillColor = Color.Black; mapList.Add(shape1); Vector2f point1_2 = pathList[i].GetPoint(2) + pathList[i].Position; Vector2f point2_2 = pathList[i].GetPoint(1) + pathList[i].Position; Vector2f point3_2 = point1_2; ConvexShape shape2 = new ConvexShape(2); Vector2f min2 = point1_2; if (point2_2.X < min2.X) { min2.X = point2_2.X; } if (point2_2.Y < min2.Y) { min2.Y = point2_2.Y; } if (point3_2.X < min2.X) { min2.X = point3_2.X; } if (point3_2.Y < min2.Y) { min2.Y = point3_2.Y; } point1_2 -= min2; point2_2 -= min2; point3_2 -= min2; shape2.Position = min2; shape2.SetPoint(0, point1_2); shape2.SetPoint(1, point2_2); //shape2.SetPoint(2, point3_2); shape2.FillColor = Color.Black; mapList.Add(shape2); } for (int i = 0; i < pathList.Count; i++) { int index = (i + 1) % pathList.Count; Vector2f point1 = pathList[i].GetPoint(3) + pathList[i].Position; Vector2f point2 = pathList[index].GetPoint(0) + pathList[index].Position; Vector2f point3 = point1; ConvexShape shape1 = new ConvexShape(2); Vector2f min = point1; if (point2.X < min.X) { min.X = point2.X; } if (point2.Y < min.Y) { min.Y = point2.Y; } if (point3.X < min.X) { min.X = point3.X; } if (point3.Y < min.Y) { min.Y = point3.Y; } point1 -= min; point2 -= min; point3 -= min; shape1.Position = min; shape1.SetPoint(0, point1); shape1.SetPoint(1, point2); //shape1.SetPoint(2, point3); shape1.FillColor = Color.Black; mapList.Add(shape1); Vector2f point1_2 = pathList[i].GetPoint(2) + pathList[i].Position; Vector2f point2_2 = pathList[index].GetPoint(1) + pathList[index].Position; Vector2f point3_2 = point1_2; ConvexShape shape2 = new ConvexShape(2); Vector2f min2 = point1_2; if (point2_2.X < min2.X) { min2.X = point2_2.X; } if (point2_2.Y < min2.Y) { min2.Y = point2_2.Y; } if (point3_2.X < min2.X) { min2.X = point3_2.X; } if (point3_2.Y < min2.Y) { min2.Y = point3_2.Y; } point1_2 -= min2; point2_2 -= min2; point3_2 -= min2; shape2.Position = min2; shape2.SetPoint(0, point1_2); shape2.SetPoint(1, point2_2); //shape2.SetPoint(2, point3_2); shape2.FillColor = Color.Black; mapList.Add(shape2); } path = new CorrectPath(new GroupShape(pathList)); return(mapList); }
public static CollisionInfo CheckCol(ConvexShape shape1, ConvexShape shape2) { Vector2f max = new Vector2f(0, 0); for (uint i = 0; i < shape1.GetPointCount(); i++) { float x = shape1.GetPoint(i).X; if (x > max.X) { max.X = x; } float y = shape1.GetPoint(i).Y; if (y > max.Y) { max.Y = y; } } Vector2f center1 = shape1.Position + .5F * max; max = new Vector2f(0, 0); for (uint i = 0; i < shape2.GetPointCount(); i++) { float x = shape2.GetPoint(i).X; if (x > max.X) { max.X = x; } float y = shape2.GetPoint(i).Y; if (y > max.Y) { max.Y = y; } } Vector2f center2 = shape2.Position + .5F * max; CollisionInfo myInfo = new CollisionInfo(); myInfo.isCollide = true; float intervalDist; float minIntervalDist = float.PositiveInfinity; Vector2f translationNormal = new Vector2f(0, 0); foreach (var polygon in new ConvexShape[] { shape1, shape2 }) { for (int i1 = 0; i1 < polygon.GetPointCount(); i1++) { int i2 = (i1 + 1) % (int)polygon.GetPointCount(); Vector2f p1 = polygon.Transform.TransformPoint(polygon.GetPoint((uint)i1)); Vector2f p2 = polygon.Transform.TransformPoint(polygon.GetPoint((uint)i2)); Vector2f normal = new Vector2f(p2.Y - p1.Y, p1.X - p2.X); normal = Trig.Normalize(normal); float minA = 0; float minB = 0; float maxA = 0; float maxB = 0; ProjectPolygon(normal, shape1, ref minA, ref maxA); ProjectPolygon(normal, shape2, ref minB, ref maxB); intervalDist = IntervalDistance(minA, maxA, minB, maxB); if (minB > maxA || minA > maxB) { myInfo.isCollide = false; break; } intervalDist = Math.Abs(intervalDist); if (intervalDist < minIntervalDist) { minIntervalDist = intervalDist; translationNormal = normal; float d = (minA + maxA) / 2 - (minB + maxB) / 2; if (d < 0) { translationNormal = -translationNormal; } } } if (myInfo.isCollide == false) { break; } } if (myInfo.isCollide) { myInfo.minIntervalDist = minIntervalDist * translationNormal; myInfo.shapes = new List <ConvexShape>() { shape2 }; } return(myInfo); }