Search Unity

Question Does Unity Physics 2D utilize BVH?

Discussion in '2D' started by EyedBread, May 7, 2023.

  1. EyedBread

    EyedBread

    Joined:
    Apr 11, 2023
    Posts:
    1
    Hi, I'm pretty new to Unity so bear with me.

    After running some tests where I try out Unity Physics 2D functions such as raycast2D and OverLapBoxAll2D, I notice that the performance for these 2 functions seem to improve when increasing the distance between the numerous gameObjects that call these increases, leading me to believe that they utilize some kind of Barnes-Hut approximation method, or Bound Volume Hierarchy in other words. I have trouble deciphering the documentation regarding the nature and implementation of these functions. I've also read on unity forums about implementing your own custom BVH to handle physics, but fail to see how that is relevant if Unity already has one.

    I've also noticed that Unity has a BoundingVolumeHierarchy struct in the Physics namespace (https://docs.unity3d.com/Packages/c...pi/Unity.Physics.BoundingVolumeHierarchy.html) , but I'm having trouble deciphering its correlation to raycast and overlapbox functions, and if it is even applicable to the 2D physics. If someone could point me in the direction of where this is mentioned, some source code unity perhaps uses that relates to this, I would be grateful. Does unity even use bounding volume hierarchy for raycasting and overlapbox in 2D? Is there some other alternative method that Unity uses to speed up these functions?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,500
    It's Box2D. The code for that and its broadphase is on Github: b2DynamicTree

    Broadphase raycast.

    As a side-note: If you're talking about performance, don't use the "All" queries; those create an array and it always ends up being thrown at the GC.