@@ -20,11 +20,11 @@ public class Sleep : Sample
2020 private static readonly int SampleSleep = SampleFactory . Shared . RegisterSample ( "Bodies" , "Sleep" , Create ) ;
2121
2222 private B2BodyId m_pendulumId ;
23+ private B2BodyId m_staticBodyId ;
2324 private B2ShapeId m_groundShapeId ;
2425 private B2ShapeId [ ] m_sensorIds = new B2ShapeId [ 2 ] ;
2526 private bool [ ] m_sensorTouching = new bool [ 2 ] ;
2627
27-
2828 private static Sample Create ( SampleAppContext ctx , Settings settings )
2929 {
3030 return new Sleep ( ctx , settings ) ;
@@ -43,7 +43,7 @@ public Sleep(SampleAppContext ctx, Settings settings) : base(ctx, settings)
4343 B2BodyDef bodyDef = b2DefaultBodyDef ( ) ;
4444 groundId = b2CreateBody ( m_worldId , ref bodyDef ) ;
4545
46- B2Segment segment = new B2Segment ( new B2Vec2 ( - 20 .0f, 0.0f ) , new B2Vec2 ( 20 .0f, 0.0f ) ) ;
46+ B2Segment segment = new B2Segment ( new B2Vec2 ( - 40 .0f, 0.0f ) , new B2Vec2 ( 40 .0f, 0.0f ) ) ;
4747 B2ShapeDef shapeDef = b2DefaultShapeDef ( ) ;
4848 shapeDef . enableSensorEvents = true ;
4949 m_groundShapeId = b2CreateSegmentShape ( groundId , ref shapeDef , ref segment ) ;
@@ -133,12 +133,48 @@ public Sleep(SampleAppContext ctx, Settings settings) : base(ctx, settings)
133133 jointDef . localAnchorB = b2Body_GetLocalPoint ( jointDef . bodyIdB , pivot ) ;
134134 b2CreateRevoluteJoint ( m_worldId , ref jointDef ) ;
135135 }
136+
137+ // A sleeping body to test waking on contact destroyed
138+ {
139+ B2BodyDef bodyDef = b2DefaultBodyDef ( ) ;
140+ bodyDef . type = B2BodyType . b2_dynamicBody ;
141+ bodyDef . position = new B2Vec2 ( - 10.0f , 1.0f ) ;
142+ bodyDef . isAwake = false ;
143+ bodyDef . enableSleep = true ;
144+ B2BodyId bodyId = b2CreateBody ( m_worldId , ref bodyDef ) ;
145+
146+ B2Polygon box = b2MakeSquare ( 1.0f ) ;
147+ B2ShapeDef shapeDef = b2DefaultShapeDef ( ) ;
148+ b2CreatePolygonShape ( bodyId , ref shapeDef , ref box ) ;
149+ }
150+
151+ m_staticBodyId = b2_nullBodyId ;
152+ }
153+
154+ void ToggleInvoker ( )
155+ {
156+ if ( B2_IS_NULL ( m_staticBodyId ) )
157+ {
158+ B2BodyDef bodyDef = b2DefaultBodyDef ( ) ;
159+ bodyDef . position = new B2Vec2 ( - 10.5f , 3.0f ) ;
160+ m_staticBodyId = b2CreateBody ( m_worldId , ref bodyDef ) ;
161+
162+ B2Polygon box = b2MakeOffsetBox ( 2.0f , 0.1f , new B2Vec2 ( 0.0f , 0.0f ) , b2MakeRot ( 0.25f * B2_PI ) ) ;
163+ B2ShapeDef shapeDef = b2DefaultShapeDef ( ) ;
164+ shapeDef . invokeContactCreation = true ;
165+ b2CreatePolygonShape ( m_staticBodyId , ref shapeDef , ref box ) ;
166+ }
167+ else
168+ {
169+ b2DestroyBody ( m_staticBodyId ) ;
170+ m_staticBodyId = b2_nullBodyId ;
171+ }
136172 }
137173
138174 public override void UpdateGui ( )
139175 {
140176 base . UpdateGui ( ) ;
141- float height = 100 .0f;
177+ float height = 160 .0f;
142178 ImGui . SetNextWindowPos ( new Vector2 ( 10.0f , m_context . camera . m_height - height - 50.0f ) , ImGuiCond . Once ) ;
143179 ImGui . SetNextWindowSize ( new Vector2 ( 240.0f , height ) ) ;
144180 ImGui . Begin ( "Sleep" , ImGuiWindowFlags . NoMove | ImGuiWindowFlags . NoResize ) ;
@@ -162,6 +198,23 @@ public override void UpdateGui()
162198
163199 ImGui . PopItemWidth ( ) ;
164200
201+ ImGui . Separator ( ) ;
202+
203+ if ( B2_IS_NULL ( m_staticBodyId ) )
204+ {
205+ if ( ImGui . Button ( "Create" ) )
206+ {
207+ ToggleInvoker ( ) ;
208+ }
209+ }
210+ else
211+ {
212+ if ( ImGui . Button ( "Destroy" ) )
213+ {
214+ ToggleInvoker ( ) ;
215+ }
216+ }
217+
165218 ImGui . End ( ) ;
166219 }
167220
@@ -208,7 +261,7 @@ public override void Step(Settings settings)
208261 public override void Draw ( Settings settings )
209262 {
210263 base . Draw ( settings ) ;
211-
264+
212265 for ( int i = 0 ; i < 2 ; ++ i )
213266 {
214267 m_context . draw . DrawString ( 5 , m_textLine , $ "sensor touch { i } = { m_sensorTouching [ i ] } ") ;
0 commit comments