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
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public override void UpdateGui()
base.UpdateGui();

float fontSize = ImGui.GetFontSize();
float height = 6.0f * fontSize;
float height = 10.0f * fontSize;
ImGui.SetNextWindowPos(new Vector2(0.5f * fontSize, m_camera.m_height - height - 2.0f * fontSize), ImGuiCond.Once);
ImGui.SetNextWindowSize(new Vector2(15.0f * fontSize, height));

Expand Down
91 changes: 91 additions & 0 deletions src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkBarrel24.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using static Box2D.NET.B2Geometries;
using static Box2D.NET.B2Types;
using static Box2D.NET.B2MathFunction;
using static Box2D.NET.B2Bodies;
using static Box2D.NET.B2Shapes;

namespace Box2D.NET.Samples.Samples.Benchmarks;

// This is used to compare performance with Box2D v2.4
public class BenchmarkBarrel24 : Sample
{
private static readonly int benchmarkBarrel24 = SampleFactory.Shared.RegisterSample("Benchmark", "Barrel 2.4", Create);

private static Sample Create(SampleContext context)
{
return new BenchmarkBarrel24(context);
}

public BenchmarkBarrel24(SampleContext context) : base(context)
{
if (m_context.settings.restart == false)
{
m_context.camera.m_center = new B2Vec2(8.0f, 53.0f);
m_context.camera.m_zoom = 25.0f * 2.35f;
}

float groundSize = 25.0f;

{
B2BodyDef bodyDef = b2DefaultBodyDef();
B2BodyId groundId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeBox(groundSize, 1.2f);
B2ShapeDef shapeDef = b2DefaultShapeDef();
b2CreatePolygonShape(groundId, ref shapeDef, ref box);

bodyDef.rotation = b2MakeRot(0.5f * B2_PI);
bodyDef.position = new B2Vec2(groundSize, 2.0f * groundSize);
groundId = b2CreateBody(m_worldId, ref bodyDef);

box = b2MakeBox(2.0f * groundSize, 1.2f);
b2CreatePolygonShape(groundId, ref shapeDef, ref box);

bodyDef.position = new B2Vec2(-groundSize, 2.0f * groundSize);
groundId = b2CreateBody(m_worldId, ref bodyDef);
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
}

int num = 26;
float rad = 0.5f;

float shift = rad * 2.0f;
float centerx = shift * num / 2.0f;
float centery = shift / 2.0f;

{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;

B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.density = 1.0f;
shapeDef.material.friction = 0.5f;

B2Polygon cuboid = b2MakeSquare(0.5f);

// b2Polygon top = b2MakeOffsetBox(0.8f, 0.2f, {0.0f, 0.8f}, 0.0f);
// b2Polygon leftLeg = b2MakeOffsetBox(0.2f, 0.5f, {-0.6f, 0.5f}, 0.0f);
// b2Polygon rightLeg = b2MakeOffsetBox(0.2f, 0.5f, {0.6f, 0.5f}, 0.0f);

#if DEBUG
int numj = 5;
#else
int numj = 5 * num;
#endif
for (int i = 0; i < num; ++i)
{
float x = i * shift - centerx;

for (int j = 0; j < numj; ++j)
{
float y = j * shift + centery + 2.0f;

bodyDef.position = new B2Vec2(x, y);

B2BodyId bodyId = b2CreateBody(m_worldId, ref bodyDef);
b2CreatePolygonShape(bodyId, ref shapeDef, ref cuboid);
}
}
}
}
}
95 changes: 95 additions & 0 deletions src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkCapacity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using static Box2D.NET.B2Geometries;
using static Box2D.NET.B2Types;
using static Box2D.NET.B2Bodies;
using static Box2D.NET.B2Shapes;
using static Box2D.NET.B2Worlds;

namespace Box2D.NET.Samples.Samples.Benchmarks;

