Skip to content

Commit

Permalink
Fix falling body assert (#92)
Browse files Browse the repository at this point in the history
API to get parent chain id
default definitions as constants
128 max worlds
removed extents from mass data
removed box2d_exports.h and implemented similar switches in api.h
user_constants.h optional and off by default
added some missing API functions
added shape filter sample
addresses #93
  • Loading branch information
erincatto authored Dec 30, 2023
1 parent 907c3a4 commit 077d04a
Show file tree
Hide file tree
Showing 36 changed files with 876 additions and 491 deletions.
1 change: 1 addition & 0 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ IncludeCategories:

IndentExternBlock: NoIndent
IndentCaseLabels: true
IndentPPDirectives: BeforeHash
IndentAccessModifiers: false
AccessModifierOffset: -4

Expand Down
12 changes: 8 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
cmake_minimum_required(VERSION 3.23)
cmake_minimum_required(VERSION 3.22)
include(FetchContent)

project(box2d
VERSION 3.0.0
DESCRIPTION "A 2D physics engine for games"
HOMEPAGE_URL "box2d.org"
HOMEPAGE_URL "https://box2d.org"
LANGUAGES C CXX
)

Expand All @@ -15,8 +15,12 @@ project(box2d
# message(STATUS "CMake C++ compiler: ${CMAKE_CXX_COMPILER_ID}")
message(STATUS "CMake system name: ${CMAKE_SYSTEM_NAME}")

set(BOX2D_LENGTH_UNIT_PER_METER "1.0" CACHE STRING "Length units per meter")
set(BOX2D_MAX_POLYGON_VERTICES "8" CACHE STRING "Maximum number of polygon vertices (affects performance)")
option(BOX2D_USER_CONSTANTS "Generate user_constants.h" OFF)

if (BOX2D_USER_CONSTANTS)
set(BOX2D_LENGTH_UNIT_PER_METER "1.0" CACHE STRING "Length units per meter")
set(BOX2D_MAX_POLYGON_VERTICES "8" CACHE STRING "Maximum number of polygon vertices (affects performance)")
endif()

# Needed for samples.exe to find box2d.dll
# set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/bin")
Expand Down
2 changes: 1 addition & 1 deletion docs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set(DOXYGEN_FILE_PATTERNS *.h)
set(DOXYGEN_ENABLE_PREPROCESSING YES)
set(DOXYGEN_MACRO_EXPANSION YES)
set(DOXYGEN_EXPAND_ONLY_PREDEF YES)
set(DOXYGEN_PREDEFINED BOX2D_API=)
set(DOXYGEN_PREDEFINED B2_API=)

set(DOXYGEN_IMAGE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/images")
set(DOXYGEN_HTML_EXTRA_STYLESHEET "${CMAKE_CURRENT_SOURCE_DIR}/extra.css")
Expand Down
26 changes: 19 additions & 7 deletions include/box2d/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@

#pragma once

#include "box2d_export.h"

#include <stdint.h>

#if defined(_WIN32) && defined(box2d_EXPORTS)
// building the Box2D DLL
#define BOX2D_EXPORT __declspec(dllexport)
#elif defined(_WIN32) && defined(BOX2D_DLL)
// using the Box2D DLL
#define BOX2D_EXPORT __declspec(dllimport)
#elif defined(__GNUC__) && defined(box2d_EXPORTS)
// building the Box2D shared library
#define BOX2D_EXPORT __attribute__((visibility("default")))
#else
// static library
#define BOX2D_EXPORT
#endif

#ifdef __cplusplus
#define BOX2D_API extern "C" BOX2D_EXPORT
#define B2_API extern "C" BOX2D_EXPORT
#else
#define BOX2D_API BOX2D_EXPORT
#define B2_API BOX2D_EXPORT
#endif

/// Prototype for user allocation function.
Expand All @@ -24,14 +36,14 @@ typedef void b2FreeFcn(void* mem);

/// This allows the user to override the allocation functions. These should be
/// set during application startup.
BOX2D_API void b2SetAllocator(b2AllocFcn* allocFcn, b2FreeFcn* freeFcn);
B2_API void b2SetAllocator(b2AllocFcn* allocFcn, b2FreeFcn* freeFcn);

/// Total bytes allocated by Box2D
BOX2D_API uint32_t b2GetByteCount(void);
B2_API uint32_t b2GetByteCount(void);

/// Prototype for the user assert callback. Return 0 to skip the debugger break.
typedef int b2AssertFcn(const char* condition, const char* fileName, int lineNumber);

/// Override the default assert callback.
/// @param assertFcn a non-null assert callback
BOX2D_API void b2SetAssertFcn(b2AssertFcn* assertFcn);
B2_API void b2SetAssertFcn(b2AssertFcn* assertFcn);
273 changes: 146 additions & 127 deletions include/box2d/box2d.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion include/box2d/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ typedef struct b2Manifold b2Manifold;
typedef bool b2PreSolveFcn(b2ShapeId shapeIdA, b2ShapeId shapeIdB, b2Manifold* manifold, void* context);

/// Register the pre-solve callback. This is optional.
BOX2D_API void b2World_SetPreSolveCallback(b2WorldId worldId, b2PreSolveFcn* fcn, void* context);
B2_API void b2World_SetPreSolveCallback(b2WorldId worldId, b2PreSolveFcn* fcn, void* context);

/// Prototype callback for AABB queries.
/// See b2World_Query
Expand Down
12 changes: 6 additions & 6 deletions include/box2d/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
/// Although most of these are not user configurable, it can be interesting for a user to see
/// these to understand the tuning values Box2D uses.

// todo move constraint hertz/damping here

#include "user_constants.h"
#ifdef BOX2D_USER_CONSTANTS
#include "user_constants.h"
#endif

/// box2d bases all length units on meters, but you may need different units for your game.
/// You can override this value to use different units.
#ifndef b2_lengthUnitsPerMeter
#define b2_lengthUnitsPerMeter 1.0f
#define b2_lengthUnitsPerMeter 1.0f
#endif

/// https://en.wikipedia.org/wiki/Pi
Expand All @@ -44,11 +44,11 @@
/// The maximum number of vertices on a convex polygon. Changing this affects performance even if you
/// don't use more vertices.
#ifndef b2_maxPolygonVertices
#define b2_maxPolygonVertices 8
#define b2_maxPolygonVertices 8
#endif

/// Maximum number of simultaneous worlds that can be allocated
#define b2_maxWorlds 32
#define b2_maxWorlds 128

/// The maximum linear translation of a body per step. This limit is very large and is used
/// to prevent numerical problems. You shouldn't need to adjust this. Meters.
Expand Down
12 changes: 6 additions & 6 deletions include/box2d/distance.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ typedef struct b2SegmentDistanceResult
} b2SegmentDistanceResult;

/// Compute the distance between two line segments, clamping at the end points if needed.
BOX2D_API b2SegmentDistanceResult b2SegmentDistance(b2Vec2 p1, b2Vec2 q1, b2Vec2 p2, b2Vec2 q2);
B2_API b2SegmentDistanceResult b2SegmentDistance(b2Vec2 p1, b2Vec2 q1, b2Vec2 p2, b2Vec2 q2);

/// A distance proxy is used by the GJK algorithm. It encapsulates any shape.
typedef struct b2DistanceProxy
Expand Down Expand Up @@ -63,7 +63,7 @@ typedef struct b2DistanceOutput
/// Compute the closest points between two shapes. Supports any combination of:
/// b2Circle, b2Polygon, b2EdgeShape. The simplex cache is input/output.
/// On the first call set b2SimplexCache.count to zero.
BOX2D_API b2DistanceOutput b2ShapeDistance(b2DistanceCache* cache, const b2DistanceInput* input);
B2_API b2DistanceOutput b2ShapeDistance(b2DistanceCache* cache, const b2DistanceInput* input);

/// Input parameters for b2ShapeCast
typedef struct b2ShapeCastPairInput
Expand All @@ -78,10 +78,10 @@ typedef struct b2ShapeCastPairInput

/// Perform a linear shape cast of shape B moving and shape A fixed. Determines the hit point, normal, and translation fraction.
/// @returns true if hit, false if there is no hit or an initial overlap
BOX2D_API b2RayCastOutput b2ShapeCast(const b2ShapeCastPairInput* input);
B2_API b2RayCastOutput b2ShapeCast(const b2ShapeCastPairInput* input);

/// Make a proxy for use in GJK and related functions.
BOX2D_API b2DistanceProxy b2MakeProxy(const b2Vec2* vertices, int32_t count, float radius);
B2_API b2DistanceProxy b2MakeProxy(const b2Vec2* vertices, int32_t count, float radius);

/// This describes the motion of a body/shape for TOI computation. Shapes are defined with respect to the body origin,
/// which may not coincide with the center of mass. However, to support dynamics we must interpolate the center of mass
Expand All @@ -98,7 +98,7 @@ typedef struct b2Sweep
float a1, a2;
} b2Sweep;

BOX2D_API b2Transform b2GetSweepTransform(const b2Sweep* sweep, float time);
B2_API b2Transform b2GetSweepTransform(const b2Sweep* sweep, float time);

/// Input parameters for b2TimeOfImpact
typedef struct b2TOIInput
Expand Down Expand Up @@ -133,4 +133,4 @@ typedef struct b2TOIOutput
/// a fraction between [0,tMax]. This uses a swept separating axis and may miss some intermediate,
/// non-tunneling collisions. If you change the time interval, you should call this function
/// again.
BOX2D_API b2TOIOutput b2TimeOfImpact(const b2TOIInput* input);
B2_API b2TOIOutput b2TimeOfImpact(const b2TOIInput* input);
38 changes: 19 additions & 19 deletions include/box2d/dynamic_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,38 +64,38 @@ typedef struct b2DynamicTree
} b2DynamicTree;

