Search Unity

Creating colliders for complex meshes in an efficient way.

Discussion in 'Physics' started by cubrman, Apr 9, 2019.

  1. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    As I approached making collision for cars in my game, I really started to wonder what are industry's best practices in this area. While I was making collisions for buildings, I made a tool that allowed me to transform collision cubes the same way you can transform shapes in Blender (that is exclusivelly through hotkeys, the tool is called blender actions btw). It was very fast and quiet efficient, but once I started working with cars, I immediately realized that cubifying them the old way would be almost an impossible choire.

    So I wanted to ask if anyone knows about more efficient way of creating collisions for complex objects. I know that Unreal has a tool that automatically creates convex collision meshes for you inside the engine and I wonder if there is a third-party solution or a plugin on the asset store that does the same.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    In cars, the best collider is a single convex volume that roughly resembles the shape of the car, like this:

    upload_2019-4-9_17-34-29.png

    Note how details like the rear spoiler and the mirrors are excluded. This makes a lot sense: imagine the real car colliding laterally. What would happen to the mirror? It would just crush like it wasn't there, and would have absolutely no effect on the collision reaction itself. Thus, it wouldn't make sense to make it part of the collider.

    Many times a single convex shape doesn't fit the shape of the car properly. In these cases you should use several convex colliders resembling the different volumes. The rules I'd apply for these colliders are:
    • A main convex collider should cover the "base" of the car, from the front to the rear, left to right, and covering as much of the height as possible.
    • Other colliders should be "extended" to overlap the main collider.
    Example:

    upload_2019-4-9_17-33-4.png

    Note how the main collider spans all the base of the car, and how the top collider overlaps it. This prevents other objects to get stuck between the colliders, or the car itself to get stuck in the scenery.

    This is an example of what may happen when colliders aren't properly configured in a car:

    upload_2019-4-9_17-41-33.png
    (https://twitter.com/VehiclePhysics/status/1099696972813004801)
     
    JamesArndt and SparrowGS like this.
  3. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    BTW, I create the colliders for the cars in Blender by hand-picking strategically selected faces from the mesh, then invoking (Space) the Convex Hull function. I may then add the Decimate modifier if the result contains too many faces (as convex colliders have a limit of 256 faces), and extrude faces for creating the overlaps if needed.
     
  4. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    Now THAT
    was useful, thank you!
     
    Edy likes this.
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Just wanna add a bit about the mirror example @Edy
    You can place a little collider/trigger on the mirror and OnCollisionEnter

    Q explosion
    Disable mirror graphic
    Etc.
     
  6. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    It should be a trigger and use OnTriggerEnter, not a collider. A collider would affect the car's behavior on collisions and likely cause unrealistic results. For example, imagine the entire car bouncing furiously just because the mirror slightly touched a static light pole. On the other hand, a trigger may implement a visual effect of the mirror breaking off without affecting the physics, as it would happen in reality.
     
    SparrowGS likes this.
  7. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    pretty much what I meant, yeah.

    I did actually mean to use a trigger like you've said, but what if I make it into it's own rigid body and use a fragile joint to connect the two?, to make it actually break off and not just explode into a particle system.
     
  8. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    Yep, that would also work.

    Another possibility with the trigger would be spawning a small rigidbody and collider in runtime when it hits something, but I think the joint method would be more realistic visually.
     
    Last edited: Apr 16, 2019
    SparrowGS likes this.
  9. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    I will say I am not a fan of convex colliders that aren't symmetrical for x (assuming z is forward) for this purpose as it will give inconsistent behaviour.

    Hoping Unity's upgrade to convex colliders will allow for this. If you are around @yant please comment if applicable :)
     
  10. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
    You may have consistent behavior regardless of the collider by configuring the rigidbody's inertia yourself. Inconsistencies may arise when the inertia is computed out of the existing colliders (as physics does by default).

    Here's how I've solved this in Vehicle Physics Pro:
    https://vehiclephysics.com/blocks/inertia/
     
    Last edited: Apr 16, 2019
    hippocoder likes this.