Skip to content

Commit

Permalink
Fixes and improvements (erincatto#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto authored Apr 26, 2024
1 parent e2504c7 commit b7722b0
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 52 deletions.
11 changes: 9 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ jobs:
- uses: actions/checkout@v4

- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DBOX2D_SAMPLES=OFF -DBOX2D_SANITIZE=ON -DBUILD_SHARED_LIBS=OFF
# some problem with simde
# run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DBOX2D_SAMPLES=OFF -DBOX2D_SANITIZE=ON -DBUILD_SHARED_LIBS=OFF
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DBOX2D_SAMPLES=OFF -DBUILD_SHARED_LIBS=OFF

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
Expand All @@ -64,10 +66,15 @@ jobs:
build-windows:
name: windows
runs-on: windows-latest

steps:

- uses: actions/checkout@v4

- name: Setup MSVC dev command prompt
uses: TheMrMilchmann/setup-msvc-dev@v3
with:
arch: x64

- name: Configure CMake
# enkiTS is failing ASAN on windows
# run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DBOX2D_SAMPLES=OFF -DBOX2D_SANITIZE=ON -DBUILD_SHARED_LIBS=OFF
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if (MSVC OR APPLE)
if(MSVC)
# address sanitizer only in the debug build
add_compile_options("$<$<CONFIG:Debug>:/fsanitize=address>")
add_link_options("$<$<CONFIG:Debug>:/INCREMENTAL:NO>")
elseif(APPLE)
# more sanitizers on Apple clang
# add_compile_options(-fsanitize=thread -fno-omit-frame-pointer)
Expand Down
3 changes: 3 additions & 0 deletions benchmark/joint_grid.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

b2WorldId JointGrid(b2WorldDef* worldDef)
{
// Turning gravity off to isolate joint performance.
worldDef->gravity = b2Vec2_zero;

b2WorldId worldId = b2CreateWorld(worldDef);

#ifdef NDEBUG
Expand Down
7 changes: 4 additions & 3 deletions benchmark/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-FileCopyrightText: 2024 Erin Catto
// SPDX-License-Identifier: MIT

#define _CRT_SECURE_NO_WARNINGS

#include "TaskScheduler_c.h"

#include "box2d/box2d.h"
Expand Down Expand Up @@ -194,9 +196,8 @@ int main(int argc, char** argv)

char fileName[64] = {0};
snprintf(fileName, 64, "%s.csv", benchmarks[benchmarkIndex].name);
FILE* file = NULL;
errno_t status = fopen_s(&file, fileName, "w");
if (status != 0 || file == NULL)
FILE* file = fopen(fileName, "w");
if (file == NULL)
{
continue;
}
Expand Down
3 changes: 3 additions & 0 deletions include/box2d/box2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,9 @@ B2_API float b2PrismaticJoint_GetConstraintTorque(b2JointId jointId);

/// Revolute Joint

/// Get the current joint angle in radians relative to the reference angle
B2_API float b2RevoluteJoint_GetAngle(b2JointId jointId);

/// Enable/disable a revolute joint limit.
B2_API void b2RevoluteJoint_EnableLimit(b2JointId jointId, bool enableLimit);

Expand Down
6 changes: 3 additions & 3 deletions include/box2d/debug_draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ typedef struct b2DebugDraw
void (*DrawSolidPolygon)(const b2Vec2* vertices, int vertexCount, b2Color color, void* context);

/// Draw a rounded polygon provided in CCW order.
void (*DrawRoundedPolygon)(const b2Vec2* vertices, int vertexCount, float radius, b2Color lineColor, b2Color fillColor, void* context);
void (*DrawRoundedPolygon)(const b2Vec2* vertices, int vertexCount, float radius, b2Color color, void* context);

/// Draw a circle.
void (*DrawCircle)(b2Vec2 center, float radius, b2Color color, void* context);
Expand All @@ -33,8 +33,7 @@ typedef struct b2DebugDraw
void (*DrawSegment)(b2Vec2 p1, b2Vec2 p2, b2Color color, void* context);

/// Draw a transform. Choose your own length scale.
/// @param xf a transform.
void (*DrawTransform)(b2Transform xf, void* context);
void (*DrawTransform)(b2Transform transform, void* context);

/// Draw a point.
void (*DrawPoint)(b2Vec2 p, float size, b2Color color, void* context);
Expand All @@ -44,6 +43,7 @@ typedef struct b2DebugDraw

bool drawShapes;
bool drawJoints;
bool drawJointExtras;
bool drawAABBs;
bool drawMass;
bool drawContacts;
Expand Down
15 changes: 15 additions & 0 deletions include/box2d/math_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#pragma once

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

#include <math.h>
Expand Down Expand Up @@ -307,6 +308,20 @@ B2_INLINE float b2RelativeAngle(b2Rot b, b2Rot a)
return atan2f(s, c);
}

B2_INLINE float b2UnwindAngle(float angle)
{
if (angle < -b2_pi)
{
return angle + 2.0f * b2_pi;
}
else if (angle > b2_pi)
{
return angle - 2.0f * b2_pi;
}

return angle;
}

/// Rotate a vector
B2_INLINE b2Vec2 b2RotateVector(b2Rot q, b2Vec2 v)
{
Expand Down
28 changes: 15 additions & 13 deletions samples/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,10 +824,10 @@ void DrawSolidPolygonFcn(const b2Vec2* vertices, int vertexCount, b2Color color,
static_cast<Draw*>(context)->DrawSolidPolygon(vertices, vertexCount, color);
}

void DrawRoundedPolygonFcn(const b2Vec2* vertices, int32_t vertexCount, float radius, b2Color fillColor, b2Color lineColor,
void DrawRoundedPolygonFcn(const b2Vec2* vertices, int32_t vertexCount, float radius, b2Color color,
void* context)
{
static_cast<Draw*>(context)->DrawRoundedPolygon(vertices, vertexCount, radius, fillColor, lineColor);
static_cast<Draw*>(context)->DrawRoundedPolygon(vertices, vertexCount, radius, color);
}

void DrawCircleFcn(b2Vec2 center, float radius, b2Color color, void* context)
Expand Down Expand Up @@ -911,6 +911,7 @@ void Draw::Create()
DrawStringFcn,
true, // shapes
true, // joints
false, // joint extras
false, // aabbs
false, // mass
false, // contacts
Expand Down Expand Up @@ -978,28 +979,29 @@ void Draw::DrawSolidPolygon(const b2Vec2* vertices, int32_t vertexCount, b2Color
// Outline needs 4*count triangles.
#define MAX_POLY_INDEXES (3 * (5 * MAX_POLY_VERTEXES - 2))

void Draw::DrawRoundedPolygon(const b2Vec2* vertices, int32_t count, float radius, b2Color fillColor, b2Color outlineColor)
void Draw::DrawRoundedPolygon(const b2Vec2* vertices, int32_t vertexCount, float radius, b2Color color)
{
assert(count <= MAX_POLY_VERTEXES);
assert(vertexCount <= MAX_POLY_VERTEXES);
b2Color fillColor = {0.5f * color.r, 0.5f * color.g, 0.5f * color.b, 0.5f};

RGBA8 fill = MakeRGBA8(fillColor);
RGBA8 outline = MakeRGBA8(outlineColor);
RGBA8 outline = MakeRGBA8(color);

uint16_t indices[MAX_POLY_INDEXES];

// Polygon fill triangles.
for (int i = 0; i < count - 2; ++i)
for (int i = 0; i < vertexCount - 2; ++i)
{
indices[3 * i + 0] = 0;
indices[3 * i + 1] = 4 * (i + 1);
indices[3 * i + 2] = 4 * (i + 2);
}

// Polygon outline triangles.
uint16_t* outlineIndices = indices + 3 * (count - 2);
for (int i0 = 0; i0 < count; ++i0)
uint16_t* outlineIndices = indices + 3 * (vertexCount - 2);
for (int i0 = 0; i0 < vertexCount; ++i0)
{
int i1 = (i0 + 1) % count;
int i1 = (i0 + 1) % vertexCount;
outlineIndices[12 * i0 + 0] = 4 * i0 + 0;
outlineIndices[12 * i0 + 1] = 4 * i0 + 1;
outlineIndices[12 * i0 + 2] = 4 * i0 + 2;
Expand Down Expand Up @@ -1029,12 +1031,12 @@ void Draw::DrawRoundedPolygon(const b2Vec2* vertices, int32_t count, float radiu
// float outset = radius + lineScale;
// float r = outset - inset;

Vertex* vertexes = m_roundedTriangles->AllocVertices(4 * count, indices, 3 * (5 * count - 2));
for (int i = 0; i < count; ++i)
Vertex* vertexes = m_roundedTriangles->AllocVertices(4 * vertexCount, indices, 3 * (5 * vertexCount - 2));
for (int i = 0; i < vertexCount; ++i)
{
b2Vec2 v_prev = vertices[(i + (count - 1)) % count];
b2Vec2 v_prev = vertices[(i + (vertexCount - 1)) % vertexCount];
b2Vec2 v0 = vertices[i];
b2Vec2 v_next = vertices[(i + (count + 1)) % count];
b2Vec2 v_next = vertices[(i + (vertexCount + 1)) % vertexCount];

b2Vec2 n1 = b2Normalize(b2CrossVS(b2Sub(v0, v_prev), 1.0f));
b2Vec2 n2 = b2Normalize(b2CrossVS(b2Sub(v_next, v0), 1.0f));
Expand Down
2 changes: 1 addition & 1 deletion samples/draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Draw

void DrawSolidPolygon(const b2Vec2* vertices, int32_t vertexCount, b2Color color);

void DrawRoundedPolygon(const b2Vec2* vertices, int32_t vertexCount, float radius, b2Color fillColor, b2Color outlineColor);
void DrawRoundedPolygon(const b2Vec2* vertices, int32_t vertexCount, float radius, b2Color color);

void DrawCircle(b2Vec2 center, float radius, b2Color color);

Expand Down
1 change: 1 addition & 0 deletions samples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ static void UpdateUI()

ImGui::Checkbox("Shapes", &s_settings.drawShapes);
ImGui::Checkbox("Joints", &s_settings.drawJoints);
ImGui::Checkbox("Joint Extras", &s_settings.drawJointExtras);
ImGui::Checkbox("AABBs", &s_settings.drawAABBs);
ImGui::Checkbox("Contact Points", &s_settings.drawContactPoints);
ImGui::Checkbox("Contact Normals", &s_settings.drawContactNormals);
Expand Down
1 change: 1 addition & 0 deletions samples/sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ void Sample::Step(Settings& settings)

g_draw.m_debugDraw.drawShapes = settings.drawShapes;
g_draw.m_debugDraw.drawJoints = settings.drawJoints;
g_draw.m_debugDraw.drawJointExtras = settings.drawJointExtras;
g_draw.m_debugDraw.drawAABBs = settings.drawAABBs;
g_draw.m_debugDraw.drawMass = settings.drawMass;
g_draw.m_debugDraw.drawContacts = settings.drawContactPoints;
Expand Down
19 changes: 19 additions & 0 deletions samples/sample_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1142,12 +1142,31 @@ class BenchmarkJointGrid : public Sample
}

free(bodies);

m_gravity = 10.0f;
}

void UpdateUI() override
{
ImGui::SetNextWindowPos(ImVec2(10.0f, 500.0f), ImGuiCond_Once);
ImGui::SetNextWindowSize(ImVec2(240.0f, 100.0f));
ImGui::Begin("Joint Grid", nullptr, ImGuiWindowFlags_NoResize);

if (ImGui::SliderFloat("gravity", &m_gravity, 0.0f, 20.0f, "%.1f"))
{
b2World_SetGravity(m_worldId, {0.0f, -m_gravity});
}

ImGui::End();
}


static Sample* Create(Settings& settings)
{
return new BenchmarkJointGrid(settings);
}

float m_gravity;
};

static int benchmarkJointGridIndex = RegisterSample("Benchmark", "Joint Grid", BenchmarkJointGrid::Create);
Expand Down
26 changes: 13 additions & 13 deletions samples/sample_collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ class RayCastWorld : public Sample
}
else
{
g_draw.DrawRoundedPolygon(points, box.count, box.radius, yellow, yellow);
g_draw.DrawRoundedPolygon(points, box.count, box.radius, yellow);
}
}
}
Expand Down Expand Up @@ -1840,7 +1840,7 @@ class RayCastWorld : public Sample
}
else
{
g_draw.DrawRoundedPolygon(points, box.count, box.radius, yellow, yellow);
g_draw.DrawRoundedPolygon(points, box.count, box.radius, yellow);
}
}
}
Expand Down Expand Up @@ -2434,8 +2434,8 @@ class Manifold : public Sample

