Skip to content

Commit cad8599

Browse files
authored
chain shape material (erincatto#874)
adjusted chain time of impact to improve performance
1 parent f248ccc commit cad8599

8 files changed

Lines changed: 61 additions & 14 deletions

File tree

include/box2d/box2d.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,13 @@ B2_API void b2Chain_SetRestitution( b2ChainId chainId, float restitution );
708708
/// Get the chain restitution
709709
B2_API float b2Chain_GetRestitution( b2ChainId chainId );
710710

711+
/// Set the chain material
712+
/// @see b2ChainDef::material
713+
B2_API void b2Chain_SetMaterial( b2ChainId chainId, int material );
714+
715+
/// Get the chain material
716+
B2_API int b2Chain_GetMaterial( b2ChainId chainId );
717+
711718
/// Chain identifier validation. Provides validation for up to 64K allocations.
712719
B2_API bool b2Chain_IsValid( b2ChainId id );
713720

include/box2d/types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ typedef struct b2ChainDef
426426
/// The restitution (elasticity) usually in the range [0,1].
427427
float restitution;
428428

429+
/// User material identifier. This is passed with query results and to friction and restitution
430+
/// combining functions. It is not used internally.
431+
int material;
432+
429433
/// Contact filtering data.
430434
b2Filter filter;
431435

samples/sample_benchmark.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <vector>
1717

1818
#ifndef NDEBUG
19+
extern "C" int b2_toiCalls;
1920
extern "C" int b2_toiHitCount;
2021
#endif
2122

@@ -1476,6 +1477,7 @@ class BenchmarkSpinner : public Sample
14761477
}
14771478

14781479
#ifndef NDEBUG
1480+
b2_toiCalls = 0;
14791481
b2_toiHitCount = 0;
14801482
#endif
14811483

@@ -1486,13 +1488,16 @@ class BenchmarkSpinner : public Sample
14861488
{
14871489
Sample::Step( settings );
14881490

1489-
if ( m_stepCount == 1000 )
1491+
if ( m_stepCount == 1000 && false )
14901492
{
1491-
m_stepCount += 0;
1493+
// 0.1 : 46544, 25752
1494+
// 0.25 : 5745, 1947
1495+
// 0.5 : 2197, 660
1496+
settings.pause = true;
14921497
}
1498+
14931499
#ifndef NDEBUG
1494-
g_draw.DrawString( 5, m_textLine, "toi hits = %d", b2_toiHitCount );
1495-
m_textLine += m_textIncrement;
1500+
DrawTextLine( "toi calls, hits = %d, %d", b2_toiCalls, b2_toiHitCount );
14961501
#endif
14971502
}
14981503

samples/sample_continuous.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,15 +330,15 @@ class ChainDrop : public Sample
330330

331331
b2ShapeDef shapeDef = b2DefaultShapeDef();
332332

333-
//b2Circle circle = { { 0.0f, 0.0f }, 0.5f };
334-
//m_shapeId = b2CreateCircleShape( m_bodyId, &shapeDef, &circle );
333+
b2Circle circle = { { 0.0f, 0.0f }, 0.5f };
334+
m_shapeId = b2CreateCircleShape( m_bodyId, &shapeDef, &circle );
335335

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

339-
float h = 0.5f;
340-
b2Polygon box = b2MakeBox( h, h );
341-
m_shapeId = b2CreatePolygonShape( m_bodyId, &shapeDef, &box );
339+
//float h = 0.5f;
340+
//b2Polygon box = b2MakeBox( h, h );
341+
//m_shapeId = b2CreatePolygonShape( m_bodyId, &shapeDef, &box );
342342
}
343343

344344
void UpdateUI() override

samples/sample_shapes.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class ChainShape : public Sample
118118
chainDef.customColor = b2_colorSteelBlue;
119119
chainDef.isLoop = true;
120120
chainDef.friction = 0.2f;
121+
chainDef.material = 42;
121122

