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
1 change: 0 additions & 1 deletion src/Box2D.NET.Samples/Draw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public Draw()
m_debugDraw.DrawStringFcn = DrawStringFcn;
m_debugDraw.drawingBounds = bounds;

m_debugDraw.useDrawingBounds = false;
m_debugDraw.drawShapes = true;
m_debugDraw.drawJoints = true;
m_debugDraw.drawJointExtras = false;
Expand Down
1 change: 0 additions & 1 deletion src/Box2D.NET.Samples/SampleApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,6 @@ private void OnWindowUpdate(double dt)
// #todo restore all drawing settings that may have been overridden by a sample
_context.settings.subStepCount = 4;
_context.settings.drawJoints = true;
_context.settings.useCameraBounds = true;

s_sample?.Dispose();
s_sample = null;
Expand Down
25 changes: 13 additions & 12 deletions src/Box2D.NET.Samples/Samples/Bodies/BodyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public BodyType(SampleContext context) : base(context)
B2BodyId groundId = b2_nullBodyId;
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.name = "ground";
groundId = b2CreateBody(m_worldId, ref bodyDef);

B2Segment segment = new B2Segment(new B2Vec2(-20.0f, 0.0f), new B2Vec2(20.0f, 0.0f));
Expand All @@ -59,6 +60,7 @@ public BodyType(SampleContext context) : base(context)
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position = new B2Vec2(-2.0f, 3.0f);
bodyDef.name = "attach1";
m_attachmentId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeBox(0.5f, 2.0f);
Expand All @@ -73,6 +75,7 @@ public BodyType(SampleContext context) : base(context)
bodyDef.type = m_type;
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = new B2Vec2(3.0f, 3.0f);
bodyDef.name = "attach2";
m_secondAttachmentId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeBox(0.5f, 2.0f);
Expand All @@ -87,6 +90,7 @@ public BodyType(SampleContext context) : base(context)
bodyDef.type = m_type;
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = new B2Vec2(-4.0f, 5.0f);
bodyDef.name = "platform";
m_platformId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeOffsetBox(0.5f, 4.0f, new B2Vec2(4.0f, 0.0f), b2MakeRot(0.5f * B2_PI));
Expand Down Expand Up @@ -137,6 +141,7 @@ public BodyType(SampleContext context) : base(context)
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position = new B2Vec2(-3.0f, 8.0f);
bodyDef.name = "crate1";
B2BodyId bodyId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeBox(0.75f, 0.75f);
Expand All @@ -153,6 +158,7 @@ public BodyType(SampleContext context) : base(context)
bodyDef.type = m_type;
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = new B2Vec2(2.0f, 8.0f);
bodyDef.name = "crate2";
m_secondPayloadId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeBox(0.75f, 0.75f);
Expand All @@ -169,6 +175,7 @@ public BodyType(SampleContext context) : base(context)
bodyDef.type = m_type;
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = new B2Vec2(8.0f, 0.2f);
bodyDef.name = "debris";
m_touchingBodyId = b2CreateBody(m_worldId, ref bodyDef);

B2Capsule capsule = new B2Capsule(new B2Vec2(0.0f, 0.0f), new B2Vec2(1.0f, 0.0f), 0.25f);
Expand All @@ -186,6 +193,7 @@ public BodyType(SampleContext context) : base(context)
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = new B2Vec2(-8.0f, 12.0f);
bodyDef.gravityScale = 0.0f;
bodyDef.name = "floater";
m_floatingBodyId = b2CreateBody(m_worldId, ref bodyDef);

