Skip to content
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
3 changes: 3 additions & 0 deletions docs/migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ I'm not going to cover all the details of v3.0 in this guide. That is the job of

The surface area of the Box2D is smaller in v3.0 because C++ is not good at hiding details. So hopefully you find the new API easier to work with.

### Should I upgrade to Version 3?
Since the behavior changed from version 2 to version 3, I recommend to only use version 3 for new projects. Version 2 no longer receives updates, but it is already battle tested. Version 3 is good for projects that need high performance.

### Creating a world
Version 2.4:
```cpp
Expand Down
15 changes: 11 additions & 4 deletions include/box2d/box2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ B2_API void b2Body_ApplyTorque( b2BodyId bodyId, float torque, bool wake );
/// @param bodyId The body id
void b2Body_ClearForces( b2BodyId bodyId );

/// Apply an impulse at a point. This immediately modifies the velocity.
/// 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 optionally wakes the body.
/// The impulse is ignored if the body is not awake.
Expand Down Expand Up @@ -368,7 +368,7 @@ B2_API void b2Body_SetMassData( b2BodyId bodyId, b2MassData massData );
/// Get the mass data for a body
B2_API b2MassData b2Body_GetMassData( b2BodyId 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.
Expand Down Expand Up @@ -686,7 +686,15 @@ B2_API b2MassData b2Shape_ComputeMassData( b2ShapeId shapeId );
/// todo need sample
B2_API b2Vec2 b2Shape_GetClosestPoint( b2ShapeId shapeId, b2Vec2 target );

B2_API void b2Shape_ApplyWindForce( b2ShapeId shapeId, b2Vec2 wind, float drag, float lift, bool wake );
/// Apply a wind force to the body for this shape using the density of air. This considers
/// the projected area of the shape in the wind direction. This also considers
/// the relative velocity of the shape.
/// @param shapeId the shape id
/// @param wind the wind velocity in world space
/// @param drag the drag coefficient, the force that opposes the relative velocity
/// @param lift the lift coefficient, the force that is perpendicular to the relative velocity
/// @param wake should this wake the body
B2_API void b2Shape_ApplyWind( b2ShapeId shapeId, b2Vec2 wind, float drag, float lift, bool wake );

/// Chain Shape

Expand Down Expand Up @@ -806,7 +814,6 @@ B2_API void b2Joint_SetTorqueThreshold( b2JointId jointId, float threshold );
/// Get the torque threshold for joint events (N-m)
B2_API float b2Joint_GetTorqueThreshold( b2JointId jointId );


/**
* @defgroup distance_joint Distance Joint
* @brief Functions for the distance joint.
Expand Down
4 changes: 2 additions & 2 deletions include/box2d/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -1341,7 +1341,7 @@ typedef struct b2DebugDraw
void ( *DrawSolidCapsuleFcn )( b2Vec2 p1, b2Vec2 p2, float radius, b2HexColor color, void* context );

/// Draw a line segment.
void ( *DrawSegmentFcn )( b2Vec2 p1, b2Vec2 p2, b2HexColor color, void* context );
void ( *DrawLineFcn )( b2Vec2 p1, b2Vec2 p2, b2HexColor color, void* context );

/// Draw a transform. Choose your own length scale.
void ( *DrawTransformFcn )( b2Transform transform, void* context );
Expand Down Expand Up @@ -1380,7 +1380,7 @@ typedef struct b2DebugDraw
bool drawBodyNames;

/// Option to draw contact points
bool drawContacts;
bool drawContactPoints;

/// Option to visualize the graph coloring used for contacts and joints
bool drawGraphColors;
Expand Down
11 changes: 8 additions & 3 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,13 @@ target_include_directories(jsmn INTERFACE ${JSMN_DIR})
set(BOX2D_SAMPLE_FILES
car.cpp
car.h
container.c
container.h
donut.cpp
donut.h
doohickey.cpp
doohickey.h
draw.cpp
draw.c
draw.h
main.cpp
sample.cpp
Expand All @@ -93,8 +95,11 @@ set(BOX2D_SAMPLE_FILES
sample_shapes.cpp
sample_stacking.cpp
sample_world.cpp
shader.cpp
shader.h)
shader.c
shader.h
stb_image_write.h
stb_truetype.h
)

add_executable(samples ${BOX2D_SAMPLE_FILES})

Expand Down
25 changes: 25 additions & 0 deletions samples/container.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT

#if defined( _MSC_VER ) && !defined( _CRT_SECURE_NO_WARNINGS )
#define _CRT_SECURE_NO_WARNINGS
#endif

#include "container.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

void* GrowAlloc( void* oldMem, int oldSize, int newSize )
{
assert( newSize > oldSize );
void* newMem = malloc( newSize );
if ( oldSize > 0 )
{
memcpy( newMem, oldMem, oldSize );
free( oldMem );
}

return newMem;
}
106 changes: 106 additions & 0 deletions samples/container.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT

#pragma once

#define NULL_INDEX -1

// Array declaration. Works with forward declaration of TYPE.
#define ARRAY_DECLARE( TYPE ) \
typedef struct \
{ \
TYPE* data; \
int count; \
int capacity; \
} TYPE##Array; \
TYPE##Array TYPE##Array_Create( int capacity ); \
void TYPE##Array_Reserve( TYPE##Array* a, int newCapacity ); \
void TYPE##Array_Destroy( TYPE##Array* a )

// Inline array functions that need the TYPE to be defined.
#define ARRAY_INLINE( TYPE ) \
static inline void TYPE##Array_Resize( TYPE##Array* a, int count ) \
{ \
TYPE##Array_Reserve( a, count ); \
a->count = count; \
} \
static inline TYPE* TYPE##Array_Get( TYPE##Array* a, int index ) \
{ \
assert( 0 <= index && index < a->count ); \
return a->data + index; \
} \
static inline TYPE* TYPE##Array_Add( TYPE##Array* a ) \
{ \
if ( a->count == a->capacity ) \
{ \
int newCapacity = a->capacity < 2 ? 2 : a->capacity + ( a->capacity >> 1 ); \
TYPE##Array_Reserve( a, newCapacity ); \
} \
a->count += 1; \
return a->data + ( a->count - 1 ); \
} \
static inline void TYPE##Array_Push( TYPE##Array* a, TYPE value ) \
{ \
if ( a->count == a->capacity ) \
{ \
int newCapacity = a->capacity < 2 ? 2 : a->capacity + ( a->capacity >> 1 ); \
TYPE##Array_Reserve( a, newCapacity ); \
} \
a->data[a->count] = value; \
a->count += 1; \
} \
static inline void TYPE##Array_Set( TYPE##Array* a, int index, TYPE value ) \
{ \
assert( 0 <= index && index < a->count ); \
a->data[index] = value; \
} \
static inline int TYPE##Array_RemoveSwap( TYPE##Array* a, int index ) \
{ \
assert( 0 <= index && index < a->count ); \
int movedIndex = NULL_INDEX; \
if ( index != a->count - 1 ) \
{ \
movedIndex = a->count - 1; \
a->data[index] = a->data[movedIndex]; \
} \
a->count -= 1; \
return movedIndex; \
} \
static inline TYPE TYPE##Array_Pop( TYPE##Array* a ) \
{ \
assert( a->count > 0 ); \
TYPE value = a->data[a->count - 1]; \
a->count -= 1; \
return value; \
} \

// These functions go in the source file
#define ARRAY_SOURCE( TYPE ) \
TYPE##Array TYPE##Array_Create( int capacity ) \
{ \
TYPE##Array a = { 0 }; \
if ( capacity > 0 ) \
{ \
a.data = malloc( capacity * sizeof( TYPE ) ); \
a.capacity = capacity; \
} \
return a; \
} \
void TYPE##Array_Reserve( TYPE##Array* a, int newCapacity ) \
{ \
if ( newCapacity <= a->capacity ) \
{ \
return; \
} \
a->data = GrowAlloc( a->data, a->capacity * sizeof( TYPE ), newCapacity * sizeof( TYPE ) ); \
a->capacity = newCapacity; \
} \
void TYPE##Array_Destroy( TYPE##Array* a ) \
{ \
free( a->data ); \
a->data = NULL; \
a->count = 0; \
a->capacity = 0; \
}

void* GrowAlloc( void* oldMem, int oldSize, int newSize );
16 changes: 16 additions & 0 deletions samples/data/font.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT

#version 330 core

in vec4 color;
in vec2 uv;

uniform sampler2D FontAtlas;

out vec4 fragColor;

void main()
{
fragColor = vec4(color.rgb, color.a * texture(FontAtlas, uv).r);
}
21 changes: 21 additions & 0 deletions samples/data/font.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT

#version 330 core

layout (location = 0) in vec2 aPosition;
layout (location = 1) in vec2 aUV;
layout (location = 2) in vec4 aColor;

out vec4 color;
out vec2 uv;

uniform mat4 ProjectionMatrix;

void main()
{
gl_Position = ProjectionMatrix * vec4(aPosition, 0.0, 1.0);

color = aColor;
uv = aUV;
}
12 changes: 12 additions & 0 deletions samples/data/line.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT

#version 330

in vec4 f_color;
out vec4 color;

void main(void)
{
color = f_color;
}
16 changes: 16 additions & 0 deletions samples/data/line.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: 2024 Erin Catto
// SPDX-License-Identifier: MIT

#version 330

uniform mat4 projectionMatrix;
layout(location = 0) in vec2 v_position;
layout(location = 1) in vec4 v_color;

out vec4 f_color;

void main(void)
{
f_color = v_color;
gl_Position = projectionMatrix * vec4(v_position, 0.0f, 1.0f);
}
12 changes: 12 additions & 0 deletions samples/data/point.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-License-Identifier: MIT

#version 330

in vec4 f_color;
out vec4 color;

void main(void)
{
color = f_color;
}
18 changes: 18 additions & 0 deletions samples/data/point.vs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-FileCopyrightText: 2024 Erin Catto
// SPDX-License-Identifier: MIT

#version 330

uniform mat4 projectionMatrix;
layout(location = 0) in vec2 v_position;
layout(location = 1) in float v_size;
layout(location = 2) in vec4 v_color;

out vec4 f_color;

void main(void)
{
f_color = v_color;
gl_Position = projectionMatrix * vec4(v_position, 0.0f, 1.0f);
gl_PointSize = v_size;
}
Loading