public class BenchmarkCapacity : Sample
{
private static readonly int benchmarkCapacity = SampleFactory.Shared.RegisterSample("Benchmark", "Capacity", Create);

private B2Polygon m_square;
private int m_reachCount;
private bool m_done;

private static Sample Create(SampleContext context)
{
return new BenchmarkCapacity(context);
}

public BenchmarkCapacity(SampleContext context) : base(context)
{
if (m_context.settings.restart == false)
{
m_context.camera.m_center = new B2Vec2(0.0f, 150.0f);
m_context.camera.m_zoom = 200.0f;
}

{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.position.Y = -5.0f;
B2BodyId groundId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeBox(800.0f, 5.0f);
B2ShapeDef shapeDef = b2DefaultShapeDef();
b2CreatePolygonShape(groundId, ref shapeDef, ref box);
}

m_square = b2MakeSquare(0.5f);
m_done = false;
m_reachCount = 0;
}

public override void Step()
{
base.Step();

float millisecondLimit = 20.0f;

B2Profile profile = b2World_GetProfile(m_worldId);
if (profile.step > millisecondLimit)
{
m_reachCount += 1;
if (m_reachCount > 60)
{
// Hit the millisecond limit 60 times in a row
m_done = true;
}
}
else
{
m_reachCount = 0;
}

if (m_done == true)
{
return;
}

if ((m_stepCount & 0x1F) != 0x1F)
{
return;
}

B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position.Y = 200.0f;

B2ShapeDef shapeDef = b2DefaultShapeDef();

int count = 200;
float x = -1.0f * count;
for (int i = 0; i < count; ++i)
{
bodyDef.position.X = x;
bodyDef.position.Y += 0.5f;

B2BodyId bodyId = b2CreateBody(m_worldId, ref bodyDef);
b2CreatePolygonShape(bodyId, ref shapeDef, ref m_square);

x += 2.0f;
}
}
}
15 changes: 13 additions & 2 deletions src/Box2D.NET.Samples/Samples/Benchmarks/BenchmarkSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,25 @@ public BenchmarkSensor(SampleContext context) : base(context)
shapeDef.enableSensorEvents = true;

float yStart = 10.0f;

m_filterRow = m_rowCount >> 1;

for (int j = 0; j < m_rowCount; ++j)
{
m_passiveSensors[j] = new ShapeUserData();
m_passiveSensors[j].row = j;
m_passiveSensors[j].active = false;
shapeDef.userData = m_passiveSensors[j];

if ( j == m_filterRow )
{
shapeDef.enableCustomFiltering = true;
shapeDef.material.customColor = (uint)B2HexColor.b2_colorFuchsia;
}
else
{
shapeDef.enableCustomFiltering = false;
shapeDef.material.customColor = 0;
}

float y = j * shift + yStart;
for (int i = 0; i < m_columnCount; ++i)
Expand All @@ -104,7 +116,6 @@ public BenchmarkSensor(SampleContext context) : base(context)
m_maxBeginCount = 0;
m_maxEndCount = 0;
m_lastStepCount = 0;
m_filterRow = m_rowCount >> 1;
}

void CreateRow(float y)
Expand Down
2 changes: 1 addition & 1 deletion src/Box2D.NET.Samples/Samples/Continuous/ChainDrop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void Launch()
B2Circle circle = new B2Circle(new B2Vec2(0.0f, 0.0f), 0.5f);
m_shapeId = b2CreateCircleShape(m_bodyId, ref shapeDef, ref circle);

//b2Capsule capsule = { { -0.5f, 0.0f }, { 0.5f, 0.0 }, 0.25f };
//b2Capsule capsule = { { -0.5f, 0.0f }, { 0.5f, 0.0f }, 0.25f };
//m_shapeId = b2CreateCapsuleShape( m_bodyId, &shapeDef, &capsule );

//float h = 0.5f;
Expand Down
4 changes: 2 additions & 2 deletions src/Box2D.NET.Samples/Samples/Events/JointEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ public JointEvent(SampleContext context) : base(context)
[email protected] = groundId;
[email protected] = bodyId;
[email protected] = position;
jointDef.maxForce = 1000.0f;
jointDef.maxTorque = 20.0f;
jointDef.maxVelocityForce = 1000.0f;
jointDef.maxVelocityTorque = 20.0f;
[email protected] = forceThreshold;
[email protected] = torqueThreshold;
[email protected] = true;
Expand Down
4 changes: 2 additions & 2 deletions src/Box2D.NET.Samples/Samples/Joints/BreakableJoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ public BreakableJoint(SampleContext context) : base(context)
[email protected] = groundId;
[email protected] = bodyId;
[email protected] = position;
jointDef.maxForce = 1000.0f;
jointDef.maxTorque = 20.0f;
jointDef.maxVelocityForce = 1000.0f;
jointDef.maxVelocityTorque = 20.0f;
[email protected] = true;
m_jointIds[index] = b2CreateMotorJoint(m_worldId, ref jointDef);
}
Expand Down
35 changes: 30 additions & 5 deletions src/Box2D.NET.Samples/Samples/Joints/DistanceJoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class DistanceJoint : Sample
private float m_hertz;
private float m_dampingRatio;
private float m_length;
private float m_tensionForce;
private float m_compressionForce;
private float m_minLength;
private float m_maxLength;
private bool m_enableSpring;
Expand All @@ -51,11 +53,13 @@ public DistanceJoint(SampleContext context) : base(context)
}

