Ghost collision improvements (#61)
- flag to disable hull-mesh speculative collision - faster atomics #55 - fix triangle vs hull logic - fix #58 and #59 - improved shadows in samples app, a bit more expensive
This commit is contained in:
parent
52f1a254ad
commit
ef8ef0187a
39 changed files with 16639 additions and 12086 deletions
|
|
@ -530,3 +530,416 @@ public:
|
|||
};
|
||||
|
||||
static int sampleIndex = RegisterSample( "Issues", "Capsule Mesh", CapsuleMeshBug::Create );
|
||||
|
||||
|
||||
// Reproduces s&box rigid body character ghost collisions on a FLAT floor.
|
||||
//
|
||||
// The s&box player is a fixed rotation dynamic body (zero radius box hull) moved by setting
|
||||
// velocity. The floor here is modeled on an s&box map area that ghosts badly: concrete slabs
|
||||
// and a run of parallel chamfered beams over deep pits. Every walkable vertex is at exactly
|
||||
// y = 0 - there is nothing to climb and nothing to trip on. The only mesh features are AT or
|
||||
// BELOW the walkable plane: chamfer facets sloping down from beam tops, pit walls and floors,
|
||||
// T-junction seams between tiles tessellated at different resolutions, and a chunk seam where
|
||||
// two mesh shapes meet (separate contact pairs, like s&box world chunks).
|
||||
//
|
||||
// Walking back and forth still launches the body upward (60-190 inch/s at run speed). No shape
|
||||
// casts, no step up, no ground snapping - any upward velocity spike is a ghost collision from
|
||||
// speculative hull vs triangle contacts against below-plane geometry.
|
||||
class SBoxGhostCollisions : public Sample
|
||||
{
|
||||
public:
|
||||
// s&box works in inches: 1 unit = 0.0254 m (40 units per meter)
|
||||
static constexpr float SRC = 0.0254f;
|
||||
|
||||
// Floor layout (s&box inches)
|
||||
static constexpr int m_halfLengthU = 256; // strip half length along x
|
||||
static constexpr int m_halfWidthU = 64; // strip half width along z
|
||||
static constexpr int m_tileSizeU = 32; // slab tile stride
|
||||
|
||||
// Beam section: beams run along z, the character walks along x across them.
|
||||
// Tops are 12 wide with 1.5 chamfers, pits between are 10 wide and 24 deep.
|
||||
// The 16 wide hull always spans the 13 gap between flat tops.
|
||||
static constexpr float m_beamPitchU = 22.0f;
|
||||
static constexpr float m_beamWidthU = 12.0f;
|
||||
static constexpr float m_chamferWidthU = 1.5f;
|
||||
static constexpr float m_chamferDropU = 1.0f;
|
||||
static constexpr float m_pitDepthU = 24.0f;
|
||||
static constexpr int m_beamCount = 9;
|
||||
static constexpr float m_beamRegion0 = -94.0f; // first beam start
|
||||
static constexpr float m_beamRegion1 = 94.0f; // last beam end
|
||||
|
||||
// Character (s&box player: 16 wide zero radius box hull, 72 tall, mass 500)
|
||||
static constexpr float m_bodyHalfWidth = 16.0f * SRC;
|
||||
static constexpr float m_bodyHalfHeight = 36.0f * SRC;
|
||||
static constexpr float m_characterMass = 500.0f;
|
||||
|
||||
explicit SBoxGhostCollisions( SampleContext* context )
|
||||
: Sample( context )
|
||||
{
|
||||
if ( m_context->restart == false )
|
||||
{
|
||||
m_camera->SetView( 90.0f, 25.0f, 10.0f, { 0.0f, 1.0f, 0.0f } );
|
||||
}
|
||||
|
||||
// Two chunks meeting at x = 0, each its own body and mesh shape, so seam contacts live
|
||||
// in separate contact pairs like s&box world mesh chunks. A beam top straddles the seam.
|
||||
CreateFloorChunk( 0, -(float)m_halfLengthU, 0.0f );
|
||||
CreateFloorChunk( 1, 0.0f, (float)m_halfLengthU );
|
||||
|
||||
// Character
|
||||
{
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = { -m_walkRange, m_bodyHalfHeight + 0.1f, 0.0f };
|
||||
bodyDef.motionLocks.angularX = true;
|
||||
bodyDef.motionLocks.angularY = true;
|
||||
bodyDef.motionLocks.angularZ = true;
|
||||
bodyDef.enableSleep = false;
|
||||
bodyDef.enableContactRecycling = false;
|
||||
bodyDef.gravityScale = 2.03f; // s&box gravity: 800 inch/s^2
|
||||
bodyDef.name = "character";
|
||||
m_characterId = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.baseMaterial.friction = 0.0f;
|
||||
shapeDef.baseMaterial.restitution = 0.0f;
|
||||
//shapeDef.baseMaterial.customColor = b3_colorLimeGreen;
|
||||
|
||||
float volume = 8.0f * m_bodyHalfWidth * m_bodyHalfHeight * m_bodyHalfWidth;
|
||||
shapeDef.density = m_characterMass / volume;
|
||||
shapeDef.enableSpeculativeContact = false;
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( m_bodyHalfWidth, m_bodyHalfHeight, m_bodyHalfWidth );
|
||||
b3CreateHullShape( m_characterId, &shapeDef, &box.base );
|
||||
}
|
||||
|
||||
m_walkDirection = 1.0f;
|
||||
m_walkSpeed = 350.0f * SRC; // s&box run speed
|
||||
m_launchCount = 0;
|
||||
m_maxLaunchSpeed = 0.0f;
|
||||
m_launchMarkerCount = 0;
|
||||
m_wasLaunched = false;
|
||||
|
||||
m_stepWhilePaused = true;
|
||||
}
|
||||
|
||||
~SBoxGhostCollisions() override
|
||||
{
|
||||
b3DestroyMesh( m_chunkMesh[0] );
|
||||
b3DestroyMesh( m_chunkMesh[1] );
|
||||
}
|
||||
|
||||
// Deterministic integer hash for tile tessellation selection
|
||||
static uint32_t Hash( uint32_t x )
|
||||
{
|
||||
x ^= x >> 16;
|
||||
x *= 0x7feb352du;
|
||||
x ^= x >> 15;
|
||||
x *= 0x846ca68bu;
|
||||
x ^= x >> 16;
|
||||
return x;
|
||||
}
|
||||
|
||||
static void EmitTriangle( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices, b3Vec3 a, b3Vec3 b, b3Vec3 c )
|
||||
{
|
||||
int32_t base = (int32_t)vertices.size();
|
||||
vertices.push_back( a );
|
||||
vertices.push_back( b );
|
||||
vertices.push_back( c );
|
||||
indices.push_back( base );
|
||||
indices.push_back( base + 1 );
|
||||
indices.push_back( base + 2 );
|
||||
}
|
||||
|
||||
// Horizontal patch at height y spanning [x0,x1]x[z0,z1] (inches), normal +y
|
||||
static void EmitPatch( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices, float x0, float x1, float z0, float z1,
|
||||
float y, float cell )
|
||||
{
|
||||
if ( x1 - x0 < 0.01f )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
int countX = (int)( ( x1 - x0 ) / cell + 0.99f );
|
||||
int countZ = (int)( ( z1 - z0 ) / cell + 0.99f );
|
||||
|
||||
for ( int ix = 0; ix < countX; ++ix )
|
||||
{
|
||||
for ( int iz = 0; iz < countZ; ++iz )
|
||||
{
|
||||
float cx0 = x0 + ( x1 - x0 ) * (float)ix / (float)countX;
|
||||
float cx1 = x0 + ( x1 - x0 ) * (float)( ix + 1 ) / (float)countX;
|
||||
float cz0 = z0 + ( z1 - z0 ) * (float)iz / (float)countZ;
|
||||
float cz1 = z0 + ( z1 - z0 ) * (float)( iz + 1 ) / (float)countZ;
|
||||
|
||||
b3Vec3 a = { SRC * cx0, SRC * y, SRC * cz0 };
|
||||
b3Vec3 b = { SRC * cx1, SRC * y, SRC * cz0 };
|
||||
b3Vec3 c = { SRC * cx1, SRC * y, SRC * cz1 };
|
||||
b3Vec3 d = { SRC * cx0, SRC * y, SRC * cz1 };
|
||||
|
||||
// Alternate the split diagonal like typical cooked map data
|
||||
if ( ( ix + iz ) & 1 )
|
||||
{
|
||||
EmitTriangle( vertices, indices, a, d, c );
|
||||
EmitTriangle( vertices, indices, a, c, b );
|
||||
}
|
||||
else
|
||||
{
|
||||
EmitTriangle( vertices, indices, a, d, b );
|
||||
EmitTriangle( vertices, indices, b, d, c );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sloped strip from edge (xLow, yLow) to edge (xHigh, yHigh) spanning the full z width
|
||||
static void EmitSlope( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices, float xLow, float yLow, float xHigh,
|
||||
float yHigh, float zCell )
|
||||
{
|
||||
int countZ = (int)( 2.0f * (float)m_halfWidthU / zCell + 0.99f );
|
||||
for ( int iz = 0; iz < countZ; ++iz )
|
||||
{
|
||||
float z0 = -(float)m_halfWidthU + 2.0f * (float)m_halfWidthU * (float)iz / (float)countZ;
|
||||
float z1 = -(float)m_halfWidthU + 2.0f * (float)m_halfWidthU * (float)( iz + 1 ) / (float)countZ;
|
||||
|
||||
b3Vec3 l0 = { SRC * xLow, SRC * yLow, SRC * z0 };
|
||||
b3Vec3 l1 = { SRC * xLow, SRC * yLow, SRC * z1 };
|
||||
b3Vec3 h0 = { SRC * xHigh, SRC * yHigh, SRC * z0 };
|
||||
b3Vec3 h1 = { SRC * xHigh, SRC * yHigh, SRC * z1 };
|
||||
|
||||
EmitTriangle( vertices, indices, l0, l1, h1 );
|
||||
EmitTriangle( vertices, indices, l0, h1, h0 );
|
||||
}
|
||||
}
|
||||
|
||||
// Vertical wall at x from y0 (bottom) to y1 (top). facing = +1 faces +x, -1 faces -x
|
||||
static void EmitWall( std::vector<b3Vec3>& vertices, std::vector<int32_t>& indices, float x, float y0, float y1, int facing,
|
||||
float zCell )
|
||||
{
|
||||
int countZ = (int)( 2.0f * (float)m_halfWidthU / zCell + 0.99f );
|
||||
for ( int iz = 0; iz < countZ; ++iz )
|
||||
{
|
||||
float z0 = -(float)m_halfWidthU + 2.0f * (float)m_halfWidthU * (float)iz / (float)countZ;
|
||||
float z1 = -(float)m_halfWidthU + 2.0f * (float)m_halfWidthU * (float)( iz + 1 ) / (float)countZ;
|
||||
|
||||
b3Vec3 b0 = { SRC * x, SRC * y0, SRC * z0 };
|
||||
b3Vec3 b1 = { SRC * x, SRC * y0, SRC * z1 };
|
||||
b3Vec3 t0 = { SRC * x, SRC * y1, SRC * z0 };
|
||||
b3Vec3 t1 = { SRC * x, SRC * y1, SRC * z1 };
|
||||
|
||||
if ( facing > 0 )
|
||||
{
|
||||
EmitTriangle( vertices, indices, b0, b1, t1 );
|
||||
EmitTriangle( vertices, indices, b0, t1, t0 );
|
||||
}
|
||||
else
|
||||
{
|
||||
EmitTriangle( vertices, indices, b0, t0, t1 );
|
||||
EmitTriangle( vertices, indices, b0, t1, b1 );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Clip [a0,a1] to [c0,c1]
|
||||
static bool ClipSpan( float a0, float a1, float c0, float c1, float* o0, float* o1 )
|
||||
{
|
||||
*o0 = a0 > c0 ? a0 : c0;
|
||||
*o1 = a1 < c1 ? a1 : c1;
|
||||
return *o1 - *o0 > 0.01f;
|
||||
}
|
||||
|
||||
void CreateFloorChunk( int chunk, float x0U, float x1U )
|
||||
{
|
||||
std::vector<b3Vec3> vertices;
|
||||
std::vector<int32_t> indices;
|
||||
|
||||
float s0, s1;
|
||||
|
||||
// --- Concrete slabs at y = 0 outside the beam region ---
|
||||
// Tiles tessellate at a hash-picked resolution so neighbors meet with T-junctions,
|
||||
// like cooked s&box map collision.
|
||||
float slabSpans[2][2] = { { -(float)m_halfLengthU, m_beamRegion0 }, { m_beamRegion1, (float)m_halfLengthU } };
|
||||
for ( int i = 0; i < 2; ++i )
|
||||
{
|
||||
if ( ClipSpan( slabSpans[i][0], slabSpans[i][1], x0U, x1U, &s0, &s1 ) == false )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for ( float tx = s0; tx < s1; tx += (float)m_tileSizeU )
|
||||
{
|
||||
float tx1 = b3MinFloat( tx + (float)m_tileSizeU, s1 );
|
||||
for ( int tz = -m_halfWidthU; tz < m_halfWidthU; tz += m_tileSizeU )
|
||||
{
|
||||
uint32_t h = Hash( (uint32_t)( (int)tx * 73856093 ) ^ (uint32_t)( tz * 19349663 ) ^
|
||||
(uint32_t)( chunk * 2654435761u ) );
|
||||
float cells[3] = { 4.0f, 8.0f, 16.0f };
|
||||
EmitPatch( vertices, indices, tx, tx1, (float)tz, (float)( tz + m_tileSizeU ), 0.0f, cells[h % 3] );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Beam section: flat tops at y = 0, chamfers dropping to pits ---
|
||||
float pitTop = -m_chamferDropU;
|
||||
float pitBottom = -m_pitDepthU;
|
||||
|
||||
for ( int k = 0; k < m_beamCount; ++k )
|
||||
{
|
||||
float bx = m_beamRegion0 + m_beamPitchU * (float)k;
|
||||
bool pitLeft = k > 0;
|
||||
bool pitRight = k < m_beamCount - 1;
|
||||
|
||||
// Flat top (flush with the slab on outer sides)
|
||||
float top0 = pitLeft ? bx + m_chamferWidthU : bx;
|
||||
float top1 = pitRight ? bx + m_beamWidthU - m_chamferWidthU : bx + m_beamWidthU;
|
||||
if ( ClipSpan( top0, top1, x0U, x1U, &s0, &s1 ) )
|
||||
{
|
||||
EmitPatch( vertices, indices, s0, s1, -(float)m_halfWidthU, (float)m_halfWidthU, 0.0f, 8.0f );
|
||||
}
|
||||
|
||||
// Chamfers sloping below the walkable plane
|
||||
if ( pitLeft && bx >= x0U && bx < x1U )
|
||||
{
|
||||
EmitSlope( vertices, indices, bx, pitTop, bx + m_chamferWidthU, 0.0f, 8.0f );
|
||||
}
|
||||
if ( pitRight && bx + m_beamWidthU > x0U && bx + m_beamWidthU <= x1U )
|
||||
{
|
||||
EmitSlope( vertices, indices, bx + m_beamWidthU, pitTop, bx + m_beamWidthU - m_chamferWidthU, 0.0f, 8.0f );
|
||||
}
|
||||
|
||||
// Pit to the right of this beam
|
||||
if ( pitRight )
|
||||
{
|
||||
float pitL = bx + m_beamWidthU;
|
||||
float pitR = bx + m_beamPitchU;
|
||||
if ( pitL >= x0U && pitL < x1U )
|
||||
{
|
||||
EmitWall( vertices, indices, pitL, pitBottom, pitTop, +1, 16.0f );
|
||||
}
|
||||
if ( pitR > x0U && pitR <= x1U )
|
||||
{
|
||||
EmitWall( vertices, indices, pitR, pitBottom, pitTop, -1, 16.0f );
|
||||
}
|
||||
if ( ClipSpan( pitL, pitR, x0U, x1U, &s0, &s1 ) )
|
||||
{
|
||||
EmitPatch( vertices, indices, s0, s1, -(float)m_halfWidthU, (float)m_halfWidthU, pitBottom, 16.0f );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b3MeshDef meshDef = {};
|
||||
meshDef.vertices = vertices.data();
|
||||
meshDef.indices = indices.data();
|
||||
meshDef.vertexCount = (int)vertices.size();
|
||||
meshDef.triangleCount = (int)( indices.size() / 3 );
|
||||
meshDef.weldVertices = true;
|
||||
meshDef.weldTolerance = 0.005f; // == B3_LINEAR_SLOP, same as s&box
|
||||
meshDef.identifyEdges = true;
|
||||
|
||||
m_chunkMesh[chunk] = b3CreateMesh( &meshDef, nullptr, 0 );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
b3BodyId body = b3CreateBody( m_worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateMeshShape( body, &shapeDef, m_chunkMesh[chunk], b3Vec3_one );
|
||||
}
|
||||
|
||||
void Step() override
|
||||
{
|
||||
// Drive the character with pure velocity control: keep the solver's vertical velocity,
|
||||
// set the horizontal velocity. This is how the s&box player controller moves.
|
||||
b3Pos position = b3Body_GetPosition( m_characterId );
|
||||
if ( position.x > m_walkRange )
|
||||
{
|
||||
m_walkDirection = -1.0f;
|
||||
}
|
||||
else if ( position.x < -m_walkRange )
|
||||
{
|
||||
m_walkDirection = 1.0f;
|
||||
}
|
||||
|
||||
b3Vec3 velocity = b3Body_GetLinearVelocity( m_characterId );
|
||||
velocity.x = m_walkDirection * m_walkSpeed;
|
||||
velocity.z = 0.0f;
|
||||
b3Body_SetLinearVelocity( m_characterId, velocity );
|
||||
|
||||
Sample::Step();
|
||||
|
||||
if ( m_didStep )
|
||||
{
|
||||
// The walkable plane is exactly y = 0, so the grounded body center never rises above
|
||||
// rest height. Any upward velocity spike while grounded is a ghost collision: there
|
||||
// is nothing to climb and nothing to bounce off.
|
||||
position = b3Body_GetPosition( m_characterId );
|
||||
velocity = b3Body_GetLinearVelocity( m_characterId );
|
||||
|
||||
bool grounded = position.y < m_bodyHalfHeight + 0.01f + 4.0f * SRC;
|
||||
bool launched = velocity.y > m_launchThreshold;
|
||||
|
||||
if ( grounded && launched && m_wasLaunched == false )
|
||||
{
|
||||
m_launchCount += 1;
|
||||
m_maxLaunchSpeed = b3MaxFloat( m_maxLaunchSpeed, velocity.y );
|
||||
|
||||
if ( m_launchMarkerCount < m_markerCapacity )
|
||||
{
|
||||
m_launchMarkers[m_launchMarkerCount] = position;
|
||||
m_launchMarkerCount += 1;
|
||||
}
|
||||
}
|
||||
|
||||
m_wasLaunched = launched;
|
||||
}
|
||||
|
||||
for ( int i = 0; i < m_launchMarkerCount; ++i )
|
||||
{
|
||||
DrawPoint( m_launchMarkers[i], 8.0f, MakeColor( b3_colorRed ) );
|
||||
}
|
||||
|
||||
b3Vec3 currentVelocity = b3Body_GetLinearVelocity( m_characterId );
|
||||
DrawTextLine( "ghost launches: %d, worst: %.2f m/s (%.0f inch/s)", m_launchCount, m_maxLaunchSpeed,
|
||||
m_maxLaunchSpeed / SRC );
|
||||
DrawTextLine( "vertical velocity: %.2f m/s", currentVelocity.y );
|
||||
}
|
||||
|
||||
bool DrawControls() override
|
||||
{
|
||||
float speedU = m_walkSpeed / SRC;
|
||||
if ( ImGui::SliderFloat( "Walk Speed (inch/s)", &speedU, 100.0f, 400.0f, "%.0f" ) )
|
||||
{
|
||||
m_walkSpeed = speedU * SRC;
|
||||
}
|
||||
|
||||
if ( ImGui::Button( "Reset Counters" ) )
|
||||
{
|
||||
m_launchCount = 0;
|
||||
m_maxLaunchSpeed = 0.0f;
|
||||
m_launchMarkerCount = 0;
|
||||
}
|
||||
|
||||
ImGui::Text( "Launches: %d", m_launchCount );
|
||||
return true;
|
||||
}
|
||||
|
||||
static Sample* Create( SampleContext* context )
|
||||
{
|
||||
return new SBoxGhostCollisions( context );
|
||||
}
|
||||
|
||||
static constexpr float m_walkRange = 3.5f; // turn around beyond +/- this x (meters)
|
||||
static constexpr float m_launchThreshold = 0.5f; // upward m/s counted as a ghost launch (~20 inch/s)
|
||||
static constexpr int m_markerCapacity = 64;
|
||||
|
||||
b3MeshData* m_chunkMesh[2] = {};
|
||||
b3BodyId m_characterId;
|
||||
float m_walkDirection;
|
||||
float m_walkSpeed;
|
||||
int m_launchCount;
|
||||
float m_maxLaunchSpeed;
|
||||
bool m_wasLaunched;
|
||||
b3Pos m_launchMarkers[m_markerCapacity];
|
||||
int m_launchMarkerCount;
|
||||
};
|
||||
|
||||
static int sampleSBoxGhostCollisions = RegisterSample( "Issues", "s&box Ghost Collisions", SBoxGhostCollisions::Create );
|
||||
|
|
|
|||
Loading…
Reference in a new issue