Doubles (#14)
Added a compile time option to use double precision body positions. Most math remains single precision with minimal performance loss. `BOX3D_DOUBLE_PRECISION` Improved accuracy of body inertia computation when shapes are far from the body origin. WARNING: several APIs changed but migration should be straight forward if doubles are not enabled.
This commit is contained in:
parent
a252f03b19
commit
09c65b08ea
115 changed files with 7591 additions and 5295 deletions
|
|
@ -4,6 +4,8 @@ set(BOX3D_TEST_FILES
|
|||
main.c
|
||||
test_allocator.c
|
||||
test_bitset.c
|
||||
test_body.c
|
||||
test_body_query.c
|
||||
test_collision.c
|
||||
test_compound.c
|
||||
test_container.c
|
||||
|
|
@ -12,6 +14,7 @@ set(BOX3D_TEST_FILES
|
|||
test_height_field.c
|
||||
test_hull.c
|
||||
test_id.c
|
||||
test_large_world.c
|
||||
test_macros.h
|
||||
test_math.c
|
||||
test_mover.c
|
||||
|
|
|
|||
|
|
@ -27,6 +27,8 @@
|
|||
|
||||
extern int AllocatorTest( void );
|
||||
extern int BitTest( void );
|
||||
extern int BodyTest( void );
|
||||
extern int BodyQueryTest( void );
|
||||
extern int CollisionTest( void );
|
||||
extern int CompoundTest( void );
|
||||
extern int ContainerTest( void );
|
||||
|
|
@ -35,6 +37,7 @@ extern int DistanceTest( void );
|
|||
extern int HeightFieldTest( void );
|
||||
extern int HullTest( void );
|
||||
extern int IdTest( void );
|
||||
extern int LargeWorldTest( void );
|
||||
extern int MathTest( void );
|
||||
extern int MoverTest( void );
|
||||
extern int ShapeTest( void );
|
||||
|
|
@ -86,6 +89,8 @@ int main( int argc, char** argv )
|
|||
|
||||
MAYBE_RUN_TEST( AllocatorTest );
|
||||
MAYBE_RUN_TEST( BitTest );
|
||||
MAYBE_RUN_TEST( BodyTest );
|
||||
MAYBE_RUN_TEST( BodyQueryTest );
|
||||
MAYBE_RUN_TEST( CollisionTest );
|
||||
MAYBE_RUN_TEST( CompoundTest );
|
||||
MAYBE_RUN_TEST( ContainerTest );
|
||||
|
|
@ -94,6 +99,7 @@ int main( int argc, char** argv )
|
|||
MAYBE_RUN_TEST( HeightFieldTest );
|
||||
MAYBE_RUN_TEST( HullTest );
|
||||
MAYBE_RUN_TEST( IdTest );
|
||||
MAYBE_RUN_TEST( LargeWorldTest );
|
||||
MAYBE_RUN_TEST( MathTest );
|
||||
MAYBE_RUN_TEST( MoverTest );
|
||||
MAYBE_RUN_TEST( ShapeTest );
|
||||
|
|
|
|||
125
test/test_body.c
Normal file
125
test/test_body.c
Normal file
|
|
@ -0,0 +1,125 @@
|
|||
// SPDX-FileCopyrightText: 2025 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
#include "box3d/box3d.h"
|
||||
#include "box3d/collision.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
||||
#include <float.h>
|
||||
|
||||
// b3UpdateBodyMassData shifts each shape's inertia to the body center of mass with the parallel
|
||||
// axis theorem. When shapes sit far from the body origin the shift term dwarfs the central inertia,
|
||||
// so any error in the per shape framing blows up the tensor. Spheres make a clean oracle: the
|
||||
// central inertia is isotropic and independent of placement, so the shift is the only thing tested.
|
||||
|
||||
static b3MassData SphereBodyMass( const b3Vec3* centers, int count, float radius, float density )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.density = density;
|
||||
|
||||
for ( int i = 0; i < count; ++i )
|
||||
{
|
||||
b3Sphere sphere = { centers[i], radius };
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
}
|
||||
|
||||
b3Body_ApplyMassFromShapes( bodyId );
|
||||
b3MassData massData = b3Body_GetMassData( bodyId );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return massData;
|
||||
}
|
||||
|
||||
// One sphere far from the body origin. The center of mass lands on the sphere and the inertia about
|
||||
// it must be the bare central inertia, with no trace of the offset.
|
||||
static int FarSingleSphereMass( void )
|
||||
{
|
||||
float radius = 0.5f;
|
||||
float density = 1.0f;
|
||||
b3Vec3 center = { 100.0f, -50.0f, 75.0f };
|
||||
b3MassData md = SphereBodyMass( ¢er, 1, radius, density );
|
||||
|
||||
float mass = density * ( 4.0f / 3.0f ) * B3_PI * radius * radius * radius;
|
||||
float central = 0.4f * mass * radius * radius;
|
||||
|
||||
ENSURE_SMALL( md.mass - mass, 1e-4f );
|
||||
|
||||
ENSURE_SMALL( md.center.x - center.x, 1e-3f );
|
||||
ENSURE_SMALL( md.center.y - center.y, 1e-3f );
|
||||
ENSURE_SMALL( md.center.z - center.z, 1e-3f );
|
||||
|
||||
ENSURE_SMALL( md.inertia.cx.x - central, 1e-3f );
|
||||
ENSURE_SMALL( md.inertia.cy.y - central, 1e-3f );
|
||||
ENSURE_SMALL( md.inertia.cz.z - central, 1e-3f );
|
||||
|
||||
ENSURE_SMALL( md.inertia.cy.x, 1e-3f );
|
||||
ENSURE_SMALL( md.inertia.cz.x, 1e-3f );
|
||||
ENSURE_SMALL( md.inertia.cz.y, 1e-3f );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Eight equal spheres on the corners of a cube, the whole cube parked far from the body origin.
|
||||
// The center of mass is the cube center and the products of inertia cancel by symmetry, so the
|
||||
// tensor stays diagonal no matter how far out the cube sits.
|
||||
static int FarCubeSphereMass( void )
|
||||
{
|
||||
float radius = 0.5f;
|
||||
float density = 1.0f;
|
||||
float h = 1.0f;
|
||||
b3Vec3 p = { 100.0f, 100.0f, 100.0f };
|
||||
|
||||
b3Vec3 centers[8];
|
||||
int k = 0;
|
||||
for ( int sx = -1; sx <= 1; sx += 2 )
|
||||
{
|
||||
for ( int sy = -1; sy <= 1; sy += 2 )
|
||||
{
|
||||
for ( int sz = -1; sz <= 1; sz += 2 )
|
||||
{
|
||||
centers[k++] = (b3Vec3){ p.x + sx * h, p.y + sy * h, p.z + sz * h };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b3MassData md = SphereBodyMass( centers, 8, radius, density );
|
||||
|
||||
float mass = density * ( 4.0f / 3.0f ) * B3_PI * radius * radius * radius;
|
||||
float totalMass = 8.0f * mass;
|
||||
|
||||
// Per sphere central inertia summed, plus the parallel axis term for each corner offset
|
||||
// (dy^2 + dz^2) = (h^2 + h^2) about every axis.
|
||||
float diag = 8.0f * 0.4f * mass * radius * radius + 16.0f * mass * h * h;
|
||||
|
||||
ENSURE_SMALL( md.mass - totalMass, 1e-3f );
|
||||
|
||||
ENSURE_SMALL( md.center.x - p.x, 1e-2f );
|
||||
ENSURE_SMALL( md.center.y - p.y, 1e-2f );
|
||||
ENSURE_SMALL( md.center.z - p.z, 1e-2f );
|
||||
|
||||
ENSURE_SMALL( md.inertia.cx.x - diag, 1e-2f );
|
||||
ENSURE_SMALL( md.inertia.cy.y - diag, 1e-2f );
|
||||
ENSURE_SMALL( md.inertia.cz.z - diag, 1e-2f );
|
||||
|
||||
ENSURE_SMALL( md.inertia.cy.x, 1e-2f );
|
||||
ENSURE_SMALL( md.inertia.cz.x, 1e-2f );
|
||||
ENSURE_SMALL( md.inertia.cz.y, 1e-2f );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BodyTest( void )
|
||||
{
|
||||
RUN_SUBTEST( FarSingleSphereMass );
|
||||
RUN_SUBTEST( FarCubeSphereMass );
|
||||
return 0;
|
||||
}
|
||||
484
test/test_body_query.c
Normal file
484
test/test_body_query.c
Normal file
|
|
@ -0,0 +1,484 @@
|
|||
// SPDX-FileCopyrightText: 2025 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "test_macros.h"
|
||||
|
||||
#include "box3d/box3d.h"
|
||||
#include "box3d/collision.h"
|
||||
|
||||
// The per-body query functions take an explicit world origin and a world body transform.
|
||||
// Everything is re-centered on the origin so the float collision math stays accurate far from
|
||||
// the world origin. These tests pin that framing: results come back in world space, the supplied
|
||||
// transform drives the geometry (not the body's stored pose), and a large origin offset must not
|
||||
// change a hit fraction or normal.
|
||||
//
|
||||
// CastRay and CastShape never touch the body's stored transform, so a static body at the origin
|
||||
// holding local-frame shapes is enough. No step is needed for any query.
|
||||
|
||||
static b3WorldId CreateQueryWorld( b3BodyId* bodyId )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
*bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
return worldId;
|
||||
}
|
||||
|
||||
static b3WorldTransform IdentityAt( float x, float y, float z )
|
||||
{
|
||||
return (b3WorldTransform){ .p = (b3Pos){ x, y, z }, .q = b3Quat_identity };
|
||||
}
|
||||
|
||||
// CastRay ----------------------------------------------------------------------------------
|
||||
|
||||
static int CastRayHitsSphere( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
|
||||
// Body sphere at world (5,0,0), ray straight at it along +X.
|
||||
b3WorldTransform bodyTransform = IdentityAt( 5.0f, 0.0f, 0.0f );
|
||||
b3BodyCastResult result =
|
||||
b3Body_CastRay( bodyId, (b3Pos){ 0.0f, 0.0f, 0.0f }, (b3Vec3){ 10.0f, 0.0f, 0.0f }, b3DefaultQueryFilter(), 1.0f, bodyTransform );
|
||||
|
||||
ENSURE( result.hit );
|
||||
ENSURE( b3Shape_IsValid( result.shapeId ) );
|
||||
ENSURE_SMALL( result.fraction - 0.4f, 1e-5f );
|
||||
ENSURE_SMALL( result.normal.x + 1.0f, 1e-5f );
|
||||
ENSURE_SMALL( result.normal.y, 1e-5f );
|
||||
ENSURE_SMALL( result.normal.z, 1e-5f );
|
||||
|
||||
b3Vec3 point = b3ToVec3( result.point );
|
||||
ENSURE_SMALL( point.x - 4.0f, 1e-4f );
|
||||
ENSURE_SMALL( point.y, 1e-4f );
|
||||
ENSURE_SMALL( point.z, 1e-4f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CastRayMiss( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
|
||||
// Ray runs parallel to the body, never reaching it.
|
||||
b3WorldTransform bodyTransform = IdentityAt( 5.0f, 0.0f, 0.0f );
|
||||
b3BodyCastResult result =
|
||||
b3Body_CastRay( bodyId, (b3Pos){ 0.0f, 0.0f, 0.0f }, (b3Vec3){ 0.0f, 10.0f, 0.0f }, b3DefaultQueryFilter(), 1.0f, bodyTransform );
|
||||
|
||||
ENSURE( result.hit == false );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CastRayClosestShape( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3Sphere nearSphere = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3Sphere farSphere = { { 4.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3ShapeId nearId = b3CreateSphereShape( bodyId, &shapeDef, &nearSphere );
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &farSphere );
|
||||
|
||||
// Ray crosses both spheres; the loop must shrink maxFraction to the nearer hit.
|
||||
b3WorldTransform bodyTransform = IdentityAt( 0.0f, 0.0f, 0.0f );
|
||||
b3BodyCastResult result =
|
||||
b3Body_CastRay( bodyId, (b3Pos){ -5.0f, 0.0f, 0.0f }, (b3Vec3){ 10.0f, 0.0f, 0.0f }, b3DefaultQueryFilter(), 1.0f, bodyTransform );
|
||||
|
||||
ENSURE( result.hit );
|
||||
ENSURE( result.shapeId.index1 == nearId.index1 );
|
||||
ENSURE( result.shapeId.generation == nearId.generation );
|
||||
ENSURE_SMALL( result.fraction - 0.4f, 1e-5f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CastRayRotatedBody( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
// Local center (0,2,0) rotated +90 deg about Z lands at world (-2,0,0).
|
||||
b3Sphere sphere = { { 0.0f, 2.0f, 0.0f }, 0.5f };
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
|
||||
b3WorldTransform bodyTransform = {
|
||||
.p = (b3Pos){ 0.0f, 0.0f, 0.0f },
|
||||
.q = b3MakeQuatFromAxisAngle( (b3Vec3){ 0.0f, 0.0f, 1.0f }, 0.5f * B3_PI ),
|
||||
};
|
||||
b3BodyCastResult result =
|
||||
b3Body_CastRay( bodyId, (b3Pos){ 0.0f, 0.0f, 0.0f }, (b3Vec3){ -4.0f, 0.0f, 0.0f }, b3DefaultQueryFilter(), 1.0f, bodyTransform );
|
||||
|
||||
ENSURE( result.hit );
|
||||
ENSURE_SMALL( result.fraction - 0.375f, 1e-5f );
|
||||
ENSURE_SMALL( result.normal.x - 1.0f, 1e-5f );
|
||||
|
||||
b3Vec3 point = b3ToVec3( result.point );
|
||||
ENSURE_SMALL( point.x + 1.5f, 1e-4f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CastRayFarFromOrigin( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 1.0f };
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
|
||||
// Same geometry as CastRayHitsSphere shifted far from the world origin. The relative framing
|
||||
// keeps the subtraction exact, so fraction and normal must be unchanged.
|
||||
b3Pos origin = { 1.0e6f, -2.0e6f, 5.0e5f };
|
||||
b3WorldTransform bodyTransform = { .p = b3OffsetPos( origin, (b3Vec3){ 5.0f, 0.0f, 0.0f } ), .q = b3Quat_identity };
|
||||
b3BodyCastResult result =
|
||||
b3Body_CastRay( bodyId, origin, (b3Vec3){ 10.0f, 0.0f, 0.0f }, b3DefaultQueryFilter(), 1.0f, bodyTransform );
|
||||
|
||||
ENSURE( result.hit );
|
||||
ENSURE_SMALL( result.fraction - 0.4f, 1e-5f );
|
||||
ENSURE_SMALL( result.normal.x + 1.0f, 1e-5f );
|
||||
ENSURE_SMALL( result.normal.y, 1e-5f );
|
||||
ENSURE_SMALL( result.normal.z, 1e-5f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// CastShape --------------------------------------------------------------------------------
|
||||
|
||||
static int CastShapeHitsBox( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
// Sphere proxy of radius 0.5 cast along +X into a box whose front face is at world x = 4.
|
||||
b3Vec3 point = { 0.0f, 0.0f, 0.0f };
|
||||
b3ShapeProxy proxy = { &point, 1, 0.5f };
|
||||
b3WorldTransform bodyTransform = IdentityAt( 5.0f, 0.0f, 0.0f );
|
||||
b3BodyCastResult result = b3Body_CastShape( bodyId, (b3Pos){ 0.0f, 0.0f, 0.0f }, &proxy, (b3Vec3){ 10.0f, 0.0f, 0.0f },
|
||||
b3DefaultQueryFilter(), 1.0f, false, bodyTransform );
|
||||
|
||||
// Front face at world x = 4. The fraction carries a small shape-cast skin, the contact point
|
||||
// and normal do not.
|
||||
ENSURE( result.hit );
|
||||
ENSURE( b3Shape_IsValid( result.shapeId ) );
|
||||
ENSURE_SMALL( result.fraction - 0.35f, 1e-2f );
|
||||
ENSURE_SMALL( result.normal.x + 1.0f, 1e-4f );
|
||||
|
||||
b3Vec3 hit = b3ToVec3( result.point );
|
||||
ENSURE_SMALL( hit.x - 4.0f, 1e-3f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CastShapeMiss( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
b3Vec3 point = { 0.0f, 0.0f, 0.0f };
|
||||
b3ShapeProxy proxy = { &point, 1, 0.5f };
|
||||
b3WorldTransform bodyTransform = IdentityAt( 5.0f, 0.0f, 0.0f );
|
||||
b3BodyCastResult result = b3Body_CastShape( bodyId, (b3Pos){ 0.0f, 0.0f, 0.0f }, &proxy, (b3Vec3){ 0.0f, 10.0f, 0.0f },
|
||||
b3DefaultQueryFilter(), 1.0f, false, bodyTransform );
|
||||
|
||||
ENSURE( result.hit == false );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CastShapeRotatedBody( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
// Body sphere local center (0,2,0) rotated +90 deg about Z lands at world (-2,0,0).
|
||||
b3Sphere sphere = { { 0.0f, 2.0f, 0.0f }, 1.0f };
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
|
||||
b3Vec3 point = { 0.0f, 0.0f, 0.0f };
|
||||
b3ShapeProxy proxy = { &point, 1, 0.5f };
|
||||
b3WorldTransform bodyTransform = {
|
||||
.p = (b3Pos){ 0.0f, 0.0f, 0.0f },
|
||||
.q = b3MakeQuatFromAxisAngle( (b3Vec3){ 0.0f, 0.0f, 1.0f }, 0.5f * B3_PI ),
|
||||
};
|
||||
b3BodyCastResult result = b3Body_CastShape( bodyId, (b3Pos){ 0.0f, 0.0f, 0.0f }, &proxy, (b3Vec3){ -4.0f, 0.0f, 0.0f },
|
||||
b3DefaultQueryFilter(), 1.0f, false, bodyTransform );
|
||||
|
||||
ENSURE( result.hit );
|
||||
ENSURE_SMALL( result.fraction - 0.125f, 1e-2f );
|
||||
ENSURE_SMALL( result.normal.x - 1.0f, 1e-4f );
|
||||
|
||||
b3Vec3 hit = b3ToVec3( result.point );
|
||||
ENSURE_SMALL( hit.x + 1.0f, 1e-3f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int CastShapeFarFromOrigin( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
b3Vec3 point = { 0.0f, 0.0f, 0.0f };
|
||||
b3ShapeProxy proxy = { &point, 1, 0.5f };
|
||||
b3Pos origin = { 1.0e6f, -2.0e6f, 5.0e5f };
|
||||
b3WorldTransform bodyTransform = { .p = b3OffsetPos( origin, (b3Vec3){ 5.0f, 0.0f, 0.0f } ), .q = b3Quat_identity };
|
||||
b3BodyCastResult result = b3Body_CastShape( bodyId, origin, &proxy, (b3Vec3){ 10.0f, 0.0f, 0.0f }, b3DefaultQueryFilter(),
|
||||
1.0f, false, bodyTransform );
|
||||
|
||||
ENSURE( result.hit );
|
||||
ENSURE_SMALL( result.fraction - 0.35f, 1e-2f );
|
||||
ENSURE_SMALL( result.normal.x + 1.0f, 1e-4f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// OverlapShape -----------------------------------------------------------------------------
|
||||
|
||||
static int OverlapTrue( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
// Proxy sits at the box center.
|
||||
b3Vec3 point = { 0.0f, 0.0f, 0.0f };
|
||||
b3ShapeProxy proxy = { &point, 1, 0.5f };
|
||||
b3WorldTransform bodyTransform = IdentityAt( 5.0f, 0.0f, 0.0f );
|
||||
bool overlaps = b3Body_OverlapShape( bodyId, (b3Pos){ 5.0f, 0.0f, 0.0f }, &proxy, b3DefaultQueryFilter(), bodyTransform );
|
||||
|
||||
ENSURE( overlaps );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int OverlapFalse( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
b3Vec3 point = { 0.0f, 0.0f, 0.0f };
|
||||
b3ShapeProxy proxy = { &point, 1, 0.5f };
|
||||
b3WorldTransform bodyTransform = IdentityAt( 5.0f, 0.0f, 0.0f );
|
||||
bool overlaps = b3Body_OverlapShape( bodyId, (b3Pos){ 20.0f, 0.0f, 0.0f }, &proxy, b3DefaultQueryFilter(), bodyTransform );
|
||||
|
||||
ENSURE( overlaps == false );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int OverlapRespectsBodyTransform( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
// Fixed proxy and origin: only the supplied transform decides the overlap.
|
||||
b3Vec3 point = { 0.0f, 0.0f, 0.0f };
|
||||
b3ShapeProxy proxy = { &point, 1, 0.5f };
|
||||
b3Pos origin = { 0.0f, 0.0f, 0.0f };
|
||||
|
||||
ENSURE( b3Body_OverlapShape( bodyId, origin, &proxy, b3DefaultQueryFilter(), IdentityAt( 0.0f, 0.0f, 0.0f ) ) );
|
||||
ENSURE( b3Body_OverlapShape( bodyId, origin, &proxy, b3DefaultQueryFilter(), IdentityAt( 20.0f, 0.0f, 0.0f ) ) == false );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int OverlapFilter( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
b3Vec3 point = { 0.0f, 0.0f, 0.0f };
|
||||
b3ShapeProxy proxy = { &point, 1, 0.5f };
|
||||
b3WorldTransform bodyTransform = IdentityAt( 0.0f, 0.0f, 0.0f );
|
||||
|
||||
// Geometry overlaps, but a zero mask rejects every category.
|
||||
b3QueryFilter filter = b3DefaultQueryFilter();
|
||||
filter.maskBits = 0;
|
||||
bool overlaps = b3Body_OverlapShape( bodyId, (b3Pos){ 0.0f, 0.0f, 0.0f }, &proxy, filter, bodyTransform );
|
||||
|
||||
ENSURE( overlaps == false );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
// CollideMover -----------------------------------------------------------------------------
|
||||
|
||||
static int MoverTouchesBox( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
// Mover core runs above the +Y face; its 0.2 radius reaches 0.1 into it.
|
||||
b3Capsule mover = { { -0.3f, 0.6f, 0.0f }, { 0.3f, 0.6f, 0.0f }, 0.2f };
|
||||
b3BodyPlaneResult planes[4];
|
||||
b3WorldTransform bodyTransform = IdentityAt( 0.0f, 0.0f, 0.0f );
|
||||
int count = b3Body_CollideMover( bodyId, planes, 4, (b3Pos){ 0.0f, 0.0f, 0.0f }, &mover, b3DefaultQueryFilter(), bodyTransform );
|
||||
|
||||
ENSURE( count == 1 );
|
||||
ENSURE( b3Shape_IsValid( planes[0].shapeId ) );
|
||||
ENSURE( b3IsNormalized( planes[0].result.plane.normal ) );
|
||||
ENSURE( planes[0].result.plane.normal.y > 0.99f );
|
||||
ENSURE_SMALL( planes[0].result.plane.offset - 0.1f, 1e-4f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverSeparated( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
b3Capsule mover = { { -0.3f, 5.0f, 0.0f }, { 0.3f, 5.0f, 0.0f }, 0.2f };
|
||||
b3BodyPlaneResult planes[4];
|
||||
b3WorldTransform bodyTransform = IdentityAt( 0.0f, 0.0f, 0.0f );
|
||||
int count = b3Body_CollideMover( bodyId, planes, 4, (b3Pos){ 0.0f, 0.0f, 0.0f }, &mover, b3DefaultQueryFilter(), bodyTransform );
|
||||
|
||||
ENSURE( count == 0 );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverRotatedBody( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
// Rotating +90 deg about X turns the local +Y face toward world +Z. The mover sits above the
|
||||
// world +Z face, so the returned normal must come back rotated into world space.
|
||||
b3Capsule mover = { { -0.3f, 0.0f, 0.6f }, { 0.3f, 0.0f, 0.6f }, 0.2f };
|
||||
b3BodyPlaneResult planes[4];
|
||||
b3WorldTransform bodyTransform = {
|
||||
.p = (b3Pos){ 0.0f, 0.0f, 0.0f },
|
||||
.q = b3MakeQuatFromAxisAngle( (b3Vec3){ 1.0f, 0.0f, 0.0f }, 0.5f * B3_PI ),
|
||||
};
|
||||
int count = b3Body_CollideMover( bodyId, planes, 4, (b3Pos){ 0.0f, 0.0f, 0.0f }, &mover, b3DefaultQueryFilter(), bodyTransform );
|
||||
|
||||
ENSURE( count == 1 );
|
||||
ENSURE( b3IsNormalized( planes[0].result.plane.normal ) );
|
||||
ENSURE( planes[0].result.plane.normal.z > 0.99f );
|
||||
ENSURE_SMALL( planes[0].result.plane.offset - 0.1f, 1e-4f );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int MoverCapacity( void )
|
||||
{
|
||||
b3BodyId bodyId;
|
||||
b3WorldId worldId = CreateQueryWorld( &bodyId );
|
||||
|
||||
// Two spheres each touch a mover that runs between them along X at y = 0.
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3Sphere left = { { -0.4f, 0.6f, 0.0f }, 0.5f };
|
||||
b3Sphere right = { { 0.4f, 0.6f, 0.0f }, 0.5f };
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &left );
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &right );
|
||||
|
||||
b3Capsule mover = { { -1.0f, 0.0f, 0.0f }, { 1.0f, 0.0f, 0.0f }, 0.2f };
|
||||
b3BodyPlaneResult planes[4];
|
||||
b3WorldTransform bodyTransform = IdentityAt( 0.0f, 0.0f, 0.0f );
|
||||
|
||||
// Capacity caps the result and prevents writing past the buffer.
|
||||
int capped = b3Body_CollideMover( bodyId, planes, 1, (b3Pos){ 0.0f, 0.0f, 0.0f }, &mover, b3DefaultQueryFilter(), bodyTransform );
|
||||
ENSURE( capped == 1 );
|
||||
|
||||
int full = b3Body_CollideMover( bodyId, planes, 4, (b3Pos){ 0.0f, 0.0f, 0.0f }, &mover, b3DefaultQueryFilter(), bodyTransform );
|
||||
ENSURE( full == 2 );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return 0;
|
||||
}
|
||||
|
||||
int BodyQueryTest( void )
|
||||
{
|
||||
RUN_SUBTEST( CastRayHitsSphere );
|
||||
RUN_SUBTEST( CastRayMiss );
|
||||
RUN_SUBTEST( CastRayClosestShape );
|
||||
RUN_SUBTEST( CastRayRotatedBody );
|
||||
RUN_SUBTEST( CastRayFarFromOrigin );
|
||||
|
||||
RUN_SUBTEST( CastShapeHitsBox );
|
||||
RUN_SUBTEST( CastShapeMiss );
|
||||
RUN_SUBTEST( CastShapeRotatedBody );
|
||||
RUN_SUBTEST( CastShapeFarFromOrigin );
|
||||
|
||||
RUN_SUBTEST( OverlapTrue );
|
||||
RUN_SUBTEST( OverlapFalse );
|
||||
RUN_SUBTEST( OverlapRespectsBodyTransform );
|
||||
RUN_SUBTEST( OverlapFilter );
|
||||
|
||||
RUN_SUBTEST( MoverTouchesBox );
|
||||
RUN_SUBTEST( MoverSeparated );
|
||||
RUN_SUBTEST( MoverRotatedBody );
|
||||
RUN_SUBTEST( MoverCapacity );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -2,10 +2,14 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "aabb.h"
|
||||
#include "shape.h"
|
||||
#include "test_macros.h"
|
||||
|
||||
#include "box3d/collision.h"
|
||||
#include "box3d/math_functions.h"
|
||||
|
||||
#include <float.h>
|
||||
|
||||
static int AABBTest( void )
|
||||
{
|
||||
b3AABB a;
|
||||
|
|
@ -189,10 +193,117 @@ static int TestRayAABBIntersection( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
// The narrow phase differences the two world positions in double then works in frame A, so a
|
||||
// manifold far from the origin must match the same manifold at the origin. Float loses this past
|
||||
// ~1e7 m where the ULP grows larger than the overlap, which is the whole point of large world mode.
|
||||
static int LargeWorldManifoldTest( void )
|
||||
{
|
||||
b3BoxHull boxA = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3BoxHull boxB = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
|
||||
// Centers 0.9 apart so the cubes overlap by 0.1 along x
|
||||
b3Vec3 sep = { 0.9f, 0.0f, 0.0f };
|
||||
|
||||
b3LocalManifoldPoint pointsOrigin[8];
|
||||
b3LocalManifold mOrigin = { 0 };
|
||||
mOrigin.points = pointsOrigin;
|
||||
|
||||
b3WorldTransform xfAo = b3WorldTransform_identity;
|
||||
b3WorldTransform xfBo = { b3OffsetPos( b3Pos_zero, sep ), b3Quat_identity };
|
||||
b3SATCache cacheOrigin = { 0 };
|
||||
b3CollideHulls( &mOrigin, 8, &boxA.base, &boxB.base, b3InvMulWorldTransforms( xfAo, xfBo ), &cacheOrigin );
|
||||
|
||||
// Two cube faces overlap, so the clipped manifold has four points
|
||||
ENSURE( mOrigin.pointCount == 4 );
|
||||
for ( int i = 0; i < mOrigin.pointCount; ++i )
|
||||
{
|
||||
ENSURE_SMALL( mOrigin.points[i].separation + 0.1f, 0.01f );
|
||||
}
|
||||
|
||||
#if defined( BOX3D_DOUBLE_PRECISION )
|
||||
// Same relative configuration shifted far from the origin. The relative pose differences the
|
||||
// world positions in double, so in double the frame A manifold is preserved to float precision.
|
||||
// In float it would collapse since the offset is below the ULP.
|
||||
b3Pos base = b3OffsetPos( b3Pos_zero, ( b3Vec3 ){ 1.0e7f, 1.0e7f, 1.0e7f } );
|
||||
|
||||
b3LocalManifoldPoint pointsLarge[8];
|
||||
b3LocalManifold mLarge = { 0 };
|
||||
mLarge.points = pointsLarge;
|
||||
|
||||
b3WorldTransform xfAl = { base, b3Quat_identity };
|
||||
b3WorldTransform xfBl = { b3OffsetPos( base, sep ), b3Quat_identity };
|
||||
b3SATCache cacheLarge = { 0 };
|
||||
b3CollideHulls( &mLarge, 8, &boxA.base, &boxB.base, b3InvMulWorldTransforms( xfAl, xfBl ), &cacheLarge );
|
||||
|
||||
ENSURE( mLarge.pointCount == mOrigin.pointCount );
|
||||
ENSURE_SMALL( mLarge.normal.x - mOrigin.normal.x, 1e-4f );
|
||||
ENSURE_SMALL( mLarge.normal.y - mOrigin.normal.y, 1e-4f );
|
||||
ENSURE_SMALL( mLarge.normal.z - mOrigin.normal.z, 1e-4f );
|
||||
for ( int i = 0; i < mLarge.pointCount; ++i )
|
||||
{
|
||||
ENSURE_SMALL( mLarge.points[i].separation - mOrigin.points[i].separation, 1e-4f );
|
||||
ENSURE_SMALL( mLarge.points[i].point.x - mOrigin.points[i].point.x, 1e-4f );
|
||||
ENSURE_SMALL( mLarge.points[i].point.y - mOrigin.points[i].point.y, 1e-4f );
|
||||
ENSURE_SMALL( mLarge.points[i].point.z - mOrigin.points[i].point.z, 1e-4f );
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Broad-phase AABBs are built in double and narrowed to float with directed outward rounding, so a
|
||||
// shape and its speculative margin stay inside their box far from the origin. A float build would
|
||||
// round the extent away into the ULP (~1 m at 1e7) and clip the shape out of its own box.
|
||||
static int LargeWorldAABBTest( void )
|
||||
{
|
||||
// Unit cube, so the tight extent is 0.5 each way
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 0.5f, 0.5f );
|
||||
b3Shape shape = { 0 };
|
||||
shape.type = b3_hullShape;
|
||||
shape.hull = &box.base;
|
||||
|
||||
b3AABB aabbOrigin = b3ComputeFatShapeAABB( &shape, b3WorldTransform_identity, 0.0f );
|
||||
ENSURE_SMALL( aabbOrigin.lowerBound.x + 0.5f, FLT_EPSILON );
|
||||
ENSURE_SMALL( aabbOrigin.lowerBound.y + 0.5f, FLT_EPSILON );
|
||||
ENSURE_SMALL( aabbOrigin.lowerBound.z + 0.5f, FLT_EPSILON );
|
||||
ENSURE_SMALL( aabbOrigin.upperBound.x - 0.5f, FLT_EPSILON );
|
||||
ENSURE_SMALL( aabbOrigin.upperBound.y - 0.5f, FLT_EPSILON );
|
||||
ENSURE_SMALL( aabbOrigin.upperBound.z - 0.5f, FLT_EPSILON );
|
||||
|
||||
#if defined( BOX3D_DOUBLE_PRECISION )
|
||||
double d = 1.0e7;
|
||||
b3WorldTransform xfLarge = { { d, d, d }, b3Quat_identity };
|
||||
|
||||
// Tight world AABB still contains the 0.5 m extent
|
||||
b3AABB tight = b3ComputeFatShapeAABB( &shape, xfLarge, 0.0f );
|
||||
ENSURE( (double)tight.lowerBound.x <= d - 0.5 );
|
||||
ENSURE( (double)tight.lowerBound.y <= d - 0.5 );
|
||||
ENSURE( (double)tight.lowerBound.z <= d - 0.5 );
|
||||
ENSURE( (double)tight.upperBound.x >= d + 0.5 );
|
||||
ENSURE( (double)tight.upperBound.y >= d + 0.5 );
|
||||
ENSURE( (double)tight.upperBound.z >= d + 0.5 );
|
||||
|
||||
// The fat helper folds the extra into the double step before the single outward rounding, so a
|
||||
// margin smaller than a float ULP at this range survives instead of becoming a no-op subtract.
|
||||
float extra = 0.05f;
|
||||
b3AABB fat = b3ComputeFatShapeAABB( &shape, xfLarge, extra );
|
||||
ENSURE( (double)fat.lowerBound.x <= d - 0.5 - (double)extra );
|
||||
ENSURE( (double)fat.lowerBound.y <= d - 0.5 - (double)extra );
|
||||
ENSURE( (double)fat.lowerBound.z <= d - 0.5 - (double)extra );
|
||||
ENSURE( (double)fat.upperBound.x >= d + 0.5 + (double)extra );
|
||||
ENSURE( (double)fat.upperBound.y >= d + 0.5 + (double)extra );
|
||||
ENSURE( (double)fat.upperBound.z >= d + 0.5 + (double)extra );
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int CollisionTest( void )
|
||||
{
|
||||
RUN_SUBTEST( AABBTest );
|
||||
RUN_SUBTEST( TestRayAABBIntersection );
|
||||
RUN_SUBTEST( LargeWorldManifoldTest );
|
||||
RUN_SUBTEST( LargeWorldAABBTest );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,15 @@
|
|||
#define TracyCFrameMark
|
||||
#endif
|
||||
|
||||
#define EXPECTED_SLEEP_STEP 306
|
||||
#define EXPECTED_HASH 0x596EA165
|
||||
// Double precision accumulates body positions in double, so the settle/sleep step and the
|
||||
// state hash differ from the float build. Both modes are internally deterministic.
|
||||
#if defined( BOX3D_DOUBLE_PRECISION )
|
||||
#define EXPECTED_SLEEP_STEP 301
|
||||
#define EXPECTED_HASH 0xE4844A97
|
||||
#else
|
||||
#define EXPECTED_SLEEP_STEP 269
|
||||
#define EXPECTED_HASH 0x50313037
|
||||
#endif
|
||||
|
||||
static int SingleMultithreadingTest( int workerCount )
|
||||
{
|
||||
|
|
|
|||
|
|
@ -42,8 +42,7 @@ static int ShapeDistanceTest( void )
|
|||
b3DistanceInput input;
|
||||
input.proxyA = (b3ShapeProxy){ vas, ARRAY_COUNT( vas ), 0.0f };
|
||||
input.proxyB = (b3ShapeProxy){ vbs, ARRAY_COUNT( vbs ), 0.0f };
|
||||
input.transformA = b3Transform_identity;
|
||||
input.transformB = b3Transform_identity;
|
||||
input.transform = b3Transform_identity;
|
||||
input.useRadii = false;
|
||||
|
||||
b3SimplexCache cache = { 0 };
|
||||
|
|
@ -67,8 +66,7 @@ static int ShapeCastTest( void )
|
|||
b3ShapeCastPairInput input;
|
||||
input.proxyA = (b3ShapeProxy){ vas, ARRAY_COUNT( vas ), 0.0f };
|
||||
input.proxyB = (b3ShapeProxy){ vbs, ARRAY_COUNT( vbs ), 0.0f };
|
||||
input.transformA = b3Transform_identity;
|
||||
input.transformB = b3Transform_identity;
|
||||
input.transform = b3Transform_identity;
|
||||
input.translationB = (b3Vec3){ -2.0f, 0.0f };
|
||||
input.maxFraction = 1.0f;
|
||||
input.canEncroach = false;
|
||||
|
|
|
|||
|
|
@ -330,8 +330,7 @@ static b3CastOutput BruteForceShapeCast( const b3HeightField* hf, const b3ShapeC
|
|||
b3ShapeCastPairInput pair = { 0 };
|
||||
pair.proxyA = (b3ShapeProxy){ tri.vertices, 3, 0.0f };
|
||||
pair.proxyB = input->proxy;
|
||||
pair.transformA = b3Transform_identity;
|
||||
pair.transformB = b3Transform_identity;
|
||||
pair.transform = b3Transform_identity;
|
||||
pair.translationB = input->translation;
|
||||
pair.maxFraction = bestFraction;
|
||||
pair.canEncroach = input->canEncroach;
|
||||
|
|
|
|||
302
test/test_large_world.c
Normal file
302
test/test_large_world.c
Normal file
|
|
@ -0,0 +1,302 @@
|
|||
// SPDX-FileCopyrightText: 2025 Erin Catto
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include "box3d/box3d.h"
|
||||
#include "test_macros.h"
|
||||
|
||||
#include <math.h>
|
||||
|
||||
#define STACK_COUNT 6
|
||||
#define MAX_STEPS 400
|
||||
|
||||
typedef struct StackResult
|
||||
{
|
||||
// Final body positions relative to the base, so origin and far runs are directly comparable
|
||||
b3Vec3 relativePositions[STACK_COUNT];
|
||||
|
||||
// First step on which the top body fell asleep, or -1 if it never settled
|
||||
int sleepStep;
|
||||
} StackResult;
|
||||
|
||||
// Drop a short stack of boxes onto a ground box centered at baseX. Records each body's final
|
||||
// position relative to the base and the step on which the stack settles.
|
||||
static StackResult RunStack( float baseX )
|
||||
{
|
||||
b3Pos base = (b3Pos){ baseX, 0.0f, 0.0f };
|
||||
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef groundDef = b3DefaultBodyDef();
|
||||
groundDef.position = base;
|
||||
b3BodyId groundId = b3CreateBody( worldId, &groundDef );
|
||||
b3BoxHull groundBox = b3MakeBoxHull( 10.0f, 1.0f, 10.0f );
|
||||
b3ShapeDef groundShapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( groundId, &groundShapeDef, &groundBox.base );
|
||||
|
||||
b3BodyId bodies[STACK_COUNT];
|
||||
for ( int i = 0; i < STACK_COUNT; ++i )
|
||||
{
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = b3OffsetPos( base, (b3Vec3){ 0.0f, 2.0f + 1.05f * i, 0.0f } );
|
||||
bodies[i] = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3BoxHull box = b3MakeCubeHull( 0.5f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.density = 1.0f;
|
||||
b3CreateHullShape( bodies[i], &shapeDef, &box.base );
|
||||
}
|
||||
|
||||
StackResult result = { 0 };
|
||||
result.sleepStep = -1;
|
||||
|
||||
for ( int step = 0; step < MAX_STEPS; ++step )
|
||||
{
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
if ( result.sleepStep < 0 && b3Body_IsAwake( bodies[STACK_COUNT - 1] ) == false )
|
||||
{
|
||||
result.sleepStep = step;
|
||||
}
|
||||
}
|
||||
|
||||
for ( int i = 0; i < STACK_COUNT; ++i )
|
||||
{
|
||||
b3Pos p = b3Body_GetPosition( bodies[i] );
|
||||
result.relativePositions[i] = b3SubPos( p, base );
|
||||
}
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return result;
|
||||
}
|
||||
|
||||
// A stack at the origin and a stack far from the origin should behave identically in double
|
||||
// precision: same settle frame, same relative geometry.
|
||||
static int LargeWorldStackTest( void )
|
||||
{
|
||||
StackResult origin = RunStack( 0.0f );
|
||||
ENSURE( origin.sleepStep >= 0 );
|
||||
|
||||
#if defined( BOX3D_DOUBLE_PRECISION )
|
||||
StackResult far = RunStack( 1.0e7f );
|
||||
ENSURE( far.sleepStep >= 0 );
|
||||
|
||||
// Sleeps on the same frame and lands in the same relative configuration
|
||||
ENSURE( far.sleepStep == origin.sleepStep );
|
||||
for ( int i = 0; i < STACK_COUNT; ++i )
|
||||
{
|
||||
ENSURE_SMALL( far.relativePositions[i].x - origin.relativePositions[i].x, 1.0e-3f );
|
||||
ENSURE_SMALL( far.relativePositions[i].y - origin.relativePositions[i].y, 1.0e-3f );
|
||||
ENSURE_SMALL( far.relativePositions[i].z - origin.relativePositions[i].z, 1.0e-3f );
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Fire a fast bullet at a thin wall. Returns the bullet's final x relative to the base. If
|
||||
// continuous collision works the bullet stops at the wall instead of tunneling past it.
|
||||
static float RunBullet( float baseX )
|
||||
{
|
||||
b3Pos base = (b3Pos){ baseX, 0.0f, 0.0f };
|
||||
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
// Thin static wall at x = base + 5, spanning y and z
|
||||
b3BodyDef wallDef = b3DefaultBodyDef();
|
||||
wallDef.type = b3_staticBody;
|
||||
wallDef.position = b3OffsetPos( base, (b3Vec3){ 5.0f, 0.0f, 0.0f } );
|
||||
b3BodyId wallId = b3CreateBody( worldId, &wallDef );
|
||||
b3BoxHull wallBox = b3MakeBoxHull( 0.05f, 5.0f, 5.0f );
|
||||
b3ShapeDef wallShapeDef = b3DefaultShapeDef();
|
||||
b3CreateHullShape( wallId, &wallShapeDef, &wallBox.base );
|
||||
|
||||
// Small fast bullet aimed at the wall, no gravity
|
||||
b3BodyDef bulletDef = b3DefaultBodyDef();
|
||||
bulletDef.type = b3_dynamicBody;
|
||||
bulletDef.isBullet = true;
|
||||
bulletDef.gravityScale = 0.0f;
|
||||
bulletDef.position = base;
|
||||
bulletDef.linearVelocity = (b3Vec3){ 200.0f, 0.0f, 0.0f };
|
||||
b3BodyId bulletId = b3CreateBody( worldId, &bulletDef );
|
||||
b3Sphere sphere = { { 0.0f, 0.0f, 0.0f }, 0.1f };
|
||||
b3ShapeDef bulletShapeDef = b3DefaultShapeDef();
|
||||
bulletShapeDef.density = 1.0f;
|
||||
b3CreateSphereShape( bulletId, &bulletShapeDef, &sphere );
|
||||
|
||||
for ( int step = 0; step < 30; ++step )
|
||||
{
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 4 );
|
||||
}
|
||||
|
||||
b3Vec3 relative = b3SubPos( b3Body_GetPosition( bulletId ), base );
|
||||
b3DestroyWorld( worldId );
|
||||
return relative.x;
|
||||
}
|
||||
|
||||
// The bullet must be caught by the wall, not tunnel through it. The blocking check is that this
|
||||
// still holds far from the origin where the swept query box rounds back to float with large ULP.
|
||||
static int LargeWorldBulletTest( void )
|
||||
{
|
||||
// Wall front face is at x = 5 - 0.05; the bullet radius is 0.1, so a caught bullet stays well
|
||||
// short of the wall center at x = 5.
|
||||
float originX = RunBullet( 0.0f );
|
||||
ENSURE( originX < 5.0f );
|
||||
|
||||
#if defined( BOX3D_DOUBLE_PRECISION )
|
||||
float farX = RunBullet( 1.0e7f );
|
||||
ENSURE( farX < 5.0f );
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct QueryResult
|
||||
{
|
||||
bool castHit;
|
||||
float castRelX; // shape cast hit point x relative to the base
|
||||
bool overlapHit;
|
||||
float moverFraction;
|
||||
int planeCount;
|
||||
bool rayHit;
|
||||
float rayRelX; // world ray cast hit point x relative to the base
|
||||
bool shapeRayHit;
|
||||
float shapeRayRelX; // direct shape ray cast hit point x relative to the base
|
||||
} QueryResult;
|
||||
|
||||
typedef struct CastContext
|
||||
{
|
||||
b3Pos base;
|
||||
bool hit;
|
||||
float relX;
|
||||
} CastContext;
|
||||
|
||||
static float QueryCastCallback( b3ShapeId shapeId, b3Pos point, b3Vec3 normal, float fraction, uint64_t materialId,
|
||||
int triangleIndex, int childIndex, void* context )
|
||||
{
|
||||
(void)shapeId, (void)normal, (void)materialId, (void)triangleIndex, (void)childIndex;
|
||||
CastContext* ctx = context;
|
||||
ctx->hit = true;
|
||||
ctx->relX = b3SubPos( point, ctx->base ).x;
|
||||
return fraction;
|
||||
}
|
||||
|
||||
static bool QueryOverlapCallback( b3ShapeId shapeId, void* context )
|
||||
{
|
||||
(void)shapeId;
|
||||
*(bool*)context = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool QueryPlaneCallback( b3ShapeId shapeId, const b3PlaneResult* planes, int count, void* context )
|
||||
{
|
||||
(void)shapeId, (void)planes;
|
||||
*(int*)context += count;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Run the four origin relative spatial queries against a static box centered at the base. The query
|
||||
// inputs are all relative to the base, so passing base as the origin keeps them precise far out.
|
||||
static QueryResult RunQueries( float baseX )
|
||||
{
|
||||
b3Pos base = (b3Pos){ baseX, 0.0f, 0.0f };
|
||||
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_staticBody;
|
||||
bodyDef.position = base;
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
b3BoxHull box = b3MakeBoxHull( 1.0f, 1.0f, 1.0f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3ShapeId shapeId = b3CreateHullShape( bodyId, &shapeDef, &box.base );
|
||||
|
||||
b3World_Step( worldId, 1.0f / 60.0f, 1 );
|
||||
|
||||
QueryResult result = { 0 };
|
||||
|
||||
// Sphere proxy swept from the left into the box, hitting the left face at relative x = -1
|
||||
b3Vec3 castPoint = { -5.0f, 0.0f, 0.0f };
|
||||
b3ShapeProxy castProxy = { &castPoint, 1, 0.25f };
|
||||
CastContext castCtx = { base, false, 0.0f };
|
||||
b3World_CastShape( worldId, base, &castProxy, (b3Vec3){ 10.0f, 0.0f, 0.0f }, b3DefaultQueryFilter(), QueryCastCallback,
|
||||
&castCtx );
|
||||
result.castHit = castCtx.hit;
|
||||
result.castRelX = castCtx.relX;
|
||||
|
||||
// Sphere proxy sitting at the box center
|
||||
b3Vec3 overlapPoint = { 0.0f, 0.0f, 0.0f };
|
||||
b3ShapeProxy overlapProxy = { &overlapPoint, 1, 0.5f };
|
||||
bool overlapHit = false;
|
||||
b3World_OverlapShape( worldId, base, &overlapProxy, b3DefaultQueryFilter(), QueryOverlapCallback, &overlapHit );
|
||||
result.overlapHit = overlapHit;
|
||||
|
||||
// Capsule swept from the left into the box
|
||||
b3Capsule moverCast = { { -5.0f, -0.3f, 0.0f }, { -5.0f, 0.3f, 0.0f }, 0.25f };
|
||||
result.moverFraction =
|
||||
b3World_CastMover( worldId, base, &moverCast, (b3Vec3){ 10.0f, 0.0f, 0.0f }, b3DefaultQueryFilter(), NULL, NULL );
|
||||
|
||||
// Capsule overlapping the left face, should report a contact plane
|
||||
b3Capsule moverCollide = { { -1.1f, -0.3f, 0.0f }, { -1.1f, 0.3f, 0.0f }, 0.3f };
|
||||
int planeCount = 0;
|
||||
b3World_CollideMover( worldId, base, &moverCollide, b3DefaultQueryFilter(), QueryPlaneCallback, &planeCount );
|
||||
result.planeCount = planeCount;
|
||||
|
||||
// World ray cast from the left into the box, hitting the left face at relative x = -1
|
||||
b3Pos rayOrigin = b3OffsetPos( base, (b3Vec3){ -5.0f, 0.0f, 0.0f } );
|
||||
b3RayResult ray = b3World_CastRayClosest( worldId, rayOrigin, (b3Vec3){ 10.0f, 0.0f, 0.0f }, b3DefaultQueryFilter() );
|
||||
result.rayHit = ray.hit;
|
||||
result.rayRelX = ray.hit ? b3SubPos( ray.point, base ).x : 0.0f;
|
||||
|
||||
// Direct shape ray cast against the same box
|
||||
b3WorldCastOutput shapeRay = b3Shape_RayCast( shapeId, rayOrigin, (b3Vec3){ 10.0f, 0.0f, 0.0f } );
|
||||
result.shapeRayHit = shapeRay.hit;
|
||||
result.shapeRayRelX = shapeRay.hit ? b3SubPos( shapeRay.point, base ).x : 0.0f;
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return result;
|
||||
}
|
||||
|
||||
// The origin relative queries hit at the origin and stay precise far from it: same hit geometry and
|
||||
// sweep fraction whether the box is at the origin or at 1e7.
|
||||
static int LargeWorldQueryTest( void )
|
||||
{
|
||||
QueryResult origin = RunQueries( 0.0f );
|
||||
ENSURE( origin.castHit );
|
||||
ENSURE( origin.overlapHit );
|
||||
ENSURE( origin.moverFraction < 1.0f );
|
||||
ENSURE( origin.planeCount > 0 );
|
||||
ENSURE_SMALL( origin.castRelX + 1.0f, 0.05f );
|
||||
ENSURE( origin.rayHit );
|
||||
ENSURE_SMALL( origin.rayRelX + 1.0f, 0.05f );
|
||||
ENSURE( origin.shapeRayHit );
|
||||
ENSURE_SMALL( origin.shapeRayRelX + 1.0f, 0.05f );
|
||||
|
||||
#if defined( BOX3D_DOUBLE_PRECISION )
|
||||
QueryResult far = RunQueries( 1.0e7f );
|
||||
ENSURE( far.castHit );
|
||||
ENSURE( far.overlapHit );
|
||||
ENSURE( far.moverFraction < 1.0f );
|
||||
ENSURE( far.planeCount > 0 );
|
||||
ENSURE( far.rayHit );
|
||||
ENSURE( far.shapeRayHit );
|
||||
|
||||
ENSURE_SMALL( far.castRelX - origin.castRelX, 1.0e-3f );
|
||||
ENSURE_SMALL( far.moverFraction - origin.moverFraction, 1.0e-3f );
|
||||
ENSURE( far.planeCount == origin.planeCount );
|
||||
ENSURE_SMALL( far.rayRelX - origin.rayRelX, 1.0e-3f );
|
||||
ENSURE_SMALL( far.shapeRayRelX - origin.shapeRayRelX, 1.0e-3f );
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int LargeWorldTest( void )
|
||||
{
|
||||
RUN_SUBTEST( LargeWorldStackTest );
|
||||
RUN_SUBTEST( LargeWorldBulletTest );
|
||||
RUN_SUBTEST( LargeWorldQueryTest );
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -273,5 +273,69 @@ int MathTest( void )
|
|||
ENSURE_SMALL( b3Dot( normal, perp ), 2.0f * FLT_EPSILON );
|
||||
}
|
||||
|
||||
{
|
||||
// World position boundary helpers. The query agrees with the built type sizes.
|
||||
ENSURE( b3IsDoublePrecision() == ( sizeof( b3Pos ) > sizeof( b3Vec3 ) ) );
|
||||
|
||||
// Deltas and offsets round trip exactly for representable inputs in both modes.
|
||||
b3Vec3 a = { 3.0f, -5.0f, 2.0f };
|
||||
b3Vec3 b = { 1.0f, 4.0f, -6.0f };
|
||||
b3Pos pa = b3ToPos( a );
|
||||
b3Pos pb = b3ToPos( b );
|
||||
|
||||
b3Vec3 d = b3SubPos( pa, pb );
|
||||
b3Vec3 sub = b3Sub( a, b );
|
||||
ENSURE( d.x == sub.x && d.y == sub.y && d.z == sub.z );
|
||||
|
||||
b3Vec3 back = b3SubPos( b3OffsetPos( pb, sub ), pa );
|
||||
ENSURE( back.x == 0.0f && back.y == 0.0f && back.z == 0.0f );
|
||||
|
||||
b3Vec3 r = b3ToVec3( pa );
|
||||
ENSURE( r.x == a.x && r.y == a.y && r.z == a.z );
|
||||
|
||||
ENSURE( b3IsValidPosition( pa ) );
|
||||
|
||||
// World transform relative ops match the pure float transform ops. Float mode is
|
||||
// bit identical, double mode keeps the relative result in float.
|
||||
b3Vec3 axis = b3Normalize( (b3Vec3){ 0.3f, -0.7f, 0.5f } );
|
||||
b3Transform tA = { a, b3MakeQuatFromAxisAngle( axis, 0.4f ) };
|
||||
b3Transform tB = { b, b3MakeQuatFromAxisAngle( axis, -1.1f ) };
|
||||
b3WorldTransform wA = b3MakeWorldTransform( tA );
|
||||
b3WorldTransform wB = b3MakeWorldTransform( tB );
|
||||
ENSURE( b3IsValidWorldTransform( wA ) );
|
||||
|
||||
b3Transform relRef = b3InvMulTransforms( tA, tB );
|
||||
b3Transform rel = b3InvMulWorldTransforms( wA, wB );
|
||||
ENSURE_SMALL( rel.p.x - relRef.p.x, 1.0e-5f );
|
||||
ENSURE_SMALL( rel.p.y - relRef.p.y, 1.0e-5f );
|
||||
ENSURE_SMALL( rel.p.z - relRef.p.z, 1.0e-5f );
|
||||
ENSURE_SMALL( rel.q.s - relRef.q.s, 1.0e-5f );
|
||||
|
||||
// Local point to world and back.
|
||||
b3Vec3 local = { 0.5f, -0.25f, 1.5f };
|
||||
b3Vec3 back2 = b3InvTransformWorldPoint( wA, b3TransformWorldPoint( wA, local ) );
|
||||
ENSURE_SMALL( back2.x - local.x, 1.0e-5f );
|
||||
ENSURE_SMALL( back2.y - local.y, 1.0e-5f );
|
||||
ENSURE_SMALL( back2.z - local.z, 1.0e-5f );
|
||||
|
||||
// Compose with a local transform, then strip it back off.
|
||||
b3Transform relAB = b3InvMulWorldTransforms( wA, b3MulWorldTransforms( wA, tB ) );
|
||||
ENSURE_SMALL( relAB.p.x - tB.p.x, 1.0e-5f );
|
||||
ENSURE_SMALL( relAB.p.y - tB.p.y, 1.0e-5f );
|
||||
ENSURE_SMALL( relAB.p.z - tB.p.z, 1.0e-5f );
|
||||
}
|
||||
|
||||
#if defined( BOX3D_DOUBLE_PRECISION )
|
||||
{
|
||||
// Far from the origin the double layer keeps the relative result accurate where pure
|
||||
// float would quantize. Two poses one meter apart at x = 1e8.
|
||||
b3Pos base = { 1.0e8, 0.0, 0.0 };
|
||||
b3WorldTransform wA = { base, b3Quat_identity };
|
||||
b3WorldTransform wB = { b3OffsetPos( base, (b3Vec3){ 1.0f, 0.0f, 0.0f } ), b3Quat_identity };
|
||||
b3Transform rel = b3InvMulWorldTransforms( wA, wB );
|
||||
ENSURE( rel.p.x == 1.0f && rel.p.y == 0.0f && rel.p.z == 0.0f );
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -22,11 +22,12 @@ static int ShapeMassTest( void )
|
|||
float mass = 4.0f / 3.0f * B3_PI;
|
||||
ENSURE_SMALL( md.mass - mass, FLT_EPSILON );
|
||||
ENSURE( md.center.x == 1.0f && md.center.y == 0.0f );
|
||||
|
||||
// Inertia is now about the shape center of mass, so the offset does not appear.
|
||||
float inertia = 2.0f / 5.0f * mass;
|
||||
float offsetInertia = inertia + mass;
|
||||
ENSURE_SMALL( md.inertia.cx.x - inertia, FLT_EPSILON );
|
||||
ENSURE_SMALL( md.inertia.cy.y - offsetInertia, FLT_EPSILON );
|
||||
ENSURE_SMALL( md.inertia.cz.z - offsetInertia, FLT_EPSILON );
|
||||
ENSURE_SMALL( md.inertia.cy.y - inertia, FLT_EPSILON );
|
||||
ENSURE_SMALL( md.inertia.cz.z - inertia, FLT_EPSILON );
|
||||
}
|
||||
|
||||
// Analytic box hull
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ int HelloWorld( void )
|
|||
|
||||
// Define the ground body.
|
||||
b3BodyDef groundBodyDef = b3DefaultBodyDef();
|
||||
groundBodyDef.position = (b3Vec3){ 0.0f, -10.0f, 0.0f };
|
||||
groundBodyDef.position = (b3Pos){ 0.0f, -10.0f, 0.0f };
|
||||
|
||||
// Call the body factory which allocates memory for the ground body
|
||||
// from a pool and creates the ground box shape (also from a pool).
|
||||
|
|
@ -47,7 +47,7 @@ int HelloWorld( void )
|
|||
// Define the dynamic body. We set its position and call the body factory.
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = (b3Vec3){ 0.0f, 4.0f, 0.0f };
|
||||
bodyDef.position = (b3Pos){ 0.0f, 4.0f, 0.0f };
|
||||
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
|
|
@ -72,7 +72,7 @@ int HelloWorld( void )
|
|||
float timeStep = 1.0f / 60.0f;
|
||||
int subStepCount = 4;
|
||||
|
||||
b3Vec3 position = b3Body_GetPosition( bodyId );
|
||||
b3Pos position = b3Body_GetPosition( bodyId );
|
||||
b3Quat rotation = b3Body_GetRotation( bodyId );
|
||||
|
||||
// This is our little game loop.
|
||||
|
|
@ -254,7 +254,7 @@ static bool CustomFilter( b3ShapeId shapeIdA, b3ShapeId shapeIdB, void* context
|
|||
return true;
|
||||
}
|
||||
|
||||
static bool PreSolveStatic( b3ShapeId shapeIdA, b3ShapeId shapeIdB, b3Vec3 point, b3Vec3 normal, void* context )
|
||||
static bool PreSolveStatic( b3ShapeId shapeIdA, b3ShapeId shapeIdB, b3Pos point, b3Vec3 normal, void* context )
|
||||
{
|
||||
(void)shapeIdA;
|
||||
(void)shapeIdB;
|
||||
|
|
@ -336,7 +336,7 @@ static int TestSensor( void )
|
|||
// Wall from x = 1 to x = 2
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_staticBody;
|
||||
bodyDef.position = (b3Vec3){ 1.5f, 11.0f, 0.0f };
|
||||
bodyDef.position = (b3Pos){ 1.5f, 11.0f, 0.0f };
|
||||
b3BodyId wallId = b3CreateBody( worldId, &bodyDef );
|
||||
b3BoxHull box = b3MakeBoxHull( 0.5f, 10.0f, 1.0f );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
|
|
@ -348,7 +348,7 @@ static int TestSensor( void )
|
|||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.isBullet = true;
|
||||
bodyDef.gravityScale = 0.0f;
|
||||
bodyDef.position = (b3Vec3){ 7.39814f, 4.0f, 0.0f };
|
||||
bodyDef.position = (b3Pos){ 7.39814f, 4.0f, 0.0f };
|
||||
bodyDef.linearVelocity = (b3Vec3){ -20.0f, 0.0f, 0.0f };
|
||||
b3BodyId bulletId = b3CreateBody( worldId, &bodyDef );
|
||||
shapeDef = b3DefaultShapeDef();
|
||||
|
|
@ -366,7 +366,7 @@ static int TestSensor( void )
|
|||
int subStepCount = 4;
|
||||
b3World_Step( worldId, timeStep, subStepCount );
|
||||
|
||||
b3Vec3 bulletPos = b3Body_GetPosition( bulletId );
|
||||
b3Pos bulletPos = b3Body_GetPosition( bulletId );
|
||||
// printf( "Bullet pos: %g %g\n", bulletPos.x, bulletPos.y );
|
||||
|
||||
b3SensorEvents events = b3World_GetSensorEvents( worldId );
|
||||
|
|
@ -403,7 +403,7 @@ static int TestContactEvents( void )
|
|||
// Static ground
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_staticBody;
|
||||
bodyDef.position = (b3Vec3){ 0.0f, -0.5f, 0.0f };
|
||||
bodyDef.position = (b3Pos){ 0.0f, -0.5f, 0.0f };
|
||||
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
|
||||
b3BoxHull groundBox = b3MakeBoxHull( 10.0f, 0.5f, 10.0f );
|
||||
b3ShapeDef groundShapeDef = b3DefaultShapeDef();
|
||||
|
|
@ -412,7 +412,7 @@ static int TestContactEvents( void )
|
|||
// Dynamic sphere dropped onto the ground; restitution causes it to bounce so we get end events
|
||||
bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = (b3Vec3){ 0.0f, 5.0f, 0.0f };
|
||||
bodyDef.position = (b3Pos){ 0.0f, 5.0f, 0.0f };
|
||||
b3BodyId sphereBodyId = b3CreateBody( worldId, &bodyDef );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
shapeDef.density = 1.0f;
|
||||
|
|
@ -465,7 +465,7 @@ static int TestHitEvents( void )
|
|||
// Static ground
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_staticBody;
|
||||
bodyDef.position = (b3Vec3){ 0.0f, -0.5f, 0.0f };
|
||||
bodyDef.position = (b3Pos){ 0.0f, -0.5f, 0.0f };
|
||||
b3BodyId groundId = b3CreateBody( worldId, &bodyDef );
|
||||
b3BoxHull groundBox = b3MakeBoxHull( 10.0f, 0.5f, 10.0f );
|
||||
b3ShapeDef groundShapeDef = b3DefaultShapeDef();
|
||||
|
|
@ -475,7 +475,7 @@ static int TestHitEvents( void )
|
|||
bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.gravityScale = 0.0f;
|
||||
bodyDef.position = (b3Vec3){ 0.0f, 2.0f, 0.0f };
|
||||
bodyDef.position = (b3Pos){ 0.0f, 2.0f, 0.0f };
|
||||
bodyDef.linearVelocity = (b3Vec3){ 0.0f, -30.0f, 0.0f };
|
||||
b3BodyId sphereBodyId = b3CreateBody( worldId, &bodyDef );
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
|
|
@ -576,7 +576,7 @@ static int TestCompoundHitEvents( void )
|
|||
bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.gravityScale = 0.0f;
|
||||
bodyDef.position = (b3Vec3){ spawnX, 3.0f, 0.0f };
|
||||
bodyDef.position = (b3Pos){ spawnX, 3.0f, 0.0f };
|
||||
bodyDef.linearVelocity = (b3Vec3){ 0.0f, -30.0f, 0.0f };
|
||||
b3BodyId sphereBodyId = b3CreateBody( worldId, &bodyDef );
|
||||
b3ShapeDef sphereShapeDef = b3DefaultShapeDef();
|
||||
|
|
@ -668,7 +668,7 @@ static int TestMeshDrop( void )
|
|||
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
MeshDropData data = CreateMeshDrop( worldId );
|
||||
MeshDropData data = CreateMeshDrop( worldId, b3Pos_zero );
|
||||
|
||||
float timeStep = 1.0f / 60.0f;
|
||||
|
||||
|
|
@ -935,6 +935,66 @@ static int TestHullDatabase( void )
|
|||
return 0;
|
||||
}
|
||||
|
||||
typedef struct ExplosionResult
|
||||
{
|
||||
b3Vec3 linearVelocity;
|
||||
b3Vec3 angularVelocity;
|
||||
} ExplosionResult;
|
||||
|
||||
// Explode just off the +x side of a centered sphere and capture the impulse it receives.
|
||||
// The shape, the blast point, and the witness math all run in the body local frame, so the
|
||||
// result must not depend on how far the body sits from the world origin.
|
||||
static ExplosionResult RunExplosion( b3Pos base )
|
||||
{
|
||||
b3WorldDef worldDef = b3DefaultWorldDef();
|
||||
worldDef.gravity = b3Vec3_zero;
|
||||
b3WorldId worldId = b3CreateWorld( &worldDef );
|
||||
|
||||
b3BodyDef bodyDef = b3DefaultBodyDef();
|
||||
bodyDef.type = b3_dynamicBody;
|
||||
bodyDef.position = base;
|
||||
b3BodyId bodyId = b3CreateBody( worldId, &bodyDef );
|
||||
|
||||
b3Sphere sphere = { b3Vec3_zero, 1.0f };
|
||||
b3ShapeDef shapeDef = b3DefaultShapeDef();
|
||||
b3CreateSphereShape( bodyId, &shapeDef, &sphere );
|
||||
|
||||
// Blast sits 3 units along +x, so the body is pushed back along -x
|
||||
b3ExplosionDef explosionDef = b3DefaultExplosionDef();
|
||||
explosionDef.position = b3OffsetPos( base, (b3Vec3){ 3.0f, 0.0f, 0.0f } );
|
||||
explosionDef.radius = 5.0f;
|
||||
explosionDef.falloff = 0.0f;
|
||||
explosionDef.impulsePerArea = 10.0f;
|
||||
b3World_Explode( worldId, &explosionDef );
|
||||
|
||||
ExplosionResult result;
|
||||
result.linearVelocity = b3Body_GetLinearVelocity( bodyId );
|
||||
result.angularVelocity = b3Body_GetAngularVelocity( bodyId );
|
||||
|
||||
b3DestroyWorld( worldId );
|
||||
return result;
|
||||
}
|
||||
|
||||
static int TestExplosion( void )
|
||||
{
|
||||
ExplosionResult origin = RunExplosion( b3Pos_zero );
|
||||
|
||||
// Pushed away from the blast along -x. A centered sphere has no transverse or angular component.
|
||||
ENSURE( origin.linearVelocity.x < -1.0e-4f );
|
||||
ENSURE_SMALL( origin.linearVelocity.y, 1.0e-6f );
|
||||
ENSURE_SMALL( origin.linearVelocity.z, 1.0e-6f );
|
||||
ENSURE_SMALL( b3Length( origin.angularVelocity ), 1.0e-6f );
|
||||
|
||||
// The same blast far from the origin must produce the same impulse. The world position only
|
||||
// reaches float in the relative difference, so the result holds where a naive cast would not.
|
||||
ExplosionResult far = RunExplosion( (b3Pos){ 1.0e7f, 1.0e7f, 1.0e7f } );
|
||||
ENSURE_SMALL( far.linearVelocity.x - origin.linearVelocity.x, 1.0e-5f );
|
||||
ENSURE_SMALL( far.linearVelocity.y - origin.linearVelocity.y, 1.0e-5f );
|
||||
ENSURE_SMALL( far.linearVelocity.z - origin.linearVelocity.z, 1.0e-5f );
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int WorldTest( void )
|
||||
{
|
||||
RUN_SUBTEST( HelloWorld );
|
||||
|
|
@ -943,6 +1003,7 @@ int WorldTest( void )
|
|||
RUN_SUBTEST( TestIsValid );
|
||||
RUN_SUBTEST( TestWorldRecycle );
|
||||
RUN_SUBTEST( TestWorldCoverage );
|
||||
RUN_SUBTEST( TestExplosion );
|
||||
RUN_SUBTEST( TestSensor );
|
||||
RUN_SUBTEST( TestContactEvents );
|
||||
RUN_SUBTEST( TestHitEvents );
|
||||
|
|
|
|||
Loading…
Reference in a new issue