211 while (
m_nodes[index].IsLeaf() ==
false)
223 float cost = 2.0f * combinedArea;
226 float inheritanceCost = 2.0f * (combinedArea - area);
234 cost1 = aabb.GetPerimeter() + inheritanceCost;
241 float newArea = aabb.GetPerimeter();
242 cost1 = (newArea - oldArea) + inheritanceCost;
251 cost2 = aabb.GetPerimeter() + inheritanceCost;
258 float newArea = aabb.GetPerimeter();
259 cost2 = newArea - oldArea + inheritanceCost;
263 if (cost < cost1 && cost < cost2)
279 int32 sibling = index;
292 if (
m_nodes[oldParent].child1 == sibling)
348 if (
m_nodes[parent].child1 == leaf)
360 if (
m_nodes[grandParent].child1 == parent)
372 int32 index = grandParent;
562 float totalArea = 0.0f;
575 return totalArea / rootArea;
591 return 1 +
b2Max(height1, height2);
661 height = 1 +
b2Max(height1, height2);
697 int32 maxBalance = 0;
711 maxBalance =
b2Max(maxBalance, balance);
746 int32 iMin = -1, jMin = -1;
747 for (
int32 i = 0; i < count; ++i)
751 for (
int32 j = i + 1; j < count; ++j)
766 int32 index1 = nodes[iMin];
767 int32 index2 = nodes[jMin];
779 child1->
parent = parentIndex;
780 child2->
parent = parentIndex;
782 nodes[jMin] = nodes[count-1];
783 nodes[iMin] = parentIndex;
void * b2Alloc(int32 size)
Implement this function to use your own memory allocator.
~b2DynamicTree()
Destroy the tree, freeing the node pool.
void Combine(const b2AABB &aabb)
Combine an AABB into this one.
bool MoveProxy(int32 proxyId, const b2AABB &aabb1, const b2Vec2 &displacement)
b2Vec2 lowerBound
the lower vertex
void Validate() const
Validate this tree. For testing.
b2AABB aabb
Enlarged AABB.
void ValidateMetrics(int32 index) const
bool Contains(const b2AABB &aabb) const
Does this aabb contain the provided AABB.
void RemoveLeaf(int32 node)
int32 Balance(int32 index)
void ValidateStructure(int32 index) const
int32 ComputeHeight() const
void DestroyProxy(int32 proxyId)
Destroy a proxy. This asserts if the id is invalid.
void FreeNode(int32 node)
void InsertLeaf(int32 node)
void RebuildBottomUp()
Build an optimal tree. Very expensive. For testing.
void ShiftOrigin(const b2Vec2 &newOrigin)
int32 CreateProxy(const b2AABB &aabb, void *userData)
Create a proxy. Provide a tight fitting AABB and a userData pointer.
b2DynamicTree()
Constructing the tree initializes the node pool.
float GetAreaRatio() const
Get the ratio of the sum of the node areas to the root area.
A node in the dynamic tree. The client does not interact with this directly.
An axis aligned bounding box.
void b2Free(void *mem)
If you implement b2Alloc, you should also implement this function.
float GetPerimeter() const
Get the perimeter length.
#define b2_aabbMultiplier
int32 GetMaxBalance() const
b2Vec2 upperBound
the upper vertex