Recommended way to build colliders in 1.80+ box2D?

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.

I have also noticed that I can’t actually generate colliders at runtime as part of an optimised compound collider. Instead I have to rely on a fixed sized pool and separate game objects for the ingame editor’s colliders. This may hurt at the collider counts I have. Any advice welcome.

Yes, we are exposing more of the Box2D APIs in the b2d.* namespace, but we are not yet exposing everything (we will in time do this). You will not be able to create shapes at runtime using the APIs.

If you need full Box2D access now I’d recommend that you use the Box2D extension by @d954mas :sob:

This is a complete replacement for the built in physics where you have full control in code.

I think creating physics shapes will be possible, but it is not the same thing as creating a collision object.

Thanks for the replies! Do you have a recommended way to have hundreds / thousands of box colliders at best performance in current Defold?

Is it possible to create and position the physics shapes at runtime? I’m happy if I can spawn those shapws within a collider object, would make runtime editor a bit easier!

Thanks!

Hello again,

I attempted to add a bunch of shapes to an existing collider but unfortunately there seems to be no way to add new shapes, only modify an existing one.

Therefore unless I am mistaken, each collider I spawn dynamically must have it’s own gameobject. In addition I am not sure it can be scaled non uniformly?