Skip to content

Commit 0bca9be

Browse files
committed
test b2PairQueryCallback
1 parent 193a50d commit 0bca9be

File tree

4 files changed

+76
-23
lines changed

4 files changed

+76
-23
lines changed

src/Box2D.NET/B2Cores.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,12 @@ public static void B2_UNUSED<T1, T2, T3, T4, T5, T6>(T1 a, T2 b, T3 c, T4 d, T5
9595
}
9696

9797
[Conditional("DEBUG")]
98-
public static void B2_ASSERT(bool condition, [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string memberName = "")
98+
public static void B2_ASSERT(bool condition, string message = "", [CallerFilePath] string fileName = "", [CallerLineNumber] int lineNumber = 0, [CallerMemberName] string memberName = "")
9999
{
100100
if (condition)
101101
return;
102102

103-
throw new InvalidOperationException($"{memberName}() {fileName}:{lineNumber}");
103+
throw new InvalidOperationException($"{message} {memberName}() {fileName}:{lineNumber}");
104104
}
105105

106106
/// @return the total bytes allocated by Box2D

src/Box2D.NET/B2Ids.cs

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

5+
using System.Runtime.CompilerServices;
6+
57
namespace Box2D.NET
68
{
79
/**
@@ -41,71 +43,89 @@ public static class B2Ids
4143
public static readonly B2JointId b2_nullJointId = new B2JointId(0, 0, 0);
4244

4345
/// Macro to determine if any id is null.
46+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4447
public static bool B2_IS_NULL(B2WorldId id) => id.index1 == 0;
45-
48+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4649
public static bool B2_IS_NULL(B2BodyId id) => id.index1 == 0;
50+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4751
public static bool B2_IS_NULL(B2ShapeId id) => id.index1 == 0;
52+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4853
public static bool B2_IS_NULL(B2ChainId id) => id.index1 == 0;
54+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4955
public static bool B2_IS_NULL(B2JointId id) => id.index1 == 0;
50-
56+
5157
/// Macro to determine if any id is non-null.
58+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5259
public static bool B2_IS_NON_NULL(B2WorldId id) => id.index1 != 0;
53-
60+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5461
public static bool B2_IS_NON_NULL(B2BodyId id) => id.index1 != 0;
62+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5563
public static bool B2_IS_NON_NULL(B2ShapeId id) => id.index1 != 0;
64+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5665
public static bool B2_IS_NON_NULL(B2ChainId id) => id.index1 != 0;
66+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5767
public static bool B2_IS_NON_NULL(B2JointId id) => id.index1 != 0;
5868

5969
/// Compare two ids for equality. Doesn't work for b2WorldId.
70+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6071
public static bool B2_ID_EQUALS(B2BodyId id1, B2BodyId id2) => id1.index1 == id2.index1 && id1.world0 == id2.world0 && id1.generation == id2.generation;
72+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6173
public static bool B2_ID_EQUALS(B2ShapeId id1, B2ShapeId id2) => id1.index1 == id2.index1 && id1.world0 == id2.world0 && id1.generation == id2.generation;
6274

6375
/// Store a body id into a ulong.
76+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
6477
public static ulong b2StoreBodyId(B2BodyId id)
6578
{
6679
return ((ulong)id.index1 << 32) | ((ulong)id.world0) << 16 | (ulong)id.generation;
6780
}
6881

6982
/// Load a ulong into a body id.
83+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7084
public static B2BodyId b2LoadBodyId(ulong x)
7185
{
7286
B2BodyId id = new B2BodyId((int)(x >> 32), (ushort)(x >> 16), (ushort)(x));
7387
return id;
7488
}
7589

7690
/// Store a shape id into a ulong.
91+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7792
public static ulong b2StoreShapeId(B2ShapeId id)
7893
{
7994
return ((ulong)id.index1 << 32) | ((ulong)id.world0) << 16 | (ulong)id.generation;
8095
}
8196

8297
/// Load a ulong into a shape id.
98+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8399
public static B2ShapeId b2LoadShapeId(ulong x)
84100
{
85101
B2ShapeId id = new B2ShapeId((int)(x >> 32), (ushort)(x >> 16), (ushort)(x));
86102
return id;
87103
}
88104

89105
/// Store a chain id into a ulong.
106+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
90107
public static ulong b2StoreChainId(B2ChainId id)
91108
{
92109
return ((ulong)id.index1 << 32) | ((ulong)id.world0) << 16 | (ulong)id.generation;
93110
}
94111

95112
/// Load a ulong into a chain id.
113+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
96114
public static B2ChainId b2LoadChainId(ulong x)
97115
{
98116
B2ChainId id = new B2ChainId((int)(x >> 32), (ushort)(x >> 16), (ushort)(x));
99117
return id;
100118
}
101119

102120
/// Store a joint id into a ulong.
121+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
103122
public static ulong b2StoreJointId(B2JointId id)
104123
{
105124
return ((ulong)id.index1 << 32) | ((ulong)id.world0) << 16 | (ulong)id.generation;
106125
}
107126

108127
/// Load a ulong into a joint id.
128+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
109129
public static B2JointId b2LoadJointId(ulong x)
110130
{
111131
B2JointId id = new B2JointId((int)(x >> 32), (ushort)(x >> 16), (ushort)(x));

test/Box2D.NET.Test/B2BoardPhasesTests.cs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using static Box2D.NET.B2Atomics;
77
using static Box2D.NET.B2Constants;
88
using static Box2D.NET.B2Worlds;
9+
using static Box2D.NET.B2DynamicTrees;
910

1011
namespace Box2D.NET.Test;
1112

@@ -322,26 +323,57 @@ public void Test_B2BroadPhases_b2BroadPhase_b2PairQueryCallback()
322323

323324
B2ShapeId shapeIdA = TestHelper.CreateCircle(worldId, new B2Vec2(0.0f, 0.0f), 1.0f);
324325
B2ShapeId shapeIdB = TestHelper.CreateCircle(worldId, new B2Vec2(1.0f, 1.0f), 2.0f);
326+
B2ShapeId shapeIdC = TestHelper.CreateCircle(worldId, new B2Vec2(50.0f, 40.0f), 1.0f);
325327

326-
// QueryContext 셋업
328+
var proxyKeyA = world.broadPhase.moveArray.data[0];
329+
var proxyKeyB = world.broadPhase.moveArray.data[1];
330+
var proxyKeyC = world.broadPhase.moveArray.data[2];
331+
332+
B2BodyType proxyTypeA = B2_PROXY_TYPE(proxyKeyA);
333+
B2BodyType proxyTypeB = B2_PROXY_TYPE(proxyKeyB);
334+
B2BodyType proxyTypeC = B2_PROXY_TYPE(proxyKeyC);
335+
336+
var proxyIdA = B2_PROXY_ID(proxyKeyA);
337+
var proxyIdB = B2_PROXY_ID(proxyKeyB);
338+
var proxyIdC = B2_PROXY_ID(proxyKeyC);
339+
340+
// QueryContext setting
327341
B2QueryPairContext queryContext = new B2QueryPairContext();
328342
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));
343+
queryContext.queryTreeType = proxyTypeA;
344+
queryContext.queryProxyKey = proxyKeyA;
345+
queryContext.queryShapeIndex = shapeIdA.index1 - 1;
346+
347+
348+
// a <-> a
349+
{
350+
queryContext.moveResult = new B2MoveResult();
351+
bool result = b2PairQueryCallback(proxyIdA, (ulong)shapeIdA.index1 - 1, ref queryContext);
352+
Assert.That(result, Is.True);
353+
Assert.That(queryContext.moveResult.pairList, Is.Null);
354+
}
355+
356+
// a <-> b
357+
{
358+
queryContext.moveResult = new B2MoveResult();
359+
bool result = b2PairQueryCallback(proxyIdB, (ulong)shapeIdB.index1 - 1, ref queryContext);
360+
361+
Assert.That(result, Is.True);
362+
Assert.That(queryContext.moveResult.pairList, Is.Not.Null);
363+
Assert.That(queryContext.moveResult.pairList.shapeIndexA, Is.EqualTo(queryContext.queryShapeIndex));
364+
Assert.That(queryContext.moveResult.pairList.shapeIndexB, Is.EqualTo((ulong)shapeIdB.index1 - 1));
365+
}
366+
367+
// a <-> c
368+
{
369+
queryContext.moveResult = new B2MoveResult();
370+
bool result = b2PairQueryCallback(proxyIdC, (ulong)shapeIdC.index1 - 1, ref queryContext);
371+
372+
Assert.That(result, Is.True);
373+
Assert.That(queryContext.moveResult.pairList, Is.Not.Null);
374+
Assert.That(queryContext.moveResult.pairList.shapeIndexA, Is.EqualTo(queryContext.queryShapeIndex));
375+
Assert.That(queryContext.moveResult.pairList.shapeIndexB, Is.EqualTo((ulong)shapeIdC.index1 - 1));
376+
}
377+
346378
}
347379
}

test/Box2D.NET.Test/Helpers/TestHelper.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static B2ShapeId CreateCircle(B2WorldId worldId, B2Vec2 position, float r
3636
bodyDef.type = B2BodyType.b2_dynamicBody;
3737
bodyDef.position = position;
3838
bodyDef.gravityScale = 0.0f;
39+
bodyDef.enableSleep = true;
3940
var bodyIdA = b2CreateBody(worldId, ref bodyDef);
4041

4142
B2ShapeDef shapeDef = b2DefaultShapeDef();

0 commit comments

Comments
 (0)