Skip to content

Commit ba439ed

Browse files
Happypig375ikpil
authored andcommitted
in modifier for b2Polygon
1 parent 0252419 commit ba439ed

4 files changed

Lines changed: 49 additions & 32 deletions

File tree

src/Box2D.NET.Samples/Samples/Collisions/Manifold.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Erin Catto
1+
// SPDX-FileCopyrightText: 2025 Erin Catto
22
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
33
// SPDX-License-Identifier: MIT
44

@@ -305,7 +305,7 @@ public override void Step()
305305
B2Transform transform1 = new B2Transform(offset, b2Rot_identity);
306306
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
307307

308-
B2Manifold m = b2CollidePolygonAndCircle(ref box, transform1, circle, transform2);
308+
B2Manifold m = b2CollidePolygonAndCircle(box, transform1, circle, transform2);
309309

310310
DrawSolidPolygon(m_draw, transform1, box.vertices.AsSpan(), box.count, m_round, color1);
311311
DrawSolidCircle(m_draw, new B2Transform(circle.center, transform2.q), circle.radius, color2);
@@ -346,7 +346,7 @@ public override void Step()
346346
B2Transform transform1 = new B2Transform(offset, b2Rot_identity);
347347
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
348348

349-
B2Manifold m = b2CollidePolygonAndCapsule(ref box, transform1, capsule, transform2);
349+
B2Manifold m = b2CollidePolygonAndCapsule(in box, transform1, capsule, transform2);
350350

351351
DrawSolidPolygon(m_draw, transform1, box.vertices.AsSpan(), box.count, box.radius, color1);
352352

@@ -394,7 +394,7 @@ public override void Step()
394394
B2Transform transform1 = new B2Transform(offset, b2Rot_identity);
395395
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
396396

397-
B2Manifold m = b2CollidePolygons(ref box1, transform1, ref box, transform2);
397+
B2Manifold m = b2CollidePolygons(in box1, transform1, in box, transform2);
398398

399399
DrawSolidPolygon(m_draw, transform1, box1.vertices.AsSpan(), box1.count, box1.radius, color1);
400400
DrawSolidPolygon(m_draw, transform2, box.vertices.AsSpan(), box.count, box.radius, color2);
@@ -413,7 +413,7 @@ public override void Step()
413413
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
414414
// b2Transform transform2 = {b2Add({0.0f, -0.1f}, offset), {0.0f, 1.0f}};
415415

416-
B2Manifold m = b2CollidePolygons(ref box1, transform1, ref box, transform2);
416+
B2Manifold m = b2CollidePolygons(in box1, transform1, in box, transform2);
417417

418418
DrawSolidPolygon(m_draw, transform1, box1.vertices.AsSpan(), box1.count, box1.radius, color1);
419419
DrawSolidPolygon(m_draw, transform2, box.vertices.AsSpan(), box.count, box.radius, color2);
@@ -433,7 +433,7 @@ public override void Step()
433433
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
434434
// b2Transform transform2 = {b2Add({0.0f, -0.1f}, offset), {0.0f, 1.0f}};
435435

436-
B2Manifold m = b2CollidePolygons(ref box, transform1, ref rox, transform2);
436+
B2Manifold m = b2CollidePolygons(in box, transform1, in rox, transform2);
437437

438438
DrawSolidPolygon(m_draw, transform1, box.vertices.AsSpan(), box.count, box.radius, color1);
439439
DrawSolidPolygon(m_draw, transform2, rox.vertices.AsSpan(), rox.count, rox.radius, color2);
@@ -453,7 +453,7 @@ public override void Step()
453453
// b2Transform transform1 = {{6.48024225f, 2.07872653f}, {-0.938356698f, 0.345668465f}};
454454
// b2Transform transform2 = {{5.52862263f, 2.51146317f}, {-0.859374702f, -0.511346340f}};
455455

456-
B2Manifold m = b2CollidePolygons(ref rox, transform1, ref rox, transform2);
456+
B2Manifold m = b2CollidePolygons(in rox, transform1, in rox, transform2);
457457

458458
DrawSolidPolygon(m_draw, transform1, rox.vertices.AsSpan(), rox.count, rox.radius, color1);
459459
DrawSolidPolygon(m_draw, transform2, rox.vertices.AsSpan(), rox.count, rox.radius, color2);
@@ -473,7 +473,7 @@ public override void Step()
473473
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
474474
// b2Transform transform2 = {b2Add({-1.44583416f, 0.397352695f}, offset), m_transform.q};
475475

