Optimising a level of colliders

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.

all-colliders
All colliders in the scene
zoomed-in-on-colliders
Displaying that the colliders are indeed individual

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:

boolean world grid.png
Grid points where coliders overlapped

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:

collider outline.PNG
Outline of the grid

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:

polygon collider.PNG
Polgon Collider after its creation
zoomed in polygon collider.PNG
Close up of the polygon collider

Problems faced:

  • 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.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s