/// Constructing the tree initializes the node pool.
BOX2D_API b2DynamicTree b2DynamicTree_Create(void);
B2_API b2DynamicTree b2DynamicTree_Create(void);

/// Destroy the tree, freeing the node pool.
BOX2D_API void b2DynamicTree_Destroy(b2DynamicTree* tree);
B2_API void b2DynamicTree_Destroy(b2DynamicTree* tree);

/// Create a proxy. Provide a tight fitting AABB and a userData value.
BOX2D_API int32_t b2DynamicTree_CreateProxy(b2DynamicTree* tree, b2AABB aabb, uint32_t categoryBits, int32_t userData);
B2_API int32_t b2DynamicTree_CreateProxy(b2DynamicTree* tree, b2AABB aabb, uint32_t categoryBits, int32_t userData);

/// Destroy a proxy. This asserts if the id is invalid.
BOX2D_API void b2DynamicTree_DestroyProxy(b2DynamicTree* tree, int32_t proxyId);
B2_API void b2DynamicTree_DestroyProxy(b2DynamicTree* tree, int32_t proxyId);

// Clone one tree to another, reusing storage in the outTree if possible
BOX2D_API void b2DynamicTree_Clone(b2DynamicTree* outTree, const b2DynamicTree* inTree);
B2_API void b2DynamicTree_Clone(b2DynamicTree* outTree, const b2DynamicTree* inTree);

