Doxygen comments (#12)

- organized headers.
- added comments to headers.
- fixed a solver determinism bug.
- removed some un-used math functions
This commit is contained in:
Erin Catto 2026-06-10 13:21:14 -07:00 • committed by GitHub
commit d8fbeb444e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
53 changed files with 8973 additions and 1812 deletions

View file

@ -96,19 +96,22 @@ B3_API int32_t b3GetByteCount( void );
/// @param assertFcn a non-null assert callback
B3_API void b3SetAssertFcn( b3AssertFcn* assertFcn );
// see https://github.com/scottt/debugbreak
/// see https://github.com/scottt/debugbreak
#if defined( _MSC_VER )
/// Break to the debugger
#define B3_BREAKPOINT __debugbreak()
#elif defined( __GNUC__ ) || defined( __clang__ )
#define B3_BREAKPOINT __builtin_trap()
#else
// Unknown compiler
/// Unknown compiler
#include <assert.h>
#define B3_BREAKPOINT assert( 0 )
#endif
#if !defined( NDEBUG ) || defined( B3_ENABLE_ASSERT )
/// Internal assertion handler. Allows for host intervention.
B3_API int b3InternalAssert( const char* condition, const char* fileName, int lineNumber );
/// Assert that a condition is true.
#define B3_ASSERT( condition ) \
( (void)( ( !!( condition ) ) || ( b3InternalAssert( #condition, __FILE__, (int)( __LINE__ ) ), 0 ) ) )
#else
@ -116,9 +119,12 @@ B3_API int b3InternalAssert( const char* condition, const char* fileName, int li
#endif
#if B3_ENABLE_VALIDATION
// Floating point tolerance checks should use this instead of the regular assertion
/// Validation is typically only enabled in debug builds.
/// Floating point tolerance checks should use this instead of the regular assertion
#define B3_VALIDATE( condition ) B3_ASSERT( condition )
#else
/// Validation is typically only enabled in debug builds.
/// Floating point tolerance checks should use this instead of the regular assertion
#define B3_VALIDATE( ... ) ( (void)0 )
#endif
@ -139,7 +145,7 @@ typedef struct b3Version
int revision;
} b3Version;
/// Get the current version of Box2D
/// Get the current version of Box3D
B3_API b3Version b3GetVersion( void );
/**@}*/

View file

@ -16,13 +16,13 @@
* These functions allow you to create a simulation world.
*
* You can add rigid bodies and joint constraints to the world and run the simulation. You can get contact
* information to get contact points and normals as well as events. You can query to world, checking for overlaps and casting rays
* information to get contact points and normals as well as events. You can query the world, checking for overlaps and casting rays
* or shapes. There is also debugging information such as debug draw, timing information, and counters. You can find documentation
* here: https://box2d.org/
* @{
*/
/// Create a world for rigid body simulation. A world contains bodies, shapes, and constraints. You make create
/// Create a world for rigid body simulation. A world contains bodies, shapes, and constraints. You may create
/// up to 128 worlds. Each world is completely independent and may be simulated in parallel.
/// @return the world id.
B3_API b3WorldId b3CreateWorld( const b3WorldDef* def );
@ -48,7 +48,7 @@ B3_API void b3World_Step( b3WorldId worldId, float timeStep, int subStepCount );
/// Call this to draw shapes and other debug draw data
B3_API void b3World_Draw( b3WorldId worldId, b3DebugDraw* draw, uint64_t maskBits );
/// Get the worlds bounds. This is the bounding box that covers the current simulation. May have a small
/// Get the world's bounds. This is the bounding box that covers the current simulation. May have a small
/// amount of padding.
B3_API b3AABB b3World_GetBounds( b3WorldId worldId );
@ -134,7 +134,7 @@ B3_API bool b3World_IsContinuousEnabled( b3WorldId worldId );
/// @see b3WorldDef
B3_API void b3World_SetRestitutionThreshold( b3WorldId worldId, float value );
/// Get the the restitution speed threshold. Usually in meters per second.
/// Get the restitution speed threshold. Usually in meters per second.
B3_API float b3World_GetRestitutionThreshold( b3WorldId worldId );
/// Adjust the hit event threshold. This controls the collision speed needed to generate a b3ContactHitEvent.
@ -142,7 +142,7 @@ B3_API float b3World_GetRestitutionThreshold( b3WorldId worldId );
/// @see b3WorldDef::hitEventThreshold
B3_API void b3World_SetHitEventThreshold( b3WorldId worldId, float value );
/// Get the the hit event speed threshold. Usually in meters per second.
/// Get the hit event speed threshold. Usually in meters per second.
B3_API float b3World_GetHitEventThreshold( b3WorldId worldId );
/// Register the custom filter callback. This is optional.
@ -151,7 +151,7 @@ B3_API void b3World_SetCustomFilterCallback( b3WorldId worldId, b3CustomFilterFc
/// Register the pre-solve callback. This is optional.
B3_API void b3World_SetPreSolveCallback( b3WorldId worldId, b3PreSolveFcn* fcn, void* context );
/// Set the gravity vector for the entire world. Box2D has no concept of an up direction and this
/// Set the gravity vector for the entire world. Box3D has no concept of an up direction and this
/// is left as a decision for the application. Usually in m/s^2.
/// @see b3WorldDef
B3_API void b3World_SetGravity( b3WorldId worldId, b3Vec3 gravity );
@ -186,7 +186,7 @@ B3_API void b3World_SetMaximumLinearSpeed( b3WorldId worldId, float maximumLinea
B3_API float b3World_GetMaximumLinearSpeed( b3WorldId worldId );
/// Enable/disable constraint warm starting. Advanced feature for testing. Disabling
/// sleeping greatly reduces stability and provides no performance gain.
/// warm starting greatly reduces stability and provides no performance gain.
B3_API void b3World_EnableWarmStarting( b3WorldId worldId, bool flag );
/// Is constraint warm starting enabled?
@ -216,7 +216,7 @@ B3_API void b3World_SetFrictionCallback( b3WorldId worldId, b3FrictionCallback*
/// Set the restitution callback. Passing NULL resets to default.
B3_API void b3World_SetRestitutionCallback( b3WorldId worldId, b3RestitutionCallback* callback );
/// Set the worker count. Must be between in the range [1, B3_MAX_WORKERS]
/// Set the worker count. Must be in the range [1, B3_MAX_WORKERS]
B3_API void b3World_SetWorkerCount( b3WorldId worldId, int count );
/// Get the worker count.
@ -241,7 +241,7 @@ B3_API void b3World_DumpAwake( b3WorldId worldId );
/// Dump world to a text file. Meshes are saved to binary b3m files.
B3_API void b3World_Dump( b3WorldId worldId );
/** @} */
/** @} */ // world
/**
* @defgroup body Body
@ -273,7 +273,7 @@ B3_API b3BodyType b3Body_GetType( b3BodyId bodyId );
/// properties regardless of the automatic mass setting.
B3_API void b3Body_SetType( b3BodyId bodyId, b3BodyType type );
/// Set the body name. Up to 31 characters excluding 0 termination.
/// Set the body name. Up to B3_NAME_LENGTH characters including null termination.
B3_API void b3Body_SetName( b3BodyId bodyId, const char* name );
/// Get the body name.
@ -288,14 +288,14 @@ B3_API void* b3Body_GetUserData( b3BodyId bodyId );
/// Get the world position of a body. This is the location of the body origin.
B3_API b3Vec3 b3Body_GetPosition( b3BodyId bodyId );
/// Get the world rotation of a body as a cosine/sine pair (complex number)
/// Get the world rotation of a body as a quaternion
B3_API b3Quat b3Body_GetRotation( b3BodyId bodyId );
/// Get the world transform of a body.
B3_API b3Transform b3Body_GetTransform( b3BodyId bodyId );
/// Set the world transform of a body. This acts as a teleport and is fairly expensive.
/// @note Generally you should create a body with then intended transform.
/// @note Generally you should create a body with the intended transform.
/// @see b3BodyDef::position and b3BodyDef::rotation
B3_API void b3Body_SetTransform( b3BodyId bodyId, b3Vec3 position, b3Quat rotation );
@ -414,7 +414,7 @@ B3_API void b3Body_SetMassData( b3BodyId bodyId, b3MassData massData );
/// Get the mass data for a body
B3_API b3MassData b3Body_GetMassData( b3BodyId bodyId );
/// This update the mass properties to the sum of the mass properties of the shapes.
/// This updates the mass properties to the sum of the mass properties of the shapes.
/// This normally does not need to be called unless you called SetMassData to override
/// the mass and you later want to reset the mass.
/// You may also use this when automatic mass computation has been disabled.
@ -539,7 +539,7 @@ B3_API bool b3Body_OverlapShape( b3BodyId bodyId, const b3ShapeProxy* proxy, b3Q
B3_API int b3Body_CollideMover( b3BodyId bodyId, b3BodyPlaneResult* bodyPlanes, int planeCapacity, const b3Capsule* mover,
b3QueryFilter filter, b3Transform bodyTransform );
/** @} */
/** @} */ // body
/**
* @defgroup shape Shape
@ -603,10 +603,10 @@ B3_API b3BodyId b3Shape_GetBody( b3ShapeId shapeId );
/// Get the world that owns this shape
B3_API b3WorldId b3Shape_GetWorld( b3ShapeId shapeId );
/// Returns true If the shape is a sensor
/// Returns true if the shape is a sensor
B3_API bool b3Shape_IsSensor( b3ShapeId shapeId );
/// Set the shape name. Up to 31 characters excluding 0 termination.
/// Set the shape name. Up to B3_NAME_LENGTH characters including null termination.
B3_API void b3Shape_SetName( b3ShapeId shapeId, const char* name );
/// Get the shape name.
@ -619,12 +619,12 @@ B3_API void b3Shape_SetUserData( b3ShapeId shapeId, void* userData );
/// from an event or query.
B3_API void* b3Shape_GetUserData( b3ShapeId shapeId );
/// Set the mass density of a shape, usually in kg/m^2.
/// Set the mass density of a shape, usually in kg/m^3.
/// This will optionally update the mass properties on the parent body.
/// @see b3ShapeDef::density, b3Body_ApplyMassFromShapes
B3_API void b3Shape_SetDensity( b3ShapeId shapeId, float density, bool updateBodyMass );
/// Get the density of a shape, usually in kg/m^2
/// Get the density of a shape, usually in kg/m^3
B3_API float b3Shape_GetDensity( b3ShapeId shapeId );
/// Set the friction on a shape
@ -775,7 +775,7 @@ B3_API b3Vec3 b3Shape_GetClosestPoint( b3ShapeId shapeId, b3Vec3 target );
/// @param wake should this wake the body
B3_API void b3Shape_ApplyWind( b3ShapeId shapeId, b3Vec3 wind, float drag, float lift, float maxSpeed, bool wake );
/** @} */
/** @} */ // shape
/**
* @defgroup joint Joint
@ -837,7 +837,7 @@ B3_API b3Vec3 b3Joint_GetConstraintTorque( b3JointId jointId );
/// Get the current linear separation error for this joint. Does not consider admissible movement. Usually in meters.
B3_API float b3Joint_GetLinearSeparation( b3JointId jointId );
/// Get the current angular separation error for this joint. Does not consider admissible movement. Usually in meters.
/// Get the current angular separation error for this joint. Does not consider admissible movement. Usually in radians.
B3_API float b3Joint_GetAngularSeparation( b3JointId jointId );
/// Set the joint constraint tuning. Advanced feature.
@ -862,7 +862,7 @@ B3_API void b3Joint_SetTorqueThreshold( b3JointId jointId, float threshold );
B3_API float b3Joint_GetTorqueThreshold( b3JointId jointId );
/**
* @defgroup cone_joint Parallel Joint
* @defgroup parallel_joint Parallel Joint
* @brief Functions for the parallel joint.
* @{
*/
@ -883,12 +883,13 @@ B3_API float b3ParallelJoint_GetSpringHertz( b3JointId jointId );
/// Get the spring damping ratio
B3_API float b3ParallelJoint_GetSpringDampingRatio( b3JointId jointId );
/// Set the maximum spring torque, usually in newtons-meters
/// Set the maximum spring torque, usually in newton-meters
B3_API void b3ParallelJoint_SetMaxTorque( b3JointId jointId, float force );
/// Get the maximum spring torque, usually in newtons-meters
/// Get the maximum spring torque, usually in newton-meters
B3_API float b3ParallelJoint_GetMaxTorque( b3JointId jointId );
/** @} */
/** @} */ // parallel_joint
/**
* @defgroup distance_joint Distance Joint
@ -971,7 +972,8 @@ B3_API float b3DistanceJoint_GetMaxMotorForce( b3JointId jointId );
/// Get the distance joint current motor force, usually in newtons
B3_API float b3DistanceJoint_GetMotorForce( b3JointId jointId );
/** @} */
/** @} */ // distance_joint
/**
* @defgroup motor_joint Motor Joint
@ -1049,14 +1051,13 @@ B3_API void b3MotorJoint_SetMaxSpringTorque( b3JointId jointId, float maxTorque
/// Get the maximum spring torque in newtons * meters
B3_API float b3MotorJoint_GetMaxSpringTorque( b3JointId jointId );
/**@}*/
/**@}*/ // motor_joint
/**@}*/
/**
* @defgroup null_joint Null Joint
* @brief Functions for the null joint.
* @defgroup filter_joint Filter Joint
* @brief Functions for the filter joint.
*
* The null joint is used to disable collision between two bodies. As a side effect of being a joint, it also
* The filter joint is used to disable collision between two bodies. As a side effect of being a joint, it also
* keeps the two bodies in the same simulation island.
* @{
*/
@ -1065,7 +1066,7 @@ B3_API float b3MotorJoint_GetMaxSpringTorque( b3JointId jointId );
/// @see b3FilterJointDef for details
B3_API b3JointId b3CreateFilterJoint( b3WorldId worldId, const b3FilterJointDef* def );
/**@}*/
/**@}*/ // filter_joint
/**
* @defgroup prismatic_joint Prismatic Joint
@ -1148,11 +1149,11 @@ B3_API float b3PrismaticJoint_GetTranslation( b3JointId jointId );
/// Get the current joint translation speed, usually in meters per second.
B3_API float b3PrismaticJoint_GetSpeed( b3JointId jointId );
/** @} */
/**@}*/ // prismatic_joint
/**
* @defgroup revolute_joint Revolute Joint
* @brief A revolute joint allows for relative rotation in the 2D plane with no relative translation.
* @brief A revolute joint allows for relative rotation about a single axis with no relative translation.
*
* The revolute joint is probably the most common joint. It can be used for ragdolls and chains.
* Also called a *hinge* or *pin* joint.
@ -1166,7 +1167,7 @@ B3_API b3JointId b3CreateRevoluteJoint( b3WorldId worldId, const b3RevoluteJoint
/// Enable/disable the revolute joint spring
B3_API void b3RevoluteJoint_EnableSpring( b3JointId jointId, bool enableSpring );
/// It the revolute angular spring enabled?
/// Is the revolute angular spring enabled?
B3_API bool b3RevoluteJoint_IsSpringEnabled( b3JointId jointId );
/// Set the revolute joint spring stiffness in Hertz
@ -1227,7 +1228,7 @@ B3_API void b3RevoluteJoint_SetMaxMotorTorque( b3JointId jointId, float torque )
/// Get the revolute joint maximum motor torque, usually in newton-meters
B3_API float b3RevoluteJoint_GetMaxMotorTorque( b3JointId jointId );
/**@}*/
/**@}*/ // revolute_joint
/**
* @defgroup spherical_joint Spherical Joint
@ -1237,8 +1238,8 @@ B3_API float b3RevoluteJoint_GetMaxMotorTorque( b3JointId jointId );
* @{
*/
/// Create a revolute joint
/// @see b3RevoluteJointDef for details
/// Create a spherical joint
/// @see b3SphericalJointDef for details
B3_API b3JointId b3CreateSphericalJoint( b3WorldId worldId, const b3SphericalJointDef* def );
/// Enable/disable the spherical joint cone limit
@ -1277,7 +1278,7 @@ B3_API float b3SphericalJoint_GetTwistAngle( b3JointId jointId );
/// Enable/disable the spherical joint spring
B3_API void b3SphericalJoint_EnableSpring( b3JointId jointId, bool enableSpring );
/// It the spherical angular spring enabled?
/// Is the spherical angular spring enabled?
B3_API bool b3SphericalJoint_IsSpringEnabled( b3JointId jointId );
/// Set the spherical joint spring stiffness in Hertz
@ -1319,7 +1320,7 @@ B3_API void b3SphericalJoint_SetMaxMotorTorque( b3JointId jointId, float torque
/// Get the spherical joint maximum motor torque, usually in newton-meters
B3_API float b3SphericalJoint_GetMaxMotorTorque( b3JointId jointId );
/**@}*/
/**@}*/ // spherical_joint
/**
* @defgroup weld_joint Weld Joint
@ -1360,7 +1361,7 @@ B3_API void b3WeldJoint_SetAngularDampingRatio( b3JointId jointId, float damping
/// Get the weld joint angular damping ratio, non-dimensional
B3_API float b3WeldJoint_GetAngularDampingRatio( b3JointId jointId );
/** @} */
/**@}*/ // weld_joint
/**
* @defgroup wheel_joint Wheel Joint
@ -1372,62 +1373,65 @@ B3_API float b3WeldJoint_GetAngularDampingRatio( b3JointId jointId );
* @{
*/
/// Create a wheel joint
/// @see b3WheelJointDef for details
/// Create a wheel joint.
/// @see b3WheelJointDef for details.
B3_API b3JointId b3CreateWheelJoint( b3WorldId worldId, const b3WheelJointDef* def );
/// Enable/disable the wheel joint spring
/// Enable/disable the wheel joint spring.
B3_API void b3WheelJoint_EnableSuspension( b3JointId jointId, bool flag );
/// Is the wheel joint spring enabled?
B3_API bool b3WheelJoint_IsSuspensionEnabled( b3JointId jointId );
/// Set the wheel joint stiffness in Hertz
/// Set the wheel joint stiffness in Hertz.
B3_API void b3WheelJoint_SetSuspensionHertz( b3JointId jointId, float hertz );
/// Get the wheel joint stiffness in Hertz
/// Get the wheel joint stiffness in Hertz.
B3_API float b3WheelJoint_GetSuspensionHertz( b3JointId jointId );
/// Set the wheel joint damping ratio, non-dimensional
/// Set the wheel joint damping ratio, non-dimensional.
B3_API void b3WheelJoint_SetSuspensionDampingRatio( b3JointId jointId, float dampingRatio );
/// Get the wheel joint damping ratio, non-dimensional
/// Get the wheel joint damping ratio, non-dimensional.
B3_API float b3WheelJoint_GetSuspensionDampingRatio( b3JointId jointId );
/// Enable/disable the wheel joint limit
/// Enable/disable the wheel joint limit.
B3_API void b3WheelJoint_EnableSuspensionLimit( b3JointId jointId, bool flag );
/// Is the wheel joint limit enabled?
B3_API bool b3WheelJoint_IsSuspensionLimitEnabled( b3JointId jointId );
/// Get the wheel joint lower limit
/// Get the wheel joint lower limit.
B3_API float b3WheelJoint_GetLowerSuspensionLimit( b3JointId jointId );
/// Get the wheel joint upper limit
/// Get the wheel joint upper limit.
B3_API float b3WheelJoint_GetUpperSuspensionLimit( b3JointId jointId );
/// Set the wheel joint limits
/// Set the wheel joint limits.
B3_API void b3WheelJoint_SetSuspensionLimits( b3JointId jointId, float lower, float upper );
/// Enable/disable the wheel joint motor
/// Enable/disable the wheel joint motor.
B3_API void b3WheelJoint_EnableSpinMotor( b3JointId jointId, bool flag );
/// Is the wheel joint motor enabled?
B3_API bool b3WheelJoint_IsSpinMotorEnabled( b3JointId jointId );
/// Set the wheel joint motor speed in radians per second
/// Set the wheel joint motor speed in radians per second.
B3_API void b3WheelJoint_SetSpinMotorSpeed( b3JointId jointId, float speed );
/// Get the wheel joint motor speed in radians per second
/// Get the wheel joint motor speed in radians per second.
B3_API float b3WheelJoint_GetSpinMotorSpeed( b3JointId jointId );
/// Set the wheel joint maximum motor torque, usually in newton-meters
/// Set the wheel joint maximum motor torque, usually in newton-meters.
B3_API void b3WheelJoint_SetMaxSpinTorque( b3JointId jointId, float torque );
/// Get the wheel joint maximum motor torque, usually in newton-meters
/// Get the wheel joint maximum motor torque, usually in newton-meters.
B3_API float b3WheelJoint_GetMaxSpinTorque( b3JointId jointId );
/// Get the wheel joint current motor torque, usually in newton-meters
/// Get the current spin speed in radians per second.
B3_API float b3WheelJoint_GetSpinSpeed( b3JointId jointId );
/// Get the wheel joint current motor torque, usually in newton-meters.
B3_API float b3WheelJoint_GetSpinTorque( b3JointId jointId );
/// Enable/disable wheel steering. Steering allows the wheel to rotate about the suspension axis.
@ -1436,16 +1440,16 @@ B3_API void b3WheelJoint_EnableSteering( b3JointId jointId, bool flag );
/// Can the wheel steer?
B3_API bool b3WheelJoint_IsSteeringEnabled( b3JointId jointId );
/// Set the wheel joint steering stiffness in Hertz
/// Set the wheel joint steering stiffness in Hertz.
B3_API void b3WheelJoint_SetSteeringHertz( b3JointId jointId, float hertz );
/// Get the wheel joint steering stiffness in Hertz
/// Get the wheel joint steering stiffness in Hertz.
B3_API float b3WheelJoint_GetSteeringHertz( b3JointId jointId );
/// Set the wheel joint steering damping ratio, non-dimensional
/// Set the wheel joint steering damping ratio, non-dimensional.
B3_API void b3WheelJoint_SetSteeringDampingRatio( b3JointId jointId, float dampingRatio );
/// Get the wheel joint steering damping ratio, non-dimensional
/// Get the wheel joint steering damping ratio, non-dimensional.
B3_API float b3WheelJoint_GetSteeringDampingRatio( b3JointId jointId );
/// Set the wheel joint maximum steering torque in N*m.
@ -1454,42 +1458,36 @@ B3_API void b3WheelJoint_SetMaxSteeringTorque( b3JointId jointId, float torque )
/// Get the wheel joint maximum steering torque in N*m.
B3_API float b3WheelJoint_GetMaxSteeringTorque( b3JointId jointId );
/// Enable/disable the wheel joint steering limit
/// Enable/disable the wheel joint steering limit.
B3_API void b3WheelJoint_EnableSteeringLimit( b3JointId jointId, bool flag );
/// Is the wheel joint steering limit enabled?
B3_API bool b3WheelJoint_IsSteeringLimitEnabled( b3JointId jointId );
/// Get the wheel joint lower steering limit in radians
/// Get the wheel joint lower steering limit in radians.
B3_API float b3WheelJoint_GetLowerSteeringLimit( b3JointId jointId );
/// Get the wheel joint upper steering limit in radians
/// Get the wheel joint upper steering limit in radians.
B3_API float b3WheelJoint_GetUpperSteeringLimit( b3JointId jointId );
/// Set the wheel joint steering limits in radians
/// Set the wheel joint steering limits in radians.
B3_API void b3WheelJoint_SetSteeringLimits( b3JointId jointId, float lowerRadians, float upperRadians );
/// Set the wheel joint target steering angle in radians
/// Set the wheel joint target steering angle in radians.
B3_API void b3WheelJoint_SetTargetSteeringAngle( b3JointId jointId, float radians );
/// Get the wheel joint target steering angle in radians
/// Get the wheel joint target steering angle in radians.
B3_API float b3WheelJoint_GetTargetSteeringAngle( b3JointId jointId );
/// Get the current spin speed in radians per second
B3_API float b3WheelJoint_GetSpinSpeed( b3JointId jointId );
/// Get the current spin torque in N*m
B3_API float b3WheelJoint_GetSpinTorque( b3JointId jointId );
/// Get the current steering angle in radians
/// Get the current steering angle in radians.
B3_API float b3WheelJoint_GetSteeringAngle( b3JointId jointId );
/// Get the current steering torque in N*m
/// Get the current steering torque in N*m.
B3_API float b3WheelJoint_GetSteeringTorque( b3JointId jointId );
/**@}*/
/**@}*/ // wheel_joint
/**@}*/
/**@}*/ // joint
/**
* @defgroup contact Contact
@ -1503,4 +1501,4 @@ B3_API bool b3Contact_IsValid( b3ContactId id );
/// Get the manifolds for a contact. The manifold may have no points if the contact is not touching.
B3_API b3ContactData b3Contact_GetData( b3ContactId contactId );
/**@}*/
/**@}*/ // contact

File diff suppressed because it is too large Load diff

View file

@ -5,10 +5,10 @@
#include "base.h"
/// Box2D bases all length units on meters, but you may need different units for your game.
/// Box3D 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
/// @warning This must be modified before any calls to Box3D
B3_API void b3SetLengthUnitsPerMeter( float lengthUnits );
/// Get the current length units per meter.
@ -21,7 +21,7 @@ B3_API float b3GetLengthUnitsPerMeter( void );
/// 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
/// Maximum number of tasks queued per world step. b3EnqueueTaskCallback 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.
@ -55,7 +55,7 @@ B3_API float b3GetLengthUnitsPerMeter( void );
/// 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.
/// @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
@ -91,6 +91,12 @@ B3_API float b3GetLengthUnitsPerMeter( void );
#define B3_NAME_LENGTH 18
#endif
/// The maximum number of contact points between two touching shapes.
#define B3_MAX_MANIFOLD_POINTS 4
/// The maximum number points to use for shape cast proxies (swept point cloud).
#define B3_MAX_SHAPE_CAST_POINTS 64
/// These generous limits allow for easy hashing. See b3ShapePairKey.
#define B3_SHAPE_POWER 22
#define B3_CHILD_POWER ( 64 - 2 * B3_SHAPE_POWER )

View file

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 Erin Catto
// SPDX-FileCopyrightText: 2026 Erin Catto
// SPDX-License-Identifier: MIT
#pragma once
@ -9,9 +9,9 @@
/**
* @defgroup id Ids
* These ids serve as handles to internal Box2D objects.
* These ids serve as handles to internal Box3D 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.
* Include this header if you need the id types and not the whole Box3D API.
* All ids are considered null if initialized to zero.
*
* For example in C++:
@ -29,7 +29,7 @@
* 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.
* @warning You should use ids to access objects in Box3D. Do not access files within the src folder. Such usage is unsupported.
* @{
*/
@ -64,7 +64,7 @@ typedef struct b3JointId
uint16_t generation;
} b3JointId;
/// Contact id references a contact instance. This should be treated as an opaque handled.
/// Contact id references a contact instance. This should be treated as an opaque handle.
typedef struct b3ContactId
{
int32_t index1;
@ -73,13 +73,19 @@ typedef struct b3ContactId
uint32_t generation;
} b3ContactId;
// clang-format off
#ifdef __cplusplus
/// A null id. Works for any id type.
#define B3_NULL_ID {}
#define B3_ID_INLINE inline
#else
/// A null id. Works for any id type.
#define B3_NULL_ID { 0 }
/// This macro bridges C and C++ inline functions. C++ has the one definition rule that C lacks.
#define B3_ID_INLINE static inline
#endif
// clang-format on
/// Use these to make your identifiers null.
/// You may also use zero initialization to get null.
@ -150,7 +156,7 @@ B3_ID_INLINE b3JointId b3LoadJointId( uint64_t x )
return id;
}
/// Store a contact id into 16 bytes
/// Store a contact id into three uint32 values
B3_ID_INLINE void b3StoreContactId( b3ContactId id, uint32_t values[3] )
{
values[0] = (uint32_t)id.index1;
@ -158,7 +164,7 @@ B3_ID_INLINE void b3StoreContactId( b3ContactId id, uint32_t values[3] )
values[2] = (uint32_t)id.generation;
}
/// Load a two uint64_t into a contact id.
/// Load a contact id from three uint32 values.
B3_ID_INLINE b3ContactId b3LoadContactId( uint32_t values[3] )
{
b3ContactId id;

View file

@ -17,12 +17,26 @@
* @{
*/
/// https://en.wikipedia.org/wiki/Pi
#define B3_PI 3.14159265359f
/// Convenience macro to convert from degrees to radians.
#define B3_DEG_TO_RAD 0.01745329251f
/// Convenience macro to convert from radians to degrees.
#define B3_RAD_TO_DEG 57.2957795131f
/// Minimum scale used for scaling collision meshes, etc.
#define B3_MIN_SCALE 0.01f
/// A 2D vector.
typedef struct b3Vec2
{
float x;
float y;
} b3Vec2;
/// A 3D vector.
typedef struct b3Vec3
{
float x;
@ -30,8 +44,8 @@ typedef struct b3Vec3
float z;
} b3Vec3;
/// Cosine and sine pair
/// This uses a custom implementation designed for cross-platform determinism
/// Cosine and sine pair.
/// This uses a custom implementation designed for cross-platform determinism.
typedef struct b3CosSin
{
/// cosine and sine
@ -39,29 +53,34 @@ typedef struct b3CosSin
float sine;
} b3CosSin;
/// A quaternion.
typedef struct b3Quat
{
b3Vec3 v;
float s;
} b3Quat;
/// A rigid transform.
typedef struct b3Transform
{
b3Vec3 p;
b3Quat q;
} b3Transform;
/// A 3x3 matrix.
typedef struct b3Matrix3
{
b3Vec3 cx, cy, cz;
} b3Matrix3;
/// Axis aligned bounding box.
typedef struct b3AABB
{
b3Vec3 lowerBound;
b3Vec3 upperBound;
} b3AABB;
/// A plane.
/// separation = dot(normal, point) - offset
typedef struct b3Plane
{
@ -69,87 +88,70 @@ typedef struct b3Plane
float offset;
} b3Plane;
/**@}*/
/**
* @addtogroup math
* @{
*/
#define B3_PI 3.141592654f
#define B3_DEG_TO_RAD 0.01745329251f
#define B3_RAD_TO_DEG 57.2957795131f
/// Minimum scale used for scaling collision meshes, etc.
#define B3_MIN_SCALE 0.01f
static const b3Vec3 b3Vec3_zero = { 0.0f, 0.0f, 0.0f };
static const b3Vec3 b3Vec3_one = { 1.0f, 1.0f, 1.0f };
static const b3Vec3 b3Vec3_axisX = { 1.0f, 0.0f, 0.0f };
static const b3Vec3 b3Vec3_axisY = { 0.0f, 1.0f, 0.0f };
static const b3Vec3 b3Vec3_axisZ = { 0.0f, 0.0f, 1.0f };
static const b3Quat b3Quat_identity = { { 0.0f, 0.0f, 0.0f }, 1.0f };
static const b3Transform b3Transform_identity = { { 0.0f, 0.0f, 0.0f }, { { 0.0f, 0.0f, 0.0f }, 1.0f } };
static const b3Matrix3 b3Mat3_zero = {
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f },
};
static const b3Matrix3 b3Mat3_identity = {
{ 1.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f },
};
/// @return the minimum of two integers
/// @return the minimum of two integers.
B3_INLINE int b3MinInt( int a, int b )
{
return a < b ? a : b;
}
/// @return the maximum of two integers
/// @return the maximum of two integers.
B3_INLINE int b3MaxInt( int a, int b )
{
return a > b ? a : b;
}
/// @return an integer clamped between a lower and upper bound
/// @return an integer clamped between a lower and upper bound.
B3_INLINE int b3ClampInt( int a, int lower, int upper )
{
return a < lower ? lower : ( upper < a ? upper : a );
}
/// Is this float valid (finite and not NaN)
/// @return is this float valid (finite and not NaN).
B3_API bool b3IsValidFloat( float a );
/// @return the absolute value of a float
/// @return the absolute value of a float.
B3_INLINE float b3AbsFloat( float a )
{
return a < 0 ? -a : a;
}
/// @return the minimum of two floats
/// @return the minimum of two floats.
B3_INLINE float b3MinFloat( float a, float b )
{
return a < b ? a : b;
}
/// @return the maximum of two floats
/// @return the maximum of two floats.
B3_INLINE float b3MaxFloat( float a, float b )
{
return a > b ? a : b;
}
/// @return a float clamped between a lower and upper bound
/// @return a float clamped between a lower and upper bound.
B3_INLINE float b3ClampFloat( float a, float lower, float upper )
{
return a < lower ? lower : ( upper < a ? upper : a );
}
/// Interpolate a scalar
/// Interpolate a scalar.
B3_INLINE float b3LerpFloat( float a, float b, float alpha )
{
return ( 1.0f - alpha ) * a + alpha * b;
@ -158,86 +160,91 @@ B3_INLINE float b3LerpFloat( float a, float b, float alpha )
/// Compute an approximate arctangent in the range [-pi, pi]
/// This is hand coded for cross-platform determinism. The atan2f
/// function in the standard library is not cross-platform deterministic.
/// Accurate to around 0.0023 degrees
/// Accurate to around 0.0023 degrees.
B3_API float b3Atan2( float y, float x );
/// Compute the cosine and sine of an angle in radians. Implemented
/// for cross-platform determinism.
B3_API b3CosSin b3ComputeCosSin( float radians );
/// @deprecated
B3_INLINE float b3Sin( float radians )
{
b3CosSin cs = b3ComputeCosSin( radians );
return cs.sine;
}
/// @deprecated
B3_INLINE float b3Cos( float radians )
{
b3CosSin cs = b3ComputeCosSin( radians );
return cs.cosine;
}
/// Convert any angle into the range [-pi, pi]
/// Convert any angle into the range [-pi, pi].
B3_INLINE float b3UnwindAngle( float radians )
{
// Assuming this is deterministic
return remainderf( radians, 2.0f * B3_PI );
}
/// Vector addition
/// Vector addition.
B3_INLINE b3Vec3 b3Add( b3Vec3 a, b3Vec3 b )
{
return B3_LITERAL( b3Vec3 ){ a.x + b.x, a.y + b.y, a.z + b.z };
}
/// Vector subtraction
/// Vector subtraction.
B3_INLINE b3Vec3 b3Sub( b3Vec3 a, b3Vec3 b )
{
return B3_LITERAL( b3Vec3 ){ a.x - b.x, a.y - b.y, a.z - b.z };
}
/// Vector component-wise multiplication
/// Vector component-wise multiplication.
B3_INLINE b3Vec3 b3Mul( b3Vec3 a, b3Vec3 b )
{
return B3_LITERAL( b3Vec3 ){ a.x * b.x, a.y * b.y, a.z * b.z };
}
/// Vector negation
/// Vector negation.
B3_INLINE b3Vec3 b3Neg( b3Vec3 a )
{
return B3_LITERAL( b3Vec3 ){ -a.x, -a.y, -a.z };
}
/// Vector dot product
/// Vector dot product.
B3_INLINE float b3Dot( b3Vec3 a, b3Vec3 b )
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
/// Vector length
/// Vector length.
B3_INLINE float b3Length( b3Vec3 v )
{
return sqrtf( b3Dot( v, v ) );
}
/// Vector length squared
/// Vector length squared.
B3_INLINE float b3LengthSquared( b3Vec3 a )
{
return a.x * a.x + a.y * a.y + a.z * a.z;
}
B3_INLINE float b3Distance( b3Vec3 v1, b3Vec3 v2 )
/// Distance between two points.
B3_INLINE float b3Distance( b3Vec3 a, b3Vec3 b )
{
b3Vec3 dv = { v2.x - v1.x, v2.y - v1.y, v2.z - v1.z };
b3Vec3 dv = { b.x - a.x, b.y - a.y, b.z - a.z };
return b3Length( dv );
}
B3_INLINE float b3DistanceSquared( b3Vec3 v1, b3Vec3 v2 )
/// Squared distance between two points.
B3_INLINE float b3DistanceSquared( b3Vec3 a, b3Vec3 b )
{
b3Vec3 dv = { v2.x - v1.x, v2.y - v1.y, v2.z - v1.z };
b3Vec3 dv = { b.x - a.x, b.y - a.y, b.z - a.z };
return dv.x * dv.x + dv.y * dv.y + dv.z * dv.z;
}
/// Normalize a vector. Returns a zero vector if the input vector is very small.
B3_INLINE b3Vec3 b3Normalize( b3Vec3 a )
{
float lengthSquared = a.x * a.x + a.y * a.y + a.z * a.z;
@ -252,37 +259,41 @@ B3_INLINE b3Vec3 b3Normalize( b3Vec3 a )
return B3_LITERAL( b3Vec3 ){ 0.0f, 0.0f, 0.0f };
}
B3_INLINE b3Vec3 b3GetLengthAndNormalize( float* length, b3Vec3 v )
/// Normalize a vector and return the length. Returns a zero vector
/// if the input is very small.
B3_INLINE b3Vec3 b3GetLengthAndNormalize( float* length, b3Vec3 a )
{
*length = b3Length( v );
*length = b3Length( a );
if ( *length < FLT_EPSILON )
{
return b3Vec3_zero;
}
float invLength = 1.0f / *length;
b3Vec3 n = { invLength * v.x, invLength * v.y, invLength * v.z };
b3Vec3 n = { invLength * a.x, invLength * a.y, invLength * a.z };
return n;
}
B3_INLINE b3Vec3 b3Perp( b3Vec3 v )
/// Get a unit vector that is perpendicular to the supplied vector.
B3_INLINE b3Vec3 b3Perp( b3Vec3 a )
{
// Suppose vector a has all equal components and is a unit vector: a = (s, s, s)
// Then 3*s*s = 1, s = sqrt(1/3) = 0.57735. This means that at least one component
// of a unit vector must be greater or equal to 0.57735.
b3Vec3 p;
if ( v.x < -0.5f || 0.5f < v.x )
if ( a.x < -0.5f || 0.5f < a.x )
{
p = B3_LITERAL( b3Vec3 ){ v.y, -v.x, 0.0f };
p = B3_LITERAL( b3Vec3 ){ a.y, -a.x, 0.0f };
}
else
{
p = B3_LITERAL( b3Vec3 ){ 0.0f, v.z, -v.y };
p = B3_LITERAL( b3Vec3 ){ 0.0f, a.z, -a.y };
}
return b3Normalize( p );
}
/// Is a vector normalized? In other words, does it have unit length?
B3_INLINE bool b3IsNormalized( b3Vec3 a )
{
float aa = b3Dot( a, a );
@ -301,12 +312,13 @@ B3_INLINE b3Vec3 b3MulSub( b3Vec3 a, float s, b3Vec3 b )
return B3_LITERAL( b3Vec3 ){ a.x - s * b.x, a.y - s * b.y, a.z - s * b.z };
}
/// a + s * b
/// s * a
B3_INLINE b3Vec3 b3MulSV( float s, b3Vec3 a )
{
return B3_LITERAL( b3Vec3 ){ s * a.x, s * a.y, s * a.z };
}
/// https://en.wikipedia.org/wiki/Cross_product
B3_INLINE b3Vec3 b3Cross( b3Vec3 a, b3Vec3 b )
{
b3Vec3 c;
@ -316,6 +328,7 @@ B3_INLINE b3Vec3 b3Cross( b3Vec3 a, b3Vec3 b )
return c;
}
/// Linearly interpolate between two vectors.
B3_INLINE b3Vec3 b3Lerp( b3Vec3 a, b3Vec3 b, float alpha )
{
B3_ASSERT( 0.0f <= alpha && alpha <= 1.0f );
@ -328,6 +341,7 @@ B3_INLINE b3Vec3 b3Lerp( b3Vec3 a, b3Vec3 b, float alpha )
return c;
}
/// Blend two vectors: s * a + t * b
B3_INLINE b3Vec3 b3Blend2( float s, b3Vec3 a, float t, b3Vec3 b )
{
b3Vec3 d = {
@ -338,16 +352,7 @@ B3_INLINE b3Vec3 b3Blend2( float s, b3Vec3 a, float t, b3Vec3 b )
return d;
}
B3_INLINE b3Vec3 b3Blend3( float s, b3Vec3 a, float t, b3Vec3 b, float u, b3Vec3 c )
{
b3Vec3 d = {
s * a.x + t * b.x + u * c.x,
s * a.y + t * b.y + u * c.y,
s * a.z + t * b.z + u * c.z,
};
return d;
}
/// Component-wise absolute value.
B3_INLINE b3Vec3 b3Abs( b3Vec3 a )
{
return B3_LITERAL( b3Vec3 ){
@ -357,7 +362,7 @@ B3_INLINE b3Vec3 b3Abs( b3Vec3 a )
};
}
// -1 or 1 (1 if zero)
/// Component-wise -1 or 1 (1 if zero).
B3_INLINE b3Vec3 b3Sign( b3Vec3 a )
{
return B3_LITERAL( b3Vec3 ){
@ -367,6 +372,7 @@ B3_INLINE b3Vec3 b3Sign( b3Vec3 a )
};
}
/// Component-wise minimum value.
B3_INLINE b3Vec3 b3Min( b3Vec3 a, b3Vec3 b )
{
return B3_LITERAL( b3Vec3 ){
@ -376,6 +382,7 @@ B3_INLINE b3Vec3 b3Min( b3Vec3 a, b3Vec3 b )
};
}
/// Component-wise maximum value.
B3_INLINE b3Vec3 b3Max( b3Vec3 a, b3Vec3 b )
{
return B3_LITERAL( b3Vec3 ){
@ -385,6 +392,7 @@ B3_INLINE b3Vec3 b3Max( b3Vec3 a, b3Vec3 b )
};
}
/// Component-wise clamped value.
B3_INLINE b3Vec3 b3Clamp( b3Vec3 a, b3Vec3 lower, b3Vec3 upper )
{
b3Vec3 b;
@ -394,8 +402,8 @@ B3_INLINE b3Vec3 b3Clamp( b3Vec3 a, b3Vec3 lower, b3Vec3 upper )
return b;
}
// Create a safe scaling value for scaling collision. This allows
// negative scale, but keeps scale sufficiently far from zero.
/// Create a safe scaling value for scaling collision. This allows
/// negative scale, but keeps scale sufficiently far from zero.
B3_INLINE b3Vec3 b3SafeScale( b3Vec3 a )
{
b3Vec3 absScale = b3Abs( a );
@ -404,13 +412,14 @@ B3_INLINE b3Vec3 b3SafeScale( b3Vec3 a )
return safeScale;
}
/// Does the supplied quaternion have unit length?
B3_INLINE bool b3IsNormalizedQuat( b3Quat q )
{
float qq = q.v.x * q.v.x + q.v.y * q.v.y + q.v.z * q.v.z + q.s * q.s;
return 1.0f - 20.0f * FLT_EPSILON < qq && qq < 1.0f + 20.0f * FLT_EPSILON;
}
/// Rotate a vector
/// Rotate a vector.
B3_INLINE b3Vec3 b3RotateVector( b3Quat q, b3Vec3 v )
{
// v + 2 * cross(q.v, cross(q.v, v) + q.s * v)
@ -421,7 +430,7 @@ B3_INLINE b3Vec3 b3RotateVector( b3Quat q, b3Vec3 v )
return b3MulAdd( v, 2.0f, t3 );
}
/// Inverse rotate a vector
/// Inverse rotate a vector.
B3_INLINE b3Vec3 b3InvRotateVector( b3Quat q, b3Vec3 v )
{
// v + 2 * cross(q.v, cross(q.v, v) - q.s * v)
@ -432,11 +441,13 @@ B3_INLINE b3Vec3 b3InvRotateVector( b3Quat q, b3Vec3 v )
return b3MulAdd( v, 2.0f, t3 );
}
/// Compute dot product of two quaternions. Useful for polarity tests.
B3_INLINE float b3DotQuat( b3Quat a, b3Quat b )
{
return a.v.x * b.v.x + a.v.y * b.v.y + a.v.z * b.v.z + a.s * b.s;
}
/// Multiply two quaternions.
B3_INLINE b3Quat b3MulQuat( b3Quat q1, b3Quat q2 )
{
b3Vec3 t1 = b3Cross( q1.v, q2.v );
@ -446,7 +457,8 @@ B3_INLINE b3Quat b3MulQuat( b3Quat q1, b3Quat q2 )
return q;
}
// inv(q1) * q2
/// Compute a relative quaternion.
/// inv(q1) * q2
B3_INLINE b3Quat b3InvMulQuat( b3Quat q1, b3Quat q2 )
{
b3Vec3 t1 = b3Cross( q2.v, q1.v );
@ -456,16 +468,19 @@ B3_INLINE b3Quat b3InvMulQuat( b3Quat q1, b3Quat q2 )
return q;
}
/// Quaternion conjugate (cheap inverse).
B3_INLINE b3Quat b3Conjugate( b3Quat q )
{
return B3_LITERAL( b3Quat ){ { -q.v.x, -q.v.y, -q.v.z }, q.s };
}
/// Component-wise quaternion negation.
B3_INLINE b3Quat b3NegateQuat( b3Quat q )
{
return B3_LITERAL( b3Quat ){ { -q.v.x, -q.v.y, -q.v.z }, -q.s };
}
/// Normalize a quaternion.
B3_INLINE b3Quat b3NormalizeQuat( b3Quat q )
{
float lengthSq = b3DotQuat( q, q );
@ -479,6 +494,7 @@ B3_INLINE b3Quat b3NormalizeQuat( b3Quat q )
return b3Quat_identity;
}
/// Make a quaternion that is equivalent to rotating around an axis by a specified angle.
B3_INLINE b3Quat b3MakeQuatFromAxisAngle( b3Vec3 axis, float radians )
{
B3_ASSERT( b3IsNormalized( axis ) );
@ -487,6 +503,7 @@ B3_INLINE b3Quat b3MakeQuatFromAxisAngle( b3Vec3 axis, float radians )
return q;
}
/// Get the axis and angle from a quaternion. Assumes the quaternion is normalized.
B3_INLINE b3Vec3 b3GetAxisAngle( float* radians, b3Quat q )
{
float length = sqrtf( q.v.x * q.v.x + q.v.y * q.v.y + q.v.z * q.v.z );
@ -508,38 +525,16 @@ B3_INLINE float b3GetQuatAngle( b3Quat q )
return 2.0f * b3Atan2( length, q.s );
}
// Twist angle around the z-axis, used for twist limit and revolute angle limit
B3_INLINE float b3GetTwistAngle( b3Quat q )
{
// Account for polarity to keep the twist angle in range.
// This is simpler than asking the user to check polarity or unwinding.
float twist = q.s < 0.0f ? b3Atan2( -q.v.z, -q.s ) : b3Atan2( q.v.z, q.s );
twist *= 2.0f;
B3_ASSERT( -B3_PI <= twist && twist <= B3_PI );
return twist;
}
// Swing angle used for cone limit
B3_INLINE float b3GetSwingAngle( b3Quat q )
{
// Polarity should not matter because all terms are squared.
float x = sqrtf( q.v.z * q.v.z + q.s * q.s );
float y = sqrtf( q.v.x * q.v.x + q.v.y * q.v.y );
float swing = 2.0f * b3Atan2( y, x );
B3_ASSERT( 0.0f <= swing && swing <= B3_PI );
return swing;
}
/// Extract a quaternion from a rotation matrix
/// Extract a quaternion from a rotation matrix.
B3_API b3Quat b3MakeQuatFromMatrix( const b3Matrix3* m );
/// Find a quaternion that rotations one vector to another
/// Find a quaternion that rotates one vector to another.
B3_API b3Quat b3ComputeQuatBetweenUnitVectors( b3Vec3 v1, b3Vec3 v2 );
/// Linearly interpolate and normalize between two quaternions
B3_INLINE b3Quat b3NLerp( b3Quat q1, b3Quat q2, float alpha )
{
B3_ASSERT( 0.0f <= alpha && alpha <= 1.0f );
B3_VALIDATE( 0.0f <= alpha && alpha <= 1.0f );
if ( b3DotQuat( q1, q2 ) < 0.0f )
{
q1 = B3_LITERAL( b3Quat ){ { -q1.v.x, -q1.v.y, -q1.v.z }, -q1.s };
@ -574,6 +569,7 @@ B3_FORCE_INLINE b3Transform b3InvMulTransforms( b3Transform a, b3Transform b )
return out;
}
/// Get the inverse of a transform.
B3_INLINE b3Transform b3InvertTransform( b3Transform t )
{
b3Transform out;
@ -582,35 +578,26 @@ B3_INLINE b3Transform b3InvertTransform( b3Transform t )
return out;
}
B3_INLINE b3Transform b3NormalizeTransform( b3Transform t )
{
b3Transform out;
out.p = t.p;
out.q = b3NormalizeQuat( t.q );
return out;
}
/// Transform a point.
B3_INLINE b3Vec3 b3TransformPoint( b3Transform t, b3Vec3 v )
{
b3Vec3 rv = b3RotateVector( t.q, v );
return b3Add( rv, t.p );
}
/// Inverse transform a point.
B3_INLINE b3Vec3 b3InvTransformPoint( b3Transform t, b3Vec3 v )
{
return b3InvRotateVector( t.q, b3Sub( v, t.p ) );
}
B3_INLINE b3Matrix3 b3MakeDiagonalMatrix( float a, float b, float c )
{
return B3_LITERAL( b3Matrix3 ){ { a, 0.0f, 0.0f }, { 0.0f, b, 0.0f }, { 0.0f, 0.0f, c } };
}
/// Compute the determinant of a 3-by-3 matrix.
B3_INLINE float b3Det( b3Matrix3 m )
{
return b3Dot( m.cx, b3Cross( m.cy, m.cz ) );
}
/// Multiply a matrix times a column vector.
B3_INLINE b3Vec3 b3MulMV( b3Matrix3 m, b3Vec3 a )
{
b3Vec3 b = {
@ -621,6 +608,7 @@ B3_INLINE b3Vec3 b3MulMV( b3Matrix3 m, b3Vec3 a )
return b;
}
/// Negate a matrix.
B3_INLINE b3Matrix3 b3NegateMat3( b3Matrix3 a )
{
return B3_LITERAL( b3Matrix3 ){
@ -630,6 +618,8 @@ B3_INLINE b3Matrix3 b3NegateMat3( b3Matrix3 a )
};
}
/// Matrix addition.
/// @return a + b
B3_INLINE b3Matrix3 b3AddMM( b3Matrix3 a, b3Matrix3 b )
{
return B3_LITERAL( b3Matrix3 ){
@ -639,6 +629,8 @@ B3_INLINE b3Matrix3 b3AddMM( b3Matrix3 a, b3Matrix3 b )
};
}
/// Matrix subtraction.
/// @return a - b
B3_INLINE b3Matrix3 b3SubMM( b3Matrix3 a, b3Matrix3 b )
{
return B3_LITERAL( b3Matrix3 ){
@ -648,6 +640,7 @@ B3_INLINE b3Matrix3 b3SubMM( b3Matrix3 a, b3Matrix3 b )
};
}
/// Multiply a matrix by a scalar, component-wise.
B3_INLINE b3Matrix3 b3MulSM( float s, b3Matrix3 a )
{
return B3_LITERAL( b3Matrix3 ){
@ -657,6 +650,8 @@ B3_INLINE b3Matrix3 b3MulSM( float s, b3Matrix3 a )
};
}
/// Matrix multiplication.
/// @return a * b
B3_INLINE b3Matrix3 b3MulMM( b3Matrix3 a, b3Matrix3 b )
{
b3Matrix3 out;
@ -666,6 +661,7 @@ B3_INLINE b3Matrix3 b3MulMM( b3Matrix3 a, b3Matrix3 b )
return out;
}
/// Matrix transpose.
B3_INLINE b3Matrix3 b3Transpose( b3Matrix3 m )
{
b3Matrix3 out;
@ -676,6 +672,7 @@ B3_INLINE b3Matrix3 b3Transpose( b3Matrix3 m )
return out;
}
/// General matrix inverse.
B3_INLINE b3Matrix3 b3InvertMatrix( b3Matrix3 m )
{
float det = b3Det( m );
@ -693,21 +690,23 @@ B3_INLINE b3Matrix3 b3InvertMatrix( b3Matrix3 m )
return b3Mat3_zero;
}
B3_INLINE b3Vec3 b3Solve3( b3Matrix3 m, b3Vec3 x )
/// Solve a matrix equation.
/// @return inv(m) * a
B3_INLINE b3Vec3 b3Solve3( b3Matrix3 m, b3Vec3 a )
{
float det = b3Det( m );
if ( b3AbsFloat( det ) > 1000.0f * FLT_MIN )
{
float invDet = 1.0f / det;
b3Matrix3 a;
a.cx = b3Cross( m.cy, m.cz );
a.cy = b3Cross( m.cz, m.cx );
a.cz = b3Cross( m.cx, m.cy );
b3Matrix3 s;
s.cx = b3Cross( m.cy, m.cz );
s.cy = b3Cross( m.cz, m.cx );
s.cz = b3Cross( m.cx, m.cy );
b3Vec3 b = {
invDet * b3Dot( a.cx, x ),
invDet * b3Dot( a.cy, x ),
invDet * b3Dot( a.cz, x ),
invDet * b3Dot( s.cx, a ),
invDet * b3Dot( s.cy, a ),
invDet * b3Dot( s.cz, a ),
};
return b;
@ -716,6 +715,7 @@ B3_INLINE b3Vec3 b3Solve3( b3Matrix3 m, b3Vec3 x )
return b3Vec3_zero;
}
/// Invert a matrix.
B3_INLINE b3Matrix3 b3InvertT( b3Matrix3 m )
{
float det = b3Det( m );
@ -732,32 +732,7 @@ B3_INLINE b3Matrix3 b3InvertT( b3Matrix3 m )
return b3Mat3_zero;
}
B3_INLINE b3Matrix3 b3InvertSymmetric( b3Matrix3 m )
{
float det = b3Det( m );
if ( b3AbsFloat( det ) > 1000.0f * FLT_MIN )
{
float invDet = 1.0f / det;
b3Matrix3 out;
out.cx = b3MulSV( invDet, b3Cross( m.cy, m.cz ) );
out.cy = b3MulSV( invDet, b3Cross( m.cz, m.cx ) );
out.cz = b3MulSV( invDet, b3Cross( m.cx, m.cy ) );
return out;
}
return b3Mat3_zero;
}
B3_INLINE b3Matrix3 b3Skew( b3Vec3 v )
{
b3Matrix3 out;
out.cx = B3_LITERAL( b3Vec3 ){ 0, v.z, -v.y };
out.cy = B3_LITERAL( b3Vec3 ){ -v.z, 0, v.x };
out.cz = B3_LITERAL( b3Vec3 ){ v.y, -v.x, 0 };
return out;
}
/// Get the component-wise absolute value of a matrix.
B3_INLINE b3Matrix3 b3AbsMatrix3( b3Matrix3 m )
{
b3Matrix3 out;
@ -768,21 +743,9 @@ B3_INLINE b3Matrix3 b3AbsMatrix3( b3Matrix3 m )
return out;
}
B3_INLINE float b3Trace( b3Matrix3 m )
{
return m.cx.x + m.cy.y + m.cz.z;
}
B3_INLINE b3Matrix3 b3ScaleMatrix( float s, b3Matrix3 m )
{
return B3_LITERAL( b3Matrix3 ){
b3MulSV( s, m.cx ),
b3MulSV( s, m.cy ),
b3MulSV( s, m.cz ),
};
}
// This force inline improves the performance of b3ShapeDistance
/// Make a matrix from a quaternion. This is useful if you need to
/// rotate many vectors.
/// The force inline improves the performance of b3ShapeDistance.
B3_FORCE_INLINE b3Matrix3 b3MakeMatrixFromQuat( b3Quat q )
{
float xx = q.v.x * q.v.x;
@ -802,9 +765,11 @@ B3_FORCE_INLINE b3Matrix3 b3MakeMatrixFromQuat( b3Quat q )
};
}
// https://en.wikipedia.org/wiki/Parallel_axis_theorem
/// Get the inertia tensor of an offset point.
/// https://en.wikipedia.org/wiki/Parallel_axis_theorem
B3_API b3Matrix3 b3Steiner( float mass, b3Vec3 origin );
/// Get the AABB of a point cloud.
B3_INLINE b3AABB b3MakeAABB( const b3Vec3* points, int count, float radius )
{
B3_ASSERT( count > 0 );
@ -822,23 +787,7 @@ B3_INLINE b3AABB b3MakeAABB( const b3Vec3* points, int count, float radius )
return a;
}
B3_INLINE b3AABB b3AABB_AddPoint( b3AABB a, b3Vec3 point )
{
return B3_LITERAL( b3AABB ){ b3Min( a.lowerBound, point ), b3Max( a.upperBound, point ) };
}
B3_INLINE bool b3AABB_ContainsPoint( b3AABB a, b3Vec3 point )
{
if ( a.lowerBound.x > point.x || point.x > a.upperBound.x )
return false;
if ( a.lowerBound.y > point.y || point.y > a.upperBound.y )
return false;
if ( a.lowerBound.z > point.z || point.z > a.upperBound.z )
return false;
return true;
}
/// Does a fully contain b?
B3_INLINE bool b3AABB_Contains( b3AABB a, b3AABB b )
{
if ( a.lowerBound.x > b.lowerBound.x || b.upperBound.x > a.upperBound.x )
@ -851,40 +800,46 @@ B3_INLINE bool b3AABB_Contains( b3AABB a, b3AABB b )
return true;
}
B3_INLINE float b3AABB_Area( b3AABB bounds )
/// Get the surface area of an axis-aligned bounding box.
B3_INLINE float b3AABB_Area( b3AABB a )
{
b3Vec3 delta = b3Sub( bounds.upperBound, bounds.lowerBound );
b3Vec3 delta = b3Sub( a.upperBound, a.lowerBound );
return 2.0f * ( delta.x * delta.y + delta.y * delta.z + delta.z * delta.x );
}
B3_INLINE b3Vec3 b3AABB_Center( b3AABB bounds )
/// Get the center of an axis-aligned bounding box.
B3_INLINE b3Vec3 b3AABB_Center( b3AABB a )
{
return b3MulSV( 0.5f, b3Add( bounds.upperBound, bounds.lowerBound ) );
return b3MulSV( 0.5f, b3Add( a.upperBound, a.lowerBound ) );
}
B3_INLINE b3Vec3 b3AABB_Extents( b3AABB bounds )
/// Get the extents (half-widths) of an axis-aligned bounding box.
B3_INLINE b3Vec3 b3AABB_Extents( b3AABB a )
{
return b3MulSV( 0.5f, b3Sub( bounds.upperBound, bounds.lowerBound ) );
return b3MulSV( 0.5f, b3Sub( a.upperBound, a.lowerBound ) );
}
B3_INLINE b3AABB b3AABB_Union( b3AABB bounds1, b3AABB bounds2 )
/// Get the union of two axis-aligned bounding boxes.
B3_INLINE b3AABB b3AABB_Union( b3AABB a, b3AABB b )
{
b3AABB out;
out.lowerBound = b3Min( bounds1.lowerBound, bounds2.lowerBound );
out.upperBound = b3Max( bounds1.upperBound, bounds2.upperBound );
out.lowerBound = b3Min( a.lowerBound, b.lowerBound );
out.upperBound = b3Max( a.upperBound, b.upperBound );
return out;
}
B3_INLINE b3AABB b3AABB_Inflate( b3AABB bounds, float extension )
/// Add uniform padding to an axis-aligned bounding box.
B3_INLINE b3AABB b3AABB_Inflate( b3AABB a, float extension )
{
b3Vec3 radius = { extension, extension, extension };
b3AABB out;
out.lowerBound = b3Sub( bounds.lowerBound, radius );
out.upperBound = b3Add( bounds.upperBound, radius );
out.lowerBound = b3Sub( a.lowerBound, radius );
out.upperBound = b3Add( a.upperBound, radius );
return out;
}
/// Do two axis-aligned boxes overlap?
B3_INLINE bool b3AABB_Overlaps( b3AABB a, b3AABB b )
{
// No intersection if separated along one axis
@ -899,6 +854,9 @@ B3_INLINE bool b3AABB_Overlaps( b3AABB a, b3AABB b )
return true;
}
/// Transform an axis-aligned bounding box. This can create a larger box
/// than if you recomputed the AABB of the original shape with the transform
/// applied.
B3_INLINE b3AABB b3AABB_Transform( b3Transform transform, b3AABB a )
{
b3Vec3 center = b3TransformPoint( transform, b3AABB_Center( a ) );
@ -908,6 +866,7 @@ B3_INLINE b3AABB b3AABB_Transform( b3Transform transform, b3AABB a )
return out;
}
/// Get the closest point on an axis-aligned bounding box.
B3_INLINE b3Vec3 b3ClosestPointToAABB( b3Vec3 point, b3AABB a )
{
return b3Clamp( point, a.lowerBound, a.upperBound );
@ -937,7 +896,7 @@ B3_API bool b3IsSaneAABB( b3AABB a );
/// Is this a valid plane? Normal is a unit vector. Not Nan or infinity.
B3_API bool b3IsValidPlane( b3Plane a );
/**@}*/
/**@}*/ // math
/**
* @defgroup math_cpp C++ Math
@ -949,6 +908,7 @@ B3_API bool b3IsValidPlane( b3Plane a );
#ifdef __cplusplus
/// Vector addition.
B3_FORCE_INLINE b3Vec3& operator+=( b3Vec3& a, b3Vec3 b )
{
a.x += b.x;
@ -957,6 +917,7 @@ B3_FORCE_INLINE b3Vec3& operator+=( b3Vec3& a, b3Vec3 b )
return a;
}
/// Vector subtraction.
B3_FORCE_INLINE b3Vec3& operator-=( b3Vec3& a, b3Vec3 b )
{
a.x -= b.x;
@ -965,6 +926,7 @@ B3_FORCE_INLINE b3Vec3& operator-=( b3Vec3& a, b3Vec3 b )
return a;
}
/// Vector scaling.
B3_FORCE_INLINE b3Vec3& operator*=( b3Vec3& a, float s )
{
a.x *= s;
@ -973,87 +935,42 @@ B3_FORCE_INLINE b3Vec3& operator*=( b3Vec3& a, float s )
return a;
}
/// Vector negation.
B3_FORCE_INLINE b3Vec3 operator-( b3Vec3 a )
{
return { -a.x, -a.y, -a.z };
}
/// Vector scaling.
B3_FORCE_INLINE b3Vec3 operator*( float s, b3Vec3 a )
{
return { s * a.x, s * a.y, s * a.z };
}
/// Vector scaling.
B3_FORCE_INLINE b3Vec3 operator*( b3Vec3 a, float s )
{
return { s * a.x, s * a.y, s * a.z };
}
/// Component-wise vector multiplication.
B3_FORCE_INLINE b3Vec3 operator*( b3Vec3 a, b3Vec3 b )
{
return { a.x * b.x, a.y * b.y, a.z * b.z };
}
/// Vector addition.
B3_FORCE_INLINE b3Vec3 operator+( b3Vec3 a, b3Vec3 b )
{
return { a.x + b.x, a.y + b.y, a.z + b.z };
}
/// Vector subtraction.
B3_FORCE_INLINE b3Vec3 operator-( b3Vec3 a, b3Vec3 b )
{
return { a.x - b.x, a.y - b.y, a.z - b.z };
}
B3_INLINE b3Vec3 operator*( b3Matrix3 m, b3Vec3 v )
{
return b3MulMV( m, v );
}
B3_INLINE b3Matrix3& operator+=( b3Matrix3& a, b3Matrix3 b )
{
a = b3AddMM( a, b );
return a;
}
B3_INLINE b3Matrix3 operator*( b3Matrix3 a, b3Matrix3 b )
{
return b3MulMM( a, b );
}
B3_INLINE b3Matrix3 operator+( b3Matrix3 a, b3Matrix3 b )
{
return b3AddMM( a, b );
}
B3_INLINE b3Matrix3 operator-( b3Matrix3 a, b3Matrix3 b )
{
return b3SubMM( a, b );
}
B3_INLINE b3Matrix3 operator*( float s, b3Matrix3 a )
{
return b3MulSM( s, a );
}
B3_INLINE b3Quat operator+( b3Quat q1, b3Quat q2 )
{
return { q1.v + q2.v, q1.s + q2.s };
}
B3_INLINE b3Quat operator-( b3Quat q1, b3Quat q2 )
{
return { q1.v - q2.v, q1.s - q2.s };
}
B3_INLINE b3Quat operator*( float f, b3Quat q )
{
return { f * q.v, f * q.s };
}
B3_INLINE b3Quat operator*( b3Quat q, float f )
{
return { q.v * f, q.s * f };
}
#endif
/**@}*/
/**@}*/ // math_cpp

File diff suppressed because it is too large Load diff