476-
B2Manifold m = b2CollideSegmentAndPolygon(segment, transform1, ref rox, transform2);
476+
B2Manifold m = b2CollideSegmentAndPolygon(segment, transform1, in rox, transform2);
477477

478478
B2Vec2 p1 = b2TransformPoint(transform1, segment.point1);
479479
B2Vec2 p2 = b2TransformPoint(transform1, segment.point2);
@@ -494,7 +494,7 @@ public override void Step()
494494
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
495495
// b2Transform transform2 = {b2Add({0.0f, -0.1f}, offset), {0.0f, 1.0f}};
496496

497-
B2Manifold m = b2CollidePolygons(ref wox, transform1, ref wox, transform2);
497+
B2Manifold m = b2CollidePolygons(in wox, transform1, in wox, transform2);
498498

499499
DrawSolidPolygon(m_draw, transform1, wox.vertices.AsSpan(), wox.count, wox.radius, color1);
500500
DrawSolidPolygon(m_draw, transform1, wox.vertices.AsSpan(), wox.count, 0.0f, color1);
@@ -521,7 +521,7 @@ public override void Step()
521521
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
522522
// b2Transform transform2 = {b2Add({0.0f, -0.1f}, offset), {0.0f, 1.0f}};
523523

524-
B2Manifold m = b2CollidePolygons(ref w1, transform1, ref w2, transform2);
524+
B2Manifold m = b2CollidePolygons(in w1, transform1, in w2, transform2);
525525

526526
DrawSolidPolygon(m_draw, transform1, w1.vertices.AsSpan(), w1.count, w1.radius, color1);
527527
DrawSolidPolygon(m_draw, transform1, w1.vertices.AsSpan(), w1.count, 0.0f, color1);
@@ -546,7 +546,7 @@ public override void Step()
546546
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
547547
// b2Transform transform2 = {b2Add({0.0f, -0.1f}, offset), {0.0f, 1.0f}};
548548

549-
B2Manifold m = b2CollidePolygons(ref box, transform1, ref tri, transform2);
549+
B2Manifold m = b2CollidePolygons(in box, transform1, in tri, transform2);
550550

551551
DrawSolidPolygon(m_draw, transform1, box.vertices.AsSpan(), box.count, 0.0f, color1);
552552
DrawSolidPolygon(m_draw, transform2, tri.vertices.AsSpan(), tri.count, 0.0f, color2);
@@ -611,8 +611,8 @@ public override void Step()
611611
B2Transform transform1 = new B2Transform(offset, b2Rot_identity);
612612
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
613613

614-
B2Manifold m1 = b2CollideChainSegmentAndPolygon(segment1, transform1, ref rox, transform2, ref m_smgroxCache1);
615-
B2Manifold m2 = b2CollideChainSegmentAndPolygon(segment2, transform1, ref rox, transform2, ref m_smgroxCache2);
614+
B2Manifold m1 = b2CollideChainSegmentAndPolygon(segment1, transform1, in rox, transform2, ref m_smgroxCache1);
615+
B2Manifold m2 = b2CollideChainSegmentAndPolygon(segment2, transform1, in rox, transform2, ref m_smgroxCache2);
616616

617617
{
618618
B2Vec2 g2 = b2TransformPoint(transform1, segment1.ghost2);

src/Box2D.NET.Samples/Samples/Collisions/SmoothManifold.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2025 Erin Catto
1+
// SPDX-FileCopyrightText: 2025 Erin Catto
22
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
33
// SPDX-License-Identifier: MIT
44

@@ -300,7 +300,7 @@ public override void Draw()
300300
{
301301
ref readonly B2ChainSegment segment = ref m_segments[i];
302302
B2SimplexCache cache = new B2SimplexCache();
303-
B2Manifold m = b2CollideChainSegmentAndPolygon(segment, transform1, ref rox, transform2, ref cache);
303+
B2Manifold m = b2CollideChainSegmentAndPolygon(segment, transform1, in rox, transform2, ref cache);
304304
DrawManifold(m);
305305
}
306306
}

src/Box2D.NET/B2Contacts.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2023 Erin Catto
1+
// SPDX-FileCopyrightText: 2023 Erin Catto
22
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
33
// SPDX-License-Identifier: MIT
44