B2Circle circle = new B2Circle(new B2Vec2(0.0f, 0.5f), 0.25f);
Expand Down Expand Up @@ -221,6 +229,9 @@ public override void UpdateGui()
{
m_type = B2BodyType.b2_kinematicBody;
b2Body_SetType(m_platformId, B2BodyType.b2_kinematicBody);
b2Body_SetLinearVelocity(m_secondAttachmentId, b2Vec2_zero);
b2Body_SetAngularVelocity(m_secondAttachmentId, 0.0f);

b2Body_SetLinearVelocity(m_platformId, new B2Vec2(-m_speed, 0.0f));
b2Body_SetAngularVelocity(m_platformId, 0.0f);
b2Body_SetType(m_secondAttachmentId, B2BodyType.b2_kinematicBody);
Expand All @@ -243,24 +254,14 @@ public override void UpdateGui()
{
if (m_isEnabled)
{
b2Body_Enable(m_platformId);
b2Body_Enable(m_secondAttachmentId);
b2Body_Enable(m_attachmentId);
b2Body_Enable(m_secondPayloadId);
b2Body_Enable(m_touchingBodyId);
b2Body_Enable(m_floatingBodyId);

if (m_type == B2BodyType.b2_kinematicBody)
{
b2Body_SetLinearVelocity(m_platformId, new B2Vec2(-m_speed, 0.0f));
b2Body_SetAngularVelocity(m_platformId, 0.0f);
}
}
else
{
b2Body_Disable(m_platformId);
b2Body_Disable(m_secondAttachmentId);
b2Body_Disable(m_attachmentId);
b2Body_Disable(m_secondPayloadId);
b2Body_Disable(m_touchingBodyId);
b2Body_Disable(m_floatingBodyId);
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/Box2D.NET.Samples/Samples/Continuous/BounceHouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public BounceHouse(SampleContext context) : base(context)
b2CreateSegmentShape(groundId, ref shapeDef, ref segment);
}

m_shapeType = ShapeType.e_boxShape;
m_shapeType = ShapeType.e_circleShape;
m_bodyId = b2_nullBodyId;
m_enableHitEvents = true;

Expand All @@ -93,6 +93,7 @@ void Launch()
bodyDef.linearVelocity = new B2Vec2(10.0f, 20.0f);
bodyDef.position = new B2Vec2(0.0f, 0.0f);
bodyDef.gravityScale = 0.0f;
bodyDef.isBullet = true;

// Circle shapes centered on the body can spin fast without risk of tunnelling.
bodyDef.allowFastRotation = m_shapeType == ShapeType.e_circleShape;
Expand All @@ -101,8 +102,8 @@ void Launch()

B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.density = 1.0f;
shapeDef.material.restitution = 1.2f;
shapeDef.material.friction = 0.3f;
shapeDef.material.restitution = 1.0f;
shapeDef.material.friction = 0.0f;
shapeDef.enableHitEvents = m_enableHitEvents;

if (m_shapeType == ShapeType.e_circleShape)
Expand Down
150 changes: 150 additions & 0 deletions src/Box2D.NET.Samples/Samples/Issues/Crash01.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT

using System.Numerics;
using ImGuiNET;
using static Box2D.NET.B2Joints;
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;
using static Box2D.NET.B2Ids;

namespace Box2D.NET.Samples.Samples.Issues;

public class Crash01 : Sample
{
private static readonly int SampleBodyType = SampleFactory.Shared.RegisterSample("Issues", "Crash01", Create);

private B2BodyId m_attachmentId;
private B2BodyId m_platformId;
private B2BodyType m_type;
private bool m_isEnabled;

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

public Crash01(SampleContext context) : base(context)
{
if (m_context.settings.restart == false)
{
m_context.camera.m_center = new B2Vec2(0.8f, 6.4f);
m_context.camera.m_zoom = 25.0f * 0.4f;
}

m_type = B2BodyType.b2_dynamicBody;
m_isEnabled = true;

B2BodyId groundId = b2_nullBodyId;
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.name = "ground";
groundId = b2CreateBody(m_worldId, ref bodyDef);

B2Segment segment = new B2Segment(new B2Vec2(-20.0f, 0.0f), new B2Vec2(20.0f, 0.0f));
B2ShapeDef shapeDef = b2DefaultShapeDef();
b2CreateSegmentShape(groundId, ref shapeDef, ref segment);
}

// Define attachment
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position = new B2Vec2(-2.0f, 3.0f);
bodyDef.name = "attach1";
m_attachmentId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeBox(0.5f, 2.0f);
B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.density = 1.0f;
b2CreatePolygonShape(m_attachmentId, ref shapeDef, ref box);
}

// Define platform
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = m_type;
bodyDef.isEnabled = m_isEnabled;
bodyDef.position = new B2Vec2(-4.0f, 5.0f);
bodyDef.name = "platform";
m_platformId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeOffsetBox(0.5f, 4.0f, new B2Vec2(4.0f, 0.0f), b2MakeRot(0.5f * B2_PI));

B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.density = 2.0f;
b2CreatePolygonShape(m_platformId, ref shapeDef, ref box);

B2RevoluteJointDef revoluteDef = b2DefaultRevoluteJointDef();
B2Vec2 pivot = new B2Vec2(-2.0f, 5.0f);
revoluteDef.@base.bodyIdA = m_attachmentId;
revoluteDef.@base.bodyIdB = m_platformId;
revoluteDef.@base.localFrameA.p = b2Body_GetLocalPoint(m_attachmentId, pivot);
revoluteDef.@base.localFrameB.p = b2Body_GetLocalPoint(m_platformId, pivot);
revoluteDef.maxMotorTorque = 50.0f;
revoluteDef.enableMotor = true;
b2CreateRevoluteJoint(m_worldId, ref revoluteDef);

B2PrismaticJointDef prismaticDef = b2DefaultPrismaticJointDef();
B2Vec2 anchor = new B2Vec2(0.0f, 5.0f);
prismaticDef.@base.bodyIdA = groundId;
prismaticDef.@base.bodyIdB = m_platformId;
prismaticDef.@base.localFrameA.p = b2Body_GetLocalPoint(groundId, anchor);
prismaticDef.@base.localFrameB.p = b2Body_GetLocalPoint(m_platformId, anchor);
prismaticDef.maxMotorForce = 1000.0f;
prismaticDef.motorSpeed = 0.0f;
prismaticDef.enableMotor = true;
prismaticDef.lowerTranslation = -10.0f;
prismaticDef.upperTranslation = 10.0f;
prismaticDef.enableLimit = true;

b2CreatePrismaticJoint(m_worldId, ref prismaticDef);
}
}

