control hull simplification

This commit is contained in:
Irlan 2018-05-09 05:43:28 -03:00
parent 97d7f99ce4
commit 3f5494211d

View File

@ -21,6 +21,10 @@
#define B3_NULL_HULL_FEATURE 0xFF #define B3_NULL_HULL_FEATURE 0xFF
// Enables or disables convex hull simplification.
// This can speed up collision detection and stability significantly.
#define B3_SIMPLIFY_HULL 1
// Used to map pointers to indices // Used to map pointers to indices
// If more performance is required then a use hash-map // If more performance is required then a use hash-map
template<class T, u32 N> template<class T, u32 N>
@ -31,7 +35,7 @@ struct b3UniqueArray
count = 0; count = 0;
} }
// Add the value if not found // Add the value
void Add(const T& value) void Add(const T& value)
{ {
B3_ASSERT(count < N); B3_ASSERT(count < N);
@ -145,7 +149,7 @@ void b3QHull::Set(const b3Vec3* points, u32 count)
// Clamp vertices into range [0, B3_MAX_HULL_VERTICES] // Clamp vertices into range [0, B3_MAX_HULL_VERTICES]
u32 n = b3Min(count, u32(B3_MAX_HULL_VERTICES)); u32 n = b3Min(count, u32(B3_MAX_HULL_VERTICES));
// Copy points into local buffer, remove coincident points. // Copy points into local buffer, weld coincident points.
b3Vec3 ps[B3_MAX_HULL_VERTICES]; b3Vec3 ps[B3_MAX_HULL_VERTICES];
u32 psCount = 0; u32 psCount = 0;
for (u32 i = 0; i < n; ++i) for (u32 i = 0; i < n; ++i)
@ -174,8 +178,11 @@ void b3QHull::Set(const b3Vec3* points, u32 count)
// Polyhedron is degenerate. // Polyhedron is degenerate.
return; return;
} }
// Create a convex hull. // Create a convex hull.
#if B3_SIMPLIFY_HULL == 1
qhHull primary; qhHull primary;
primary.Construct(ps, psCount); primary.Construct(ps, psCount);
@ -202,7 +209,7 @@ void b3QHull::Set(const b3Vec3* points, u32 count)
b3Plane plane = f->plane; b3Plane plane = f->plane;
B3_ASSERT(plane.offset > 0.0f); B3_ASSERT(plane.offset > 0.0f);
b3Vec3 v = plane.normal / plane.offset; b3Vec3 v = plane.normal / plane.offset;
bool unique = true; bool unique = true;
// There's a magic portion of code lost in the island that would cluster those planes. // There's a magic portion of code lost in the island that would cluster those planes.
@ -273,6 +280,13 @@ void b3QHull::Set(const b3Vec3* points, u32 count)
// Translate the hull back to the origin // Translate the hull back to the origin
hull.Translate(s); hull.Translate(s);
#else
qhHull hull;
hull.Construct(ps, psCount);
#endif
if (hull.GetVertexList().count > B3_MAX_HULL_VERTICES) if (hull.GetVertexList().count > B3_MAX_HULL_VERTICES)
{ {
// Vertex excess // Vertex excess
@ -297,14 +311,14 @@ void b3QHull::Set(const b3Vec3* points, u32 count)
vs.Add(vertex); vs.Add(vertex);
} }
// Add faces and to the map // Add faces and half-edges to the map
for (qhFace* face = hull.GetFaceList().head; face != NULL; face = face->next) for (qhFace* face = hull.GetFaceList().head; face != NULL; face = face->next)
{ {
// Add face // Add face
B3_ASSERT(fs_count < B3_MAX_HULL_FACES); B3_ASSERT(fs_count < B3_MAX_HULL_FACES);
++fs_count; ++fs_count;
// Add vertices and half-edges // Add half-edges
qhHalfEdge* begin = face->edge; qhHalfEdge* begin = face->edge;
qhHalfEdge* edge = begin; qhHalfEdge* edge = begin;
do do