@@ -62,19 +62,19 @@ internal static B2Manifold b2CapsuleManifold(B2Shape shapeA, in B2Transform xfA,
6262
internal static B2Manifold b2PolygonAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
6363
{
6464
B2_UNUSED(cache);
65-
return b2CollidePolygonAndCircle(ref shapeA.us.polygon, xfA, shapeB.us.circle, xfB);
65+
return b2CollidePolygonAndCircle(shapeA.us.polygon, xfA, shapeB.us.circle, xfB);
6666
}
6767

6868
internal static B2Manifold b2PolygonAndCapsuleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
6969
{
7070
B2_UNUSED(cache);
71-
return b2CollidePolygonAndCapsule(ref shapeA.us.polygon, xfA, shapeB.us.capsule, xfB);
71+
return b2CollidePolygonAndCapsule(shapeA.us.polygon, xfA, shapeB.us.capsule, xfB);
7272
}
7373

7474
internal static B2Manifold b2PolygonManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
7575
{
7676
B2_UNUSED(cache);
77-
return b2CollidePolygons(ref shapeA.us.polygon, xfA, ref shapeB.us.polygon, xfB);
77+
return b2CollidePolygons(shapeA.us.polygon, xfA, shapeB.us.polygon, xfB);
7878
}
7979

8080
internal static B2Manifold b2SegmentAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
@@ -92,7 +92,7 @@ internal static B2Manifold b2SegmentAndCapsuleManifold(B2Shape shapeA, in B2Tran
9292
internal static B2Manifold b2SegmentAndPolygonManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
9393
{
9494
B2_UNUSED(cache);
95-
return b2CollideSegmentAndPolygon(shapeA.us.segment, xfA, ref shapeB.us.polygon, xfB);
95+
return b2CollideSegmentAndPolygon(shapeA.us.segment, xfA, shapeB.us.polygon, xfB);
9696
}
9797

9898
internal static B2Manifold b2ChainSegmentAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
@@ -108,7 +108,7 @@ internal static B2Manifold b2ChainSegmentAndCapsuleManifold(B2Shape shapeA, in B
108108

109109
internal static B2Manifold b2ChainSegmentAndPolygonManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
110110
{
111-
return b2CollideChainSegmentAndPolygon(shapeA.us.chainSegment, xfA, ref shapeB.us.polygon, xfB, ref cache);
111+
return b2CollideChainSegmentAndPolygon(shapeA.us.chainSegment, xfA, shapeB.us.polygon, xfB, ref cache);
112112
}
113113

114114
internal static void b2AddType(b2ManifoldFcn fcn, B2ShapeType type1, B2ShapeType type2)

src/Box2D.NET/B2Manifolds.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// SPDX-FileCopyrightText: 2023 Erin Catto
1+
// SPDX-FileCopyrightText: 2023 Erin Catto
22
// SPDX-FileCopyrightText: 2025 Ikpil Choi(ikpil@naver.com)
33
// SPDX-License-Identifier: MIT
44

@@ -140,7 +140,7 @@ public static B2Manifold b2CollideCapsuleAndCircle(in B2Capsule capsuleA, in B2T
140140
}
141141

142142
/// Compute the contact manifold between a polygon and a circle
143-
public static B2Manifold b2CollidePolygonAndCircle(ref B2Polygon polygonA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB)
143+
public static B2Manifold b2CollidePolygonAndCircle(in B2Polygon polygonA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB)
144144
{
145145
B2Manifold manifold = new B2Manifold();
146146
float speculativeDistance = B2_SPECULATIVE_DISTANCE;
@@ -157,8 +157,8 @@ public static B2Manifold b2CollidePolygonAndCircle(ref B2Polygon polygonA, in B2
157157
int normalIndex = 0;
158158
float separation = -float.MaxValue;
159159
int vertexCount = polygonA.count;
160-
ReadOnlySpan<B2Vec2> vertices = polygonA.vertices.AsSpan();
161-
ReadOnlySpan<B2Vec2> normals = polygonA.normals.AsSpan();
160+
ReadOnlySpan<B2Vec2> vertices = polygonA.vertices.AsReadOnlySpan();
161+
ReadOnlySpan<B2Vec2> normals = polygonA.normals.AsReadOnlySpan();
162162

163163
for (int i = 0; i < vertexCount; ++i)
164164
{
@@ -537,10 +537,10 @@ public static B2Manifold b2CollideSegmentAndCapsule(in B2Segment segmentA, in B2
537537
}
538538

539539
/// Compute the contact manifold between a polygon and capsule
540-
public static B2Manifold b2CollidePolygonAndCapsule(ref B2Polygon polygonA, in B2Transform xfA, in B2Capsule capsuleB, in B2Transform xfB)
540+
public static B2Manifold b2CollidePolygonAndCapsule(in B2Polygon polygonA, in B2Transform xfA, in B2Capsule capsuleB, in B2Transform xfB)
541541
{
542542
B2Polygon polyB = b2MakeCapsule(capsuleB.center1, capsuleB.center2, capsuleB.radius);
543-
return b2CollidePolygons(ref polygonA, xfA, ref polyB, xfB);
543+
return b2CollidePolygons(in polygonA, xfA, in polyB, xfB);
544544
}
545545

546546
// Polygon clipper used to compute contact points when there are potentially two contact points.
@@ -718,7 +718,24 @@ internal static float b2FindMaxSeparation(ref int edgeIndex, ref B2Polygon poly1
718718
}
719719

720720
/// Compute the contact manifold between two polygons
721-
public static B2Manifold b2CollidePolygons(ref B2Polygon polygonA, in B2Transform xfA, ref B2Polygon polygonB, in B2Transform xfB)
721+
// Due to speculation, every polygon is rounded
722+
// Algorithm:
723+
//
724+
// compute edge separation using the separating axis test (SAT)
725+
// if (separation > speculation_distance)
726+
// return
727+
// find reference and incident edge
728+
// if separation >= 0.1f * B2_LINEAR_SLOP
729+
// compute closest points between reference and incident edge
730+
// if vertices are closest
731+
// single vertex-vertex contact
732+
// else
733+
// clip edges
734+
// end
735+
// else
736+
// clip edges
737+
// end
738+
public static B2Manifold b2CollidePolygons(in B2Polygon polygonA, in B2Transform xfA, in B2Polygon polygonB, in B2Transform xfB)
722739
{
723740
B2Vec2 origin = polygonA.vertices[0];
724741
float linearSlop = B2_LINEAR_SLOP;
@@ -1071,10 +1088,10 @@ public static B2Manifold b2CollideSegmentAndCircle(in B2Segment segmentA, in B2T
10711088
}
10721089

10731090
/// Compute the contact manifold between an segment and a polygon
1074-
public static B2Manifold b2CollideSegmentAndPolygon(in B2Segment segmentA, in B2Transform xfA, ref B2Polygon polygonB, in B2Transform xfB)
1091+
public static B2Manifold b2CollideSegmentAndPolygon(in B2Segment segmentA, in B2Transform xfA, in B2Polygon polygonB, in B2Transform xfB)
10751092
{
10761093
B2Polygon polygonA = b2MakeCapsule(segmentA.point1, segmentA.point2, 0.0f);
1077-
return b2CollidePolygons(ref polygonA, xfA, ref polygonB, xfB);
1094+
return b2CollidePolygons(in polygonA, xfA, polygonB, xfB);
10781095
}
10791096

10801097
/// Compute the contact manifold between a chain segment and a circle
@@ -1169,7 +1186,7 @@ public static B2Manifold b2CollideChainSegmentAndCircle(in B2ChainSegment segmen
11691186
public static B2Manifold b2CollideChainSegmentAndCapsule(in B2ChainSegment segmentA, in B2Transform xfA, in B2Capsule capsuleB, in B2Transform xfB, ref B2SimplexCache cache)
11701187
{
11711188
B2Polygon polyB = b2MakeCapsule(capsuleB.center1, capsuleB.center2, capsuleB.radius);
1172-
return b2CollideChainSegmentAndPolygon(segmentA, xfA, ref polyB, xfB, ref cache);
1189+
return b2CollideChainSegmentAndPolygon(segmentA, xfA, in polyB, xfB, ref cache);
11731190
}
11741191

11751192
internal static B2Manifold b2ClipSegments(B2Vec2 a1, B2Vec2 a2, B2Vec2 b1, B2Vec2 b2, B2Vec2 normal, float ra, float rb, ushort id1, ushort id2)
@@ -1287,7 +1304,7 @@ internal static B2NormalType b2ClassifyNormal(B2ChainSegmentParams param, B2Vec2
12871304
}
12881305

12891306
/// Compute the contact manifold between a chain segment and a rounded polygon
1290-
public static B2Manifold b2CollideChainSegmentAndPolygon(in B2ChainSegment segmentA, in B2Transform xfA, ref B2Polygon polygonB,
1307+
public static B2Manifold b2CollideChainSegmentAndPolygon(in B2ChainSegment segmentA, in B2Transform xfA, in B2Polygon polygonB,
12911308
B2Transform xfB, ref B2SimplexCache cache)
12921309
{
12931310
B2Manifold manifold = new B2Manifold();

0 commit comments

Comments
 (0)