public override void UpdateGui()
{
float fontSize = ImGui.GetFontSize();
float height = 11.0f * fontSize;
ImGui.SetNextWindowPos(new Vector2(0.5f * fontSize, m_camera.m_height - height - 2.0f * fontSize), ImGuiCond.Once);
ImGui.SetNextWindowSize(new Vector2(9.0f * fontSize, height));
ImGui.Begin("Crash 01", ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoResize);

if (ImGui.RadioButton("Static", m_type == B2BodyType.b2_staticBody))
{
m_type = B2BodyType.b2_staticBody;
b2Body_SetType(m_platformId, B2BodyType.b2_staticBody);
}

if (ImGui.RadioButton("Kinematic", m_type == B2BodyType.b2_kinematicBody))
{
m_type = B2BodyType.b2_kinematicBody;
b2Body_SetType(m_platformId, B2BodyType.b2_kinematicBody);
b2Body_SetLinearVelocity(m_platformId, new B2Vec2(-0.1f, 0.0f));
}

if (ImGui.RadioButton("Dynamic", m_type == B2BodyType.b2_dynamicBody))
{
m_type = B2BodyType.b2_dynamicBody;
b2Body_SetType(m_platformId, B2BodyType.b2_dynamicBody);
}

if (ImGui.Checkbox("Enable", ref m_isEnabled))
{
if (m_isEnabled)
{
b2Body_Enable(m_attachmentId);
}
else
{
b2Body_Disable(m_attachmentId);
}
}

ImGui.End();
}
}
100 changes: 100 additions & 0 deletions src/Box2D.NET.Samples/Samples/Issues/DisableCrash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
// SPDX-License-Identifier: MIT

using System.Numerics;
using ImGuiNET;
using static Box2D.NET.B2Joints;
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.Issues;

public class DisableCrash : Sample
{
private static readonly int SampleDisableCrash = SampleFactory.Shared.RegisterSample("Issues", "Disable", Create);

private B2BodyId m_attachmentId;
private B2BodyId m_platformId;
bool m_isEnabled;

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

public DisableCrash(SampleContext context)
: base(context)
{
if (m_context.settings.restart == false)
{
m_context.camera.m_center = new B2Vec2(0.8f, 6.4f);
m_context.camera.m_zoom = 25.0f * 0.4f;
}

m_isEnabled = true;

// Define attachment
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position = new B2Vec2(-2.0f, 3.0f);
bodyDef.isEnabled = m_isEnabled;
m_attachmentId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeBox(0.5f, 2.0f);
B2ShapeDef shapeDef = b2DefaultShapeDef();
b2CreatePolygonShape(m_attachmentId, ref shapeDef, ref box);
}

// Define platform
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.position = new B2Vec2(-4.0f, 5.0f);
m_platformId = b2CreateBody(m_worldId, ref bodyDef);

B2Polygon box = b2MakeOffsetBox(0.5f, 4.0f, new B2Vec2(4.0f, 0.0f), b2MakeRot(0.5f * B2_PI));

B2ShapeDef shapeDef = b2DefaultShapeDef();
b2CreatePolygonShape(m_platformId, ref shapeDef, ref box);

B2RevoluteJointDef revoluteDef = b2DefaultRevoluteJointDef();
B2Vec2 pivot = new B2Vec2(-2.0f, 5.0f);
revoluteDef.@base.bodyIdA = m_attachmentId;
revoluteDef.@base.bodyIdB = m_platformId;
revoluteDef.@base.localFrameA.p = b2Body_GetLocalPoint(m_attachmentId, pivot);
revoluteDef.@base.localFrameB.p = b2Body_GetLocalPoint(m_platformId, pivot);
revoluteDef.maxMotorTorque = 50.0f;
revoluteDef.enableMotor = true;
b2CreateRevoluteJoint(m_worldId, ref revoluteDef);
}
}

public override void UpdateGui()
{
float fontSize = ImGui.GetFontSize();
float height = 11.0f * fontSize;
float winX = 0.5f * fontSize;
float winY = m_camera.m_height - height - 2.0f * fontSize;
ImGui.SetNextWindowPos(new Vector2(winX, winY), ImGuiCond.Once);
ImGui.SetNextWindowSize(new Vector2(9.0f * fontSize, height));
ImGui.Begin("Disable Crash", ImGuiWindowFlags.NoMove | ImGuiWindowFlags.NoResize);

if (ImGui.Checkbox("Enable", ref m_isEnabled))
{
if (m_isEnabled)
{
b2Body_Enable(m_attachmentId);
}
else
{
b2Body_Disable(m_attachmentId);
}
}

ImGui.End();
}
}
Loading
Loading