Convex Polygon Collider Support in Tiled2Unity

by Seanba on February 7, 2016

Convex Polygons

I’ve said this before but I think the single best feature in Tiled2Unity is the PolygonCollider2D instances that are created from Tiled layers, where the colliders on each tile are merged into one big polygon. Concave edges? Holes? Not a problem.

Wonder Boy in Monster World, Concave Colliders

In this scene from Wonder Boy in Monster World, the room colliders are just one big polygon with concave edges and a hole in the middle. It’s a great way to represent the collisions but some code may not work with it.

However, some users would like to use the PolygonCollider2D instances created by Tiled2Unity and feed them into another Unity script or plugin that performs pathfinding, or real-time lighting, or whatever. The problem is, these other plugins commonly only work with polygons that are convex.

And by the way, if that’s a plugin you bought with actual money from the Unity Asset Store then I think that’s weak sauce. Tell those guys to fix their tools!

But I digress … with the latest Tiled2Unity version you can now set an option to have only convex polygons exported.

Convex Polygon Option

Note: Convex polygon output can be also be set through the -c or --convex options for those of you using the command line.

This will increase the number of PolygonCollider2D components in your Tiled prefabs but through the Hertel-Mehlhorn Algorithm I’ve made that number “reasonably” low. For most cases I’d be surprised if there was a noticeable change in performance.

Wonder Boy in Monster World, Convex Polygons

With the Convex Polygon Colliders option enabled we have replaced the one “complex” polygon with seven convex polygons. This is an increase in prefab complexity but at least we can use it with other scripts that are limited to convex polygons.

Different Ways to Set Convex Polygon Output

There is some flexibility when it comes to enabling convex polygon exporting:

  • Tiled2Unity Utility: Either use the Convex Polygon Colliders checkbox in the Win32 utility or use the -c|--convex command line option. This will be the preferred setting for exporting polygons for all maps.
  • Tiled Map: Add a property named unity:convex to your Map Properties in Tiled set to true or false. This will be the preferred setting for all the layers in your map and will override the setting from the Tiled2Unity utility.
  • Tiled Layer: Add the unity:convex property to your Layer Properties. This will be the final word on how the polygons in your layer will be exported, overriding the map and utility preferences.

Convex Properties in Tiled Map File

Make sure to keep these precedence rules in mind. If you set unity:convex to true or false in a Tiled layer, for instance, then this will always be obeyed, no matter how you “prefer” to export maps in the Tiled2Unity Utility.

As always (and especially with a new feature such as this) you can email me any bugs you come across and I’ll find the spare time to address them. Just be aware a full-time game development which slows me down at times. 🙂

Special thanks go to the Poly2Tri contributors. Their polygon triangulation library came in handy for this feature.

{ 1 comment… read it below or add one }

Joel February 9, 2016 at 7:08 pm

Thank you thank you THANK YOU for making this tool! It’s exactly what I needed.

Leave a Comment

Previous post:

Next post: