Search Unity

How does physics work internally, and can we implement our own optimizations if needed?

Discussion in 'Physics' started by Steel_Arm, Nov 5, 2018.

  1. Steel_Arm

    Steel_Arm

    Joined:
    Apr 9, 2013
    Posts:
    22
    Basically, what does Unity use for organization of colliders? Octrees (or quadtrees for 2D)? And does unity use a simple AABB test before doing more complex tests such as mesh colliders?

    Mesh colliders (and polygon colliders, especially concave) are more expensive than primitive colliders, this we know.

    A simple AABB (axis aligned bounding box) test is pretty fast. Or even SAT for rotated box colliders.

    Octrees (and quadtrees) are a fast way of collision search. Not in Unity, but in Java I found 1000 rigidbodies colliding with a naive list iteration was about ~20fps, and with a quadtree, the fps went up to ~45fps.

    Take this example: a 2D space game, in an asteroid field (like the game Asteroids), where everything can collide (unlike the game Asteroids). Hundreds of asteroids within range of the player, and maybe even dozens of AI ships.

    The asteroids are procedurally generated, and potentially so are ships (using an L-system). Any arbitrary set of vertices can be triangulated using the Two Ears Theorem, and I want accurate collisions (maybe a giant asteroid the player can fly inside of, highly concave.) Fortunately, triangulation just makes a bunch of triangles to check again. But this can be slow for very large meshes (lots of triangles in a detailed ship, and lots of triangles in a detailed level).

    It makes sense then, that we can minimize our collision checks by first checking bounding boxes, and then only doing more complex checks if the bounding boxes are colliding.

    I just want to know Unity's internals so I can plan accordingly, because I have a high volume of colliders.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,510
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    You're talking about the broadphase which is standard in both 2D/3D physics. 2D physics uses Box2D. Its broadphase code can be found here.
     
  4. Steel_Arm

    Steel_Arm

    Joined:
    Apr 9, 2013
    Posts:
    22
    Thanks everyone. Exactly what I was looking for, just didn't know the terminology.