Hello,
I’m building a platformer, but my levels aren’t tile based. They’re simply flat, generated shaped meshes from a polygon editor I made.
I now need to build colliders for it. It seems like my options are building convex shapes or a much easier way of doing box coliders for every poly line segment I have. I have a LOT of line segments that need collision so this is a place to optimise.
In Unity you can use Box2D to create your level colliders in the following ways:
PolygonCollider2D (Unity - Manual: Polygon Collider 2D reference)
Collider for 2D physics representing an arbitrary polygon defined by its vertices.
It’s shape is a freeform edge of line segments that you can adjust to fit the shape of a sprite
or any other shape. Note: The edge must enclose an area for the collider to work.
EdgeCollider2D (Unity - Manual: Edge Collider 2D)
Collider for 2D physics representing an arbitrary set of connected edges (lines) defined by its vertices.
The Collider’s shape is a freeform edge made of line segments that you can adjust to fit the shape of a Sprite or any other shape. The Collider’s start and end points do not need to meet or completely enclose an area to function (unlike the [Polygon Collider 2D] can form a straight line or other single edge shape.
As I understand it, both of these methods are significantly more optimised than using hundreds of box colliders per overall level shape. Potentially, I have thousands of box colliders for my levels.
Basically I have done my editor and am able to generate all the required data, and in Unity I’d be using one of the above to handle my level collision needs. If memory serves I recall the author of Unity’s Box2D implementation explaining that these were more optimised than just throwing a bunch of boxes on gameobjects out there.
I want to optimise this! So I am asking for your advice, particularly as Defold is now upgrading the Box2D implementation.