#pragma once
#include "core.h"
#include "box2d/math_functions.h"
#include <stdbool.h>
#include <stdint.h>
#if B2_SIMD_WIDTH == 8
#define B2_SIMD_SHIFT 3
#elif B2_SIMD_WIDTH == 4
#define B2_SIMD_SHIFT 2
#else
#define B2_SIMD_SHIFT 0
#endif
typedef struct b2BodySim b2BodySim;
typedef struct b2BodyState b2BodyState;
typedef struct b2ContactSim b2ContactSim;
typedef struct b2ContactConstraintWide b2ContactConstraintWide;
typedef struct b2JointSim b2JointSim;
typedef struct b2World b2World;
typedef enum b2SolverStageType
{
b2_stagePrepareJoints,
b2_stagePrepareContacts,
b2_stageIntegrateVelocities,
b2_stageWarmStart,
b2_stageSolve,
b2_stageIntegratePositions,
b2_stageRelax,
b2_stageRestitution,
b2_stageStoreImpulses
} b2SolverStageType;
typedef enum b2SolverBlockType
{
b2_bodyBlock,
b2_jointBlock,
b2_contactBlock,
b2_graphJointBlock,
b2_graphContactBlock
} b2SolverBlockType;
typedef struct b2SolverBlock
{
int startIndex;
uint16_t count;
uint8_t blockType;
uint8_t colorIndex;
} b2SolverBlock;
typedef struct b2SyncBlock
{
b2SolverBlock block;
b2AtomicInt syncIndex;
} b2SyncBlock;
typedef struct b2SolverStage
{
b2SyncBlock* blocks;
b2SolverStageType type;
int blockCount;
uint8_t colorIndex;
b2AtomicInt completionCount;
} b2SolverStage;
typedef struct b2Softness
{
float biasRate;
float massScale;
float impulseScale;
} b2Softness;
typedef struct b2ContactPrepareSpan
{
int start;
int count;
b2ContactSim* contacts;
} b2ContactPrepareSpan;
typedef struct b2JointPrepareSpan
{
int start;
int count;
b2JointSim* joints;
} b2JointPrepareSpan;
typedef struct b2StepContext
{
float dt;
float inv_dt;
float h;
float inv_h;
int subStepCount;
b2Softness contactSoftness;
b2Softness staticSoftness;
float restitutionThreshold;
float maxLinearVelocity;
struct b2World* world;
struct b2ConstraintGraph* graph;
b2BodyState* states;
b2BodySim* sims;
int* enlargedShapes;
int enlargedShapeCount;
int* bulletBodies;
b2AtomicInt bulletBodyCount;
b2ContactSim** contactSims;
b2ContactConstraintWide* wideContactConstraints;
b2ContactPrepareSpan* contactPrepareSpans;
int wideContactCount;
b2JointPrepareSpan* jointPrepareSpans;
int jointCount;
int activeColorCount;
int workerCount;
b2SolverStage* stages;
int stageCount;
bool enableWarmStarting;
char padding1[64];
b2AtomicU32 atomicSyncBits;
char padding2[64];
b2AtomicInt mainClaimed;
char padding3[64];
} b2StepContext;
static inline b2Softness b2MakeSoft( float hertz, float zeta, float h )
{
if ( hertz == 0.0f )
{
return (b2Softness){
.biasRate = 0.0f,
.massScale = 0.0f,
.impulseScale = 0.0f,
};
}
float omega = 2.0f * B2_PI * hertz;
float a1 = 2.0f * zeta + h * omega;
float a2 = h * omega * a1;
float a3 = 1.0f / ( 1.0f + a2 );
return (b2Softness){
.biasRate = omega / a1,
.massScale = a2 * a3,
.impulseScale = a3,
};
}
void b2Solve( b2World* world, b2StepContext* stepContext );