b2Color color1 = {0.3f, 0.8f, 0.6f, 1.0f};
b2Color color2 = {0.8f, 0.6f, 0.3f, 1.0f};
b2Color fillColor1 = {0.5f * color1.r, 0.5f * color1.g, 0.5f * color1.b, 0.5f};
b2Color fillColor2 = {0.5f * color2.r, 0.5f * color2.g, 0.5f * color2.b, 0.5f};
//b2Color fillColor1 = {0.5f * color1.r, 0.5f * color1.g, 0.5f * color1.b, 0.5f};
//b2Color fillColor2 = {0.5f * color2.r, 0.5f * color2.g, 0.5f * color2.b, 0.5f};

b2Color dim1 = {0.5f * color1.r, 0.5f * color1.g, 0.5f * color1.b, 1.0f};

Expand Down Expand Up @@ -2546,7 +2546,7 @@ class Manifold : public Sample
{
vertices[i] = b2TransformPoint(xf1, box.vertices[i]);
}
g_draw.DrawRoundedPolygon(vertices, box.count, m_round, fillColor1, color1);
g_draw.DrawRoundedPolygon(vertices, box.count, m_round, color1);

b2Vec2 c2 = b2TransformPoint(xf2, circle.center);
b2Vec2 axis2 = b2RotateVector(xf2.q, {1.0f, 0.0f});
Expand Down Expand Up @@ -2687,7 +2687,7 @@ class Manifold : public Sample
{
vertices[i] = b2TransformPoint(xf2, rox.vertices[i]);
}
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, fillColor2, color2);
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, color2);

