use unsigned integers instead of integers in some collision code

This commit is contained in:
Irlan
2018-04-18 17:20:39 -03:00
parent 235b59a560
commit efae0fc202
15 changed files with 151 additions and 148 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -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

View File

@ -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;

View File

@ -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;