Skip to content

Commit

Permalink
b2_defaultWorldDef
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Dec 29, 2023
1 parent 8ebdc4b commit 04cd0b1
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 26 deletions.
75 changes: 55 additions & 20 deletions include/box2d/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,32 +156,39 @@ typedef struct b2WorldDef
/// Stack allocator capacity. This controls how much space box2d reserves for per-frame calculations.
/// Larger worlds require more space. b2Counters can be used to determine a good capacity for your
/// application.
int32_t stackAllocatorCapacity;
int32_t arenaAllocatorCapacity;

/// task system hookup
uint32_t workerCount;

/// function to spawn task
b2EnqueueTaskCallback* enqueueTask;

/// function to finish a task
b2FinishTaskCallback* finishTask;

/// User context that is provided to enqueueTask and finishTask
void* userTaskContext;
} b2WorldDef;

/// Use this to initialize your world definition
static inline b2WorldDef b2DefaultWorldDef(void)
{
b2WorldDef def = B2_ZERO_INIT;
def.gravity = B2_LITERAL(b2Vec2){0.0f, -10.0f};
def.restitutionThreshold = 1.0f * b2_lengthUnitsPerMeter;
def.contactPushoutVelocity = 3.0f * b2_lengthUnitsPerMeter;
def.contactHertz = 30.0f;
def.contactDampingRatio = 1.0f;
def.enableSleep = true;
def.bodyCapacity = 8;
def.shapeCapacity = 8;
def.contactCapacity = 8;
def.jointCapacity = 8;
def.stackAllocatorCapacity = 1024 * 1024;
return def;
}
static const b2WorldDef b2_defaultWorldDef = {
{0.0f, -10.0f}, // gravity
1.0f * b2_lengthUnitsPerMeter, // restitutionThreshold
3.0f * b2_lengthUnitsPerMeter, // contactPushoutVelocity
30.0, // contactHertz
1.0f, // contactDampingRatio
true, // enableSleep
0, // bodyCapacity
0, // shapeCapacity
0, // contactCapacity
0, // jointCapacity
1024 * 1024, // arenaAllocatorCapacity
0, // workerCount
NULL, // enqueueTask
NULL, // finishTask
NULL, // userTaskContext
};

/// The body type.
/// static: zero mass, zero velocity, may be manually moved
Expand Down Expand Up @@ -248,7 +255,19 @@ typedef struct b2BodyDef

/// Use this to initialize your body definition
static const b2BodyDef b2_defaultBodyDef = {
b2_staticBody, {0.0f, 0.0f}, 0.0f, {0.0f, 0.0f}, 0.0f, 0.0f, 0.0f, 1.0f, NULL, true, true, false, true,
b2_staticBody, // bodyType
{0.0f, 0.0f}, // position
0.0f, // angle
{0.0f, 0.0f}, // linearVelocity
0.0f, // angularVelocity
0.0f, // linearDamping
0.0f, // angularDamping
1.0f, // gravityScale
NULL, // userData
true, // enableSleep
true, // isAwake
false, // fixedRotation
true, // isEnabled
};

/// This holds contact filtering data.
Expand Down Expand Up @@ -330,7 +349,15 @@ typedef struct b2ShapeDef

/// Use this to initialize your shape definition
static const b2ShapeDef b2_defaultShapeDef = {
NULL, 0.6f, 0.0f, 1.0f, {0x00000001, 0xFFFFFFFF, 0}, false, true, true, false,
NULL, // userData
0.6f, // friction
0.0f, // restitution
1.0f, // density
{0x00000001, 0xFFFFFFFF, 0}, // filter
false, // isSensor
true, // enableSensorEvents
true, // enableContactEvents
false, // enablePreSolveEvents
};

/// Used to create a chain of edges. This is designed to eliminate ghost collisions with some limitations.
Expand Down Expand Up @@ -370,7 +397,15 @@ typedef struct b2ChainDef
} b2ChainDef;

/// Use this to initialize your chain definition
static const b2ChainDef b2_defaultChainDef = {NULL, 0, false, NULL, 0.6f, 0.0f, {0x00000001, 0xFFFFFFFF, 0}};
static const b2ChainDef b2_defaultChainDef = {
NULL, // points
0, // count
false, // loop
NULL, // userData
0.6f, // friction
0.0f, // restitution
{0x00000001, 0xFFFFFFFF, 0} // filter
};

/// Profiling data. Times are in milliseconds.
typedef struct b2Profile
Expand Down
4 changes: 2 additions & 2 deletions samples/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Sample::Sample(const Settings& settings)
m_scheduler.Initialize(settings.workerCount);
m_taskCount = 0;

b2WorldDef worldDef = b2DefaultWorldDef();
b2WorldDef worldDef = b2_defaultWorldDef;
worldDef.workerCount = settings.workerCount;
worldDef.enqueueTask = &EnqueueTask;
worldDef.finishTask = &FinishTask;
Expand All @@ -66,7 +66,7 @@ Sample::Sample(const Settings& settings)
// These are not ideal, but useful for testing Box2D
worldDef.bodyCapacity = 2;
worldDef.contactCapacity = 2;
worldDef.stackAllocatorCapacity = 0;
worldDef.arenaAllocatorCapacity = 0;

m_worldId = b2CreateWorld(&worldDef);
m_textLine = 30;
Expand Down
2 changes: 1 addition & 1 deletion src/world.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ b2WorldId b2CreateWorld(const b2WorldDef* def)
world->index = id.index;

world->blockAllocator = b2CreateBlockAllocator();
world->stackAllocator = b2CreateStackAllocator(def->stackAllocatorCapacity);
world->stackAllocator = b2CreateStackAllocator(def->arenaAllocatorCapacity);

b2CreateBroadPhase(&world->broadPhase);
b2CreateGraph(&world->graph, def->bodyCapacity, def->contactCapacity, def->jointCapacity);
Expand Down
2 changes: 1 addition & 1 deletion test/test_determinism.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void TiltedStacks(int testIndex, int workerCount)
b2Vec2 gravity = {0.0f, -10.0f};

// Construct a world object, which will hold and simulate the rigid bodies.
b2WorldDef worldDef = b2DefaultWorldDef();
b2WorldDef worldDef = b2_defaultWorldDef;
worldDef.gravity = gravity;
worldDef.enqueueTask = EnqueueTask;
worldDef.finishTask = FinishTask;
Expand Down
4 changes: 2 additions & 2 deletions test/test_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int HelloWorld(void)
b2Vec2 gravity = {0.0f, -10.0f};

// Construct a world object, which will hold and simulate the rigid bodies.
b2WorldDef worldDef = b2DefaultWorldDef();
b2WorldDef worldDef = b2_defaultWorldDef;
worldDef.gravity = gravity;

b2WorldId worldId = b2CreateWorld(&worldDef);
Expand Down Expand Up @@ -98,7 +98,7 @@ int HelloWorld(void)

int EmptyWorld(void)
{
b2WorldDef worldDef = b2DefaultWorldDef();
b2WorldDef worldDef = b2_defaultWorldDef;
b2WorldId worldId = b2CreateWorld(&worldDef);
ENSURE(b2World_IsValid(worldId) == true);

Expand Down

0 comments on commit 04cd0b1

Please sign in to comment.