122123
b2BodyDef bodyDef = b2DefaultBodyDef();
123124
m_groundId = b2CreateBody( m_worldId, &bodyDef );
@@ -210,8 +211,7 @@ class ChainShape : public Sample
210211
g_draw.DrawSegment( b2Vec2_zero, { 0.0f, 0.5f }, b2_colorGreen );
211212

212213
#ifndef NDEBUG
213-
g_draw.DrawString( 5, m_textLine, "toi calls, hits = %d, %d", b2_toiCalls, b2_toiHitCount );
214-
m_textLine += m_textIncrement;
214+
DrawTextLine( "toi calls, hits = %d, %d", b2_toiCalls, b2_toiHitCount );
215215
#endif
216216
}
217217

@@ -814,7 +814,7 @@ class Restitution : public Sample
814814
}
815815
}
816816

817-
b2Circle circle = { };
817+
b2Circle circle = {};
818818
circle.radius = 0.5f;
819819

820820
b2Polygon box = b2MakeBox( 0.5f, 0.5f );

src/shape.c

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,13 +371,15 @@ b2ChainId b2CreateChain( b2BodyId bodyId, const b2ChainDef* def )
371371
chainShape->generation += 1;
372372
chainShape->friction = def->friction;
373373
chainShape->restitution = def->restitution;
374+
chainShape->material = def->material;
374375

375376
body->headChainId = chainId;
376377

377378
b2ShapeDef shapeDef = b2DefaultShapeDef();
378379
shapeDef.userData = def->userData;
379-
shapeDef.restitution = def->restitution;
380380
shapeDef.friction = def->friction;
381+
shapeDef.restitution = def->restitution;
382+
shapeDef.material = def->material;
381383
shapeDef.filter = def->filter;
382384
shapeDef.customColor = def->customColor;
383385
shapeDef.enableContactEvents = false;
@@ -1417,6 +1419,34 @@ float b2Chain_GetRestitution( b2ChainId chainId )
14171419
return chainShape->restitution;
14181420
}
14191421

1422+
void b2Chain_SetMaterial( b2ChainId chainId, int material )
1423+
{
1424+
b2World* world = b2GetWorldLocked( chainId.world0 );
1425+
if ( world == NULL )
1426+
{
1427+
return;
1428+
}
1429+
1430+
b2ChainShape* chainShape = b2GetChainShape( world, chainId );
1431+
chainShape->material = material;
1432+
1433+
int count = chainShape->count;
1434+
1435+
for ( int i = 0; i < count; ++i )
1436+
{
1437+
int shapeId = chainShape->shapeIndices[i];
1438+
b2Shape* shape = b2ShapeArray_Get( &world->shapes, shapeId );
1439+
shape->material = material;
1440+
}
1441+
}
1442+
1443+
int b2Chain_GetMaterial( b2ChainId chainId )
1444+
{
1445+
b2World* world = b2GetWorld( chainId.world0 );
1446+
b2ChainShape* chainShape = b2GetChainShape( world, chainId );
1447+
return chainShape->material;
1448+
}
1449+
14201450
int b2Shape_GetContactCapacity( b2ShapeId shapeId )
14211451
{
14221452
b2World* world = b2GetWorldLocked( shapeId.world0 );

src/shape.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ typedef struct b2ChainShape
5757
int* shapeIndices;
5858
float friction;
5959
float restitution;
60+
int material;
6061
uint16_t generation;
6162
} b2ChainShape;
6263

src/solver.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ static bool b2ContinuousQueryCallback( int proxyId, int shapeId, void* context )
289289
b2Vec2 c2 = continuousContext->centroid2;
290290
float offset2 = b2Cross( b2Sub( c2, p1 ), e );
291291

292-
const float allowedFraction = 0.1f;
292+
const float allowedFraction = 0.25f;
293293
if ( offset1 < 0.0f || offset1 - offset2 < allowedFraction * fastBodySim->minExtent )
294294
{
295295
// Minimal clipping

0 commit comments

Comments
 (0)