Khaligufzel / Dimensionfall

A survival game inspired by Cataclysm: Dark Days Ahead and Bright Nights.
MIT License
12 stars 5 forks source link

Improve Chunk performance: Collider reduction #254

Closed snipercup closed 2 months ago

snipercup commented 2 months ago

Requires #253

Been wanting to do this for a while and now I had the algorithm to do it. Previously, each block would get it's own collider, creating a maximum of 1024 box colliders per level in a chunk. Now the box shapes are combined, creating fewer collisionshapes.

  1. Slopes are separated from cubes. Slopes still have their own collision shapes and are not reduced in any way.
  2. Blocks are merged horizontally over the x-axis. this creates long boxes of combined cubes
  3. Blocks are merged over the z-axis if their start and end position match.

This algorithm works for our game because we often deal with flat terrain and houses on top of it. Houses have thin walls that are stretched either horizontally or vertically. I considered this when creating the algorithm. It should even work if the player starts digging round into the ground down the line.

It is essential to reduce the amount of colliders, because they can only be added to the scene on the main thread. This will cause strain on the physics server.

Here is an image with the colliders visualized: image

If you look at the garage and the road, you can see there are few lines and big box collisionshapes.