Skip to content

Commit

Permalink
chain shape material (#874)
Browse files Browse the repository at this point in the history
adjusted chain time of impact to improve performance
  • Loading branch information
erincatto authored Jan 23, 2025
1 parent f248ccc commit cad8599
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 14 deletions.
7 changes: 7 additions & 0 deletions include/box2d/box2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,13 @@ B2_API void b2Chain_SetRestitution( b2ChainId chainId, float restitution );
/// Get the chain restitution
B2_API float b2Chain_GetRestitution( b2ChainId chainId );

/// Set the chain material
/// @see b2ChainDef::material
B2_API void b2Chain_SetMaterial( b2ChainId chainId, int material );

/// Get the chain material
B2_API int b2Chain_GetMaterial( b2ChainId chainId );

/// Chain identifier validation. Provides validation for up to 64K allocations.
B2_API bool b2Chain_IsValid( b2ChainId id );

Expand Down
4 changes: 4 additions & 0 deletions include/box2d/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ typedef struct b2ChainDef
/// The restitution (elasticity) usually in the range [0,1].
float restitution;

/// User material identifier. This is passed with query results and to friction and restitution
/// combining functions. It is not used internally.
int material;

/// Contact filtering data.
b2Filter filter;

Expand Down
13 changes: 9 additions & 4 deletions samples/sample_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <vector>

#ifndef NDEBUG
extern "C" int b2_toiCalls;
extern "C" int b2_toiHitCount;
#endif

Expand Down Expand Up @@ -1476,6 +1477,7 @@ class BenchmarkSpinner : public Sample
}

#ifndef NDEBUG
b2_toiCalls = 0;
b2_toiHitCount = 0;
#endif

Expand All @@ -1486,13 +1488,16 @@ class BenchmarkSpinner : public Sample
{
Sample::Step( settings );

if ( m_stepCount == 1000 )
if ( m_stepCount == 1000 && false )
{
m_stepCount += 0;
// 0.1 : 46544, 25752
// 0.25 : 5745, 1947
// 0.5 : 2197, 660
settings.pause = true;
}

#ifndef NDEBUG
g_draw.DrawString( 5, m_textLine, "toi hits = %d", b2_toiHitCount );
m_textLine += m_textIncrement;
DrawTextLine( "toi calls, hits = %d, %d", b2_toiCalls, b2_toiHitCount );
#endif
}

Expand Down
10 changes: 5 additions & 5 deletions samples/sample_continuous.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,15 @@ class ChainDrop : public Sample

b2ShapeDef shapeDef = b2DefaultShapeDef();

//b2Circle circle = { { 0.0f, 0.0f }, 0.5f };
//m_shapeId = b2CreateCircleShape( m_bodyId, &shapeDef, &circle );
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 );
//float h = 0.5f;
//b2Polygon box = b2MakeBox( h, h );
//m_shapeId = b2CreatePolygonShape( m_bodyId, &shapeDef, &box );
}

void UpdateUI() override
Expand Down
6 changes: 3 additions & 3 deletions samples/sample_shapes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class ChainShape : public Sample
chainDef.customColor = b2_colorSteelBlue;
chainDef.isLoop = true;
chainDef.friction = 0.2f;
chainDef.material = 42;

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

#ifndef NDEBUG
g_draw.DrawString( 5, m_textLine, "toi calls, hits = %d, %d", b2_toiCalls, b2_toiHitCount );
m_textLine += m_textIncrement;
DrawTextLine( "toi calls, hits = %d, %d", b2_toiCalls, b2_toiHitCount );
#endif
}

Expand Down Expand Up @@ -814,7 +814,7 @@ class Restitution : public Sample
}
}

b2Circle circle = { };
b2Circle circle = {};
circle.radius = 0.5f;

b2Polygon box = b2MakeBox( 0.5f, 0.5f );
Expand Down
32 changes: 31 additions & 1 deletion src/shape.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,15 @@ b2ChainId b2CreateChain( b2BodyId bodyId, const b2ChainDef* def )
chainShape->generation += 1;
chainShape->friction = def->friction;
chainShape->restitution = def->restitution;
chainShape->material = def->material;

body->headChainId = chainId;

b2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.userData = def->userData;
shapeDef.restitution = def->restitution;
shapeDef.friction = def->friction;
shapeDef.restitution = def->restitution;
shapeDef.material = def->material;
shapeDef.filter = def->filter;
shapeDef.customColor = def->customColor;
shapeDef.enableContactEvents = false;
Expand Down Expand Up @@ -1417,6 +1419,34 @@ float b2Chain_GetRestitution( b2ChainId chainId )
return chainShape->restitution;
}

void b2Chain_SetMaterial( b2ChainId chainId, int material )
{
b2World* world = b2GetWorldLocked( chainId.world0 );
if ( world == NULL )
{
return;
}

b2ChainShape* chainShape = b2GetChainShape( world, chainId );
chainShape->material = material;

int count = chainShape->count;

for ( int i = 0; i < count; ++i )
{
int shapeId = chainShape->shapeIndices[i];
b2Shape* shape = b2ShapeArray_Get( &world->shapes, shapeId );
shape->material = material;
}
}

int b2Chain_GetMaterial( b2ChainId chainId )
{
b2World* world = b2GetWorld( chainId.world0 );
b2ChainShape* chainShape = b2GetChainShape( world, chainId );
return chainShape->material;
}

int b2Shape_GetContactCapacity( b2ShapeId shapeId )
{
b2World* world = b2GetWorldLocked( shapeId.world0 );
Expand Down
1 change: 1 addition & 0 deletions src/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ typedef struct b2ChainShape
int* shapeIndices;
float friction;
float restitution;
int material;
uint16_t generation;
} b2ChainShape;

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 );

const float allowedFraction = 0.1f;
const float allowedFraction = 0.25f;
if ( offset1 < 0.0f || offset1 - offset2 < allowedFraction * fastBodySim->minExtent )
{
// Minimal clipping
Expand Down

0 comments on commit cad8599

Please sign in to comment.