Vector2 HandleCollisions(Polygon polygon, Vector2 direction) { Vector2 newPosition = direction * speed * Time.deltaTime; Polygon fake = PolygonManager.Create2D(polygon); fake.Center = newPosition; List <Polygon> collisions = CollidingPolygons(fake); if (collisions == null || collisions.Count <= 0) { return(direction); } for (int i = 0; i < collisions.Count; i++) { if (fake.Intersects(collisions[i])) // It may have been corrected { fake.ProcessCollision(polygon, collisions[i]); } } Vector2 newDirection = -direction + new Vector2(Random.Range(-1, 1), Random.Range(-1, 1)); return(newDirection.normalized); }
void OnDrawGizmos() { if (!Application.isPlaying) { Init(); } Gizmos.DrawWireSphere(polygon.Center, gizmosSize); polygon.DrawEdges(); Polygon fake = PolygonManager.Create2D(polygon); fake.Translate(debugMovement); Gizmos.color = Color.gray; // fake.DrawEdges(); List <Collider2D> collisions = fake.CheckCollisionsAt(fake.Center, polygonMask).ToList();// PolygonManager.CheckCollisions(fake); collisions.Remove(GetComponent <CircleCollider2D>()); for (int i = 0; i < collisions.Count; i++) { Polygon obstacle = new RegularPolygon((CircleCollider2D)collisions[i], vertices); List <Vector2> o_vertices = obstacle.VerticesInside(fake); Gizmos.color = Color.white; obstacle.DrawEdges(); DrawDebugCollision(fake, obstacle); fake.ProcessCollision(polygon, obstacle); Gizmos.color = Color.yellow; fake.DrawEdges(); } }
protected void DrawFakeSolution(Polygon original, Polygon obstacle, Vector2 offset) { Polygon fakeSolution = PolygonManager.Create2D(original); fakeSolution.Translate(offset); fakeSolution.DrawEdges(); List <Vector2> remaining = fakeSolution.VerticesInside(obstacle); Gizmos.color = Color.black; foreach (Vector2 v in obstacle.IntersectionPoints(fakeSolution)) { Gizmos.DrawWireSphere(v, gizmosSize / 2); } foreach (Vector2 v in remaining) { Gizmos.DrawWireSphere(v, gizmosSize / 2); } }
/// <summary> /// Calculates the intersection point of the line AB with a Collider. /// </summary> /// <param name="collider"></param> /// <param name="pointA">First point.</param> /// <param name="pointB">Second point.</param> /// <returns>The intersection points of a line with the edges of a collider.</returns> public static List <Vector2> IntersectionPoints(Collider2D collider, Line2D line) { Polygon polygon = PolygonManager.Create2D(collider); return(polygon.IntersectionPoints(line)); }
/// <summary> /// Calculates the intersection point of the segment AB with a Collider. /// </summary> /// <param name="collider"></param> /// <param name="pointA">First point.</param> /// <param name="pointB">Second point.</param> /// <returns>The intersection points of a segment with the edges of a collider.</returns> public static List <Vector2> IntersectionPoints(Collider2D collider, Segment2D segment) { Polygon polygon = PolygonManager.Create2D(collider); return(polygon.IntersectionPoints(segment)); }