Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Feb 24, 2024
1 parent 944d66c commit 5db873f
Show file tree
Hide file tree
Showing 55 changed files with 835 additions and 840 deletions.
11 changes: 7 additions & 4 deletions include/box2d/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@
#define BOX2D_INLINE extern inline
#endif
#else
//#pragma message("BOX2D inline")
// #pragma message("BOX2D inline")
#define BOX2D_INLINE inline
#endif

#ifdef __cplusplus
#define B2_API extern "C" BOX2D_EXPORT
#define B2_INLINE extern "C" BOX2D_INLINE
#define B2_LITERAL(T) T
#else
#define B2_API BOX2D_EXPORT
#define B2_INLINE BOX2D_INLINE
/// Used for C literals like (b2Vec2){1.0f, 2.0f} where C++ requires b2Vec2{1.0f, 2.0f}
#define B2_LITERAL(T) (T)
#endif

/// Prototype for user allocation function.
Expand All @@ -54,16 +57,16 @@ typedef void* b2AllocFcn(uint32_t size, int32_t alignment);
/// @param mem the memory previously allocated through `b2AllocFcn`
typedef void b2FreeFcn(void* mem);

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

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

/// Total bytes allocated by Box2D
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
B2_API void b2SetAssertFcn(b2AssertFcn* assertFcn);
55 changes: 44 additions & 11 deletions include/box2d/box2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ B2_API void b2World_EnableContinuous(b2WorldId worldId, bool flag);
/// Adjust the restitution threshold. Advanced feature for testing.
B2_API void b2World_SetRestitutionThreshold(b2WorldId worldId, float value);

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

/// Adjust contact tuning parameters:
/// - hertz is the contact stiffness (cycles per second)
/// - damping ratio is the contact bounciness with 1 being critical damping (non-dimensional)
Expand Down Expand Up @@ -212,18 +215,25 @@ B2_API void b2Body_ApplyTorque(b2BodyId bodyId, float torque, bool wake);
/// Apply an impulse at a point. This immediately modifies the velocity.
/// It also modifies the angular velocity if the point of application
/// is not at the center of mass. This wakes up the body.
/// This should be used for one-shot impulses. If you need a steady force,
/// use a force instead, which will work better with the sub-stepping solver.
/// @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
/// @param point the world position of the point of application.
/// @param wake also wake up the body
B2_API void b2Body_ApplyLinearImpulse(b2BodyId bodyId, b2Vec2 impulse, b2Vec2 point, bool wake);

/// Apply an impulse to the center of mass. This immediately modifies the velocity.
/// This should be used for one-shot impulses. If you need a steady force,
/// use a force instead, which will work better with the sub-stepping solver.
/// @param impulse the world impulse vector, usually in N-seconds or kg-m/s.
/// @param wake also wake up the body
B2_API void b2Body_ApplyLinearImpulseToCenter(b2BodyId bodyId, b2Vec2 impulse, bool wake);

/// Apply an angular impulse.
/// @param impulse the angular impulse in units of kg*m*m/s
/// This should be used for one-shot impulses. If you need a steady force,
/// use a force instead, which will work better with the sub-stepping solver.
/// @param impulse the angular impulse in units of
/// kg*m*m/s
/// @param wake also wake up the body
B2_API void b2Body_ApplyAngularImpulse(b2BodyId bodyId, float impulse, bool wake);

Expand All @@ -247,6 +257,11 @@ B2_API void b2Body_SetMassData(b2BodyId bodyId, b2MassData massData);
/// Get the mass data for a body.
B2_API b2MassData b2Body_GetMassData(b2BodyId bodyId);

/// This resets the mass properties to the sum of the mass properties of the fixtures.
/// This normally does not need to be called unless you called SetMassData to override
/// the mass and you later want to reset the mass.
B2_API void b2Body_ResetMassData(b2BodyId bodyId);

/// Adjust the linear damping. Normally this is set in b2BodyDef before creation.
B2_API void b2Body_SetLinearDamping(b2BodyId bodyId, float linearDamping);

Expand Down Expand Up @@ -352,7 +367,8 @@ B2_API bool b2Shape_IsSensor(b2ShapeId shapeId);
B2_API void* b2Shape_GetUserData(b2ShapeId shapeId);

/// Set the density on a shape. Normally this is specified in b2ShapeDef.
/// This will recompute the mass properties on the parent body.
/// This will not update the mass properties on the parent body until you
/// call b2Body_ResetMassData.
B2_API void b2Shape_SetDensity(b2ShapeId shapeId, float density);

