Search Unity

What is the most efficient way to collide with heightmap-based data?

Discussion in 'Physics' started by SLMillion, Jul 15, 2018.

  1. SLMillion

    SLMillion

    Joined:
    Sep 25, 2014
    Posts:
    3
    Hello you lovely Unity forums. I'm working a custom terrain system based on the mesh class, and am running into some questions I may have to work on further down the road. The primary question is - what is the most efficient way to calculate physics with detailed, procedural, height-map systems.

    Seen in the picture below, the grid is uniform, with the only difference being the actual height per vertex, with no concavity, so it's quite simple. Considering this, I thought per-vertex collisions would be overkill, and that I could take some shortcuts. My initial thought was to rearrange four collision planes around the player depending on their position in the grid, simply adjusting their angle and placement to allow proper physics at low performance costs, despite how messy that might be. Thoughts? Should I just bake the mesh colliders at runtime? Any other solutions?
     

    Attached Files:

  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,512
    Hello, and wellcome to the Unity forums!

    In think that PhysX (the underlying physics engine in Unity) can be very efficient if feed properly. In your case, I think that per-vertex collision would be ok as long as the collider is split in smaller pieces, think about squares of 100m x 100m. PhysX uses AABB to determine object pairs that won't make any contact. Having the collider divided that way would optimize the collisions as only the collider pieces that are closer to the player would be considered to their full detail.

    Also, note that having different collision planes and enabling/disabling them in runtime may cause other problems. For example, think on an object that is distant to the player. It will behave in a different way when the player is far away than when the player is close to it. Switching the collision plane would surely cause noticeable "bumps" or "hiccups" in other objects as the player approaches or walks away of them.

    Hope this helps!
     
    SLMillion likes this.