/// Move a proxy to a new AABB by removing and reinserting into the tree.
BOX2D_API void b2DynamicTree_MoveProxy(b2DynamicTree* tree, int32_t proxyId, b2AABB aabb);
B2_API void b2DynamicTree_MoveProxy(b2DynamicTree* tree, int32_t proxyId, b2AABB aabb);

/// Enlarge a proxy and enlarge ancestors as necessary.
BOX2D_API void b2DynamicTree_EnlargeProxy(b2DynamicTree* tree, int32_t proxyId, b2AABB aabb);
B2_API void b2DynamicTree_EnlargeProxy(b2DynamicTree* tree, int32_t proxyId, b2AABB aabb);

/// This function receives proxies found in the AABB query.
/// @return true if the query should continue
typedef bool b2TreeQueryCallbackFcn(int32_t proxyId, int32_t userData, void* context);

/// Query an AABB for overlapping proxies. The callback class
/// is called for each proxy that overlaps the supplied AABB.
BOX2D_API void b2DynamicTree_QueryFiltered(const b2DynamicTree* tree, b2AABB aabb, uint32_t maskBits,
B2_API void b2DynamicTree_QueryFiltered(const b2DynamicTree* tree, b2AABB aabb, uint32_t maskBits,
b2TreeQueryCallbackFcn* callback, void* context);

/// Query an AABB for overlapping proxies. The callback class
/// is called for each proxy that overlaps the supplied AABB.
BOX2D_API void b2DynamicTree_Query(const b2DynamicTree* tree, b2AABB aabb, b2TreeQueryCallbackFcn* callback, void* context);
B2_API void b2DynamicTree_Query(const b2DynamicTree* tree, b2AABB aabb, b2TreeQueryCallbackFcn* callback, void* context);

/// This function receives clipped raycast input for a proxy. The function
/// returns the new ray fraction.
Expand All @@ -111,7 +111,7 @@ typedef float b2TreeRayCastCallbackFcn(const b2RayCastInput* input, int32_t prox
/// number of proxies in the tree.
/// @param input the ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
/// @param callback a callback class that is called for each proxy that is hit by the ray.
BOX2D_API void b2DynamicTree_RayCast(const b2DynamicTree* tree, const b2RayCastInput* input, uint32_t maskBits,
B2_API void b2DynamicTree_RayCast(const b2DynamicTree* tree, const b2RayCastInput* input, uint32_t maskBits,
b2TreeRayCastCallbackFcn* callback, void* context);

/// This function receives clipped raycast input for a proxy. The function
Expand All @@ -128,35 +128,35 @@ typedef float b2TreeShapeCastCallbackFcn(const b2ShapeCastInput* input, int32_t
/// number of proxies in the tree.
/// @param input the ray-cast input data. The ray extends from p1 to p1 + maxFraction * (p2 - p1).
/// @param callback a callback class that is called for each proxy that is hit by the ray.
BOX2D_API void b2DynamicTree_ShapeCast(const b2DynamicTree* tree, const b2ShapeCastInput* input, uint32_t maskBits,
B2_API void b2DynamicTree_ShapeCast(const b2DynamicTree* tree, const b2ShapeCastInput* input, uint32_t maskBits,
b2TreeShapeCastCallbackFcn* callback, void* context);

/// Validate this tree. For testing.
BOX2D_API void b2DynamicTree_Validate(const b2DynamicTree* tree);
B2_API void b2DynamicTree_Validate(const b2DynamicTree* tree);

/// Compute the height of the binary tree in O(N) time. Should not be
/// called often.
BOX2D_API int32_t b2DynamicTree_GetHeight(const b2DynamicTree* tree);
B2_API int32_t b2DynamicTree_GetHeight(const b2DynamicTree* tree);

/// Get the maximum balance of the tree. The balance is the difference in height of the two children of a node.
BOX2D_API int32_t b2DynamicTree_GetMaxBalance(const b2DynamicTree* tree);
B2_API int32_t b2DynamicTree_GetMaxBalance(const b2DynamicTree* tree);

/// Get the ratio of the sum of the node areas to the root area.
BOX2D_API float b2DynamicTree_GetAreaRatio(const b2DynamicTree* tree);
B2_API float b2DynamicTree_GetAreaRatio(const b2DynamicTree* tree);

/// Build an optimal tree. Very expensive. For testing.
BOX2D_API void b2DynamicTree_RebuildBottomUp(b2DynamicTree* tree);
B2_API void b2DynamicTree_RebuildBottomUp(b2DynamicTree* tree);

/// Get the number of proxies created
BOX2D_API int32_t b2DynamicTree_GetProxyCount(const b2DynamicTree* tree);
B2_API int32_t b2DynamicTree_GetProxyCount(const b2DynamicTree* tree);

/// Rebuild the tree while retaining subtrees that haven't changed. Returns the number of boxes sorted.
BOX2D_API int32_t b2DynamicTree_Rebuild(b2DynamicTree* tree, bool fullBuild);
B2_API int32_t b2DynamicTree_Rebuild(b2DynamicTree* tree, bool fullBuild);

/// Shift the world origin. Useful for large worlds.
/// The shift formula is: position -= newOrigin
/// @param newOrigin the new origin with respect to the old origin
BOX2D_API void b2DynamicTree_ShiftOrigin(b2DynamicTree* tree, b2Vec2 newOrigin);
B2_API void b2DynamicTree_ShiftOrigin(b2DynamicTree* tree, b2Vec2 newOrigin);

/// Get proxy user data
/// @return the proxy user data or 0 if the id is invalid
Expand Down
Loading

0 comments on commit 077d04a

Please sign in to comment.