DrawManifold(&m);

Expand All @@ -2711,13 +2711,13 @@ class Manifold : public Sample
{
vertices[i] = b2TransformPoint(xf1, rox.vertices[i]);
}
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, fillColor1, color1);
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, color1);

for (int i = 0; i < rox.count; ++i)
{
vertices[i] = b2TransformPoint(xf2, rox.vertices[i]);
}
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, fillColor2, color2);
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, color2);

DrawManifold(&m);

Expand Down Expand Up @@ -2748,7 +2748,7 @@ class Manifold : public Sample

if (m_round > 0.0f)
{
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, fillColor2, color2);
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, color2);
}
else
{
Expand All @@ -2775,13 +2775,13 @@ class Manifold : public Sample
{
vertices[i] = b2TransformPoint(xf1, wox.vertices[i]);
}
g_draw.DrawRoundedPolygon(vertices, wox.count, wox.radius, fillColor1, color1);
g_draw.DrawRoundedPolygon(vertices, wox.count, wox.radius, color1);

for (int i = 0; i < wox.count; ++i)
{
vertices[i] = b2TransformPoint(xf2, wox.vertices[i]);
}
g_draw.DrawRoundedPolygon(vertices, wox.count, wox.radius, fillColor2, color2);
g_draw.DrawRoundedPolygon(vertices, wox.count, wox.radius, color2);

DrawManifold(&m);

Expand Down Expand Up @@ -2866,7 +2866,7 @@ class Manifold : public Sample

if (m_round > 0.0f)
{
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, fillColor2, color2);
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, color2);
g_draw.DrawPolygon(vertices, rox.count, color2);
}
else
Expand Down Expand Up @@ -3218,7 +3218,7 @@ class SmoothManifold : public Sample

if (m_round > 0.0f)
{
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, fillColor2, color2);
g_draw.DrawRoundedPolygon(vertices, rox.count, rox.radius, color2);
}
else
{
Expand Down
Loading

0 comments on commit b7722b0

Please sign in to comment.