Sync body flags (#7)

- Fixed mesh point culling for vertically stacked points
- Fixed two point capsule vs capsule normal
- Fixed bug where body flags can go out of sync
- Added tests
- Updated benchmarks
This commit is contained in:
Erin Catto 2026-05-16 23:27:25 -07:00 • committed by GitHub
commit 79cc160275
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 1686 additions and 931 deletions

View file

@ -2571,251 +2571,3 @@ public:
};
static int sampleCapsuleCastRay = SampleManager::Register( "Collision", "Capsule Cast Ray", CapsuleCastRay::Create );
class Hull2D : public Sample
{
public:
enum SourceMode
{
e_random = 0,
e_preset = 1,
};
static Sample* Create( SampleContext* context )
{
return new Hull2D( context );
}
explicit Hull2D( SampleContext* context )
: Sample( context )
{
if ( context->restart == false )
{
m_camera->SetView( 0.0f, 0.0f, 20.0f, { 0.0f, 0.0f, 0.0f } );
}
m_sourceMode = e_preset;
m_inputCount = 0;
m_hullCount = 0;
m_simplifiedCount = 0;
m_targetCount = 6;
m_randomCount = 20;
m_simpleCull = true;
m_drawLabels = true;
Generate();
}
void Generate()
{
if ( m_sourceMode == e_random )
{
m_inputCount = m_randomCount;
for ( int i = 0; i < m_inputCount; ++i )
{
m_inputPoints[i].p = { RandomFloatRange( -5.0f, 5.0f ), RandomFloatRange( -5.0f, 5.0f ) };
m_inputPoints[i].separation = 0.0f;
m_inputPoints[i].originalIndex = (int16_t)i;
m_inputPoints[i].persisted = false;
}
}
else
{
#if 0
b3Vec2 points[m_pointCapacity];
int n = b3MinInt(15, m_pointCapacity);
float delta = 2.0f * B3_PI / (float)n;
// This makes a difference when comparing areas
//b3CosSin cs = b3ComputeCosSin( delta );
b3CosSin cs = {
cosf( delta ),
sinf( delta ),
};
;
float x1 = 0.5f, y1 = 0.0f;
for ( int i = 0; i < n; ++i )
{
points[i] = {x1, y1};
float x2 = cs.cosine * x1 - cs.sine * y1;
float y2 = cs.sine * x1 + cs.cosine * y1;
x1 = x2;
y1 = y2;
}
m_drawScale = 1.0f;
#else
b3Vec2 points[] = {
{ 0.00000000, 0.00000000 }, { -0.00305632968, 5.54900616e-05 }, { -0.00305632968, 0.0246563386 },
{ -0.00305632968, 0.0246562902 }, { -0.00305632968, 0.0800221488 },
};
int n = sizeof( points ) / sizeof( b3Vec2 );
m_drawScale = 50.0f;
#endif
m_targetCount = b3MinInt( 6, n );
m_inputCount = n;
for ( int i = 0; i < m_inputCount; ++i )
{
m_inputPoints[i].p = { points[i].x, points[i].y };
m_inputPoints[i].separation = 0.0f;
m_inputPoints[i].originalIndex = (int16_t)i;
m_inputPoints[i].persisted = false;
}
}
}
void ComputeHull()
{
// Copy input for hull computation because b3Hull2D may modify the array
if ( m_simpleCull )
{
for ( int i = 0; i < m_inputCount; ++i )
{
m_hullPoints[i] = m_inputPoints[i];
}
m_hullCount = b3CullPoints( m_hullPoints, m_inputCount, m_targetCount );
}
else
{
// Copy input for hull computation because b3Hull2D may modify the array
b3Point2D points[m_pointCapacity];
for ( int i = 0; i < m_inputCount; ++i )
{
points[i] = m_inputPoints[i];
}
m_hullCount = b3Hull2D( points, m_inputCount, m_hullPoints );
// Copy hull for simplification
for ( int i = 0; i < m_hullCount; ++i )
{
m_simplifiedPoints[i] = m_hullPoints[i];
}
int target = m_targetCount < 3 ? 3 : m_targetCount;
m_simplifiedCount = b3SimplifyHull2D( m_simplifiedPoints, m_hullCount, target );
}
}
void UpdateUI() override
{
float fontSize = ImGui::GetFontSize();
float height = 16.0f * fontSize;
ImGui::SetNextWindowPos( ImVec2( 0.5f * fontSize, m_camera->m_height - height - 2.0f * fontSize ), ImGuiCond_Once );
ImGui::SetNextWindowSize( ImVec2( 19.0f * fontSize, height ) );
ImGui::Begin( "Hull 2D", nullptr, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize );
const char* modes[] = { "Random", "Preset" };
if ( ImGui::Combo( "Source", (int*)&m_sourceMode, modes, IM_ARRAYSIZE( modes ) ) )
{
Generate();
}
if ( m_sourceMode == e_random )
{
if ( ImGui::SliderInt( "Point Count", &m_randomCount, 4, m_pointCapacity ) )
{
Generate();
}
if ( ImGui::Button( "Regenerate" ) )
{
Generate();
}
}
ImGui::Checkbox( "Simple Cull", &m_simpleCull );
ImGui::SliderInt( "Target Count", &m_targetCount, 1, m_inputCount );
ImGui::Checkbox( "Draw Labels", &m_drawLabels );
ImGui::End();
}
void Render() override
{
Sample::Render();
DrawTextLine( "Hull 2D: input=%d, hull=%d, simplified=%d", m_inputCount, m_hullCount, m_simplifiedCount );
DrawTransform( m_scene, b3Transform_identity, 0.4f );
bool onHull[m_pointCapacity] = {};
for ( int i = 0; i < m_hullCount; ++i )
{
onHull[m_hullPoints[i].originalIndex] = true;
}
float s = m_drawScale;
// Draw input points (small gray)
for ( int i = 0; i < m_inputCount; ++i )
{
if ( onHull[i] )
{
continue;
}
b3Vec3 p = { s * m_inputPoints[i].p.x, s * m_inputPoints[i].p.y, 0.0f };
DrawPoint( m_scene, p, 4.0f, b3_colorGray );
if ( m_drawLabels )
{
DrawWorldString( m_camera, p, b3_colorGray, " %d", i );
}
}
// Draw convex hull edges and vertices (cyan)
for ( int i = 0; i < m_hullCount; ++i )
{
int next = ( i + 1 ) % m_hullCount;
b3Vec3 p1 = { s * m_hullPoints[i].p.x, s * m_hullPoints[i].p.y, 0.0f };
b3Vec3 p2 = { s * m_hullPoints[next].p.x, s * m_hullPoints[next].p.y, 0.0f };
DrawLine( m_scene, p1, p2, b3_colorCyan );
DrawPoint( m_scene, p1, 6.0f, b3_colorCyan );
if ( m_drawLabels )
{
DrawWorldString( m_camera, p1, b3_colorCyan, " %d : %d", m_hullPoints[i].originalIndex, i );
}
}
if ( m_simpleCull == false )
{
// Draw simplified hull edges and vertices (yellow, slightly offset in z so visible)
for ( int i = 0; i < m_simplifiedCount; ++i )
{
int next = ( i + 1 ) % m_simplifiedCount;
b3Vec3 p1 = { s * m_simplifiedPoints[i].p.x, s * m_simplifiedPoints[i].p.y, 0.01f };
b3Vec3 p2 = { s * m_simplifiedPoints[next].p.x, s * m_simplifiedPoints[next].p.y, 0.01f };
DrawLine( m_scene, p1, p2, b3_colorYellow );
DrawPoint( m_scene, p1, 10.0f, b3_colorYellow );
}
}
}
void Step() override
{
ComputeHull();
}
static constexpr int m_pointCapacity = 128;
SourceMode m_sourceMode;
b3Point2D m_inputPoints[m_pointCapacity];
b3Point2D m_hullPoints[2 * m_pointCapacity];
b3Point2D m_simplifiedPoints[2 * m_pointCapacity];
float m_drawScale;
int m_inputCount;
int m_hullCount;
int m_simplifiedCount;
int m_targetCount;
int m_randomCount;
bool m_simpleCull;
bool m_drawLabels;
};
static int sampleHull2D = SampleManager::Register( "Collision", "Hull 2D", Hull2D::Create );