/// Get the density on a shape.
Expand All @@ -379,20 +395,37 @@ B2_API void b2Shape_SetFilter(b2ShapeId shapeId, b2Filter filter);
/// Test a point for overlap with a shape
B2_API bool b2Shape_TestPoint(b2ShapeId shapeId, b2Vec2 point);

/// Access the circle geometry of a shape.
B2_API const b2Circle* b2Shape_GetCircle(b2ShapeId shapeId);
/// Ray cast a shape directly
B2_API b2RayResult b2Shape_RayCast(b2ShapeId shapeId, b2Vec2 origin, b2Vec2 translation);

/// Access the line segment geometry of a shape.
B2_API const b2Segment* b2Shape_GetSegment(b2ShapeId shapeId);
/// Access the circle geometry of a shape. Asserts the type is correct.
B2_API const b2Circle b2Shape_GetCircle(b2ShapeId shapeId);

/// Access the line segment geometry of a shape. Asserts the type is correct.
B2_API const b2Segment b2Shape_GetSegment(b2ShapeId shapeId);

/// Access the smooth line segment geometry of a shape. These come from chain shapes.
B2_API const b2SmoothSegment* b2Shape_GetSmoothSegment(b2ShapeId shapeId);
/// Asserts the type is correct.
B2_API const b2SmoothSegment b2Shape_GetSmoothSegment(b2ShapeId shapeId);

/// Access the capsule geometry of a shape. Asserts the type is correct.
B2_API const b2Capsule b2Shape_GetCapsule(b2ShapeId shapeId);

/// Access the convex polygon geometry of a shape. Asserts the type is correct.
B2_API const b2Polygon b2Shape_GetPolygon(b2ShapeId shapeId);

/// Allows you to change a shape to be a circle or update the current circle.
/// This does not modify the mass properties.
B2_API const void b2Shape_SetCircle(b2ShapeId shapeId, b2Circle circle);

/// Allows you to change a shape to be a capsule or update the current capsule.
B2_API const void b2Shape_SetCapsule(b2ShapeId shapeId, b2Capsule capsule);

/// Access the capsule geometry of a shape.
B2_API const b2Capsule* b2Shape_GetCapsule(b2ShapeId shapeId);
/// Allows you to change a shape to be a segment or update the current segment.
B2_API const void b2Shape_SetSegment(b2ShapeId shapeId, b2Segment segment);

/// Access the convex polygon geometry of a shape.
B2_API const b2Polygon* b2Shape_GetPolygon(b2ShapeId shapeId);
/// Allows you to change a shape to be a segment or update the current segment.
B2_API const void b2Shape_SetPolygon(b2ShapeId shapeId, b2Polygon polygon);

/// If the type is b2_smoothSegmentShape then you can get the parent chain id.
/// If the shape is not a smooth segment then this will return b2_nullChainId.
Expand Down
50 changes: 35 additions & 15 deletions include/box2d/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,44 @@

#pragma once

#include "api.h"
#include "id.h"
#include "types.h"
#include "math_types.h"

#include <stdbool.h>

typedef struct b2Manifold b2Manifold;

/// Task interface
/// This is prototype for a Box2D task. Your task system is expected to invoke the Box2D task with these arguments.
/// The task spans a range of the parallel-for: [startIndex, endIndex)
/// The worker index must correctly identify each worker in the user thread pool, expected in [0, workerCount).
/// A worker must only exist on only one thread at a time and is analogous to the thread index.
/// The task context is the context pointer sent from Box2D when it is enqueued.
/// The startIndex and endIndex are expected in the range [0, itemCount) where itemCount is the argument to b2EnqueueTaskCallback
/// below. Box2D expects startIndex < endIndex and will execute a loop like this:
///
/// for (int i = startIndex; i < endIndex; ++i)
/// {
/// DoWork();
/// }
typedef void b2TaskCallback(int32_t startIndex, int32_t endIndex, uint32_t workerIndex, void* taskContext);

/// These functions can be provided to Box2D to invoke a task system. These are designed to work well with enkiTS.
/// Returns a pointer to the user's task object. May be nullptr. A nullptr indicates to Box2D that the work was executed
/// serially within the callback and there is no need to call b2FinishTaskCallback.
/// The itemCount is the number of Box2D work items that are to be partitioned among workers by the user's task system.
/// This is essentially a parallel-for. The minRange parameter is a suggestion of the minimum number of items to assign
/// per worker to reduce overhead. For example, suppose the task is small and that itemCount is 16. A minRange of 8 suggests
/// that your task system should split the work items among just two workers, even if you have more available.
/// In general the range [startIndex, endIndex) send to b2TaskCallback should obey:
/// endIndex - startIndex >= minRange
/// The exception of course is when itemCount < minRange.
typedef void* b2EnqueueTaskCallback(b2TaskCallback* task, int32_t itemCount, int32_t minRange, void* taskContext,
void* userContext);

