Skip to content

Commit

Permalink
disable sensors on disabled bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Feb 6, 2025
1 parent 2cb6aea commit 6a70379
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
31 changes: 28 additions & 3 deletions samples/sample_events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ class SensorBookend : public Sample

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

Expand All @@ -421,6 +421,18 @@ class SensorBookend : public Sample
m_visitorBodyId = b2_nullBodyId;
m_visitorShapeId = b2_nullShapeId;
}
else
{
bool enabled = b2Body_IsEnabled( m_visitorBodyId );
if ( enabled == true && ImGui::Button( "disable visitor" ) )
{
b2Body_Disable( m_visitorBodyId );
}
else if ( enabled == false && ImGui::Button( "enable visitor" ) )
{
b2Body_Enable( m_visitorBodyId );
}
}
}

if ( B2_IS_NULL( m_sensorBodyId ) )
Expand All @@ -438,6 +450,18 @@ class SensorBookend : public Sample
m_sensorBodyId = b2_nullBodyId;
m_sensorShapeId = b2_nullShapeId;
}
else
{
bool enabled = b2Body_IsEnabled( m_sensorBodyId );
if ( enabled == true && ImGui::Button( "disable sensor" ) )
{
b2Body_Disable( m_sensorBodyId );
}
else if ( enabled == false && ImGui::Button( "enable sensor" ) )
{
b2Body_Enable( m_sensorBodyId );
}
}
}

ImGui::End();
Expand Down Expand Up @@ -1360,6 +1384,7 @@ class BodyMove : public Sample
for ( int32_t i = 0; i < 10 && m_count < e_count; ++i )
{
bodyDef.position = { x, y };
bodyDef.isBullet = (m_count % 12 == 0);
bodyDef.userData = m_bodyIds + m_count;
m_bodyIds[m_count] = b2CreateBody( m_worldId, &bodyDef );
m_sleeping[m_count] = false;
Expand Down Expand Up @@ -1466,8 +1491,8 @@ class BodyMove : public Sample
return new BodyMove( settings );
}

b2BodyId m_bodyIds[e_count];
bool m_sleeping[e_count];
b2BodyId m_bodyIds[e_count] = {};
bool m_sleeping[e_count] = {};
int m_count;
int m_sleepCount;
b2Vec2 m_explosionPosition;
Expand Down
12 changes: 11 additions & 1 deletion src/sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,17 @@ static void b2SensorTask( int startIndex, int endIndex, uint32_t threadIndex, vo
sensor->overlaps2 = temp;
b2ShapeRefArray_Clear( &sensor->overlaps2 );

b2Transform transform = b2GetBodyTransform( world, sensorShape->bodyId );
b2Body* body = b2BodyArray_Get( &world->bodies, sensorShape->bodyId );
if (body->setIndex == b2_disabledSet)
{
if (sensor->overlaps1.count != 0)
{
b2SetBit( &taskContext->eventBits, sensorIndex );
}
continue;
}

b2Transform transform = b2GetBodyTransformQuick( world, body );

struct b2SensorQueryContext queryContext = {
.world = world,
Expand Down

0 comments on commit 6a70379

Please sign in to comment.