Improve compound serialization (#183)
- no more pointer fixups - reduced storage 20% by removing leaf proxies
This commit is contained in:
parent
3d9db79ee1
commit
5d83df83ab
25 changed files with 845 additions and 376 deletions
|
|
@ -1340,3 +1340,276 @@ void StepSleep( b3WorldId worldId, int stepCount )
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define VILLAGE_TILE_WIDTH 4
|
||||
#define VILLAGE_CAST_RADIUS 0.05f
|
||||
#define VILLAGE_CAST_HEIGHT 10.0f
|
||||
#define VILLAGE_CAST_LENGTH 12.0f
|
||||
|
||||
#if BENCHMARK_DEBUG
|
||||
#define VILLAGE_GRID 10
|
||||
#define VILLAGE_BODY_GRID 10
|
||||
#else
|
||||
#define VILLAGE_GRID 100
|
||||
#define VILLAGE_BODY_GRID 70
|
||||
#endif
|
||||
|
||||
// One cast per meter across the full span
|
||||
#define VILLAGE_MAX_CASTS ( VILLAGE_GRID * VILLAGE_TILE_WIDTH + 1 )
|
||||
|
||||
typedef struct VillageData
|
||||
{
|
||||
b3CompoundData* compound;
|
||||
b3Quat rotation;
|
||||
float halfSpan;
|
||||
b3Vec3 castTranslation;
|
||||
int castCount;
|
||||
VillageCast casts[VILLAGE_MAX_CASTS];
|
||||
} VillageData;
|
||||
|
||||
static VillageData g_villageData;
|
||||
|
||||
void GetVillageCapacity( b3Capacity* capacity )
|
||||
{
|
||||
int bodyCount = VILLAGE_BODY_GRID * VILLAGE_BODY_GRID;
|
||||
capacity->staticShapeCount = 1;
|
||||
capacity->staticBodyCount = 1;
|
||||
capacity->dynamicShapeCount = bodyCount;
|
||||
capacity->dynamicBodyCount = bodyCount;
|
||||
capacity->contactCount = 4 * bodyCount;
|
||||
}
|
||||
|
||||
void CreateVillage( b3WorldId worldId )
|
||||
{
|
||||
memset( &g_villageData, 0, sizeof( g_villageData ) );
|
||||
g_randomSeed = 2718281828;
|
||||
|
||||
int gridCount = VILLAGE_GRID;
|
||||
float tileWidth = VILLAGE_TILE_WIDTH;
|
||||
float halfSpan = 0.5f * tileWidth * gridCount;
|
||||
g_villageData.halfSpan = halfSpan;
|
||||
|
||||
{
|
||||
int tileCount = gridCount * gridCount;
|
||||
int propCapacity = tileCount / 8 + 1;
|
||||
|
||||
b3CompoundHullDef* hulls = malloc( tileCount * sizeof( b3CompoundHullDef ) );
|
||||
b3CompoundMeshDef* meshes = malloc( tileCount * sizeof( b3CompoundMeshDef ) );
|
||||
b3CompoundCapsuleDef* capsules = malloc( propCapacity * sizeof( b3CompoundCapsuleDef ) );
|
||||
b3CompoundSphereDef* spheres = malloc( propCapacity * sizeof( b3CompoundSphereDef ) );
|
||||
|
||||
b3SurfaceMaterial material = b3DefaultSurfaceMaterial();
|
||||
b3BoxHull tile = b3MakeBoxHull( 0.5f * tileWidth, 0.5f, 0.5f * tileWidth );
|
||||
|
||||
// A wave patch replaces a sparse set of tiles so the compound carries mesh children
|
||||
b3MeshData* wave = b3CreateWaveMesh( 8, 8, tileWidth / 8.0f, 0.25f, 0.25f, 0.25f );
|
||||
|
||||
int hullCount = 0;
|
||||
int meshCount = 0;
|
||||
int capsuleCount = 0;
|
||||
int sphereCount = 0;
|
||||
|
||||
for ( int i = 0; i < gridCount; ++i )
|
||||
{
|
||||
float x = -halfSpan + ( i + 0.5f ) * tileWidth;
|
||||
|
||||
for ( int j = 0; j < gridCount; ++j )
|
||||
{
|
||||
float z = -halfSpan + ( j + 0.5f ) * tileWidth;
|
||||
|
||||
if ( i % 16 == 8 && j % 16 == 8 )
|
||||
{
|
||||
meshes[meshCount] = (b3CompoundMeshDef){
|
||||
.meshData = wave,
|
||||
.transform = { { x, 0.0f, z }, b3Quat_identity },
|
||||
.scale = b3Vec3_one,
|
||||
.materials = &material,
|
||||
.materialCount = 1,
|
||||
};
|
||||
meshCount += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
hulls[hullCount] = (b3CompoundHullDef){
|
||||
.hull = &tile.base,
|
||||
.transform = { { x, -0.5f, z }, b3Quat_identity },
|
||||
.material = material,
|
||||
};
|
||||
hullCount += 1;
|
||||
|
||||
int tileIndex = i * gridCount + j;
|
||||
if ( tileIndex % 8 != 0 )
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( ( tileIndex / 8 ) & 1 )
|
||||
{
|
||||
float radius = 0.25f;
|
||||
capsules[capsuleCount] = (b3CompoundCapsuleDef){
|
||||
.capsule = { { x - 1.0f, radius, z }, { x + 1.0f, radius, z }, radius },
|
||||
.material = material,
|
||||
};
|
||||
capsuleCount += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
float radius = 0.5f;
|
||||
spheres[sphereCount] = (b3CompoundSphereDef){
|
||||
.sphere = { { x, radius, z }, radius },
|
||||
.material = material,
|
||||
};
|
||||
sphereCount += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b3CompoundDef def = {
|
||||
.capsules = capsules,
|
||||
.capsuleCount = capsuleCount,
|
||||
.hulls = hulls,
|
||||
.hullCount = hullCount,
|
||||
.meshes = meshes,
|
||||
.meshCount = meshCount,
|
||||
.spheres = spheres,
|
||||
.sphereCount = sphereCount,
|
||||
};
|
||||
|
||||
g_villageData.compound = b3CreateCompound( &def );
|
||||
|
||||
// The compound clones its inputs
|
||||
b3DestroyMesh( wave );
|
||||
free( hulls );
|
||||
free( meshes );
|
||||
free( capsules );
|
||||
free( spheres );
|
||||
|
||||
// Rotated so broad-phase lookups transform into the compound frame
|
||||
g_villageData.rotation = b3MakeQuatFromAxisAngle( (b3Vec3){ 0.0f, 1.0f, 0.0f }, -1.15f * B3_PI );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.rotation = g_villageData.rotation;
|
||||
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateBakedCompoundShape( groundId, &shapeDef, g_villageData.compound );
|
||||
}
|
||||
|
||||
{
|
||||
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.3f };
|
||||
b3Capsule capsule = { { -0.3f, 0.0f, 0.0f }, { 0.3f, 0.0f, 0.0f }, 0.2f };
|
||||
b3BoxHull box = b3MakeCubeHull( 0.3f );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
|
||||
int bodyGrid = VILLAGE_BODY_GRID;
|
||||
float spacing = 1.6f;
|
||||
float offset = 0.5f * spacing * ( 1 - bodyGrid );
|
||||
|
||||
for ( int i = 0; i < bodyGrid; ++i )
|
||||
{
|
||||
for ( int j = 0; j < bodyGrid; ++j )
|
||||
{
|
||||
int index = i * bodyGrid + j;
|
||||
|
||||
bodyDef.position = (b3Pos){ offset + spacing * i, RandomFloatRange( 2.0f, 6.0f ), offset + spacing * j };
|
||||
bodyDef.linearVelocity = b3Vec3_zero;
|
||||
bodyDef.angularVelocity = RandomVec3Uniform( -2.0f, 2.0f );
|
||||
|
||||
// Fast enough to need continuous collision against the compound
|
||||
if ( index % 20 == 0 )
|
||||
{
|
||||
bodyDef.position.y = 20.0f;
|
||||
bodyDef.linearVelocity = (b3Vec3){ 0.0f, -80.0f, 0.0f };
|
||||
}
|
||||
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
switch ( index % 3 )
|
||||
{
|
||||
case 0:
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
break;
|
||||
case 1:
|
||||
b3CreateCapsuleShape( bodyId, &shapeDef, &capsule );
|
||||
break;
|
||||
default:
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static float VillageCastCallback( b3ShapeId shapeId, b3Pos point, b3Vec3 normal, float fraction, uint64_t userMaterialId,
|
||||
int triangleIndex, int childIndex, void* context )
|
||||
{
|
||||
(void)shapeId;
|
||||
(void)userMaterialId;
|
||||
(void)triangleIndex;
|
||||
(void)childIndex;
|
||||
|
||||
// Clipping makes each report closer than the last, so the final one is the closest
|
||||
VillageCast* cast = context;
|
||||
cast->point = point;
|
||||
cast->normal = normal;
|
||||
cast->fraction = fraction;
|
||||
cast->hit = true;
|
||||
return fraction;
|
||||
}
|
||||
|
||||
// A curtain of tilted sphere casts, one per meter, sweeps across the compound once per run.
|
||||
void StepVillage( b3WorldId worldId, int stepIndex )
|
||||
{
|
||||
float halfSpan = g_villageData.halfSpan;
|
||||
float t = (float)( stepIndex % VILLAGE_STEP_COUNT ) / (float)( VILLAGE_STEP_COUNT - 1 );
|
||||
float x = -halfSpan + 2.0f * halfSpan * t;
|
||||
|
||||
b3CosSin tilt = b3ComputeCosSin( ( 20.0f / 180.0f ) * B3_PI );
|
||||
b3Vec3 localTranslation = { VILLAGE_CAST_LENGTH * tilt.sine, -VILLAGE_CAST_LENGTH * tilt.cosine, 0.0f };
|
||||
|
||||
// The curtain is laid out in the compound frame
|
||||
b3Quat q = g_villageData.rotation;
|
||||
b3Vec3 translation = b3RotateVector( q, localTranslation );
|
||||
|
||||
b3Vec3 point = b3Vec3_zero;
|
||||
b3ShapeProxy proxy = { &point, 1, VILLAGE_CAST_RADIUS };
|
||||
b3QueryFilter filter = b3DefaultQueryFilter();
|
||||
|
||||
int castCount = (int)( 2.0f * halfSpan ) + 1;
|
||||
assert( castCount <= VILLAGE_MAX_CASTS );
|
||||
|
||||
for ( int i = 0; i < castCount; ++i )
|
||||
{
|
||||
b3Vec3 localOrigin = { x, VILLAGE_CAST_HEIGHT, -halfSpan + (float)i };
|
||||
|
||||
VillageCast* cast = g_villageData.casts + i;
|
||||
*cast = (VillageCast){ 0 };
|
||||
cast->origin = b3ToPos( b3RotateVector( q, localOrigin ) );
|
||||
cast->fraction = 1.0f;
|
||||
|
||||
b3World_CastShape( worldId, cast->origin, &proxy, translation, filter, VillageCastCallback, cast );
|
||||
}
|
||||
|
||||
g_villageData.castTranslation = translation;
|
||||
g_villageData.castCount = castCount;
|
||||
}
|
||||
|
||||
VillageCurtain GetVillageCurtain( void )
|
||||
{
|
||||
return (VillageCurtain){
|
||||
.casts = g_villageData.casts,
|
||||
.castCount = g_villageData.castCount,
|
||||
.translation = g_villageData.castTranslation,
|
||||
.radius = VILLAGE_CAST_RADIUS,
|
||||
};
|
||||
}
|
||||
|
||||
void DestroyVillage( void )
|
||||
{
|
||||
b3DestroyCompound( g_villageData.compound );
|
||||
memset( &g_villageData, 0, sizeof( g_villageData ) );
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "box3d/id.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
|
|
@ -52,6 +53,32 @@ void GetSleepCapacity( b3Capacity* capacity );
|
|||
void CreateSleep( b3WorldId worldId );
|
||||
void StepSleep( b3WorldId worldId, int stepCount );
|
||||
|
||||
#define VILLAGE_STEP_COUNT 600
|
||||
|
||||
typedef struct VillageCast
|
||||
{
|
||||
b3Pos origin;
|
||||
b3Pos point;
|
||||
b3Vec3 normal;
|
||||
float fraction;
|
||||
bool hit;
|
||||
} VillageCast;
|
||||
|
||||
// The sphere casts from the latest village step, for drawing
|
||||
typedef struct VillageCurtain
|
||||
{
|
||||
const VillageCast* casts;
|
||||
int castCount;
|
||||
b3Vec3 translation;
|
||||
float radius;
|
||||
} VillageCurtain;
|
||||
|
||||
void GetVillageCapacity( b3Capacity* capacity );
|
||||
void CreateVillage( b3WorldId worldId );
|
||||
void StepVillage( b3WorldId worldId, int stepIndex );
|
||||
void DestroyVillage( void );
|
||||
VillageCurtain GetVillageCurtain( void );
|
||||
|
||||
// void CreateSmash( b3WorldId worldId );
|
||||
// void CreateTumbler( b3WorldId worldId );
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue