Skip to content

Pr/cast initial overlap 933 #29

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

Merged
merged 2 commits into from
May 28, 2025
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
31 changes: 17 additions & 14 deletions src/Box2D.NET.Samples/Samples/Collisions/CastWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

namespace Box2D.NET.Samples.Samples.Collisions;

// This sample shows how to use the ray and shape cast functions on a b2World. This
// sample is configured to ignore initial overlap.
public class CastWorld : Sample
{
private static readonly int SampleRayCastWorld = SampleFactory.Shared.RegisterSample("Collision", "Cast World", Create);
Expand Down Expand Up @@ -383,7 +385,7 @@ public override void Step()
// This version doesn't have a callback, but it doesn't skip the ignored shape
B2RayResult result = b2World_CastRayClosest(m_worldId, m_rayStart, rayTranslation, b2DefaultQueryFilter());

if (result.hit == true)
if (result.hit == true && result.fraction > 0.0f)
{
B2Vec2 c = b2MulAdd(m_rayStart, result.fraction, rayTranslation);
m_draw.DrawPoint(result.point, 5.0f, color1);
Expand Down Expand Up @@ -453,7 +455,7 @@ public override void Step()
B2Vec2 n = context.normals[i];
m_draw.DrawPoint(p, 5.0f, colors[i]);
m_draw.DrawSegment(m_rayStart, c, color2);
B2Vec2 head = b2MulAdd(p, 0.5f, n);
B2Vec2 head = b2MulAdd(p, 1.0f, n);
m_draw.DrawSegment(p, head, color3);

B2Vec2 t = b2MulSV(context.fractions[i], rayTranslation);
Expand Down Expand Up @@ -505,16 +507,11 @@ public override void Draw(Settings settings)
base.Draw(settings);

DrawTextLine("Click left mouse button and drag to modify ray cast");

DrawTextLine("Shape 7 is intentionally ignored by the ray");




if (m_simple)
{
DrawTextLine("Simple closest point ray cast");

}
else
{
Expand All @@ -540,8 +537,6 @@ public override void Draw(Settings settings)
B2_ASSERT(false);
break;
}


}

if (B2_IS_NON_NULL(m_bodyIds[m_ignoreIndex]))
Expand All @@ -559,7 +554,9 @@ static float RayCastClosestCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 norm
CastContext rayContext = (CastContext)context;

ShapeUserData userData = (ShapeUserData)b2Shape_GetUserData(shapeId);
if (userData != null && userData.ignore)

// Ignore a specific shape. Also ignore initial overlap.
if ((userData != null && userData.ignore) || fraction == 0.0f)
{
// By returning -1, we instruct the calling code to ignore this shape and
// continue the ray-cast to the next shape.
Expand All @@ -585,7 +582,9 @@ static float RayCastAnyCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal,
CastContext rayContext = (CastContext)context;

ShapeUserData userData = (ShapeUserData)b2Shape_GetUserData(shapeId);
if (userData != null && userData.ignore)

// Ignore a specific shape. Also ignore initial overlap.
if ((userData != null && userData.ignore) || fraction == 0.0f)
{
// By returning -1, we instruct the calling code to ignore this shape and
// continue the ray-cast to the next shape.
Expand Down Expand Up @@ -613,7 +612,9 @@ static float RayCastMultipleCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 nor
CastContext rayContext = (CastContext)context;

ShapeUserData userData = (ShapeUserData)b2Shape_GetUserData(shapeId);
if (userData != null && userData.ignore)

// Ignore a specific shape. Also ignore initial overlap.
if ((userData != null && userData.ignore) || fraction == 0.0f)
{
// By returning -1, we instruct the calling code to ignore this shape and
// continue the ray-cast to the next shape.
Expand All @@ -639,13 +640,15 @@ static float RayCastMultipleCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 nor
return 1.0f;
}

// This ray cast collects multiple hits along the ray and sorts them.
// This ray cast collects multiple hits along the ray and sorts them.
static float RayCastSortedCallback(B2ShapeId shapeId, B2Vec2 point, B2Vec2 normal, float fraction, object context)
{
CastContext rayContext = (CastContext)context;

ShapeUserData userData = (ShapeUserData)b2Shape_GetUserData(shapeId);
if (userData != null && userData.ignore)

// Ignore a specific shape. Also ignore initial overlap.
if ((userData != null && userData.ignore) || fraction == 0.0f)
{
// By returning -1, we instruct the calling code to ignore this shape and
// continue the ray-cast to the next shape.
Expand Down
34 changes: 17 additions & 17 deletions src/Box2D.NET.Samples/Samples/Collisions/RayCast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using static Box2D.NET.B2Hulls;
using static Box2D.NET.B2Geometries;
using static Box2D.NET.B2MathFunction;
using static Box2D.NET.B2Diagnostics;

namespace Box2D.NET.Samples.Samples.Collisions;

Expand Down Expand Up @@ -182,19 +183,24 @@ void DrawRay(ref B2CastOutput output)

if (output.hit)
{
B2Vec2 p = b2MulAdd(p1, output.fraction, d);
m_draw.DrawSegment(p1, p, B2HexColor.b2_colorWhite);
m_draw.DrawPoint(p1, 5.0f, B2HexColor.b2_colorGreen);
m_draw.DrawPoint(output.point, 5.0f, B2HexColor.b2_colorWhite);
B2Vec2 p;

B2Vec2 n = b2MulAdd(p, 1.0f, output.normal);
m_draw.DrawSegment(p, n, B2HexColor.b2_colorViolet);
if (output.fraction == 0.0f)
{
B2_ASSERT(output.normal.X == 0.0f && output.normal.Y == 0.0f);
p = output.point;
m_draw.DrawPoint(output.point, 5.0f, B2HexColor.b2_colorPeru);
}
else
{
p = b2MulAdd(p1, output.fraction, d);
m_draw.DrawSegment(p1, p, B2HexColor.b2_colorWhite);
m_draw.DrawPoint(p1, 5.0f, B2HexColor.b2_colorGreen);
m_draw.DrawPoint(output.point, 5.0f, B2HexColor.b2_colorWhite);

// if (m_rayRadius > 0.0f)
//{
// m_context.g_draw.DrawCircle(p1, m_rayRadius, b2HexColor.b2_colorGreen);
// m_context.g_draw.DrawCircle(p, m_rayRadius, b2HexColor.b2_colorRed);
// }
B2Vec2 n = b2MulAdd(p, 1.0f, output.normal);
m_draw.DrawSegment(p, n, B2HexColor.b2_colorViolet);
}

if (m_showFraction)
{
Expand All @@ -207,12 +213,6 @@ void DrawRay(ref B2CastOutput output)
m_draw.DrawSegment(p1, p2, B2HexColor.b2_colorWhite);
m_draw.DrawPoint(p1, 5.0f, B2HexColor.b2_colorGreen);
m_draw.DrawPoint(p2, 5.0f, B2HexColor.b2_colorRed);

// if (m_rayRadius > 0.0f)
//{
// m_context.g_draw.DrawCircle(p1, m_rayRadius, b2HexColor.b2_colorGreen);
// m_context.g_draw.DrawCircle(p2, m_rayRadius, b2HexColor.b2_colorRed);
// }
}
}

Expand Down
19 changes: 14 additions & 5 deletions src/Box2D.NET.Samples/Samples/Collisions/ShapeCast.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public ShapeCast(SampleContext context) : base(context)
m_box = b2MakePolygon( &hull, 0.0f );
}
#endif
m_transform = new B2Transform( new B2Vec2( -0.6f, 0.0f ), b2Rot_identity );

m_transform = new B2Transform(new B2Vec2(-0.6f, 0.0f), b2Rot_identity);
m_translation = new B2Vec2(2.0f, 0.0f);
m_angle = 0.0f;
m_startPoint = new B2Vec2(0.0f, 0.0f);
Expand Down Expand Up @@ -376,7 +376,7 @@ public override void Draw(Settings settings)
{
base.Draw(settings);

DrawTextLine($"hit = {output.hit}, iterations = {output.iterations}, lambda = {output.fraction}, distance = {_distanceOutput.distance}");
DrawTextLine($"hit = {output.hit}, iterations = {output.iterations}, fraction = {output.fraction}, distance = {_distanceOutput.distance}");

DrawShape(m_typeA, b2Transform_identity, m_radiusA, B2HexColor.b2_colorCyan);
DrawShape(m_typeB, m_transform, m_radiusB, B2HexColor.b2_colorLightGreen);
Expand All @@ -386,8 +386,17 @@ public override void Draw(Settings settings)
if (output.hit)
{
DrawShape(m_typeB, inputTransform, m_radiusB, B2HexColor.b2_colorPlum);
m_draw.DrawPoint(output.point, 5.0f, B2HexColor.b2_colorWhite);
m_draw.DrawSegment(output.point, output.point + 0.5f * output.normal, B2HexColor.b2_colorYellow);


if (output.fraction > 0.0f)
{
m_draw.DrawPoint(output.point, 5.0f, B2HexColor.b2_colorWhite);
m_draw.DrawSegment(output.point, output.point + 0.5f * output.normal, B2HexColor.b2_colorYellow);
}
else
{
m_draw.DrawPoint(output.point, 5.0f, B2HexColor.b2_colorPeru);
}
}

if (m_showIndices)
Expand Down
4 changes: 3 additions & 1 deletion src/Box2D.NET.Samples/Samples/Collisions/SmoothManifold.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ void DrawManifold(ref B2Manifold manifold)
}
}

public override void Step()
public override void Draw(Settings settings)
{
base.Draw(settings);

B2HexColor color1 = B2HexColor.b2_colorYellow;
B2HexColor color2 = B2HexColor.b2_colorMagenta;

Expand Down
129 changes: 129 additions & 0 deletions src/Box2D.NET.Samples/Samples/Joints/Door.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// SPDX-FileCopyrightText: 2025 Erin Catto
// SPDX-FileCopyrightText: 2025 Ikpil Choi([email protected])
// 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;
using static Box2D.NET.B2RevoluteJoints;

namespace Box2D.NET.Samples.Samples.Joints;

// A top down door
public class Door : Sample
{
private static readonly int SampleDoor = SampleFactory.Shared.RegisterSample("Joints", "Door", Create);

private B2BodyId m_doorId;
private B2JointId m_jointId;
private float m_impulse;
private float m_translationError;
private bool m_enableLimit;

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

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

B2BodyId groundId = b2_nullBodyId;
{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.position = new B2Vec2(0.0f, 0.0f);
groundId = b2CreateBody(m_worldId, ref bodyDef);
}

m_enableLimit = true;

{
B2BodyDef bodyDef = b2DefaultBodyDef();
bodyDef.type = B2BodyType.b2_dynamicBody;
bodyDef.position = new B2Vec2(0.0f, 1.5f);
bodyDef.gravityScale = 0.0f;

m_doorId = b2CreateBody(m_worldId, ref bodyDef);

B2ShapeDef shapeDef = b2DefaultShapeDef();
shapeDef.density = 1000.0f;

B2Polygon box = b2MakeBox(0.1f, 1.5f);
b2CreatePolygonShape(m_doorId, ref shapeDef, ref box);

B2Vec2 pivot = new B2Vec2(0.0f, 0.0f);
B2RevoluteJointDef jointDef = b2DefaultRevoluteJointDef();
jointDef.bodyIdA = groundId;
jointDef.bodyIdB = m_doorId;
jointDef.localAnchorA = b2Body_GetLocalPoint(jointDef.bodyIdA, pivot);
jointDef.localAnchorB = b2Body_GetLocalPoint(jointDef.bodyIdB, pivot);
jointDef.targetAngle = 0.0f;
jointDef.enableSpring = true;
jointDef.hertz = 1.0f;
jointDef.dampingRatio = 0.5f;
jointDef.motorSpeed = 0.0f;
jointDef.maxMotorTorque = 0.0f;
jointDef.enableMotor = false;
jointDef.referenceAngle = 0.0f;
jointDef.lowerAngle = -0.5f * B2_PI;
jointDef.upperAngle = 0.5f * B2_PI;
jointDef.enableLimit = m_enableLimit;

m_jointId = b2CreateRevoluteJoint(m_worldId, ref jointDef);
}

m_impulse = 50000.0f;
m_translationError = 0.0f;
}

public override void UpdateGui()
{
float height = 220.0f;
ImGui.SetNextWindowPos(new Vector2(10.0f, m_context.camera.m_height - height - 50.0f), ImGuiCond.Once);
ImGui.SetNextWindowSize(new Vector2(240.0f, height));

ImGui.Begin("Door", ImGuiWindowFlags.NoResize);

if (ImGui.Button("impulse"))
{
B2Vec2 p = b2Body_GetWorldPoint(m_doorId, new B2Vec2(0.0f, 1.5f));
b2Body_ApplyLinearImpulse(m_doorId, new B2Vec2(m_impulse, 0.0f), p, true);
m_translationError = 0.0f;
}

ImGui.SliderFloat("magnitude", ref m_impulse, 1000.0f, 100000.0f, "%.0f");

if (ImGui.Checkbox("limit", ref m_enableLimit))
{
b2RevoluteJoint_EnableLimit(m_jointId, m_enableLimit);
}

ImGui.End();
}

public override void Draw(Settings settings)
{
base.Draw(settings);

B2Vec2 p = b2Body_GetWorldPoint(m_doorId, new B2Vec2(0.0f, 1.5f));
m_draw.DrawPoint(p, 5.0f, B2HexColor.b2_colorDarkKhaki);

m_draw.DrawTransform(b2Transform_identity);

float translationError = b2Joint_GetLinearSeparation(m_jointId);
m_translationError = b2MaxFloat(m_translationError, translationError);

DrawTextLine($"translation error = {m_translationError}");
}
}
Loading
Loading