Reduce ghosts (#212)
Full ghost collision handling for sphere, capsule, and hull versus a single mesh or heightfield. This requires mesh to be built with edge identification. Optimized mesh builder. Removed B3_MAX_MESH_CONTACT_TRIANGLES. You will still hit performance problems for dense meshes. Fixes #184
This commit is contained in:
parent
3362633204
commit
38f2ae1e19
61 changed files with 17756 additions and 1492 deletions
215
src/hull.c
215
src/hull.c
|
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include "algorithm.h"
|
||||
#include "math_internal.h"
|
||||
#include "qsort.h"
|
||||
#include "shape.h"
|
||||
#include "simd.h"
|
||||
|
||||
|
|
@ -398,7 +399,8 @@ static void b3FindFarthestPointsAlongCardinalAxes( int* index1Out, int* index2Ou
|
|||
}
|
||||
}
|
||||
|
||||
static int b3FindFarthestPointFromLine( int index1, int index2, float tolerance, int vertexCount, const b3Vec3* vertexBase )
|
||||
static int b3FindFarthestPointFromLine( int index1, int index2, float tolerance, int vertexCount,
|
||||
const b3Vec3* vertexBase )
|
||||
{
|
||||
b3Vec3 a = vertexBase[index1];
|
||||
b3Vec3 b = vertexBase[index2];
|
||||
|
|
@ -764,7 +766,8 @@ static void b3HullBuilder_RecacheConflicts( b3QHFace* face, float minOutside )
|
|||
b3QHVertex* maxVertex = NULL;
|
||||
float maxDistance = minOutside;
|
||||
|
||||
for ( b3QHListNode* node = face->conflictListHead.link.next; node != &face->conflictListHead.link; node = node->next )
|
||||
for ( b3QHListNode* node = face->conflictListHead.link.next; node != &face->conflictListHead.link;
|
||||
node = node->next )
|
||||
{
|
||||
b3QHVertex* vertex = (b3QHVertex*)node;
|
||||
float distance = b3PlaneSeparation( face->plane, vertex->position );
|
||||
|
|
@ -784,7 +787,8 @@ static b3QHVertex* b3HullBuilder_NextConflictVertex( const b3HullBuilder* b )
|
|||
b3QHVertex* maxVertex = NULL;
|
||||
float maxDistance = b->minOutside;
|
||||
|
||||
for ( const b3QHListNode* faceNode = b->faceList.link.next; faceNode != &b->faceList.link; faceNode = faceNode->next )
|
||||
for ( const b3QHListNode* faceNode = b->faceList.link.next; faceNode != &b->faceList.link;
|
||||
faceNode = faceNode->next )
|
||||
{
|
||||
const b3QHFace* face = (const b3QHFace*)faceNode;
|
||||
if ( face->maxConflict != NULL && face->maxConflictDistance > maxDistance )
|
||||
|
|
@ -817,7 +821,8 @@ static void b3HullBuilder_DrainConflictList( b3HullBuilder* b, b3QHFace* face )
|
|||
// `entryEdge` is the half-edge in `face` whose twin lies in the just-deleted parent face, or
|
||||
// NULL for the seed. The frame skips `entryEdge` on recursive entries (it would be ignored
|
||||
// anyway since the parent's mark is now DELETE, but skipping saves one iteration).
|
||||
static void b3HullBuilder_EnterHorizonFace( b3HullBuilder* b, b3QHFace* face, b3QHHalfEdge* entryEdge, b3HorizonFrame* frameOut )
|
||||
static void b3HullBuilder_EnterHorizonFace( b3HullBuilder* b, b3QHFace* face, b3QHHalfEdge* entryEdge,
|
||||
b3HorizonFrame* frameOut )
|
||||
{
|
||||
face->mark = B3_MARK_DELETE;
|
||||
b3HullBuilder_DrainConflictList( b, face );
|
||||
|
|
@ -1357,7 +1362,8 @@ static bool b3HullBuilder_IsConsistent( const b3HullBuilder* b )
|
|||
return false;
|
||||
}
|
||||
|
||||
for ( const b3QHListNode* faceNode = b->faceList.link.next; faceNode != &b->faceList.link; faceNode = faceNode->next )
|
||||
for ( const b3QHListNode* faceNode = b->faceList.link.next; faceNode != &b->faceList.link;
|
||||
faceNode = faceNode->next )
|
||||
{
|
||||
const b3QHFace* face = (const b3QHFace*)faceNode;
|
||||
if ( face->edge->face != face )
|
||||
|
|
@ -1427,8 +1433,8 @@ static bool b3HullBuilder_HasHull( const b3HullBuilder* b )
|
|||
}
|
||||
|
||||
// Build the entire hull. Returns true iff the result satisfies Euler's identity.
|
||||
static bool b3HullBuilder_Construct( b3HullBuilder* b, const b3Vec3* points, int pointCount, int maxVertexCount, b3Vec3 origin,
|
||||
b3Vec3* shiftedPoints )
|
||||
static bool b3HullBuilder_Construct( b3HullBuilder* b, const b3Vec3* points, int pointCount, int maxVertexCount,
|
||||
b3Vec3 origin, b3Vec3* shiftedPoints )
|
||||
{
|
||||
if ( pointCount < 4 )
|
||||
{
|
||||
|
|
@ -2195,7 +2201,8 @@ b3HullData* b3CreateHull( const b3Vec3* points, int pointCount, int maxVertexCou
|
|||
int faceCount = 0;
|
||||
int edgeCount = 0;
|
||||
|
||||
for ( b3QHListNode* faceNode = builder.faceList.link.next; faceNode != &builder.faceList.link; faceNode = faceNode->next )
|
||||
for ( b3QHListNode* faceNode = builder.faceList.link.next; faceNode != &builder.faceList.link;
|
||||
faceNode = faceNode->next )
|
||||
{
|
||||
B3_ASSERT( faceCount < B3_HULL_MAX_COUNT );
|
||||
|
||||
|
|
@ -3098,20 +3105,20 @@ int b3SimplifyHull2D( b3Point2D* hull, int count1, int target )
|
|||
return count2;
|
||||
}
|
||||
|
||||
// Sort comparator: by x, then by y. Insertion since n is typically 20 or less.
|
||||
static void b3Sort2D( b3Point2D* pts, int count )
|
||||
// Comparator for sorting points for the 2D hull.
|
||||
static inline bool b3IsPoint2DLess( const b3Point2D* a, const b3Point2D* b )
|
||||
{
|
||||
for ( int i = 1; i < count; ++i )
|
||||
if ( a->p.x != b->p.x )
|
||||
{
|
||||
b3Point2D base = pts[i];
|
||||
int j = i - 1;
|
||||
while ( j >= 0 && ( pts[j].p.x > base.p.x || ( pts[j].p.x == base.p.x && pts[j].p.y > base.p.y ) ) )
|
||||
{
|
||||
pts[j + 1] = pts[j];
|
||||
j -= 1;
|
||||
}
|
||||
pts[j + 1] = base;
|
||||
return a->p.x < b->p.x;
|
||||
}
|
||||
|
||||
if ( a->p.y != b->p.y )
|
||||
{
|
||||
return a->p.y < b->p.y;
|
||||
}
|
||||
|
||||
return a->originalIndex < b->originalIndex;
|
||||
}
|
||||
|
||||
// Welds points that have been sorted O(n) time
|
||||
|
|
@ -3148,79 +3155,151 @@ static int b3Weld2D( b3Point2D* pts, int count1 )
|
|||
return count2;
|
||||
}
|
||||
|
||||
// Only merge points that line up exactly.
|
||||
static int b3MergeExact2D( b3Point2D* pts, int count1 )
|
||||
{
|
||||
float separationTol = B3_LINEAR_SLOP;
|
||||
|
||||
int count2 = 0;
|
||||
for ( int i = 0; i < count1; i++ )
|
||||
{
|
||||
b3Point2D point = pts[i];
|
||||
if ( count2 > 0 && pts[count2 - 1].p.x == point.p.x && pts[count2 - 1].p.y == point.p.y )
|
||||
{
|
||||
if ( point.separation < pts[count2 - 1].separation - separationTol )
|
||||
{
|
||||
pts[count2 - 1] = point;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
pts[count2] = point;
|
||||
count2 += 1;
|
||||
}
|
||||
}
|
||||
|
||||
return count2;
|
||||
}
|
||||
|
||||
// Andrew's monotone chain convex hull algorithm
|
||||
// hull must have space for 2 * count1 points
|
||||
int b3Hull2D( b3Point2D* pts, int count, b3Point2D* hull )
|
||||
int b3Hull2D( b3Point2D* pts, int count, b3Point2D* hull, int flags )
|
||||
{
|
||||
int count1 = count;
|
||||
|
||||
if ( count1 <= 0 )
|
||||
if ( count <= 0 )
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ( count1 == 1 )
|
||||
#define LESS( i, j ) b3IsPoint2DLess( pts + (int)( i ), pts + (int)( j ) )
|
||||
#define SWAP( i, j ) \
|
||||
do \
|
||||
{ \
|
||||
b3Point2D swapPoint = pts[(int)( i )]; \
|
||||
pts[(int)( i )] = pts[(int)( j )]; \
|
||||
pts[(int)( j )] = swapPoint; \
|
||||
} \
|
||||
while ( 0 )
|
||||
|
||||
QSORT( count, LESS, SWAP );
|
||||
|
||||
#undef LESS
|
||||
#undef SWAP
|
||||
|
||||
int count1;
|
||||
if ( flags & b3_hull2DExactMerge )
|
||||
{
|
||||
hull[0] = pts[0];
|
||||
return 1;
|
||||
count1 = b3MergeExact2D( pts, count );
|
||||
}
|
||||
else
|
||||
{
|
||||
count1 = b3Weld2D( pts, count );
|
||||
}
|
||||
|
||||
b3Sort2D( pts, count1 );
|
||||
count1 = b3Weld2D( pts, count1 );
|
||||
|
||||
if ( count1 == 1 )
|
||||
{
|
||||
hull[0] = pts[0];
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ( count1 == 2 )
|
||||
{
|
||||
hull[0] = pts[0];
|
||||
hull[1] = pts[1];
|
||||
return 2;
|
||||
}
|
||||
bool keepCollinear = ( flags & b3_hull2DKeepCollinear ) != 0;
|
||||
|
||||
int count2 = 0;
|
||||
|
||||
for ( int i = 0; i < count1; i++ )
|
||||
if ( count1 <= 2 )
|
||||
{
|
||||
while ( count2 >= 2 )
|
||||
for ( int i = 0; i < count1; i++ )
|
||||
{
|
||||
float area = b3Cross2D( hull[count2 - 2].p, hull[count2 - 1].p, pts[i].p );
|
||||
if ( area > 0.0f )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
hull[i] = pts[i];
|
||||
}
|
||||
|
||||
B3_VALIDATE( count2 < 2 * count1 );
|
||||
hull[count2] = pts[i];
|
||||
count2 += 1;
|
||||
count2 = count1;
|
||||
}
|
||||
|
||||
int lowerCount = count2 + 1;
|
||||
|
||||
for ( int i = count1 - 2; i >= 0; i-- )
|
||||
else
|
||||
{
|
||||
while ( count2 >= lowerCount )
|
||||
for ( int i = 0; i < count1; i++ )
|
||||
{
|
||||
float area = b3Cross2D( hull[count2 - 2].p, hull[count2 - 1].p, pts[i].p );
|
||||
if ( area > 0.0f )
|
||||
while ( count2 >= 2 )
|
||||
{
|
||||
break;
|
||||
float area = b3Cross2D( hull[count2 - 2].p, hull[count2 - 1].p, pts[i].p );
|
||||
if ( area > 0.0f || ( keepCollinear && area == 0.0f ) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
B3_VALIDATE( count2 < 2 * count1 );
|
||||
hull[count2] = pts[i];
|
||||
count2 += 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( count2 < 2 * count1 );
|
||||
hull[count2] = pts[i];
|
||||
count2 += 1;
|
||||
int lowerCount = count2 + 1;
|
||||
int lowerIndex = count2 - 2;
|
||||
|
||||
for ( int i = count1 - 2; i >= 0; i-- )
|
||||
{
|
||||
if ( lowerIndex >= 0 && hull[lowerIndex].p.x == pts[i].p.x && hull[lowerIndex].p.y == pts[i].p.y )
|
||||
{
|
||||
lowerIndex -= 1;
|
||||
if ( i > 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
while ( count2 >= lowerCount )
|
||||
{
|
||||
float area = b3Cross2D( hull[count2 - 2].p, hull[count2 - 1].p, pts[i].p );
|
||||
if ( area > 0.0f || ( keepCollinear && area == 0.0f ) )
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
count2 -= 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( count2 < 2 * count1 );
|
||||
hull[count2] = pts[i];
|
||||
count2 += 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( hull[0].originalIndex == hull[count2 - 1].originalIndex );
|
||||
count2 -= 1;
|
||||
}
|
||||
|
||||
B3_VALIDATE( hull[0].originalIndex == hull[count2 - 1].originalIndex );
|
||||
return count2 - 1;
|
||||
B3_VALIDATE( count2 <= count1 );
|
||||
|
||||
if ( flags & b3_hull2DIndexOrder )
|
||||
{
|
||||
#define LESS( i, j ) ( hull[(int)( i )].originalIndex < hull[(int)( j )].originalIndex )
|
||||
#define SWAP( i, j ) \
|
||||
do \
|
||||
{ \
|
||||
b3Point2D swapPoint = hull[(int)( i )]; \
|
||||
hull[(int)( i )] = hull[(int)( j )]; \
|
||||
hull[(int)( j )] = swapPoint; \
|
||||
} \
|
||||
while ( 0 )
|
||||
|
||||
QSORT( count2, LESS, SWAP );
|
||||
|
||||
#undef LESS
|
||||
#undef SWAP
|
||||
}
|
||||
|
||||
return count2;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue