NOTE: The Wisp uses procedural generation to create its map. All images in this blog post will look different as no 2 generations are the same.
Now that The Wisp’s development has concluded, I would like to talk about one of the problems I faced during development: The problem of using too many colliders.
The game uses a 2D lighting system bought off the asset store which draws a mesh that represents a light shining amongst geometry. The system uses Physics2D.Raycast to detect where light is allowed to be. The problem with this, is it needs to check against every collider in the scene.
The performance of this is quite low since it needs to do this every frame, and with multiple light sources as well. The asset we were using was already optimised on its end, so I had to look the other way: the colliders. The best way to achieve this, would be to convert them all into one large collider. The advantage this would yield, would be the single call the physics engine needs to make when each raycast is made.
Luckily, all of the colliders were axis-aligned squares, which meant that if a grid were to be drawn over the scene, the colliders would fit evenly into a certain number of grid spaces.
Since they would fit evenly into a grid, I thought it be best to make the grid full of booleans where true meant a collider was overlapping in that specific grid point. Going over all colliders and setting the overlapping grid points, gave me this:
With this I could now create the collider.
To achieve this, I made another grid, but with an additional column and row. This new grid would act as an outline, where each point in the new grid would store the direction to the next outline grid point.
The result looks like this:
The next thing I did was find the first available direction, go to where it points, and repeat that until I made it back to the starting point. I repeated this until there were no unchecked directions.
I removed the points that had no change in direction, then gave it to the Polygon Collider component:
- The lighting system we were using, I found out didn’t support Polygon Colliders
- The new lighting system I am currently using has this support, but doesn’t have some features.
- I had a loop counter in a method which I hoped would stop infinite while loops., but instead broke the system.