Skip to content

Commit 193a50d

Browse files
committed
added b2PairQueryCallback test
1 parent 2783e3f commit 193a50d

File tree

6 files changed

+158
-3
lines changed

6 files changed

+158
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using static Box2D.NET.B2Worlds;
2+
using static Box2D.NET.B2Types;
3+
using static Box2D.NET.B2MathFunction;
4+
using static Box2D.NET.B2Bodies;
5+
using static Box2D.NET.B2Shapes;
6+
7+
namespace Box2D.NET.Samples.Samples.Worlds;
8+
9+
public class WorkbenchWorld : Sample
10+
{
11+
private static readonly int SampleLargeWorld = SampleFactory.Shared.RegisterSample("World", "Workbench World", Create);
12+
13+
private static Sample Create(SampleAppContext ctx, Settings settings)
14+
{
15+
return new WorkbenchWorld(ctx, settings);
16+
}
17+
18+
public WorkbenchWorld(SampleAppContext ctx, Settings settings) : base(ctx, settings)
19+
{
20+
if (settings.restart == false)
21+
{
22+
m_context.camera.m_center = new B2Vec2(0.0f, 5.0f);
23+
m_context.camera.m_zoom = 25.0f * 0.5f;
24+
}
25+
26+
B2ShapeId shapeIdA = CreateCircle(m_worldId, new B2Vec2(0.0f, 0.0f), 1.0f);
27+
B2ShapeId shapeIdB = CreateCircle(m_worldId, new B2Vec2(1.0f, 1.0f), 1.0f);
28+
}
29+
30+
public static B2ShapeId CreateCircle(B2WorldId worldId, B2Vec2 position, float radius)
31+
{
32+
B2BodyDef bodyDef = b2DefaultBodyDef();
33+
bodyDef.type = B2BodyType.b2_dynamicBody;
34+
bodyDef.position = position;
35+
bodyDef.gravityScale = 0.0f;
36+
var bodyIdA = b2CreateBody(worldId, ref bodyDef);
37+
38+
B2ShapeDef shapeDef = b2DefaultShapeDef();
39+
shapeDef.density = 1.0f;
40+
shapeDef.material.friction = 0.5f;
41+
42+
B2Circle circle = new B2Circle(new B2Vec2(0.0f, 0.0f), radius);
43+
B2ShapeId shapeId = b2CreateCircleShape(bodyIdA, ref shapeDef, ref circle);
44+
45+
return shapeId;
46+
}
47+
48+
}

src/Box2D.NET/B2DistanceJoints.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-FileCopyrightText: 2025 Ikpil Choi([email protected])
33
// SPDX-License-Identifier: MIT
44

5-
using System.Diagnostics;
65
using static Box2D.NET.B2Arrays;
76
using static Box2D.NET.B2Cores;
87
using static Box2D.NET.B2Constants;

test/Box2D.NET.Test/B2BoardPhasesTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using Box2D.NET.Test.Helpers;
23
using NUnit.Framework;
34
using static Box2D.NET.B2BoardPhases;
45
using static Box2D.NET.B2Tables;
56
using static Box2D.NET.B2Atomics;
67
using static Box2D.NET.B2Constants;
8+
using static Box2D.NET.B2Worlds;
79

810
namespace Box2D.NET.Test;
911

@@ -309,4 +311,37 @@ public void Test_B2BroadPhases_b2BroadPhase_EnlargeProxy()
309311
}
310312
#endif
311313
}
314+
315+
[Test]
316+
public void Test_B2BroadPhases_b2BroadPhase_b2PairQueryCallback()
317+
{
318+
// Arrange
319+
using TestWorldHandle worldHandle = TestHelper.CreateWorld();
320+
var worldId = worldHandle.Id;
321+
var world = b2GetWorldFromId(worldId);
322+
323+
B2ShapeId shapeIdA = TestHelper.CreateCircle(worldId, new B2Vec2(0.0f, 0.0f), 1.0f);
324+
B2ShapeId shapeIdB = TestHelper.CreateCircle(worldId, new B2Vec2(1.0f, 1.0f), 2.0f);
325+
326+
// QueryContext 셋업
327+
B2QueryPairContext queryContext = new B2QueryPairContext();
328+
queryContext.world = world;
329+
queryContext.queryTreeType = B2BodyType.b2_dynamicBody;
330+
queryContext.moveResult = world.broadPhase.moveResults[0];
331+
queryContext.moveResult.pairList = null;
332+
queryContext.queryProxyKey = 1;
333+
queryContext.queryShapeIndex = 1;
334+
335+
int proxyId = 0;
336+
ulong userData = 0; // shape index 0번
337+
338+
// Act
339+
bool result = b2PairQueryCallback(proxyId, userData, ref queryContext);
340+
341+
// Assert
342+
// Assert.That(result, Is.True);
343+
// Assert.That(queryContext.moveResult.pairList, Is.Not.Null);
344+
// Assert.That(queryContext.moveResult.pairList.shapeIndexA, Is.EqualTo(0));
345+
// Assert.That(queryContext.moveResult.pairList.shapeIndexB, Is.EqualTo(1));
346+
}
312347
}