m_count = 0;
m_hertz = 2.0f;
m_hertz = 5.0f;
m_dampingRatio = 0.5f;
m_length = 1.0f;
m_minLength = m_length;
m_maxLength = m_length;
m_tensionForce = 2000.0f;
m_compressionForce = 100.0f;
m_enableSpring = false;
m_enableLimit = false;

Expand Down Expand Up @@ -97,6 +101,8 @@ void CreateScene(int newCount)
jointDef.hertz = m_hertz;
jointDef.dampingRatio = m_dampingRatio;
jointDef.length = m_length;
jointDef.lowerSpringForce = -m_tensionForce;
jointDef.upperSpringForce = m_compressionForce;
jointDef.minLength = m_minLength;
jointDef.maxLength = m_maxLength;
jointDef.enableSpring = m_enableSpring;
Expand All @@ -107,7 +113,7 @@ void CreateScene(int newCount)
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.angularDamping = 0.1f;
bodyDef.angularDamping = 1.0f;
bodyDef.position = new B2Vec2(m_length * (i + 1.0f), yOffset);
m_bodyIds[i] = b2CreateBody(m_worldId, ref bodyDef);
b2CreateCircleShape(m_bodyIds[i], ref shapeDef, ref circle);
Expand All @@ -129,12 +135,12 @@ public override void UpdateGui()
base.UpdateGui();

float fontSize = ImGui.GetFontSize();
float height = 240.0f;
float height = 20.0f * fontSize;
ImGui.SetNextWindowPos(new Vector2(0.5f * fontSize, m_camera.m_height - height - 2.0f * fontSize), ImGuiCond.Once);
ImGui.SetNextWindowSize(new Vector2(180.0f, height));
ImGui.SetNextWindowSize(new Vector2(18.0f * fontSize, height));

ImGui.Begin("Distance Joint", ImGuiWindowFlags.NoResize);
ImGui.PushItemWidth(100.0f);
ImGui.PushItemWidth(10.0f * fontSize);

if (ImGui.SliderFloat("Length", ref m_length, 0.1f, 4.0f, "%3.1f"))
{
Expand All @@ -156,6 +162,25 @@ public override void UpdateGui()

if (m_enableSpring)
{

if ( ImGui.SliderFloat( "Tension", ref m_tensionForce, 0.0f, 4000.0f ) )
{
for ( int i = 0; i < m_count; ++i )
{
b2DistanceJoint_SetSpringForceRange( m_jointIds[i], -m_tensionForce, m_compressionForce );
b2Joint_WakeBodies( m_jointIds[i] );
}
}

if ( ImGui.SliderFloat( "Compression", ref m_compressionForce, 0.0f, 200.0f ) )
{
for ( int i = 0; i < m_count; ++i )
{
b2DistanceJoint_SetSpringForceRange( m_jointIds[i], -m_tensionForce, m_compressionForce );
b2Joint_WakeBodies( m_jointIds[i] );
}
}

if (ImGui.SliderFloat("Hertz", ref m_hertz, 0.0f, 15.0f, "%3.1f"))
{
for (int i = 0; i < m_count; ++i)
Expand Down
4 changes: 2 additions & 2 deletions src/Box2D.NET.Samples/Samples/Joints/MotionLocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public MotionLocks(SampleContext context) : base(context)
[email protected] = groundId;
[email protected] = m_bodyIds[index];
[email protected] = position;
jointDef.maxForce = 200.0f;
jointDef.maxTorque = 200.0f;
jointDef.maxVelocityForce = 200.0f;
jointDef.maxVelocityTorque = 200.0f;
b2CreateMotorJoint(m_worldId, ref jointDef);
}

Expand Down
Loading
Loading