Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

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:
    1,230
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,504
    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.