initial commit

This commit is contained in:
Erin Catto 2026-05-10 13:36:52 -07:00
commit 573e1a9fa3
220 changed files with 150440 additions and 0 deletions

169
include/box3d/base.h Normal file
View file

@ -0,0 +1,169 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT
#pragma once
#include <stdint.h>
// clang-format off
//
// Shared library macros
#if defined(_WIN32) && defined(box3d_EXPORTS)
// build the Windows DLL
#define BOX3D_EXPORT __declspec(dllexport)
#elif defined(_WIN32) && defined(BOX3D_DLL)
// using the Windows DLL
#define BOX3D_EXPORT __declspec(dllimport)
#elif defined(box3d_EXPORTS)
// building or using the shared library
#define BOX3D_EXPORT __attribute__((visibility("default")))
#else
// static library
#define BOX3D_EXPORT
#endif
// C++ macros
#ifdef __cplusplus
#define B3_API extern "C" BOX3D_EXPORT
#define B3_INLINE inline
#if defined( _MSC_VER )
#define B3_FORCE_INLINE __forceinline
#elif defined( __GNUC__ ) || defined( __clang__ )
#define B3_FORCE_INLINE inline __attribute__((always_inline))
#else
#define B3_FORCE_INLINE inline
#endif
#define B3_LITERAL(T) T
#define B3_ZERO_INIT {}
#else
#define B3_API BOX3D_EXPORT
#define B3_INLINE static inline
#if defined( _MSC_VER )
#define B3_FORCE_INLINE static __forceinline
#elif defined( __GNUC__ ) || defined( __clang__ )
#define B3_FORCE_INLINE static inline __attribute__((always_inline))
#else
#define B3_FORCE_INLINE static inline
#endif
/// Used for C literals like (b3Vec3){1.0f, 2.0f, 3.0f} where C++ requires b3Vec3{1.0f, 2.0f, 3.0f}
#define B3_LITERAL(T) (T)
#define B3_ZERO_INIT {0}
#endif
// clang-format on
#if defined( BOX3D_VALIDATE ) && !defined( NDEBUG )
#define B3_ENABLE_VALIDATION 1
#else
#define B3_ENABLE_VALIDATION 0
#endif
/**
* @defgroup base Base
* Base functionality
* @{
*/
/// This is used to indicate null for interfaces that work with indices instead of pointers
#define B3_NULL_INDEX -1
/// Prototype for user allocation function.
/// @param size the allocation size in bytes
/// @param alignment the required alignment, guaranteed to be a power of 2
typedef void* b3AllocFcn( int32_t size, int32_t alignment );
/// Prototype for user free function.
/// @param mem the memory previously allocated through `b3AllocFcn`
typedef void b3FreeFcn( void* mem );
/// Prototype for the user assert callback. Return 0 to skip the debugger break.
typedef int b3AssertFcn( const char* condition, const char* fileName, int lineNumber );
/// Prototype for user log callback. Used to log warnings.
typedef void b3LogFcn( const char* message );
/// This allows the user to override the allocation functions. These should be
/// set during application startup.
B3_API void b3SetAllocator( b3AllocFcn* allocFcn, b3FreeFcn* freeFcn );
/// Total bytes allocated by Box3D
B3_API int32_t b3GetByteCount( void );
/// Override the default assert callback.
/// @param assertFcn a non-null assert callback
B3_API void b3SetAssertFcn( b3AssertFcn* assertFcn );
// see https://github.com/scottt/debugbreak
#if defined( _MSC_VER )
#define B3_BREAKPOINT __debugbreak()
#elif defined( __GNUC__ ) || defined( __clang__ )
#define B3_BREAKPOINT __builtin_trap()
#else
// Unknown compiler
#include <assert.h>
#define B3_BREAKPOINT assert( 0 )
#endif
#if !defined( NDEBUG ) || defined( B3_ENABLE_ASSERT )
B3_API int b3InternalAssert( const char* condition, const char* fileName, int lineNumber );
#define B3_ASSERT( condition ) \
( (void)( ( !!( condition ) ) || ( b3InternalAssert( #condition, __FILE__, (int)( __LINE__ ) ), 0 ) ) )
#else
#define B3_ASSERT( ... ) ( (void)0 )
#endif
#if B3_ENABLE_VALIDATION
// Floating point tolerance checks should use this instead of the regular assertion
#define B3_VALIDATE( condition ) B3_ASSERT( condition )
#else
#define B3_VALIDATE( ... ) ( (void)0 )
#endif
/// Override the default logging callback.
B3_API void b3SetLogFcn( b3LogFcn* logFcn );
/// Version numbering scheme.
/// See https://semver.org/
typedef struct b3Version
{
/// Significant changes
int major;
/// Incremental changes
int minor;
/// Bug fixes
int revision;
} b3Version;
/// Get the current version of Box2D
B3_API b3Version b3GetVersion( void );
/**@}*/
//! @cond
/// Get the absolute number of system ticks. The value is platform specific.
B3_API uint64_t b3GetTicks( void );
/// Get the milliseconds passed from an initial tick value.
B3_API float b3GetMilliseconds( uint64_t ticks );
/// Get the milliseconds passed from an initial tick value.
B3_API float b3GetMillisecondsAndReset( uint64_t* ticks );
/// Yield to be used in a busy loop.
B3_API void b3Yield( void );
// Simple djb2 hash function for determinism testing
#define B3_HASH_INIT 5381
B3_API uint32_t b3Hash( uint32_t hash, const uint8_t* data, int count );
// Dump file support functions
B3_API void b3WriteBinaryFile( void* data, int size, const char* fileName );
B3_API void* b3ReadBinaryFile( const char* prefix, const char* fileName, int* memSize );
//! @endcond

1492
include/box3d/box3d.h Normal file

File diff suppressed because it is too large Load diff

1543
include/box3d/collision.h Normal file

File diff suppressed because it is too large Load diff

82
include/box3d/constants.h Normal file
View file

@ -0,0 +1,82 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT
#pragma once
#include "base.h"
/// Box2D bases all length units on meters, but you may need different units for your game.
/// You can set this value to use different units. This should be done at application startup
/// and only modified once. Default value is 1.
/// @warning This must be modified before any calls to Box2D
B3_API void b3SetLengthUnitsPerMeter( float lengthUnits );
/// Get the current length units per meter.
B3_API float b3GetLengthUnitsPerMeter( void );
// Used to detect bad values. Positions greater than about 16km will have precision
// problems, so 100km as a limit should be fine in all cases.
#define B3_HUGE ( 100000.0f * b3GetLengthUnitsPerMeter() )
/// Maximum parallel workers. Used for some fixed size arrays.
#define B3_MAX_WORKERS 32
/// Maximum number of tasks queued per world step. b2EnqueueTaskCallback will never be called
/// more than this per world step. This is related to B3_MAX_WORKERS. With 32 workers,
/// the maximum observed task count is 130. This allows an external task system to use a fixed
/// size array for Box3D task, which may help with creating stable user task pointers.
#define B3_MAX_TASKS 256
// Maximum number of colors in the constraint graph. Constraints that cannot
// find a color are added to the overflow set which are solved single-threaded.
// The compound barrel benchmark has minor overflow with 24 colors
#define B3_GRAPH_COLOR_COUNT 24
// Number of contact point buckets for counting the number of contact points per
// shape contact pair. This is just for reporting and doesn't affect simulation.
#define B3_CONTACT_MANIFOLD_COUNT_BUCKETS 8
// A small length used as a collision and constraint tolerance. Usually it is
// chosen to be numerically significant, but visually insignificant. In meters.
// @warning modifying this can have a significant impact on stability
#define B3_LINEAR_SLOP ( 0.005f * b3GetLengthUnitsPerMeter() )
#define B3_MIN_CAPSULE_LENGTH ( B3_LINEAR_SLOP )
// The distance between shapes where they are considered overlapped. This is needed
// because GJK may return small positive values for overlapped shapes in degenerate
// configurations.
#define B3_OVERLAP_SLOP ( 0.1f * B3_LINEAR_SLOP )
// Maximum number of simultaneous worlds that can be allocated
#ifndef B3_MAX_WORLDS
#define B3_MAX_WORLDS 128
#endif
// The maximum rotation of a body per time step. This limit is very large and is used
// to prevent numerical problems. You shouldn't need to adjust this.
// @warning increasing this to 0.5f * b3_pi or greater will break continuous collision.
#define B3_MAX_ROTATION ( 0.25f * B3_PI )
// @warning modifying this can have a significant impact on performance and stability
#define B3_SPECULATIVE_DISTANCE ( 4.0f * B3_LINEAR_SLOP )
/// The default contact recycling distance.
#define B3_CONTACT_RECYCLE_DISTANCE ( 10.0f * B3_LINEAR_SLOP )
// This is used to fatten AABBs in the dynamic tree. This allows proxies
// to move by a small amount without triggering a tree adjustment. This is in meters.
// @warning modifying this can have a significant impact on performance
#define B3_AABB_MARGIN ( 0.05f * b3GetLengthUnitsPerMeter() )
// The time that a body must be still before it will go to sleep. In seconds.
#define B3_TIME_TO_SLEEP 0.5f
// Body and shape name lengths
#define B3_NAME_LENGTH 64
// These generous limits allow for easy hashing. See b3ShapePairKey.
#define B3_SHAPE_POWER 22
#define B3_CHILD_POWER ( 64 - 2 * B3_SHAPE_POWER )
#define B3_MAX_SHAPES ( 1 << B3_SHAPE_POWER )
#define B3_MAX_CHILD_SHAPES ( 1 << B3_CHILD_POWER )

172
include/box3d/id.h Normal file
View file

@ -0,0 +1,172 @@
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-License-Identifier: MIT
#pragma once
#include <stdint.h>
// Note: this file should be stand-alone
/**
* @defgroup id Ids
* These ids serve as handles to internal Box2D objects.
* These should be considered opaque data and passed by value.
* Include this header if you need the id types and not the whole Box2D API.
* All ids are considered null if initialized to zero.
*
* For example in C++:
*
* @code{.cxx}
* b3WorldId worldId = {};
* @endcode
*
* Or in C:
*
* @code{.c}
* b3WorldId worldId = {0};
* @endcode
*
* These are both considered null.
*
* @warning Do not use the internals of these ids. They are subject to change. Ids should be treated as opaque objects.
* @warning You should use ids to access objects in Box2D. Do not access files within the src folder. Such usage is unsupported.
* @{
*/
/// World id references a world instance. This should be treated as an opaque handle.
typedef struct b3WorldId
{
uint16_t index1;
uint16_t generation;
} b3WorldId;
/// Body id references a body instance. This should be treated as an opaque handle.
typedef struct b3BodyId
{
int32_t index1;
uint16_t world0;
uint16_t generation;
} b3BodyId;
/// Shape id references a shape instance. This should be treated as an opaque handle.
typedef struct b3ShapeId
{
int32_t index1;
uint16_t world0;
uint16_t generation;
} b3ShapeId;
/// Joint id references a joint instance. This should be treated as an opaque handle.
typedef struct b3JointId
{
int32_t index1;
uint16_t world0;
uint16_t generation;
} b3JointId;
/// Contact id references a contact instance. This should be treated as an opaque handled.
typedef struct b3ContactId
{
int32_t index1;
uint16_t world0;
int16_t padding;
uint32_t generation;
} b3ContactId;
#ifdef __cplusplus
#define B3_NULL_ID {}
#define B3_ID_INLINE inline
#else
#define B3_NULL_ID { 0 }
#define B3_ID_INLINE static inline
#endif
/// Use these to make your identifiers null.
/// You may also use zero initialization to get null.
static const b3WorldId b3_nullWorldId = B3_NULL_ID;
static const b3BodyId b3_nullBodyId = B3_NULL_ID;
static const b3ShapeId b3_nullShapeId = B3_NULL_ID;
static const b3JointId b3_nullJointId = B3_NULL_ID;
static const b3ContactId b3_nullContactId = B3_NULL_ID;
/// Macro to determine if any id is null.
#define B3_IS_NULL( id ) ( id.index1 == 0 )
/// Macro to determine if any id is non-null.
#define B3_IS_NON_NULL( id ) ( id.index1 != 0 )
/// Compare two ids for equality. Doesn't work for b3WorldId. Don't mix types.
#define B3_ID_EQUALS( id1, id2 ) ( id1.index1 == id2.index1 && id1.world0 == id2.world0 && id1.generation == id2.generation )
/// Store a world id into a uint32_t.
B3_ID_INLINE uint32_t b3StoreWorldId( b3WorldId id )
{
return ( (uint32_t)id.index1 << 16 ) | (uint32_t)id.generation;
}
/// Load a uint32_t into a world id.
B3_ID_INLINE b3WorldId b3LoadWorldId( uint32_t x )
{
b3WorldId id = { (uint16_t)( x >> 16 ), (uint16_t)( x ) };
return id;
}
/// Store a body id into a uint64_t.
B3_ID_INLINE uint64_t b3StoreBodyId( b3BodyId id )
{
return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation;
}
/// Load a uint64_t into a body id.
B3_ID_INLINE b3BodyId b3LoadBodyId( uint64_t x )
{
b3BodyId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) };
return id;
}
/// Store a shape id into a uint64_t.
B3_ID_INLINE uint64_t b3StoreShapeId( b3ShapeId id )
{
return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation;
}
/// Load a uint64_t into a shape id.
B3_ID_INLINE b3ShapeId b3LoadShapeId( uint64_t x )
{
b3ShapeId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) };
return id;
}
/// Store a joint id into a uint64_t.
B3_ID_INLINE uint64_t b3StoreJointId( b3JointId id )
{
return ( (uint64_t)id.index1 << 32 ) | ( (uint64_t)id.world0 ) << 16 | (uint64_t)id.generation;
}
/// Load a uint64_t into a joint id.
B3_ID_INLINE b3JointId b3LoadJointId( uint64_t x )
{
b3JointId id = { (int32_t)( x >> 32 ), (uint16_t)( x >> 16 ), (uint16_t)( x ) };
return id;
}
/// Store a contact id into 16 bytes
B3_ID_INLINE void b3StoreContactId( b3ContactId id, uint32_t values[3] )
{
values[0] = (uint32_t)id.index1;
values[1] = (uint32_t)id.world0;
values[2] = (uint32_t)id.generation;
}
/// Load a two uint64_t into a contact id.
B3_ID_INLINE b3ContactId b3LoadContactId( uint32_t values[3] )
{
b3ContactId id;
id.index1 = (int32_t)values[0];
id.world0 = (uint16_t)values[1];
id.padding = 0;
id.generation = (uint32_t)values[2];
return id;
}
/**@}*/

File diff suppressed because it is too large Load diff

1523
include/box3d/types.h Normal file

File diff suppressed because it is too large Load diff