1- #include " box2d/box2d.h"
21#include " sample.h"
32
3+ #include " box2d/box2d.h"
4+
45#include < GLFW/glfw3.h>
56
67struct PhysicsHitQueryResult2D
@@ -22,11 +23,11 @@ struct CastContext_Single
2223 bool hit = false ;
2324};
2425
25- static float b2CastResult_Closest (b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void * c)
26+ static float b2CastResult_Closest ( b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void * c )
2627{
27- CastContext_Single* context = static_cast <CastContext_Single*>(c );
28+ CastContext_Single* context = static_cast <CastContext_Single*>( c );
2829
29- if (b2Dot (context->translation , normal) >= 0 .0f )
30+ if ( b2Dot ( context->translation , normal ) >= 0 .0f )
3031 return -1 .0f ;
3132
3233 context->result .shapeId = shapeId;
@@ -40,15 +41,15 @@ static float b2CastResult_Closest(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal
4041 return fraction;
4142}
4243
43- static b2ShapeProxy TransformShapeProxy (const b2Transform& t, const b2ShapeProxy& proxy)
44+ static b2ShapeProxy TransformShapeProxy ( const b2Transform& t, const b2ShapeProxy& proxy )
4445{
4546 b2ShapeProxy ret;
4647 ret.count = proxy.count ;
4748 ret.radius = proxy.radius ;
4849
49- for (int i = 0 ; i < proxy.count ; ++i)
50+ for ( int i = 0 ; i < proxy.count ; ++i )
5051 {
51- ret.points [i] = b2TransformPoint (t, proxy.points [i]);
52+ ret.points [i] = b2TransformPoint ( t, proxy.points [i] );
5253 }
5354
5455 return ret;
@@ -58,20 +59,14 @@ static b2ShapeProxy TransformShapeProxy(const b2Transform& t, const b2ShapeProxy
5859class ShapeCastChain : public Sample
5960{
6061public:
61- explicit ShapeCastChain (SampleContext* context)
62- : Sample(context)
62+ explicit ShapeCastChain ( SampleContext* context )
63+ : Sample( context )
6364 {
6465 // World Body & Shape
6566 b2BodyDef worldBodyDef = b2DefaultBodyDef ();
66- const b2BodyId groundId = b2CreateBody (m_worldId, &worldBodyDef);
67+ const b2BodyId groundId = b2CreateBody ( m_worldId, &worldBodyDef );
6768
68- b2Vec2 points[4 ] =
69- {
70- { 1 .0f , 0 .0f },
71- { -1 .0f , 0 .0f },
72- { -1 .0f , -1 .0f },
73- { 1 .0f , -1 .0f }
74- };
69+ b2Vec2 points[4 ] = { { 1 .0f , 0 .0f }, { -1 .0f , 0 .0f }, { -1 .0f , -1 .0f }, { 1 .0f , -1 .0f } };
7570
7671 b2ChainDef worldChainDef = b2DefaultChainDef ();
7772 worldChainDef.userData = nullptr ;
@@ -82,12 +77,12 @@ class ShapeCastChain : public Sample
8277 worldChainDef.filter .groupIndex = 0 ;
8378 worldChainDef.isLoop = true ;
8479 worldChainDef.enableSensorEvents = false ;
85- b2CreateChain (groundId, &worldChainDef);
80+ b2CreateChain ( groundId, &worldChainDef );
8681
8782 // "Character" Body & Shape
8883 b2BodyDef characterBodyDef = b2DefaultBodyDef ();
8984 characterBodyDef.position = { 0 .0f , 0 .103f };
90- characterBodyDef.rotation = b2MakeRot (0 .0f );
85+ characterBodyDef.rotation = b2MakeRot ( 0 .0f );
9186 characterBodyDef.linearDamping = 0 .0f ;
9287 characterBodyDef.angularDamping = 0 .0f ;
9388 characterBodyDef.gravityScale = 1 .0f ;
@@ -98,7 +93,7 @@ class ShapeCastChain : public Sample
9893 characterBodyDef.userData = nullptr ;
9994 characterBodyDef.type = b2_kinematicBody;
10095
101- characterBodyId_ = b2CreateBody (m_worldId, &characterBodyDef);
96+ characterBodyId_ = b2CreateBody ( m_worldId, &characterBodyDef );
10297
10398 b2ShapeDef characterShapeDef = b2DefaultShapeDef ();
10499
@@ -114,8 +109,8 @@ class ShapeCastChain : public Sample
114109 characterShapeDef.invokeContactCreation = false ;
115110 characterShapeDef.updateBodyMass = false ;
116111
117- characterBox_ = b2MakeBox (0 .1f , 0 .1f );
118- b2CreatePolygonShape (characterBodyId_, &characterShapeDef, &characterBox_);
112+ characterBox_ = b2MakeBox ( 0 .1f , 0 .1f );
113+ b2CreatePolygonShape ( characterBodyId_, &characterShapeDef, &characterBox_ );
119114
120115 context->camera .m_center = b2Vec2_zero;
121116 }
@@ -125,32 +120,32 @@ class ShapeCastChain : public Sample
125120 const float timeStep = m_context->hertz > 0 .0f ? 1 .0f / m_context->hertz : 0 .0f ;
126121
127122 bool noXInput = true ;
128- if (glfwGetKey (m_context->window , GLFW_KEY_A) )
123+ if ( glfwGetKey ( m_context->window , GLFW_KEY_A ) )
129124 {
130125 characterVelocity_.x -= timeStep * 5 .0f ;
131126 noXInput = false ;
132127 }
133- if (glfwGetKey (m_context->window , GLFW_KEY_D) )
128+ if ( glfwGetKey ( m_context->window , GLFW_KEY_D ) )
134129 {
135130 characterVelocity_.x += timeStep * 5 .0f ;
136131 noXInput = false ;
137132 }
138133
139134 bool noYInput = true ;
140- if (glfwGetKey (m_context->window , GLFW_KEY_S) )
135+ if ( glfwGetKey ( m_context->window , GLFW_KEY_S ) )
141136 {
142137 characterVelocity_.y -= timeStep * 5 .0f ;
143138 noYInput = false ;
144139 }
145- if (glfwGetKey (m_context->window , GLFW_KEY_W) )
140+ if ( glfwGetKey ( m_context->window , GLFW_KEY_W ) )
146141 {
147142 characterVelocity_.y += timeStep * 5 .0f ;
148143 noYInput = false ;
149144 }
150145
151- if (noXInput)
146+ if ( noXInput )
152147 {
153- if (b2AbsFloat (characterVelocity_.x ) > 0 .01f )
148+ if ( b2AbsFloat ( characterVelocity_.x ) > 0 .01f )
154149 {
155150 const float decel = characterVelocity_.x > 0 .0f ? 5 .0f : -5 .0f ;
156151 if ( b2AbsFloat ( decel ) < characterVelocity_.x )
@@ -168,7 +163,7 @@ class ShapeCastChain : public Sample
168163 }
169164 }
170165
171- if (noYInput)
166+ if ( noYInput )
172167 {
173168 if ( b2AbsFloat ( characterVelocity_.y ) > 0 .01f )
174169 {
@@ -188,42 +183,39 @@ class ShapeCastChain : public Sample
188183 }
189184 }
190185
191- b2Vec2 characterPos = b2Body_GetPosition (characterBodyId_);
186+ b2Vec2 characterPos = b2Body_GetPosition ( characterBodyId_ );
192187 b2Vec2 newCharacterPos = characterPos;
193188 newCharacterPos.x += characterVelocity_.x * timeStep;
194189 newCharacterPos.y += characterVelocity_.y * timeStep;
195- b2Body_SetTransform (characterBodyId_, newCharacterPos, b2Rot_identity);
190+ b2Body_SetTransform ( characterBodyId_, newCharacterPos, b2Rot_identity );
196191
197192 PhysicsHitQueryResult2D hitResult;
198- const b2ShapeProxy shapeProxy = b2MakeProxy (characterBox_.vertices , characterBox_.count , characterBox_.radius );
199- if (ShapeCastSingle (hitResult, characterPos, newCharacterPos, 0 .0f , shapeProxy) )
193+ const b2ShapeProxy shapeProxy = b2MakeProxy ( characterBox_.vertices , characterBox_.count , characterBox_.radius );
194+ if ( ShapeCastSingle ( hitResult, characterPos, newCharacterPos, 0 .0f , shapeProxy ) )
200195 {
201196 hitPos = hitResult.point ;
202197 hitNormal = hitResult.normal ;
203198 }
204199
205- m_draw->DrawLine (hitPos, { hitPos.x + hitNormal.x , hitPos.y + hitNormal.y }, b2_colorRed);
200+ m_draw->DrawLine ( hitPos, { hitPos.x + hitNormal.x , hitPos.y + hitNormal.y }, b2_colorRed );
206201
207202 Sample::Step ();
208203 }
209204
210- bool ShapeCastSingle (PhysicsHitQueryResult2D& outResult, b2Vec2 start, b2Vec2 end, float rotation, const b2ShapeProxy& shape) const
205+ bool ShapeCastSingle ( PhysicsHitQueryResult2D& outResult, b2Vec2 start, b2Vec2 end, float rotation,
206+ const b2ShapeProxy& shape ) const
211207 {
212- const b2Transform transform =
213- {
214- start,
215- b2MakeRot (rotation)
216- };
217- const b2ShapeProxy transformedShape = TransformShapeProxy (transform, shape);
208+ const b2Transform transform = { start, b2MakeRot ( rotation ) };
209+ const b2ShapeProxy transformedShape = TransformShapeProxy ( transform, shape );
218210
219211 const b2Vec2 translation = { end.x - start.x , end.y - start.y };
220212 const b2QueryFilter filter = { 0x1 , 0x1 };
221213 CastContext_Single context;
222214 context.startPos = start;
223215 context.translation = { translation.x , translation.y };
224- b2World_CastShape (m_worldId, &transformedShape, translation, filter, &b2CastResult_Closest, &context);
216+ b2World_CastShape ( m_worldId, &transformedShape, translation, filter, &b2CastResult_Closest, &context );
225217
226- if (context.hit )
218+ if ( context.hit )
227219 {
228220 outResult = context.result ;
229221 return true ;
@@ -232,9 +224,9 @@ class ShapeCastChain : public Sample
232224 return false ;
233225 }
234226
235- static Sample* Create (SampleContext* context)
227+ static Sample* Create ( SampleContext* context )
236228 {
237- return new ShapeCastChain (context);
229+ return new ShapeCastChain ( context );
238230 }
239231
240232private:
@@ -245,4 +237,53 @@ class ShapeCastChain : public Sample
245237 b2Vec2 hitNormal = b2Vec2_zero;
246238};
247239
248- static int sampleShapeCastChain = RegisterSample(" Issues" , " Shape Cast Chain" , ShapeCastChain::Create );
240+ static int sampleShapeCastChain = RegisterSample( " Issues" , " Shape Cast Chain" , ShapeCastChain::Create );
241+
242+ class BadSteiner : public Sample
243+ {
244+ public:
245+ explicit BadSteiner ( SampleContext* context )
246+ : Sample( context )
247+ {
248+ if ( m_context->restart == false )
249+ {
250+ m_context->camera .m_center = { 0 .0f , 1 .75f };
251+ m_context->camera .m_zoom = 2 .5f ;
252+ }
253+
254+ {
255+ b2BodyDef bodyDef = b2DefaultBodyDef ();
256+ b2BodyId groundId = b2CreateBody ( m_worldId, &bodyDef );
257+
258+ b2ShapeDef shapeDef = b2DefaultShapeDef ();
259+ b2Segment segment = { { -100 .0f , 0 .0f }, { 100 .0f , 0 .0f } };
260+ b2CreateSegmentShape ( groundId, &shapeDef, &segment );
261+ }
262+
263+ {
264+ b2BodyDef bodyDef = b2DefaultBodyDef ();
265+ bodyDef.type = b2_dynamicBody;
266+ bodyDef.position = { -48 .0f , 62 .0f };
267+ b2BodyId bodyId = b2CreateBody ( m_worldId, &bodyDef );
268+
269+ b2ShapeDef shapeDef = b2DefaultShapeDef ();
270+
271+ b2Vec2 points[3 ] = {
272+ { 48 .7599983f , -60 .5699997f },
273+ { 48 .7400017f , -60 .5400009f },
274+ { 48 .6800003f , -60 .5600014f },
275+ };
276+
277+ b2Hull hull = b2ComputeHull ( points, 3 );
278+ b2Polygon poly = b2MakePolygon ( &hull, 0 .0f );
279+ b2CreatePolygonShape ( bodyId, &shapeDef, &poly );
280+ }
281+ }
282+
283+ static Sample* Create ( SampleContext* context )
284+ {
285+ return new BadSteiner ( context );
286+ }
287+ };
288+
289+ static int sampleBadSteiner = RegisterSample( " Issues" , " Bad Steiner" , BadSteiner::Create );
0 commit comments