test/Box2D.NET.Test/B2WorldTest.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// SPDX-FileCopyrightText: 2025 Ikpil Choi([email protected])
33
// SPDX-License-Identifier: MIT
44

5-
using System.Diagnostics;
65
using NUnit.Framework;
76
using static Box2D.NET.B2Worlds;
87
using static Box2D.NET.B2Types;
@@ -15,7 +14,6 @@
1514
using static Box2D.NET.B2Joints;
1615
using static Box2D.NET.B2Cores;
1716

18-
1917
namespace Box2D.NET.Test;
2018

2119
public class B2WorldTest
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using static Box2D.NET.B2Worlds;
2+
using static Box2D.NET.B2Types;
3+
using static Box2D.NET.B2Bodies;
4+
using static Box2D.NET.B2Shapes;
5+
6+
namespace Box2D.NET.Test.Helpers;
7+
8+
public static class TestHelper
9+
{
10+
public static TestWorldHandle CreateWorld()
11+
{
12+
B2WorldDef worldDef = b2DefaultWorldDef();
13+
worldDef.workerCount = 1;
14+
worldDef.enqueueTask = EnqueueTask;
15+
worldDef.finishTask = FinishTask;
16+
worldDef.userTaskContext = null;
17+
worldDef.enableSleep = true;
18+
19+
B2WorldId worldId = b2CreateWorld(ref worldDef);
20+
21+
return new TestWorldHandle(worldId);
22+
}
23+
24+
private static object EnqueueTask(b2TaskCallback task, int itemCount, int minRange, object taskContext, object userContext)
25+
{
26+
return null;
27+
}
28+
29+
private static void FinishTask(object userTask, object userContext)
30+
{
31+
}
32+
33+
public static B2ShapeId CreateCircle(B2WorldId worldId, B2Vec2 position, float radius)
34+
{
35+
B2BodyDef bodyDef = b2DefaultBodyDef();
36+
bodyDef.type = B2BodyType.b2_dynamicBody;
37+
bodyDef.position = position;
38+
bodyDef.gravityScale = 0.0f;
39+
var bodyIdA = b2CreateBody(worldId, ref bodyDef);
40+
41+
B2ShapeDef shapeDef = b2DefaultShapeDef();
42+
shapeDef.density = 1.0f;
43+
shapeDef.material.friction = 0.5f;
44+
45+
B2Circle circle = new B2Circle(new B2Vec2(0.0f, 0.0f), radius);
46+
B2ShapeId shapeId = b2CreateCircleShape(bodyIdA, ref shapeDef, ref circle);
47+
48+
return shapeId;
49+
}
50+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using static Box2D.NET.B2Worlds;
3+
using static Box2D.NET.B2Ids;
4+
5+
namespace Box2D.NET.Test.Helpers;
6+
7+
public class TestWorldHandle : IDisposable
8+
{
9+
public B2WorldId Id { get; private set; }
10+
11+
public TestWorldHandle(B2WorldId worldId)
12+
{
13+
Id = worldId;
14+
}
15+
16+
public void Dispose()
17+
{
18+
if (B2_IS_NON_NULL(Id))
19+
{
20+
b2DestroyWorld(Id);
21+
Id = b2_nullWorldId;
22+
}
23+
}
24+
25+
}

0 commit comments

Comments
 (0)