Skip to content

Commit

Permalink
Improve accuracy of world overlap functions. Also add a tolerance to …
Browse files Browse the repository at this point in the history
…consider small distances as overlap.
  • Loading branch information
erincatto committed Feb 9, 2025
1 parent c902c3c commit 5b9f690
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 104 deletions.
2 changes: 1 addition & 1 deletion samples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ static void UpdateUI()

ImGui::End();

s_sample->UpdateUI();
s_sample->UpdateGui();
}
}

Expand Down
2 changes: 1 addition & 1 deletion samples/sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Sample

void DrawTitle( const char* string );
virtual void Step( Settings& settings );
virtual void UpdateUI()
virtual void UpdateGui()
{
}
virtual void Keyboard( int )
Expand Down
6 changes: 3 additions & 3 deletions samples/sample_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ class BenchmarkBarrel : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 80.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -468,7 +468,7 @@ class BenchmarkManyTumblers : public Sample
m_bodyIndex = 0;
}

void UpdateUI() override
void UpdateGui() override
{
float height = 110.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -1199,7 +1199,7 @@ class BenchmarkCast : public Sample
m_minTime = 1e6f;
}

void UpdateUI() override
void UpdateGui() override
{
float height = 240.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down
6 changes: 3 additions & 3 deletions samples/sample_bodies.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class BodyType : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 140.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -534,7 +534,7 @@ class Weeble : public Sample
m_explosionMagnitude = 8.0f;
}