/// Finishes a user task object that wraps a Box2D task.
typedef void b2FinishTaskCallback(void* userTask, void* userContext);

/// Prototype for a pre-solve callback.
/// This is called after a contact is updated. This allows you to inspect a
/// contact before it goes to the solver. If you are careful, you can modify the
Expand All @@ -22,9 +54,6 @@ typedef struct b2Manifold b2Manifold;
/// Return false if you want to disable the contact this step
typedef bool b2PreSolveFcn(b2ShapeId shapeIdA, b2ShapeId shapeIdB, b2Manifold* manifold, void* context);

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

/// Prototype callback for AABB queries.
/// See b2World_Query
/// Called for each shape found in the query AABB.
Expand All @@ -35,6 +64,7 @@ typedef bool b2QueryResultFcn(b2ShapeId shapeId, void* context);
/// See b2World::RayCast
/// Called for each shape found in the query. You control how the ray cast
/// proceeds by returning a float:
/// #todo rework this to return penetration
/// return -1: ignore this shape and continue
/// return 0: terminate the ray cast
/// return fraction: clip the ray to this point
Expand All @@ -46,13 +76,3 @@ typedef bool b2QueryResultFcn(b2ShapeId shapeId, void* context);
/// @return -1 to filter, 0 to terminate, fraction to clip the ray for
/// closest hit, 1 to continue
typedef float b2CastResultFcn(b2ShapeId shapeId, b2Vec2 point, b2Vec2 normal, float fraction, void* context);

/// Use an instance of this structure and the callback below to get the closest hit.
typedef struct b2RayResult
{
b2ShapeId shapeId;
b2Vec2 point;
b2Vec2 normal;
float fraction;
bool hit;
} b2RayResult;
7 changes: 1 addition & 6 deletions include/box2d/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@
#pragma once

#include "api.h"

/// Color for debug drawing. Each value has the range [0,1].
typedef struct b2Color
{
float r, g, b, a;
} b2Color;
#include "types.h"

/// All the colors! Credit to wherever I got this from, I forget.
typedef enum b2HexColor
Expand Down
2 changes: 1 addition & 1 deletion include/box2d/debug_draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#pragma once

#include "types.h"
#include "math_types.h"

/// This struct holds callbacks you can implement to draw a box2d world.
typedef struct b2DebugDraw
Expand Down
4 changes: 2 additions & 2 deletions include/box2d/distance.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

#include "api.h"
#include "constants.h"
#include "types.h"
#include "geometry.h"

/// Result of computing the distance between two line segments
typedef struct b2SegmentDistanceResult
Expand Down Expand Up @@ -79,7 +79,7 @@ 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
B2_API b2RayCastOutput b2ShapeCast(const b2ShapeCastPairInput* input);
B2_API b2CastOutput b2ShapeCast(const b2ShapeCastPairInput* input);

/// Make a proxy for use in GJK and related functions.
B2_API b2DistanceProxy b2MakeProxy(const b2Vec2* vertices, int32_t count, float radius);
Expand Down
7 changes: 6 additions & 1 deletion include/box2d/dynamic_tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@

#include "api.h"
#include "constants.h"
#include "types.h"
#include "math_types.h"

#include <stdbool.h>

#define b2_defaultCategoryBits (0x00000001)
#define b2_defaultMaskBits (0xFFFFFFFF)

typedef struct b2RayCastInput b2RayCastInput;
typedef struct b2ShapeCastInput b2ShapeCastInput;

/// A node in the dynamic tree. The user does not interact with this directly.
/// 16 + 16 + 8 + pad(8)
typedef struct b2TreeNode
Expand Down
47 changes: 36 additions & 11 deletions include/box2d/geometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,33 @@
#include "types.h"

typedef struct b2Hull b2Hull;
typedef struct b2CastOutput b2RayCastOutput;
typedef struct b2RayCastInput b2RayCastInput;

/// Low level ray-cast input data
typedef struct b2RayCastInput
{
b2Vec2 origin, translation;
float maxFraction;
} b2RayCastInput;

