Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Feedback Add an interface for accessing paths in 2D colliders

Discussion in 'General Discussion' started by Pascal_NotVeryMoe, May 16, 2019.

  1. Pascal_NotVeryMoe

    Pascal_NotVeryMoe

    Joined:
    Sep 11, 2018
    Posts:
    6
    Hey just wanted to put forth a small suggestion,

    I've recently been writing some code to handle casting boxes/spheres against the complex 2D colliders in order to return multiple hits and I've noticed that the CompositeCollider2D and PolygonCollider2D have similar methods for extracting paths. Unfortunately, since there's no common interface that they share, so code working with all types of collision path data require a bit of messy indirection or repeating code.

    My suggestion is to introduce an interface for these classes exposing the following methods:
    Code (csharp):
    1. int pathCount { get; }
    2. int GetPath(int index, List<Vector2> points);
    3. int GetPath(int index, Vector2[] points);
    4. int GetPathPointCount(int index);
    5. GeometryType geometryType { get; }
    Certain methods only exist in one version of the class, such as PolygonCollider2D only having the list variant of GetPath. I believe these would be fairly straightforward to implement for these two classes. GeometryType would need to be introduced to PolygonCollider2D to differentiate the paths from outlines.

    Additionally, I'd also like to suggest extending this to include EdgeCollider2D, as it too defines a path but obviously hasn't included the above methods as it only defines a single path. As such, it'd simply just return a path count of 1. If it were included then an additional method would need to be introduced to differentiate between closed and open paths:
    Code (csharp):
    1. bool pathsAreClosed { get; }
    I'm sure there's some considerations I'm entirely missing here, but, I just thought I'd get this out there - see if there's some interest.

    Anyway, thanks for reading my rambling!