Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes #852

Merged
merged 5 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ settings.ini
.DS_Store
CMakeUserPresets.json
.cache/
.idea/
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ project(box2d
)

# stuff to help debug cmake
# message(STATUS "cmake tool chain: ${CMAKE_TOOLCHAIN_FILE}")
# message(STATUS "cmake source dir: ${CMAKE_SOURCE_DIR}")
# message(STATUS "library postfix: ${CMAKE_DEBUG_POSTFIX}")
message(STATUS "CMake C compiler: ${CMAKE_C_COMPILER_ID}")
Expand Down Expand Up @@ -66,7 +67,7 @@ endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)

# set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(CMAKE_VERBOSE_MAKEFILE ON)

Expand Down
2 changes: 1 addition & 1 deletion docs/collision.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ semicircles that are connected by a rectangle.
```c
b2Capsule capsule;
capsule.center1 = (b2Vec2){1.0f, 1.0f};
capsule.center1 = (b2Vec2){2.0f, 3.0f};
capsule.center2 = (b2Vec2){2.0f, 3.0f};
capsule.radius = 0.25f;
```

Expand Down
2 changes: 1 addition & 1 deletion include/box2d/base.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ typedef struct b2Timer
{
#if defined( _WIN32 )
int64_t start;
#elif defined( __linux__ ) || defined( __APPLE__ )
#elif defined( __linux__ ) || defined( __APPLE__ ) || defined( __EMSCRIPTEN__ )
unsigned long long start_sec;
unsigned long long start_usec;
#else
Expand Down
4 changes: 3 additions & 1 deletion include/box2d/box2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -542,15 +542,17 @@ B2_API b2Filter b2Shape_GetFilter( b2ShapeId shapeId );
/// @see b2ShapeDef::filter
B2_API void b2Shape_SetFilter( b2ShapeId shapeId, b2Filter filter );

/// Enable sensor events for this shape. Only applies to kinematic and dynamic bodies. Ignored for sensors.
/// Enable sensor events for this shape. Only applies to kinematic and dynamic bodies.
/// @see b2ShapeDef::isSensor
/// @warning changing this at run-time may lead to lost begin/end events
B2_API void b2Shape_EnableSensorEvents( b2ShapeId shapeId, bool flag );

/// Returns true if sensor events are enabled
B2_API bool b2Shape_AreSensorEventsEnabled( b2ShapeId shapeId );

/// Enable contact events for this shape. Only applies to kinematic and dynamic bodies. Ignored for sensors.
/// @see b2ShapeDef::enableContactEvents
/// @warning changing this at run-time may lead to lost begin/end events
B2_API void b2Shape_EnableContactEvents( b2ShapeId shapeId, bool flag );

/// Returns true if contact events are enabled
Expand Down
6 changes: 0 additions & 6 deletions include/box2d/collision.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,7 @@

#include <stdbool.h>

typedef struct b2Circle b2Circle;
typedef struct b2Capsule b2Capsule;
typedef struct b2DistanceCache b2DistanceCache;
typedef struct b2Polygon b2Polygon;
typedef struct b2Segment b2Segment;
typedef struct b2ChainSegment b2ChainSegment;

typedef struct b2Hull b2Hull;

/**
Expand Down
103 changes: 57 additions & 46 deletions include/box2d/math_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef struct b2Vec2
} b2Vec2;

/// Cosine and sine pair
/// This uses a custom implementation designed for cross platform determinism
/// This uses a custom implementation designed for cross-platform determinism
typedef struct b2CosSin
{
/// cosine and sine
Expand Down Expand Up @@ -69,67 +69,71 @@ typedef struct b2AABB
*/

/// https://en.wikipedia.org/wiki/Pi
#define b2_pi 3.14159265359f
#define B2_PI 3.14159265359f

static const b2Vec2 b2Vec2_zero = { 0.0f, 0.0f };
static const b2Rot b2Rot_identity = { 1.0f, 0.0f };
static const b2Transform b2Transform_identity = { { 0.0f, 0.0f }, { 1.0f, 0.0f } };
static const b2Mat22 b2Mat22_zero = { { 0.0f, 0.0f }, { 0.0f, 0.0f } };

/// 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
B2_API float b2Atan2( float y, float x );

/// @return the minimum of two floats
B2_INLINE float b2MinFloat( float a, float b )
/// @return the minimum of two integers
B2_INLINE int b2MinInt( int a, int b )
{
return a < b ? a : b;
}

/// @return the maximum of two floats
B2_INLINE float b2MaxFloat( float a, float b )
/// @return the maximum of two integers
B2_INLINE int b2MaxInt( int a, int b )
{
return a > b ? a : b;
}

/// @return the absolute value of a float
B2_INLINE float b2AbsFloat( float a )
/// @return the absolute value of an integer
B2_INLINE int b2AbsInt( int a )
{
return a < 0 ? -a : a;
}

/// @return a float clamped between a lower and upper bound
B2_INLINE float b2ClampFloat( float a, float lower, float upper )
/// @return an integer clamped between a lower and upper bound
B2_INLINE int b2ClampInt( int a, int lower, int upper )
{
return a < lower ? lower : ( a > upper ? upper : a );
}

/// @return the minimum of two integers
B2_INLINE int b2MinInt( int a, int b )
/// @return the minimum of two floats
B2_INLINE float b2MinFloat( float a, float b )
{
return a < b ? a : b;
}

/// @return the maximum of two integers
B2_INLINE int b2MaxInt( int a, int b )
/// @return the maximum of two floats
B2_INLINE float b2MaxFloat( float a, float b )
{
return a > b ? a : b;
}

/// @return the absolute value of an integer
B2_INLINE int b2AbsInt( int a )
/// @return the absolute value of a float
B2_INLINE float b2AbsFloat( float a )
{
return a < 0 ? -a : a;
}

/// @return an integer clamped between a lower and upper bound
B2_INLINE int b2ClampInt( int a, int lower, int upper )
/// @return a float clamped between a lower and upper bound
B2_INLINE float b2ClampFloat( float a, float lower, float upper )
{
return a < lower ? lower : ( a > upper ? upper : a );
}

/// 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
B2_API float b2Atan2( float y, float x );

/// Compute the cosine and sine of an angle in radians. Implemented
/// for cross-platform determinism.
B2_API b2CosSin b2ComputeCosSin( float radians );

/// Vector dot product
B2_INLINE float b2Dot( b2Vec2 a, b2Vec2 b )
{
Expand Down Expand Up @@ -191,11 +195,11 @@ B2_INLINE b2Vec2 b2Lerp( b2Vec2 a, b2Vec2 b, float t )
return B2_LITERAL( b2Vec2 ){ ( 1.0f - t ) * a.x + t * b.x, ( 1.0f - t ) * a.y + t * b.y };
}

///// Component-wise multiplication
//B2_INLINE b2Vec2 b2Mul( b2Vec2 a, b2Vec2 b )
//{
// return B2_LITERAL( b2Vec2 ){ a.x * b.x, a.y * b.y };
//}
/// Component-wise multiplication
B2_INLINE b2Vec2 b2Mul( b2Vec2 a, b2Vec2 b )
{
return B2_LITERAL( b2Vec2 ){ a.x * b.x, a.y * b.y };
}

/// Multiply a scalar and vector
B2_INLINE b2Vec2 b2MulSV( float s, b2Vec2 v )
Expand Down Expand Up @@ -333,12 +337,9 @@ B2_INLINE float b2DistanceSquared( b2Vec2 a, b2Vec2 b )
}