void UpdateUI() override
void UpdateGui() override
{
float height = 120.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -700,7 +700,7 @@ class Sleep : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 100.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down
52 changes: 36 additions & 16 deletions samples/sample_collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@

constexpr int SIMPLEX_CAPACITY = 20;

/*
- input 0x0000008f63efcee0 {proxyA={points=0x0000008f63efcee0 {{...}, {...}, {...}, {...}, {...}, {...}, {...}, ...} ...} ...} const b2DistanceInput *
+ [0] {x=-0.400000006 y=-0.400000006 } b2Vec2
+ [1] {x=0.400000006 y=-0.400000006 } b2Vec2
+ [2] {x=0.400000006 y=0.400000006 } b2Vec2
+ [3] {x=-0.400000006 y=0.400000006 } b2Vec2
+ [0] {x=-0.500000000 y=-0.500000000 } b2Vec2
+ [1] {x=0.500000000 y=-0.500000000 } b2Vec2
+ [2] {x=0.500000000 y=0.500000000 } b2Vec2
+ [3] {x=-0.500000000 y=0.500000000 } b2Vec2
+ transformA {p={x=3.00000000 y=5.00000000 } q={c=1.00000000 s=0.00000000 } } b2Transform
+ transformB {p={x=3.00000000 y=5.00000000 } q={c=1.00000000 s=0.00000000 } } b2Transform
useRadii true bool
*/
class ShapeDistance : public Sample
{
public:
Expand Down Expand Up @@ -43,11 +59,14 @@ class ShapeDistance : public Sample
b2Vec2 points[3] = { { -0.5f, 0.0f }, { 0.5f, 0.0f }, { 0.0f, 1.0f } };
b2Hull hull = b2ComputeHull( points, 3 );
m_triangle = b2MakePolygon( &hull, 0.0f );

m_triangle = b2MakeSquare( 0.4f );
}

m_box = b2MakeBox( 0.5f, 0.5f );
m_box = b2MakeSquare( 0.5f );

m_transform = { { 1.5f, -1.5f }, b2Rot_identity };
//m_transform = { { 1.5f, -1.5f }, b2Rot_identity };
m_transform = { { 0.0f, 0.0f }, b2Rot_identity };
m_angle = 0.0f;

m_cache = b2_emptySimplexCache;
Expand Down Expand Up @@ -90,10 +109,11 @@ class ShapeDistance : public Sample
break;

case e_triangle:
proxy.points[0] = m_triangle.vertices[0];
proxy.points[1] = m_triangle.vertices[1];
proxy.points[2] = m_triangle.vertices[2];
proxy.count = 3;
for (int i = 0; i < m_triangle.count; ++i)
{
proxy.points[i] = m_triangle.vertices[i];
}
proxy.count = m_triangle.count;
break;

case e_box:
Expand Down Expand Up @@ -146,19 +166,19 @@ class ShapeDistance : public Sample
break;

case e_triangle:
g_draw.DrawSolidPolygon( transform, m_triangle.vertices, 3, radius, color );
g_draw.DrawSolidPolygon( transform, m_triangle.vertices, m_triangle.count, radius, color );
break;

case e_box:
g_draw.DrawSolidPolygon( transform, m_box.vertices, 4, radius, color );
g_draw.DrawSolidPolygon( transform, m_box.vertices, m_box.count, radius, color );
break;

default:
assert( false );
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 310.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -311,7 +331,7 @@ class ShapeDistance : public Sample
input.proxyB = m_proxyB;
input.transformA = b2Transform_identity;
input.transformB = m_transform;
input.useRadii = m_radiusA > 0.0f || m_radiusB > 0.0f;
input.useRadii = true || m_radiusA > 0.0f || m_radiusB > 0.0f;

if ( m_useCache == false )
{
Expand Down Expand Up @@ -567,7 +587,7 @@ class DynamicTree : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 320.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -912,7 +932,7 @@ class RayCast : public Sample
m_showFraction = false;
}

void UpdateUI() override
void UpdateGui() override
{
float height = 230.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -1580,7 +1600,7 @@ class RayCastWorld : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 300.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -2066,7 +2086,7 @@ class OverlapWorld : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 330.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -2272,7 +2292,7 @@ class Manifold : public Sample
m_wedge = b2ComputeHull( points, 3 );
}

void UpdateUI() override
void UpdateGui() override
{
float height = 300.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -2963,7 +2983,7 @@ class SmoothManifold : public Sample
free( m_segments );
}

void UpdateUI() override
void UpdateGui() override
{
float height = 290.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down
8 changes: 4 additions & 4 deletions samples/sample_continuous.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class BounceHouse : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 100.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -341,7 +341,7 @@ class ChainDrop : public Sample
//m_shapeId = b2CreatePolygonShape( m_bodyId, &shapeDef, &box );
}

void UpdateUI() override
void UpdateGui() override
{
float height = 140.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -547,7 +547,7 @@ class SkinnyBox : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 110.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -816,7 +816,7 @@ class GhostBumps : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 140.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down
58 changes: 58 additions & 0 deletions samples/sample_determinism.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,61 @@ class BulletBug : public Sample
};

static int sampleBulletBug = RegisterSample( "Bugs", "Bullet Bug", BulletBug::Create );

bool hit = false;
bool callback( b2ShapeId id, void* context )
{
printf( "hit" );
hit = true;
return false;
}

class OverlapBug : public Sample
{
public:
explicit OverlapBug( Settings& settings )
: Sample( settings )
{
if ( settings.restart == false )
{
g_camera.m_center = { 0.0f, 2.5f };
g_camera.m_zoom = 3.5f;
}

float boxSize = 0.5f;
b2BodyDef body_def = b2DefaultBodyDef();
body_def.type = b2_staticBody;
body_def.position = { x, y };
b2BodyId body_id = b2CreateBody( m_worldId, &body_def );
b2Polygon polygon = b2MakeSquare( boxSize );
b2ShapeDef shape_def = b2DefaultShapeDef();
b2CreatePolygonShape( body_id, &shape_def, &polygon );
}

void Step( Settings& settings ) override
{
Sample::Step( settings );

float testSize = 0.4f;
b2Polygon test_polygon = b2MakeSquare( testSize );
b2Transform tfm = { { x, y }, { 1.0f, 0.0f } };
b2World_OverlapPolygon( m_worldId, &test_polygon, tfm, b2DefaultQueryFilter(), callback, nullptr );

b2Vec2 vertices[4];
vertices[0] = b2TransformPoint(tfm, test_polygon.vertices[0]);
vertices[1] = b2TransformPoint(tfm, test_polygon.vertices[1]);
vertices[2] = b2TransformPoint(tfm, test_polygon.vertices[2]);
vertices[3] = b2TransformPoint(tfm, test_polygon.vertices[3]);
g_draw.DrawPolygon(vertices, 4, b2_colorOrange);
}

static Sample* Create( Settings& settings )
{
return new OverlapBug( settings );
}

float x = 3.0f;
float y = 5.0f;
};

static int sampleSingleBox = RegisterSample( "Bugs", "Overlap", OverlapBug::Create );
10 changes: 5 additions & 5 deletions samples/sample_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ class SensorFunnel : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 90.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -398,7 +398,7 @@ class SensorBookend : public Sample
m_visitorShapeId = b2CreateCircleShape( m_visitorBodyId, &shapeDef, &circle );
}

void UpdateUI() override
void UpdateGui() override
{
float height = 160.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -764,7 +764,7 @@ class ContactEvent : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 60.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -1199,7 +1199,7 @@ class Platformer : public Sample
return false;
}

void UpdateUI() override
void UpdateGui() override
{
float height = 100.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down Expand Up @@ -1414,7 +1414,7 @@ class BodyMove : public Sample
}
}

void UpdateUI() override
void UpdateGui() override
{
float height = 100.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
Expand Down
Loading

0 comments on commit 5b9f690

Please sign in to comment.