use unsigned integers instead of integers in some collision code
This commit is contained in:
parent
235b59a560
commit
efae0fc202
@ -25,8 +25,8 @@
|
||||
// A pair of broad-phase proxies.
|
||||
struct b3Pair
|
||||
{
|
||||
i32 proxy1;
|
||||
i32 proxy2;
|
||||
u32 proxy1;
|
||||
u32 proxy2;
|
||||
};
|
||||
|
||||
// The broad-phase interface.
|
||||
@ -39,28 +39,28 @@ public:
|
||||
~b3BroadPhase();
|
||||
|
||||
// Create a proxy and return a index to it.
|
||||
i32 CreateProxy(const b3AABB3& aabb, void* userData);
|
||||
u32 CreateProxy(const b3AABB3& aabb, void* userData);
|
||||
|
||||
// Destroy a given proxy and remove it from the broadphase.
|
||||
void DestroyProxy(i32 proxyId);
|
||||
void DestroyProxy(u32 proxyId);
|
||||
|
||||
// Update an existing proxy AABB with a given AABB and a displacement.
|
||||
// displacement = dt * velocity
|
||||
// Return true if the proxy has moved.
|
||||
bool MoveProxy(i32 proxyId, const b3AABB3& aabb, const b3Vec3& displacement);
|
||||
bool MoveProxy(u32 proxyId, const b3AABB3& aabb, const b3Vec3& displacement);
|
||||
|
||||
// Add a proxy to the list of moved proxies.
|
||||
// Only moved proxies will be used internally as an AABB query reference object.
|
||||
void BufferMove(i32 proxyId);
|
||||
void BufferMove(u32 proxyId);
|
||||
|
||||
// Get the AABB of a given proxy.
|
||||
const b3AABB3& GetAABB(i32 proxyId) const;
|
||||
const b3AABB3& GetAABB(u32 proxyId) const;
|
||||
|
||||
// Get the user data attached to a proxy.
|
||||
void* GetUserData(i32 proxyId) const;
|
||||
void* GetUserData(u32 proxyId) const;
|
||||
|
||||
// Test if two proxy AABBs are overlapping.
|
||||
bool TestOverlap(i32 proxy1, i32 proxy2) const;
|
||||
bool TestOverlap(u32 proxy1, u32 proxy2) const;
|
||||
|
||||
// Notify the client callback the AABBs that are overlapping with the passed AABB.
|
||||
template<class T>
|
||||
@ -71,10 +71,11 @@ public:
|
||||
template<class T>
|
||||
void RayCast(T* callback, const b3RayCastInput& input) const;
|
||||
|
||||
// Find and store overlapping AABB pairs.
|
||||
// Notify the client callback the AABB pairs that are overlapping.
|
||||
// The client must store the notified pairs.
|
||||
template<class T>
|
||||
void FindNewPairs(T* callback);
|
||||
void FindPairs(T* callback);
|
||||
|
||||
// Draw the proxy AABBs.
|
||||
void Draw() const;
|
||||
@ -83,17 +84,17 @@ private :
|
||||
|
||||
// The client callback used to add an overlapping pair
|
||||
// to the overlapping pair buffer.
|
||||
bool Report(i32 proxyId);
|
||||
bool Report(u32 proxyId);
|
||||
|
||||
// The dynamic tree.
|
||||
b3DynamicTree m_tree;
|
||||
|
||||
// The current proxy being queried for overlap with another proxies.
|
||||
// It is used to avoid a proxy overlap with itself.
|
||||
i32 m_queryProxyId;
|
||||
u32 m_queryProxyId;
|
||||
|
||||
// The objects that have moved in a step.
|
||||
i32* m_moveBuffer;
|
||||
u32* m_moveBuffer;
|
||||
u32 m_moveBufferCount;
|
||||
u32 m_moveBufferCapacity;
|
||||
|
||||
@ -103,12 +104,12 @@ private :
|
||||
u32 m_pairCount;
|
||||
};
|
||||
|
||||
inline const b3AABB3& b3BroadPhase::GetAABB(i32 proxyId) const
|
||||
inline const b3AABB3& b3BroadPhase::GetAABB(u32 proxyId) const
|
||||
{
|
||||
return m_tree.GetAABB(proxyId);
|
||||
}
|
||||
|
||||
inline void* b3BroadPhase::GetUserData(i32 proxyId) const
|
||||
inline void* b3BroadPhase::GetUserData(u32 proxyId) const
|
||||
{
|
||||
return m_tree.GetUserData(proxyId);
|
||||
}
|
||||
@ -125,7 +126,7 @@ inline void b3BroadPhase::RayCast(T* callback, const b3RayCastInput& input) cons
|
||||
return m_tree.RayCast(callback, input);
|
||||
}
|
||||
|
||||
inline bool operator<(const b3Pair& pair1, const b3Pair& pair2)
|
||||
static B3_FORCE_INLINE bool operator<(const b3Pair& pair1, const b3Pair& pair2)
|
||||
{
|
||||
if (pair1.proxy1 < pair2.proxy1)
|
||||
{
|
||||
@ -141,7 +142,7 @@ inline bool operator<(const b3Pair& pair1, const b3Pair& pair2)
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void b3BroadPhase::FindNewPairs(T* callback)
|
||||
inline void b3BroadPhase::FindPairs(T* callback)
|
||||
{
|
||||
// Reset the overlapping pairs buffer count for the current step.
|
||||
m_pairCount = 0;
|
||||
@ -151,7 +152,7 @@ inline void b3BroadPhase::FindNewPairs(T* callback)
|
||||
{
|
||||
// Keep the current queried proxy ID to avoid self overlapping.
|
||||
m_queryProxyId = m_moveBuffer[i];
|
||||
if (m_queryProxyId == NULL_NODE)
|
||||
if (m_queryProxyId == B3_NULL_NODE_D)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
@ -23,7 +23,7 @@
|
||||
#include <bounce/collision/shapes/aabb3.h>
|
||||
#include <bounce/collision/collision.h>
|
||||
|
||||
#define NULL_NODE (-1)
|
||||
#define B3_NULL_NODE_D (0xFFFFFFFF)
|
||||
|
||||
// AABB tree for dynamic AABBs.
|
||||
class b3DynamicTree
|
||||
@ -33,22 +33,22 @@ public :
|
||||
~b3DynamicTree();
|
||||
|
||||
// Insert a node into the tree and return its ID.
|
||||
i32 InsertNode(const b3AABB3& aabb, void* userData);
|
||||
u32 InsertNode(const b3AABB3& aabb, void* userData);
|
||||
|
||||
// Remove a node from the tree.
|
||||
void RemoveNode(i32 proxyId);
|
||||
void RemoveNode(u32 proxyId);
|
||||
|
||||
// Update a node AABB.
|
||||
void UpdateNode(i32 proxyId, const b3AABB3& aabb);
|
||||
void UpdateNode(u32 proxyId, const b3AABB3& aabb);
|
||||
|
||||
// Get the (fat) AABB of a given proxy.
|
||||
const b3AABB3& GetAABB(i32 proxyId) const;
|
||||
const b3AABB3& GetAABB(u32 proxyId) const;
|
||||
|
||||
// Get the data associated with a given proxy.
|
||||
void* GetUserData(i32 proxyId) const;
|
||||
void* GetUserData(u32 proxyId) const;
|
||||
|
||||
// Check if two aabbs in this tree are overlapping.
|
||||
bool TestOverlap(i32 proxy1, i32 proxy2) const;
|
||||
bool TestOverlap(u32 proxy1, u32 proxy2) const;
|
||||
|
||||
// Keep reporting the client callback the AABBs that are overlapping with
|
||||
// the given AABB. The client callback must return true if the query
|
||||
@ -63,7 +63,7 @@ public :
|
||||
void RayCast(T* callback, const b3RayCastInput& input) const;
|
||||
|
||||
// Validate a given node of this tree.
|
||||
void Validate(i32 node) const;
|
||||
void Validate(u32 node) const;
|
||||
|
||||
// Draw this tree.
|
||||
void Draw() const;
|
||||
@ -73,8 +73,8 @@ private :
|
||||
// Is this node a leaf?
|
||||
bool IsLeaf() const
|
||||
{
|
||||
//A node is a leaf if child 2 == NULL_NODE or height == 0.
|
||||
return child1 == NULL_NODE;
|
||||
//A node is a leaf if child 2 == B3_NULL_NODE_D or height == 0.
|
||||
return child1 == B3_NULL_NODE_D;
|
||||
}
|
||||
|
||||
// The fattened node AABB.
|
||||
@ -85,80 +85,81 @@ private :
|
||||
|
||||
union
|
||||
{
|
||||
i32 parent;
|
||||
i32 next;
|
||||
u32 parent;
|
||||
u32 next;
|
||||
};
|
||||
|
||||
i32 child1;
|
||||
i32 child2;
|
||||
u32 child1;
|
||||
u32 child2;
|
||||
|
||||
// Flag
|
||||
// leaf if 0, free node if -1
|
||||
i32 height;
|
||||
};
|
||||
|
||||
// Insert a node into the tree.
|
||||
void InsertLeaf(i32 node);
|
||||
void InsertLeaf(u32 node);
|
||||
|
||||
// Remove a node from the tree.
|
||||
void RemoveLeaf(i32 node);
|
||||
void RemoveLeaf(u32 node);
|
||||
|
||||
// Rebuild the hierarchy starting from the given node.
|
||||
void WalkBackNodeAndCombineVolumes(i32 node);
|
||||
void WalkBackNodeAndCombineVolumes(u32 node);
|
||||
|
||||
// Find the best node that can be merged with a given AABB.
|
||||
i32 FindBest(const b3AABB3& aabb) const;
|
||||
u32 FindBest(const b3AABB3& aabb) const;
|
||||
|
||||
// Peel a node from the free list and insert into the node array.
|
||||
// Allocate a new node if necessary. The function returns the new node index.
|
||||
i32 AllocateNode();
|
||||
u32 AllocateNode();
|
||||
|
||||
// Free a node from the node pool and add it to the free list.
|
||||
void FreeNode(i32 node);
|
||||
void FreeNode(u32 node);
|
||||
|
||||
// Make a node available for the next allocation.
|
||||
void AddToFreeList(i32 node);
|
||||
void AddToFreeList(u32 node);
|
||||
|
||||
// The root of this tree.
|
||||
i32 m_root;
|
||||
u32 m_root;
|
||||
|
||||
// The nodes of this tree stored in an array.
|
||||
b3Node* m_nodes;
|
||||
i32 m_nodeCount;
|
||||
i32 m_nodeCapacity;
|
||||
i32 m_freeList;
|
||||
u32 m_nodeCount;
|
||||
u32 m_nodeCapacity;
|
||||
u32 m_freeList;
|
||||
};
|
||||
|
||||
inline const b3AABB3& b3DynamicTree::GetAABB(i32 proxyId) const
|
||||
inline const b3AABB3& b3DynamicTree::GetAABB(u32 proxyId) const
|
||||
{
|
||||
B3_ASSERT(proxyId != NULL_NODE && proxyId < m_nodeCapacity);
|
||||
B3_ASSERT(proxyId != B3_NULL_NODE_D && proxyId < m_nodeCapacity);
|
||||
return m_nodes[proxyId].aabb;
|
||||
}
|
||||
|
||||
inline void* b3DynamicTree::GetUserData(i32 proxyId) const
|
||||
inline void* b3DynamicTree::GetUserData(u32 proxyId) const
|
||||
{
|
||||
B3_ASSERT(proxyId != NULL_NODE && proxyId < m_nodeCapacity);
|
||||
B3_ASSERT(proxyId != B3_NULL_NODE_D && proxyId < m_nodeCapacity);
|
||||
return m_nodes[proxyId].userData;
|
||||
}
|
||||
|
||||
inline bool b3DynamicTree::TestOverlap(i32 proxy1, i32 proxy2) const
|
||||
inline bool b3DynamicTree::TestOverlap(u32 proxy1, u32 proxy2) const
|
||||
{
|
||||
B3_ASSERT(proxy1 != NULL_NODE && proxy1 < m_nodeCapacity);
|
||||
B3_ASSERT(proxy2 != NULL_NODE && proxy2 < m_nodeCapacity);
|
||||
B3_ASSERT(proxy1 != B3_NULL_NODE_D && proxy1 < m_nodeCapacity);
|
||||
B3_ASSERT(proxy2 != B3_NULL_NODE_D && proxy2 < m_nodeCapacity);
|
||||
return b3TestOverlap(m_nodes[proxy1].aabb, m_nodes[proxy2].aabb);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
inline void b3DynamicTree::QueryAABB(T* callback, const b3AABB3& aabb) const
|
||||
{
|
||||
b3Stack<i32, 256> stack;
|
||||
b3Stack<u32, 256> stack;
|
||||
stack.Push(m_root);
|
||||
|
||||
while (stack.IsEmpty() == false)
|
||||
{
|
||||
i32 nodeIndex = stack.Top();
|
||||
u32 nodeIndex = stack.Top();
|
||||
stack.Pop();
|
||||
|
||||
if (nodeIndex == NULL_NODE)
|
||||
if (nodeIndex == B3_NULL_NODE_D)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@ -194,16 +195,16 @@ inline void b3DynamicTree::RayCast(T* callback, const b3RayCastInput& input) con
|
||||
// Ensure non-degenerate segment.
|
||||
B3_ASSERT(b3Dot(d, d) > B3_EPSILON * B3_EPSILON);
|
||||
|
||||
b3Stack<i32, 256> stack;
|
||||
b3Stack<u32, 256> stack;
|
||||
stack.Push(m_root);
|
||||
|
||||
while (stack.IsEmpty() == false)
|
||||
{
|
||||
i32 nodeIndex = stack.Top();
|
||||
u32 nodeIndex = stack.Top();
|
||||
|
||||
stack.Pop();
|
||||
|
||||
if (nodeIndex == NULL_NODE)
|
||||
if (nodeIndex == B3_NULL_NODE_D)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
@ -23,7 +23,7 @@
|
||||
#include <bounce/collision/shapes/aabb3.h>
|
||||
#include <bounce/collision/collision.h>
|
||||
|
||||
#define NULL_NODE_S (0xFFFFFFFF)
|
||||
#define B3_NULL_NODE_S (0xFFFFFFFF)
|
||||
|
||||
// AABB tree for static AABBs.
|
||||
class b3StaticTree
|
||||
@ -72,7 +72,7 @@ private :
|
||||
// Is this node a leaf?
|
||||
bool IsLeaf() const
|
||||
{
|
||||
return child1 == NULL_NODE_S;
|
||||
return child1 == B3_NULL_NODE_S;
|
||||
}
|
||||
};
|
||||
|
||||
@ -114,7 +114,7 @@ inline void b3StaticTree::QueryAABB(T* callback, const b3AABB3& aabb) const
|
||||
{
|
||||
u32 nodeIndex = stack.Top();
|
||||
|
||||
if (nodeIndex == NULL_NODE_S)
|
||||
if (nodeIndex == B3_NULL_NODE_S)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@ -164,10 +164,10 @@ inline void b3StaticTree::RayCast(T* callback, const b3RayCastInput& input) cons
|
||||
|
||||
while (stack.IsEmpty() == false)
|
||||
{
|
||||
i32 nodeIndex = stack.Top();
|
||||
u32 nodeIndex = stack.Top();
|
||||
stack.Pop();
|
||||
|
||||
if (nodeIndex == NULL_NODE_S)
|
||||
if (nodeIndex == B3_NULL_NODE_S)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
@ -258,9 +258,9 @@ inline b3Quat b3Mat33Quat(const b3Mat33& m)
|
||||
}
|
||||
|
||||
// Diagonal is negative.
|
||||
const i32 next[3] = { 1, 2, 0 };
|
||||
const u32 next[3] = { 1, 2, 0 };
|
||||
|
||||
i32 i = 0;
|
||||
u32 i = 0;
|
||||
|
||||
if (m[1][1] > m[0][0])
|
||||
{
|
||||
@ -272,8 +272,8 @@ inline b3Quat b3Mat33Quat(const b3Mat33& m)
|
||||
i = 2;
|
||||
}
|
||||
|
||||
i32 j = next[i];
|
||||
i32 k = next[j];
|
||||
u32 j = next[i];
|
||||
u32 k = next[j];
|
||||
|
||||
float32 s = sqrt((m[i][i] - (m[j][j] + m[k][k])) + 1.0f);
|
||||
|
||||
|
@ -41,6 +41,8 @@ typedef float float32;
|
||||
#define B3_MAX_FLOAT (FLT_MAX)
|
||||
#define B3_EPSILON (FLT_EPSILON)
|
||||
|
||||
#define B3_MAX_U32 (0xFFFFFFFF)
|
||||
|
||||
// Collision
|
||||
|
||||
// How much an AABB in the broad-phase should be extended by
|
||||
|
@ -325,7 +325,7 @@ private:
|
||||
bool ShouldCollide(const b3Body* other) const;
|
||||
|
||||
b3BodyType m_type;
|
||||
i32 m_islandID;
|
||||
u32 m_islandID;
|
||||
u32 m_flags;
|
||||
float32 m_sleepTime;
|
||||
|
||||
|
@ -140,7 +140,7 @@ public:
|
||||
void SetUserData(void* data);
|
||||
|
||||
// Dump this shape to the log file.
|
||||
void Dump(i32 bodyIndex) const;
|
||||
void Dump(u32 bodyIndex) const;
|
||||
|
||||
// Get the next shape in the body shape list.
|
||||
const b3Shape* GetNext() const;
|
||||
@ -169,7 +169,7 @@ protected:
|
||||
float32 m_density;
|
||||
float32 m_restitution;
|
||||
float32 m_friction;
|
||||
i32 m_broadPhaseID;
|
||||
u32 m_broadPhaseID;
|
||||
|
||||
// Contact edges for this shape contact graph.
|
||||
b3List2<b3ContactEdge> m_contactEdges;
|
||||
|
@ -21,8 +21,8 @@
|
||||
b3BroadPhase::b3BroadPhase()
|
||||
{
|
||||
m_moveBufferCapacity = 16;
|
||||
m_moveBuffer = (i32*)b3Alloc(m_moveBufferCapacity * sizeof(i32));
|
||||
memset(m_moveBuffer, 0, m_moveBufferCapacity * sizeof(i32));
|
||||
m_moveBuffer = (u32*)b3Alloc(m_moveBufferCapacity * sizeof(u32));
|
||||
memset(m_moveBuffer, 0, m_moveBufferCapacity * sizeof(u32));
|
||||
m_moveBufferCount = 0;
|
||||
|
||||
m_pairCapacity = 16;
|
||||
@ -37,7 +37,7 @@ b3BroadPhase::~b3BroadPhase()
|
||||
b3Free(m_pairs);
|
||||
}
|
||||
|
||||
void b3BroadPhase::BufferMove(i32 proxyId)
|
||||
void b3BroadPhase::BufferMove(u32 proxyId)
|
||||
{
|
||||
// The proxy has been moved. Add it to the buffer of moved proxies.
|
||||
// Check capacity.
|
||||
@ -46,9 +46,9 @@ void b3BroadPhase::BufferMove(i32 proxyId)
|
||||
// Duplicate capacity.
|
||||
m_moveBufferCapacity *= 2;
|
||||
|
||||
i32* oldMoveBuffer = m_moveBuffer;
|
||||
m_moveBuffer = (i32*)b3Alloc(m_moveBufferCapacity * sizeof(i32));
|
||||
memcpy(m_moveBuffer, oldMoveBuffer, m_moveBufferCount * sizeof(i32));
|
||||
u32* oldMoveBuffer = m_moveBuffer;
|
||||
m_moveBuffer = (u32*)b3Alloc(m_moveBufferCapacity * sizeof(u32));
|
||||
memcpy(m_moveBuffer, oldMoveBuffer, m_moveBufferCount * sizeof(u32));
|
||||
b3Free(oldMoveBuffer);
|
||||
}
|
||||
|
||||
@ -57,12 +57,12 @@ void b3BroadPhase::BufferMove(i32 proxyId)
|
||||
++m_moveBufferCount;
|
||||
}
|
||||
|
||||
bool b3BroadPhase::TestOverlap(i32 proxy1, i32 proxy2) const
|
||||
bool b3BroadPhase::TestOverlap(u32 proxy1, u32 proxy2) const
|
||||
{
|
||||
return m_tree.TestOverlap(proxy1, proxy2);
|
||||
}
|
||||
|
||||
i32 b3BroadPhase::CreateProxy(const b3AABB3& aabb, void* userData)
|
||||
u32 b3BroadPhase::CreateProxy(const b3AABB3& aabb, void* userData)
|
||||
{
|
||||
// Later, if the node aabb has changed then it should be reinserted into the tree.
|
||||
// However, this can be expansive due to the hierarchy reconstruction.
|
||||
@ -70,17 +70,17 @@ i32 b3BroadPhase::CreateProxy(const b3AABB3& aabb, void* userData)
|
||||
// so we can check later if the new (original) AABB is inside the old (fat) AABB.
|
||||
b3AABB3 fatAABB = aabb;
|
||||
fatAABB.Extend(B3_AABB_EXTENSION);
|
||||
i32 proxyId = m_tree.InsertNode(fatAABB, userData);
|
||||
u32 proxyId = m_tree.InsertNode(fatAABB, userData);
|
||||
BufferMove(proxyId);
|
||||
return proxyId;
|
||||
}
|
||||
|
||||
void b3BroadPhase::DestroyProxy(i32 proxyId)
|
||||
void b3BroadPhase::DestroyProxy(u32 proxyId)
|
||||
{
|
||||
return m_tree.RemoveNode(proxyId);
|
||||
}
|
||||
|
||||
bool b3BroadPhase::MoveProxy(i32 proxyId, const b3AABB3& aabb, const b3Vec3& displacement)
|
||||
bool b3BroadPhase::MoveProxy(u32 proxyId, const b3AABB3& aabb, const b3Vec3& displacement)
|
||||
{
|
||||
if (m_tree.GetAABB(proxyId).Contains(aabb))
|
||||
{
|
||||
@ -133,7 +133,7 @@ bool b3BroadPhase::MoveProxy(i32 proxyId, const b3AABB3& aabb, const b3Vec3& dis
|
||||
return true;
|
||||
}
|
||||
|
||||
bool b3BroadPhase::Report(i32 proxyId)
|
||||
bool b3BroadPhase::Report(u32 proxyId)
|
||||
{
|
||||
if (proxyId == m_queryProxyId)
|
||||
{
|
||||
|
@ -21,7 +21,7 @@
|
||||
|
||||
b3DynamicTree::b3DynamicTree()
|
||||
{
|
||||
m_root = NULL_NODE;
|
||||
m_root = B3_NULL_NODE_D;
|
||||
|
||||
// Preallocate 32 nodes.
|
||||
m_nodeCapacity = 32;
|
||||
@ -40,9 +40,11 @@ b3DynamicTree::~b3DynamicTree()
|
||||
}
|
||||
|
||||
// Return a node from the pool.
|
||||
i32 b3DynamicTree::AllocateNode()
|
||||
u32 b3DynamicTree::AllocateNode()
|
||||
{
|
||||
if (m_freeList == NULL_NODE)
|
||||
B3_ASSERT(m_nodeCapacity > 0);
|
||||
|
||||
if (m_freeList == B3_NULL_NODE_D)
|
||||
{
|
||||
B3_ASSERT(m_nodeCount == m_nodeCapacity);
|
||||
|
||||
@ -60,13 +62,13 @@ i32 b3DynamicTree::AllocateNode()
|
||||
}
|
||||
|
||||
// Grab the free node.
|
||||
i32 node = m_freeList;
|
||||
u32 node = m_freeList;
|
||||
|
||||
m_freeList = m_nodes[node].next;
|
||||
|
||||
m_nodes[node].parent = NULL_NODE;
|
||||
m_nodes[node].child1 = NULL_NODE;
|
||||
m_nodes[node].child2 = NULL_NODE;
|
||||
m_nodes[node].parent = B3_NULL_NODE_D;
|
||||
m_nodes[node].child1 = B3_NULL_NODE_D;
|
||||
m_nodes[node].child2 = B3_NULL_NODE_D;
|
||||
m_nodes[node].height = 0;
|
||||
m_nodes[node].userData = NULL;
|
||||
|
||||
@ -75,35 +77,37 @@ i32 b3DynamicTree::AllocateNode()
|
||||
return node;
|
||||
}
|
||||
|
||||
void b3DynamicTree::FreeNode(i32 node)
|
||||
void b3DynamicTree::FreeNode(u32 node)
|
||||
{
|
||||
B3_ASSERT(node != NULL_NODE && node < m_nodeCapacity);
|
||||
B3_ASSERT(node != B3_NULL_NODE_D && node < m_nodeCapacity);
|
||||
m_nodes[node].next = m_freeList;
|
||||
m_nodes[node].height = -1;
|
||||
m_freeList = node;
|
||||
--m_nodeCount;
|
||||
}
|
||||
|
||||
void b3DynamicTree::AddToFreeList(i32 node)
|
||||
void b3DynamicTree::AddToFreeList(u32 node)
|
||||
{
|
||||
B3_ASSERT(m_nodeCapacity > 0);
|
||||
|
||||
// Starting from the given node, relink the linked list of nodes.
|
||||
for (i32 i = node; i < m_nodeCapacity - 1; ++i)
|
||||
for (u32 i = node; i < m_nodeCapacity - 1; ++i)
|
||||
{
|
||||
m_nodes[i].next = i + 1;
|
||||
m_nodes[i].height = -1;
|
||||
}
|
||||
|
||||
m_nodes[m_nodeCapacity - 1].next = NULL_NODE;
|
||||
m_nodes[m_nodeCapacity - 1].next = B3_NULL_NODE_D;
|
||||
m_nodes[m_nodeCapacity - 1].height = -1;
|
||||
|
||||
// Make the node available for the next allocation.
|
||||
m_freeList = node;
|
||||
}
|
||||
|
||||
i32 b3DynamicTree::InsertNode(const b3AABB3& aabb, void* userData)
|
||||
u32 b3DynamicTree::InsertNode(const b3AABB3& aabb, void* userData)
|
||||
{
|
||||
// Insert into the array.
|
||||
i32 node = AllocateNode();
|
||||
u32 node = AllocateNode();
|
||||
m_nodes[node].aabb = aabb;
|
||||
m_nodes[node].userData = userData;
|
||||
m_nodes[node].height = 0;
|
||||
@ -115,7 +119,7 @@ i32 b3DynamicTree::InsertNode(const b3AABB3& aabb, void* userData)
|
||||
return node;
|
||||
}
|
||||
|
||||
void b3DynamicTree::RemoveNode(i32 proxyId)
|
||||
void b3DynamicTree::RemoveNode(u32 proxyId)
|
||||
{
|
||||
// Remove from the tree.
|
||||
RemoveLeaf(proxyId);
|
||||
@ -123,9 +127,9 @@ void b3DynamicTree::RemoveNode(i32 proxyId)
|
||||
FreeNode(proxyId);
|
||||
}
|
||||
|
||||
void b3DynamicTree::UpdateNode(i32 proxyId, const b3AABB3& aabb)
|
||||
void b3DynamicTree::UpdateNode(u32 proxyId, const b3AABB3& aabb)
|
||||
{
|
||||
B3_ASSERT(m_root != NULL_NODE);
|
||||
B3_ASSERT(m_root != B3_NULL_NODE_D);
|
||||
B3_ASSERT(m_nodes[proxyId].IsLeaf());
|
||||
// Remove old AABB from the tree.
|
||||
RemoveLeaf(proxyId);
|
||||
@ -134,13 +138,9 @@ void b3DynamicTree::UpdateNode(i32 proxyId, const b3AABB3& aabb)
|
||||
InsertLeaf(proxyId);
|
||||
}
|
||||
|
||||
i32 b3DynamicTree::FindBest(const b3AABB3& leafAABB) const
|
||||
u32 b3DynamicTree::FindBest(const b3AABB3& leafAABB) const
|
||||
{
|
||||
// To find a good branch node, the manhattan distance could be used as heuristic.
|
||||
// However, the current propagated node and the leaf node volume are incompletely considerable.
|
||||
// Therefore, an approximation of the surface are heuristic (SAH) is used.
|
||||
|
||||
i32 index = m_root;
|
||||
u32 index = m_root;
|
||||
while (!m_nodes[index].IsLeaf())
|
||||
{
|
||||
float32 branchArea = m_nodes[index].aabb.SurfaceArea();
|
||||
@ -155,8 +155,8 @@ i32 b3DynamicTree::FindBest(const b3AABB3& leafAABB) const
|
||||
float32 inheritanceCost = 2.0f * (combinedArea - branchArea);
|
||||
|
||||
// The branch node child nodes cost.
|
||||
i32 child1 = m_nodes[index].child1;
|
||||
i32 child2 = m_nodes[index].child2;
|
||||
u32 child1 = m_nodes[index].child1;
|
||||
u32 child2 = m_nodes[index].child2;
|
||||
|
||||
// Cost of descending onto child1.
|
||||
float32 childCost1 = 0.0f;
|
||||
@ -201,14 +201,14 @@ i32 b3DynamicTree::FindBest(const b3AABB3& leafAABB) const
|
||||
return index;
|
||||
}
|
||||
|
||||
void b3DynamicTree::InsertLeaf(i32 leaf)
|
||||
void b3DynamicTree::InsertLeaf(u32 leaf)
|
||||
{
|
||||
if (m_root == NULL_NODE)
|
||||
if (m_root == B3_NULL_NODE_D)
|
||||
{
|
||||
// If this tree root node is empty then just set the leaf
|
||||
// node to it.
|
||||
m_root = leaf;
|
||||
m_nodes[m_root].parent = NULL_NODE;
|
||||
m_nodes[m_root].parent = B3_NULL_NODE_D;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -216,12 +216,12 @@ void b3DynamicTree::InsertLeaf(i32 leaf)
|
||||
b3AABB3 leafAabb = m_nodes[leaf].aabb;
|
||||
|
||||
// Search for the best branch node of this tree starting from the tree root node.
|
||||
i32 sibling = FindBest(leafAabb);
|
||||
u32 sibling = FindBest(leafAabb);
|
||||
|
||||
i32 oldParent = m_nodes[sibling].parent;
|
||||
u32 oldParent = m_nodes[sibling].parent;
|
||||
|
||||
// Create and setup new parent.
|
||||
i32 newParent = AllocateNode();
|
||||
u32 newParent = AllocateNode();
|
||||
m_nodes[newParent].parent = oldParent;
|
||||
m_nodes[newParent].child1 = sibling;
|
||||
m_nodes[sibling].parent = newParent;
|
||||
@ -231,7 +231,7 @@ void b3DynamicTree::InsertLeaf(i32 leaf)
|
||||
m_nodes[newParent].aabb = b3Combine(leafAabb, m_nodes[sibling].aabb);
|
||||
m_nodes[newParent].height = m_nodes[sibling].height + 1;
|
||||
|
||||
if (oldParent != NULL_NODE)
|
||||
if (oldParent != B3_NULL_NODE_D)
|
||||
{
|
||||
// The sibling was not the root.
|
||||
// Find which child node of the old parent is the sibling
|
||||
@ -256,17 +256,17 @@ void b3DynamicTree::InsertLeaf(i32 leaf)
|
||||
WalkBackNodeAndCombineVolumes(newParent);
|
||||
}
|
||||
|
||||
void b3DynamicTree::RemoveLeaf(i32 leaf)
|
||||
void b3DynamicTree::RemoveLeaf(u32 leaf)
|
||||
{
|
||||
if (leaf == m_root)
|
||||
{
|
||||
m_root = NULL_NODE;
|
||||
m_root = B3_NULL_NODE_D;
|
||||
return;
|
||||
}
|
||||
|
||||
i32 parent = m_nodes[leaf].parent;
|
||||
i32 grandParent = m_nodes[parent].parent;
|
||||
i32 sibling;
|
||||
u32 parent = m_nodes[leaf].parent;
|
||||
u32 grandParent = m_nodes[parent].parent;
|
||||
u32 sibling;
|
||||
if (m_nodes[parent].child1 == leaf)
|
||||
{
|
||||
sibling = m_nodes[parent].child2;
|
||||
@ -276,7 +276,7 @@ void b3DynamicTree::RemoveLeaf(i32 leaf)
|
||||
sibling = m_nodes[parent].child1;
|
||||
}
|
||||
|
||||
if (grandParent != NULL_NODE)
|
||||
if (grandParent != B3_NULL_NODE_D)
|
||||
{
|
||||
if (m_nodes[grandParent].child1 == parent)
|
||||
{
|
||||
@ -297,23 +297,23 @@ void b3DynamicTree::RemoveLeaf(i32 leaf)
|
||||
else
|
||||
{
|
||||
m_root = sibling;
|
||||
m_nodes[sibling].parent = NULL_NODE;
|
||||
m_nodes[sibling].parent = B3_NULL_NODE_D;
|
||||
// Remove parent node.
|
||||
FreeNode(parent);
|
||||
}
|
||||
}
|
||||
|
||||
void b3DynamicTree::WalkBackNodeAndCombineVolumes(i32 node)
|
||||
void b3DynamicTree::WalkBackNodeAndCombineVolumes(u32 node)
|
||||
{
|
||||
while (node != NULL_NODE)
|
||||
while (node != B3_NULL_NODE_D)
|
||||
{
|
||||
//@todo node = Balance(node);
|
||||
|
||||
i32 child1 = m_nodes[node].child1;
|
||||
i32 child2 = m_nodes[node].child2;
|
||||
u32 child1 = m_nodes[node].child1;
|
||||
u32 child2 = m_nodes[node].child2;
|
||||
|
||||
B3_ASSERT(child1 != NULL_NODE);
|
||||
B3_ASSERT(child2 != NULL_NODE);
|
||||
B3_ASSERT(child1 != B3_NULL_NODE_D);
|
||||
B3_ASSERT(child2 != B3_NULL_NODE_D);
|
||||
|
||||
m_nodes[node].height = 1 + b3Max(m_nodes[child1].height, m_nodes[child2].height);
|
||||
m_nodes[node].aabb = b3Combine(m_nodes[child1].aabb, m_nodes[child2].aabb);
|
||||
@ -322,9 +322,9 @@ void b3DynamicTree::WalkBackNodeAndCombineVolumes(i32 node)
|
||||
}
|
||||
}
|
||||
|
||||
void b3DynamicTree::Validate(i32 nodeID) const
|
||||
void b3DynamicTree::Validate(u32 nodeID) const
|
||||
{
|
||||
if (nodeID == NULL_NODE)
|
||||
if (nodeID == B3_NULL_NODE_D)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -332,19 +332,19 @@ void b3DynamicTree::Validate(i32 nodeID) const
|
||||
// The root node has no parent.
|
||||
if (nodeID == m_root)
|
||||
{
|
||||
B3_ASSERT(m_nodes[nodeID].parent == NULL_NODE);
|
||||
B3_ASSERT(m_nodes[nodeID].parent == B3_NULL_NODE_D);
|
||||
}
|
||||
|
||||
const b3Node* node = m_nodes + nodeID;
|
||||
|
||||
i32 child1 = node->child1;
|
||||
i32 child2 = node->child2;
|
||||
u32 child1 = node->child1;
|
||||
u32 child2 = node->child2;
|
||||
|
||||
if (node->IsLeaf())
|
||||
{
|
||||
// Leaf nodes has no children and its height is zero.
|
||||
B3_ASSERT(child1 == NULL_NODE);
|
||||
B3_ASSERT(child2 == NULL_NODE);
|
||||
B3_ASSERT(child1 == B3_NULL_NODE_D);
|
||||
B3_ASSERT(child2 == B3_NULL_NODE_D);
|
||||
B3_ASSERT(node->height == 0);
|
||||
}
|
||||
else
|
||||
@ -370,15 +370,15 @@ void b3DynamicTree::Draw() const
|
||||
return;
|
||||
}
|
||||
|
||||
b3Stack<i32, 256> stack;
|
||||
b3Stack<u32, 256> stack;
|
||||
stack.Push(m_root);
|
||||
|
||||
while (!stack.IsEmpty())
|
||||
{
|
||||
i32 nodeIndex = stack.Top();
|
||||
u32 nodeIndex = stack.Top();
|
||||
stack.Pop();
|
||||
|
||||
if (nodeIndex == NULL_NODE)
|
||||
if (nodeIndex == B3_NULL_NODE_D)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
@ -120,7 +120,7 @@ void b3StaticTree::Build(const b3AABB3* set, b3Node* node, u32* ids, u32 count,
|
||||
if (count <= minObjectsPerLeaf)
|
||||
{
|
||||
++leafCount;
|
||||
node->child1 = NULL_NODE_S;
|
||||
node->child1 = B3_NULL_NODE_S;
|
||||
node->index = ids[0];
|
||||
}
|
||||
else
|
||||
|
@ -83,7 +83,6 @@ b3Body::b3Body(const b3BodyDef& def, b3World* world)
|
||||
m_angularDamping = def.angularDamping;
|
||||
m_gravityScale = def.gravityScale;
|
||||
m_userData = def.userData;
|
||||
m_islandID = -1;
|
||||
m_sleepTime = 0.0f;
|
||||
}
|
||||
|
||||
@ -464,7 +463,7 @@ void b3Body::SetType(b3BodyType type)
|
||||
|
||||
void b3Body::Dump() const
|
||||
{
|
||||
i32 bodyIndex = m_islandID;
|
||||
u32 bodyIndex = m_islandID;
|
||||
|
||||
b3Log(" {\n");
|
||||
b3Log(" b3BodyDef bd;\n");
|
||||
|
@ -154,7 +154,7 @@ void b3ContactManager::SynchronizeShapes()
|
||||
// Find potentially overlapping shape pairs.
|
||||
void b3ContactManager::FindNewContacts()
|
||||
{
|
||||
m_broadPhase.FindNewPairs(this);
|
||||
m_broadPhase.FindPairs(this);
|
||||
|
||||
b3MeshContactLink* c = m_meshContactList.m_head;
|
||||
while (c)
|
||||
@ -175,11 +175,11 @@ void b3ContactManager::UpdateContacts()
|
||||
b3OverlappingPair* pair = &c->m_pair;
|
||||
|
||||
b3Shape* shapeA = pair->shapeA;
|
||||
i32 proxyA = shapeA->m_broadPhaseID;
|
||||
u32 proxyA = shapeA->m_broadPhaseID;
|
||||
b3Body* bodyA = shapeA->m_body;
|
||||
|
||||
b3Shape* shapeB = pair->shapeB;
|
||||
i32 proxyB = shapeB->m_broadPhaseID;
|
||||
u32 proxyB = shapeB->m_broadPhaseID;
|
||||
b3Body* bodyB = shapeB->m_body;
|
||||
|
||||
// Check if the bodies must not collide with each other.
|
||||
|
@ -384,7 +384,7 @@ bool b3HullShape::RayCast(b3RayCastOutput* output, const b3RayCastInput& input,
|
||||
float32 lower = 0.0f;
|
||||
float32 upper = input.maxFraction;
|
||||
|
||||
i32 index = -1;
|
||||
u32 index = B3_MAX_U32;
|
||||
|
||||
// s(lower) = p1 + lower * d, 0 <= lower <= kupper
|
||||
// The segment intersects the plane if a 'lower' exists
|
||||
@ -451,7 +451,7 @@ bool b3HullShape::RayCast(b3RayCastOutput* output, const b3RayCastInput& input,
|
||||
|
||||
B3_ASSERT(lower >= 0.0f && lower <= input.maxFraction);
|
||||
|
||||
if (index >= 0)
|
||||
if (index != B3_MAX_U32)
|
||||
{
|
||||
output->fraction = lower;
|
||||
output->normal = b3Mul(xf.rotation, planes[index].normal);
|
||||
|
@ -65,7 +65,7 @@ void b3Shape::DestroyContacts()
|
||||
}
|
||||
}
|
||||
|
||||
void b3Shape::Dump(i32 bodyIndex) const
|
||||
void b3Shape::Dump(u32 bodyIndex) const
|
||||
{
|
||||
switch (m_type)
|
||||
{
|
||||
|
@ -312,7 +312,7 @@ void b3World::Solve(float32 dt, u32 velocityIterations, u32 positionIterations)
|
||||
|
||||
struct b3RayCastCallback
|
||||
{
|
||||
float32 Report(const b3RayCastInput& input, i32 proxyId)
|
||||
float32 Report(const b3RayCastInput& input, u32 proxyId)
|
||||
{
|
||||
// Get shape associated with the proxy.
|
||||
void* userData = broadPhase->GetUserData(proxyId);
|
||||
@ -360,7 +360,7 @@ void b3World::RayCast(b3RayCastListener* listener, const b3Vec3& p1, const b3Vec
|
||||
|
||||
struct b3RayCastSingleCallback
|
||||
{
|
||||
float32 Report(const b3RayCastInput& input, i32 proxyId)
|
||||
float32 Report(const b3RayCastInput& input, u32 proxyId)
|
||||
{
|
||||
// Get shape associated with the proxy.
|
||||
void* userData = broadPhase->GetUserData(proxyId);
|
||||
@ -425,7 +425,7 @@ bool b3World::RayCastSingle(b3RayCastSingleOutput* output, const b3Vec3& p1, con
|
||||
|
||||
struct b3QueryAABBCallback
|
||||
{
|
||||
bool Report(i32 proxyID)
|
||||
bool Report(u32 proxyID)
|
||||
{
|
||||
b3Shape* shape = (b3Shape*)broadPhase->GetUserData(proxyID);
|
||||
return listener->ReportShape(shape);
|
||||
|
Loading…
x
Reference in New Issue
Block a user