Optimize Broad-Phase (#159)

Broad-phase optimization ported from Box2D.
This commit is contained in:
Erin Catto 2026-09-16 22:28:39 -07:00 • committed by GitHub
commit f555ee4208
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
51 changed files with 3802 additions and 1830 deletions

View file

@ -12,6 +12,8 @@
#include "box3d/box3d.h"
#include "dynamic_tree.h"
#include <queue>
#include <stdio.h>
#include <unordered_map>
@ -88,45 +90,55 @@ public:
}
// Breath-first search to compute node depth values start with 0 at the root.
// The tree is walked a sibling pair at a time, so depth is tracked alongside
// each queued pair rather than looked up through the parent index.
int ComputeDepths()
{
m_depths.resize( m_tree.nodeCapacity, 0 );
if ( m_tree.nodes == nullptr || m_tree.nodeCount == 0 || m_tree.root == B3_NULL_INDEX )
if ( m_tree.nodes == nullptr || m_tree.proxyCount == 0 )
{
return 0;
}
int* queue = (int*)malloc( m_tree.nodeCount * sizeof( int ) );
b3TreeNode* nodes = m_tree.nodes;
m_depths[B3_ROOT_NODE] = 0;
if ( b3IsLeaf( nodes + B3_ROOT_NODE ) )
{
return 0;
}
struct QueueItem
{
int pair;
int depth;
};
QueueItem* queue = (QueueItem*)malloc( m_tree.nodeEnd * sizeof( QueueItem ) );
int front = 0;
int back = 0;
b3TreeNode* nodes = m_tree.nodes;
queue[0] = m_tree.root;
back += 1;
queue[back++] = { b3GetLeftChild( nodes + B3_ROOT_NODE ), 1 };
int depth = 0;
while ( back > front )
{
int index = queue[front];
front += 1;
QueueItem item = queue[front++];
depth = b3MaxInt( depth, item.depth );
b3TreeNode* node = nodes + index;
if ( node->parent == B3_NULL_INDEX )
for ( int i = 0; i < 2; ++i )
{
m_depths[index] = 0;
}
else
{
m_depths[index] = m_depths[node->parent] + 1;
depth = b3MaxInt( depth, m_depths[index] );
}
int index = item.pair + i;
b3TreeNode* node = nodes + index;
m_depths[index] = item.depth;
if ( ( node->flags & b3_leafNode ) == 0 )
{
B3_ASSERT( back < m_tree.nodeCount - 1 );
queue[back++] = node->children.child1;
queue[back++] = node->children.child2;
if ( b3IsLeaf( node ) == false )
{
B3_ASSERT( back < m_tree.nodeEnd );
queue[back++] = { b3GetLeftChild( node ), item.depth + 1 };
}
}
}
@ -284,17 +296,15 @@ public:
int maxArea = 0.0f;
int maxAreaIndex = -1;
for ( int i = 0; i < m_tree.nodeCapacity; ++i )
for ( int i = 0; i < m_tree.nodeEnd; ++i )
{
b3TreeNode* node = m_tree.nodes + i;
if ( ( node->flags & b3_leafNode ) == 0 )
if ( b3IsLeaf( node ) == false || b3IsEmptyNode( node ) )
{
continue;
}
assert( node->flags & b3_allocatedNode );
Proxy proxy = {};
proxy.aabb = node->aabb;
@ -305,8 +315,9 @@ public:
maxAreaIndex = (int)m_proxies.size();
}
proxy.proxyId = i;
m_proxies[node->userData] = proxy;
int proxyId = b3GetProxyId( node );
proxy.proxyId = proxyId;
m_proxies[m_tree.proxies[proxyId].userData] = proxy;
}
printf( "max index = %d\n", maxAreaIndex );
@ -363,7 +374,7 @@ public:
{
srand( 42 );
b3AABB bounds = m_tree.nodes[m_tree.root].aabb;
b3AABB bounds = m_tree.nodes[B3_ROOT_NODE].aabb;
b3Vec3 extents = b3AABB_Extents( bounds );
float radius = ( extents.x + extents.y + extents.z ) / 3.0f;
@ -509,13 +520,11 @@ public:
b3_colorCoral, b3_colorCornflowerBlue, b3_colorCornsilk, b3_colorCrimson, b3_colorCyan,
};
int capacity = m_tree.nodeCapacity;
for ( int i = 0; i < capacity; ++i )
for ( int i = 0; i < m_tree.nodeEnd; ++i )
{
b3TreeNode* node = nodes + i;
if ( m_depths[i] != m_drawLevel || ( node->flags & b3_allocatedNode ) == 0 )
if ( m_depths[i] != m_drawLevel || b3IsEmptyNode( node ) )
{
// skip internal nodes
continue;
}
@ -528,12 +537,13 @@ public:
}
else
{
uint16_t requiredFlags = b3_allocatedNode | b3_leafNode;
for ( int i = 0; i < m_tree.nodeCapacity; ++i )
// Live leaves only. A mark on a leaf says its ancestors still need a refit, the
// leaf box itself is already current, so moved leaves still draw.
for ( int i = 0; i < m_tree.nodeEnd; ++i )
{
b3TreeNode* node = nodes + i;
if ( node->flags != requiredFlags )
if ( b3IsEmptyNode( node ) || b3IsLeaf( node ) == false )
{
continue;
}
@ -544,7 +554,7 @@ public:
continue;
}
Proxy& proxy = m_proxies[node->userData];
Proxy& proxy = m_proxies[m_tree.proxies[b3GetProxyId( node )].userData];
if ( proxy.queryTimeStamp == m_timeStamp || proxy.rayTimeStamp == m_timeStamp )
{
DrawBounds( node->aabb, 0.0f, MakeColor( b3_colorLightGray ) );