Search Unity

Collision handling; dealing with multiple collisions

Discussion in 'Physics' started by ShiroAisu, Oct 7, 2017.

  1. ShiroAisu

    ShiroAisu

    Joined:
    Jun 21, 2017
    Posts:
    3
    I'm trying to create a character controller from scratch, and right now I'm struggling a bit with collision resolution. I want it to be 3d and to work with mesh colliders (only convex ones), and so far, I've been using the physics engine to detect collisions and penetrations; so far so good. But I can't seem to handle multiple collisions properly, and this presents a bunch of problems. One of the first problems I have, is that a diagonally moving object will get stuck in wall boundaries. Another problem, is the collision handling algorithm pushing the object inside of another object.

    The way I handle this satisfactorily when I work with AABBs, is that I move my object along each axis in turn, and check and resolve the collisions for each axis that way. This will ensure that my object always ends up being in a valid state, and it has the added benefit of preventing tunneling (which is not a huge concern of mine in this case). I can't do this in an OBB scenario, however, since the minimum slope between the axis and a face would mean I couldn't move along it either.

    What's the best way of solving this problem? I've been looking all over the place, but I probably am not aware of what the problem is called, and so couldn't find anything on it.

    Any help is appreciated.
     
  2. EnokV

    EnokV

    Joined:
    Apr 14, 2016
    Posts:
    56
    Physics.OverlapCapsule(or OverlapSphere) and Physics.ComputePenetration to the resque!

    Just check your surroundings with OverlapCapsule to get all nearby colliders and pass them into ComputePenetration to recieve a minimal translation vector used to separate the colliders. It's a matter of
    Code (CSharp):
    1. transform.position += direction * distance;
    When resolving collisions using this method, climbing slopes just comes as a bonus. Slap on some ground clamping and you can walk down them aswell. Enable gravity and suddenly you're sliding. It uses SAT(Separating Axis Theorem) internally.

    I haven't encountered any tunneling so far, and thus haven't really needed to deal with it explicitly.

    This way you don't really need to focus anything on the broadphase either (AABBs) since OverlapCapsule only checks for colliders inside the given volume and it's used within a PxScene where colliders are placed in an octree or something similar.

    You'll have no issue dealing with multiple colliders.

    I'm going to eagerly await the moment where you discover how annoyingly hard it is to get ground detection right with Unity's scripting API, at which point you're more than welcome to complain in this thread about it so we hopefully, at some point, get a proper PhysX exposure:
    https://forum.unity.com/threads/any-plans-on-exposing-more-of-physx-to-the-scripting-api.498464/

    Furthermore I suggest you look at PhilSA's KinematicCharacterController(on asset store) and Iron-Warrior's SuperCharacterController(read his blog, it's super).
     
    scvnathan likes this.