Skip to content

Commit

Permalink
adjusted allowed clip fraction usage
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Dec 30, 2024
1 parent 19b9f07 commit 7490025
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
25 changes: 21 additions & 4 deletions samples/sample_continuous.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ class ChainDrop : public Sample
m_bodyId = b2_nullBodyId;
m_yOffset = -0.1f;
m_speed = -42.0f;
m_clipFraction = 0.1f;

Launch();
}
Expand All @@ -241,24 +242,38 @@ class ChainDrop : public Sample
bodyDef.type = b2_dynamicBody;
bodyDef.linearVelocity = { 0.0f, m_speed };
bodyDef.position = { 0.0f, 10.0f + m_yOffset };
bodyDef.gravityScale = 1.0f;
bodyDef.rotation = b2MakeRot( 0.5f * B2_PI );
bodyDef.fixedRotation = true;
m_bodyId = b2CreateBody( m_worldId, &bodyDef );

b2ShapeDef shapeDef = b2DefaultShapeDef();
b2Circle circle = { { 0.0f, 0.0f }, 0.5f };
b2CreateCircleShape( m_bodyId, &shapeDef, &circle );
shapeDef.allowedClipFraction = m_clipFraction;

//b2Circle circle = { { 0.0f, 0.0f }, 0.5f };
//m_shapeId = b2CreateCircleShape( m_bodyId, &shapeDef, &circle );

//b2Capsule capsule = { { -0.5f, 0.0f }, { 0.5f, 0.0 }, 0.25f };
//m_shapeId = b2CreateCapsuleShape( m_bodyId, &shapeDef, &capsule );

float h = 0.5f;
b2Polygon box = b2MakeBox( h, h );
m_shapeId = b2CreatePolygonShape( m_bodyId, &shapeDef, &box );
}

void UpdateUI() override
{
float height = 120.0f;
float height = 140.0f;
ImGui::SetNextWindowPos( ImVec2( 10.0f, g_camera.m_height - height - 50.0f ), ImGuiCond_Once );
ImGui::SetNextWindowSize( ImVec2( 240.0f, height ) );

ImGui::Begin( "Chain Drop", nullptr, ImGuiWindowFlags_NoResize );

ImGui::SliderFloat( "Speed", &m_speed, -100.0f, 0.0f, "%.0f" );
ImGui::SliderFloat( "Y Offset", &m_yOffset, -1.0f, 1.0f, "%.1f" );
if (ImGui::SliderFloat( "Clip Fraction", &m_clipFraction, 0.0f, 1.0f, "%.1f" ))
{
b2Shape_SetAllowedClipFraction( m_shapeId, m_clipFraction );
}

if ( ImGui::Button( "Launch" ) )
{
Expand All @@ -274,8 +289,10 @@ class ChainDrop : public Sample
}

b2BodyId m_bodyId;
b2ShapeId m_shapeId;
float m_yOffset;
float m_speed;
float m_clipFraction;
};

static int sampleChainDrop = RegisterSample( "Continuous", "Chain Drop", ChainDrop::Create );
Expand Down
13 changes: 13 additions & 0 deletions src/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ typedef struct b2Shape
int bodyId;
int prevShapeId;
int nextShapeId;
int sensorIndex;
b2ShapeType type;
float density;
float friction;
Expand Down Expand Up @@ -66,6 +67,18 @@ typedef struct b2ShapeExtent
float maxExtent;
} b2ShapeExtent;

// Sensors are shapes that live in the broad-phase but never have contacts.
// At the end of the time step all sensors are queried for overlap with any other shapes.
// Sensors ignore body type and sleeping.
// Sensors generate events when there is a new overlap or and overlap disappears.
// The sensor overlaps don't get cleared until the next time step regardless of the overlapped
// shapes being destroyed.
// When a sensor is destroyed.
typedef struct
{
b2IntArray overlaps;
} b2SensorOverlaps;

void b2CreateShapeProxy( b2Shape* shape, b2BroadPhase* bp, b2BodyType type, b2Transform transform, bool forcePairCreation );
void b2DestroyShapeProxy( b2Shape* shape, b2BroadPhase* bp );

Expand Down
2 changes: 1 addition & 1 deletion src/solver.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ static bool b2ContinuousQueryCallback( int proxyId, int shapeId, void* context )
b2Vec2 c2 = continuousContext->centroid2;
float offset2 = b2Cross( b2Sub( c2, p1 ), e );

if ( offset1 < 0.0f || offset2 > (1.0f - fastShape->allowedClipFraction) * fastBodySim->minExtent )
if ( offset1 < 0.0f || offset1 - offset2 < fastShape->allowedClipFraction * fastBodySim->minExtent )
{
// Minimal clipping
return true;
Expand Down
3 changes: 3 additions & 0 deletions src/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ typedef struct b2World
b2ShapeArray shapes;
b2ChainShapeArray chainShapes;

// Array of all sensor shape indices
b2IntArray sensors;

// Per thread storage
b2TaskContextArray taskContexts;

Expand Down

0 comments on commit 7490025

Please sign in to comment.