/// Make a rotation using an angle in radians
B2_API b2CosSin b2ComputeCosSin( float angle );

/// Make a rotation using an angle in radians
B2_INLINE b2Rot b2MakeRot( float angle )
B2_INLINE b2Rot b2MakeRot( float radians )
{
b2CosSin cs = b2ComputeCosSin( angle );
b2CosSin cs = b2ComputeCosSin( radians );
return B2_LITERAL( b2Rot ){ cs.cosine, cs.sine };
}

Expand Down Expand Up @@ -444,34 +445,34 @@ B2_INLINE float b2RelativeAngle( b2Rot b, b2Rot a )
}

/// Convert an angle in the range [-2*pi, 2*pi] into the range [-pi, pi]
B2_INLINE float b2UnwindAngle( float angle )
B2_INLINE float b2UnwindAngle( float radians )
{
if ( angle < -b2_pi )
if ( radians < -B2_PI )
{
return angle + 2.0f * b2_pi;
return radians + 2.0f * B2_PI;
}
else if ( angle > b2_pi )
else if ( radians > B2_PI )
{
return angle - 2.0f * b2_pi;
return radians - 2.0f * B2_PI;
}

return angle;
return radians;
}

/// Convert any into the range [-pi, pi] (slow)
B2_INLINE float b2UnwindLargeAngle( float angle )
B2_INLINE float b2UnwindLargeAngle( float radians )
{
while ( angle > b2_pi )
while ( radians > B2_PI )
{
angle -= 2.0f * b2_pi;
radians -= 2.0f * B2_PI;
}

while ( angle < -b2_pi )
while ( radians < -B2_PI )
{
angle += 2.0f * b2_pi;
radians += 2.0f * B2_PI;
}

return angle;
return radians;
}

/// Rotate a vector
Expand Down Expand Up @@ -619,6 +620,16 @@ B2_API bool b2IsValidAABB( b2AABB aabb );
/// 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.
/// For example, if your game uses pixels for units you can use pixels for all length values
/// sent to Box2D. There should be no extra cost. However, Box2D has some internal tolerances
/// and thresholds that have been tuned for meters. By calling this function, Box2D is able
/// to adjust those tolerances and thresholds to improve accuracy.
/// A good rule of thumb is to pass the height of your player character to this function. So
/// if your player character is 32 pixels high, then pass 32 to this function. Then you may
/// confidently use pixels for all the length values sent to Box2D. All length values returned
/// from Box2D will also be pixels because Box2D does not do any scaling internally.
/// However, you are now on the hook for coming up with good values for gravity, density, and
/// forces.
/// @warning This must be modified before any calls to Box2D
B2_API void b2SetLengthUnitsPerMeter( float lengthUnits );

Expand Down
Loading
Loading