/// Low level shape cast input in generic form
typedef struct b2ShapeCastInput
{
b2Vec2 points[b2_maxPolygonVertices];
int32_t count;
float radius;
b2Vec2 translation;
float maxFraction;
} b2ShapeCastInput;

/// Low level ray-cast or shape-cast output data
typedef struct b2CastOutput
{
b2Vec2 normal;
b2Vec2 point;
float fraction;
int32_t iterations;
bool hit;
} b2CastOutput;

/// This holds the mass data computed for a shape.
typedef struct b2MassData
Expand Down Expand Up @@ -98,7 +123,7 @@ B2_API b2Polygon b2MakeRoundedBox(float hx, float hy, float radius);
/// Make an offset box, bypassing the need for a convex hull.
B2_API b2Polygon b2MakeOffsetBox(float hx, float hy, b2Vec2 center, float angle);

/// Transform a polygon. This is useful for transfering a shape from one body to another.
/// Transform a polygon. This is useful for transferring a shape from one body to another.
B2_API b2Polygon b2TransformPolygon(b2Transform transform, const b2Polygon* polygon);

/// Compute mass properties of a circle
Expand Down Expand Up @@ -132,26 +157,26 @@ B2_API bool b2PointInCapsule(b2Vec2 point, const b2Capsule* shape);
B2_API bool b2PointInPolygon(b2Vec2 point, const b2Polygon* shape);

/// Ray cast versus circle in shape local space. Initial overlap is treated as a miss.
B2_API b2RayCastOutput b2RayCastCircle(const b2RayCastInput* input, const b2Circle* shape);
B2_API b2CastOutput b2RayCastCircle(const b2RayCastInput* input, const b2Circle* shape);

/// Ray cast versus capsule in shape local space. Initial overlap is treated as a miss.
B2_API b2RayCastOutput b2RayCastCapsule(const b2RayCastInput* input, const b2Capsule* shape);
B2_API b2CastOutput b2RayCastCapsule(const b2RayCastInput* input, const b2Capsule* shape);

/// Ray cast versus segment in shape local space. Optionally treat the segment as one-sided with hits from
/// the left side being treated as a miss.
B2_API b2RayCastOutput b2RayCastSegment(const b2RayCastInput* input, const b2Segment* shape, bool oneSided);
B2_API b2CastOutput b2RayCastSegment(const b2RayCastInput* input, const b2Segment* shape, bool oneSided);

/// Ray cast versus polygon in shape local space. Initial overlap is treated as a miss.
B2_API b2RayCastOutput b2RayCastPolygon(const b2RayCastInput* input, const b2Polygon* shape);
B2_API b2CastOutput b2RayCastPolygon(const b2RayCastInput* input, const b2Polygon* shape);

/// Shape cast versus a circle. Initial overlap is treated as a miss.
B2_API b2RayCastOutput b2ShapeCastCircle(const b2ShapeCastInput* input, const b2Circle* shape);
B2_API b2CastOutput b2ShapeCastCircle(const b2ShapeCastInput* input, const b2Circle* shape);

/// Shape cast versus a capsule. Initial overlap is treated as a miss.
B2_API b2RayCastOutput b2ShapeCastCapsule(const b2ShapeCastInput* input, const b2Capsule* shape);
B2_API b2CastOutput b2ShapeCastCapsule(const b2ShapeCastInput* input, const b2Capsule* shape);

/// Shape cast versus a line segment. Initial overlap is treated as a miss.
B2_API b2RayCastOutput b2ShapeCastSegment(const b2ShapeCastInput* input, const b2Segment* shape);
B2_API b2CastOutput b2ShapeCastSegment(const b2ShapeCastInput* input, const b2Segment* shape);

/// Shape cast versus a convex polygon. Initial overlap is treated as a miss.
B2_API b2RayCastOutput b2ShapeCastPolygon(const b2ShapeCastInput* input, const b2Polygon* shape);
B2_API b2CastOutput b2ShapeCastPolygon(const b2ShapeCastInput* input, const b2Polygon* shape);
15 changes: 0 additions & 15 deletions include/box2d/joint_util.h

This file was deleted.

3 changes: 2 additions & 1 deletion include/box2d/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
#pragma once

#include "api.h"
#include "types.h"
#include "math_types.h"

#include <float.h>
#include <math.h>
#include <stdbool.h>

/// Macro to get the minimum of two values
#define B2_MIN(A, B) ((A) < (B) ? (A) : (B))
Expand Down
Loading

0 comments on commit 5db873f

Please sign in to comment.