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

@ -48,8 +48,8 @@ B3_API b3TreeStats b3DynamicTree_Query( const b3DynamicTree* tree, b3AABB aabb,
/// point.
/// @param tree the dynamic tree to query
/// @param point the query point
/// @param maskBits nodes are skipped if the bit-wise AND with the node category bits is zero
/// @param requireAllBits nodes are skipped if the bit-wise AND with the node category bits does not equal the maskBits
/// @param maskBits proxies are skipped if the bit-wise AND with the proxy category bits is zero
/// @param requireAllBits proxies are skipped if the bit-wise AND with the proxy category bits does not equal the maskBits
/// @param callback a user provided instance of b3TreeQueryClosestCallbackFcn
/// @param context a user context object that is provided to the callback
/// @param minDistanceSqr the initial and final minimum squared distance. Provide a small initial to restrict the search and
@ -68,8 +68,8 @@ B3_API b3TreeStats b3DynamicTree_QueryClosest( const b3DynamicTree* tree, b3Vec3
/// However, this filtering may be approximate, so the user should still apply filtering to results.
/// @param tree the dynamic tree to ray cast
/// @param input the ray cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1)
/// @param maskBits bit mask test: `bool accept = (maskBits & node->categoryBits) != 0;`
/// @param requireAllBits modifies bit mask test: `bool accept = (maskBits & node->categoryBits) == maskBits;`
/// @param maskBits bit mask test: `bool accept = (maskBits & proxy->categoryBits) != 0;`
/// @param requireAllBits modifies bit mask test: `bool accept = (maskBits & proxy->categoryBits) == maskBits;`
/// @param callback a callback function that is called for each proxy that is hit by the ray
/// @param context user context that is passed to the callback
/// @return performance data
@ -103,8 +103,8 @@ B3_API int b3DynamicTree_GetByteCount( const b3DynamicTree* tree );
/// Validate this tree. For testing.
B3_API void b3DynamicTree_Validate( const b3DynamicTree* tree );
/// Validate this tree has no enlarged AABBs. For testing.
B3_API void b3DynamicTree_ValidateNoEnlarged( const b3DynamicTree* tree );
/// Validate this tree has no moved nodes. For testing.
B3_API void b3DynamicTree_ValidateNoMoved( const b3DynamicTree* tree );
/// Save this tree to a file for debugging
B3_API void b3DynamicTree_Save( const b3DynamicTree* tree, const char* fileName );
@ -115,13 +115,13 @@ B3_API b3DynamicTree b3DynamicTree_Load( const char* fileName, float scale );
/// Get proxy user data
B3_INLINE uint64_t b3DynamicTree_GetUserData( const b3DynamicTree* tree, int proxyId )
{
return tree->nodes[proxyId].userData;
return tree->proxies[proxyId].userData;
}
/// Get the AABB of a proxy
B3_INLINE b3AABB b3DynamicTree_GetAABB( const b3DynamicTree* tree, int proxyId )
{
return tree->nodes[proxyId].aabb;
return tree->nodes[tree->proxies[proxyId].node].aabb;
}
/**@}*/ // tree

View file

@ -1683,58 +1683,46 @@ typedef struct b3TOIOutput
* @{
*/
/// Flags for tree nodes. For internal usage.
typedef enum b3TreeNodeFlags
{
b3_allocatedNode = 0x0001,
b3_enlargedNode = 0x0002,
b3_leafNode = 0x0004,
} b3TreeNodeFlags;
/// Tree node child indices. For internal usage.
typedef struct b3TreeNodeChildren
{
int child1; ///< child node index 1
int child2; ///< child node index 2
} b3TreeNodeChildren;
/// A node in the dynamic tree. This is private data placed here for performance reasons.
/// todo test padding to 64 bytes to avoid straddling cache lines
/// A node in the dynamic tree. Siblings sit together at an even index so two nodes fit in
/// a 64 byte cache line. The root is at index zero and index one always empty.
typedef struct b3TreeNode
{
/// The node bounding box
b3AABB aabb; // 24
/// Category bits for collision filtering
uint64_t categoryBits; // 8
/// bit 31 : 1 for leaf node
/// bit 30 : 1 for moved flag
/// bits 0-29 : index of the sibling pair node or the proxy id for a leaf
uint32_t flagIndex; // 4
union
{
/// Children (internal node)
b3TreeNodeChildren children;
/// The height of an internal node. A leaf has zero height.
int32_t height;
/// User data (leaf node)
uint64_t userData;
}; // 8
union
{
/// The node parent index (allocated node)
int parent;
/// The node freelist next index (free node)
int next;
/// The shape index for a leaf. Truncated from proxy user data.
int32_t shapeIndex;
}; // 4
/// Height of the node. Leaves have a height of 0.
uint16_t height; // 2
/// @see b3TreeNodeFlags
uint16_t flags; // 2
} b3TreeNode;
/// Separate storage for tree leaves.
typedef struct b3TreeProxy
{
/// User data is an index instead of void* because it is used internally as a shape index.
uint64_t userData;
/// Category bits for collision filtering.
uint64_t categoryBits;
/// The leaf node. B3_NULL_INDEX for a free proxy.
int32_t node;
/// Next free proxy.
int32_t next;
} b3TreeProxy;
/// Dynamic tree version for compatibility testing.
#define B3_DYNAMIC_TREE_VERSION 0x93EDAF889FD30B4Aull
#define B3_DYNAMIC_TREE_VERSION 0x1D6F4C2A73B80E91ull
/// The dynamic tree structure. This should be considered private data.
/// It is placed here for performance reasons.
@ -1744,26 +1732,42 @@ typedef struct b3DynamicTree
/// if the tree is serialized.
uint64_t version;
/// The tree nodes
/// Array of nodes. The root is at index zero and index 1 is empty.
/// Otherwise siblings are paired at even indices. Has holes for free node pairs.
b3TreeNode* nodes;
/// The root index
int root;
/// Parent index per node. The free list is interweaved.
int32_t* parents;
/// The number of nodes
int nodeCount;
/// Proxy data split from node array as cold data.
b3TreeProxy* proxies;
/// Every allocated node has a lower index than this.
int32_t nodeEnd;
/// The allocated node space
int nodeCapacity;
int32_t nodeCapacity;
/// Free pairs below nodeEnd
int32_t pairFreeList;
/// Number of proxies created
int proxyCount;
int32_t proxyCount;
/// Node free list
int freeList;
/// The allocated proxy space
int32_t proxyCapacity;
/// Proxy free list
int32_t proxyFreeList;
/// Array of nodes for rebuild.
b3TreeNode* swapNodes;
/// Leaf indices for rebuild
int* leafIndices;
int32_t* leafIndices;
/// Leaves for the rebuild. May represent a proxy or a retained subtree.
b3TreeNode* leafNodes;
/// Leaf bounding boxes for rebuild
b3AABB* leafBoxes;
@ -1772,10 +1776,14 @@ typedef struct b3DynamicTree
b3Vec3* leafCenters;
/// Bins for sorting during rebuild
int* binIndices;
int32_t* binIndices;
/// Allocated space for rebuilding
int rebuildCapacity;
int32_t rebuildCapacity;
/// Rebuild orders the nodes so the children follow parents. Cache friendly for queries
/// and refitting. The order can be disrupted by proxy creation.
bool dfsOrdered;
} b3DynamicTree;
/// These are performance results returned by dynamic tree queries.
@ -2488,7 +2496,11 @@ typedef struct b3CompoundData
/// Offset of the tree node array in bytes from the struct address.
int nodeOffset;
/// Immutable dynamic tree. The tree node pointer must be fixed up using the node offset
/// Offset of the tree proxy array in bytes from the struct address.
int proxyOffset;
/// Immutable dynamic tree. The node and proxy pointers must be fixed up using the offsets
/// above. A baked tree is never inserted into, so the parent array stays null.
b3DynamicTree tree;
/// Offset of the material array in bytes from the struct address.