From ff782bf7c18cd00e64abb9f8f835e61c37349e2f Mon Sep 17 00:00:00 2001 From: ikpil Date: Sat, 28 Feb 2026 13:08:48 +0900 Subject: [PATCH] Refactor: Use 'in' modifier for B2Circle parameters - Changed `B2Circle` struct parameters from `ref` to the `in` modifier in collision functions (`b2CollideCircles`, `b2CollidePolygonAndCircle`, etc.) and `b2Shape_SetCircle`. - This is a C# performance optimization that passes the struct by read-only reference, avoiding unnecessary copies. - Updated all call sites in the core library and samples to match the new, more efficient method signatures. --- src/Box2D.NET.Samples/Samples/Collisions/Manifold.cs | 10 +++++----- .../Samples/Collisions/SmoothManifold.cs | 2 +- .../Samples/Shapes/ModifyGeometry.cs | 2 +- src/Box2D.NET/B2Contacts.cs | 10 +++++----- src/Box2D.NET/B2Manifolds.cs | 12 ++++++------ src/Box2D.NET/B2Shapes.cs | 2 +- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/Box2D.NET.Samples/Samples/Collisions/Manifold.cs b/src/Box2D.NET.Samples/Samples/Collisions/Manifold.cs index f9766ea..95a6766 100644 --- a/src/Box2D.NET.Samples/Samples/Collisions/Manifold.cs +++ b/src/Box2D.NET.Samples/Samples/Collisions/Manifold.cs @@ -244,7 +244,7 @@ public override void Step() B2Transform transform1 = new B2Transform(offset, b2Rot_identity); B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q); - B2Manifold m = b2CollideCircles(ref circle1, transform1, ref circle2, transform2); + B2Manifold m = b2CollideCircles(circle1, transform1, circle2, transform2); DrawSolidCircle(m_draw, new B2Transform(circle1.center, transform1.q), circle1.radius, color1); DrawSolidCircle(m_draw, new B2Transform(circle2.center, transform2.q), circle2.radius, color2); @@ -262,7 +262,7 @@ public override void Step() B2Transform transform1 = new B2Transform(offset, b2Rot_identity); B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q); - B2Manifold m = b2CollideCapsuleAndCircle(capsule, transform1, ref circle, transform2); + B2Manifold m = b2CollideCapsuleAndCircle(capsule, transform1, circle, transform2); B2Vec2 v1 = b2TransformPoint(transform1, capsule.center1); B2Vec2 v2 = b2TransformPoint(transform1, capsule.center2); @@ -283,7 +283,7 @@ public override void Step() B2Transform transform1 = new B2Transform(offset, b2Rot_identity); B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q); - B2Manifold m = b2CollideSegmentAndCircle(segment, transform1, ref circle, transform2); + B2Manifold m = b2CollideSegmentAndCircle(segment, transform1, circle, transform2); B2Vec2 p1 = b2TransformPoint(transform1, segment.point1); B2Vec2 p2 = b2TransformPoint(transform1, segment.point2); @@ -305,7 +305,7 @@ public override void Step() B2Transform transform1 = new B2Transform(offset, b2Rot_identity); B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q); - B2Manifold m = b2CollidePolygonAndCircle(ref box, transform1, ref circle, transform2); + B2Manifold m = b2CollidePolygonAndCircle(ref box, transform1, circle, transform2); DrawSolidPolygon(m_draw, transform1, box.vertices.AsSpan(), box.count, m_round, color1); DrawSolidCircle(m_draw, new B2Transform(circle.center, transform2.q), circle.radius, color2); @@ -569,7 +569,7 @@ public override void Step() B2Transform transform1 = new B2Transform(offset, b2Rot_identity); B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q); - B2Manifold m = b2CollideChainSegmentAndCircle(segment, transform1, ref circle, transform2); + B2Manifold m = b2CollideChainSegmentAndCircle(segment, transform1, circle, transform2); B2Vec2 g1 = b2TransformPoint(transform1, segment.ghost1); B2Vec2 g2 = b2TransformPoint(transform1, segment.ghost2); diff --git a/src/Box2D.NET.Samples/Samples/Collisions/SmoothManifold.cs b/src/Box2D.NET.Samples/Samples/Collisions/SmoothManifold.cs index 5c4dbc0..96303c5 100644 --- a/src/Box2D.NET.Samples/Samples/Collisions/SmoothManifold.cs +++ b/src/Box2D.NET.Samples/Samples/Collisions/SmoothManifold.cs @@ -286,7 +286,7 @@ public override void Draw() for (int i = 0; i < m_count; ++i) { ref readonly B2ChainSegment segment = ref m_segments[i]; - B2Manifold m = b2CollideChainSegmentAndCircle(segment, transform1, ref circle, transform2); + B2Manifold m = b2CollideChainSegmentAndCircle(segment, transform1, circle, transform2); DrawManifold(m); } } diff --git a/src/Box2D.NET.Samples/Samples/Shapes/ModifyGeometry.cs b/src/Box2D.NET.Samples/Samples/Shapes/ModifyGeometry.cs index 3128436..4a6d9c7 100644 --- a/src/Box2D.NET.Samples/Samples/Shapes/ModifyGeometry.cs +++ b/src/Box2D.NET.Samples/Samples/Shapes/ModifyGeometry.cs @@ -81,7 +81,7 @@ void UpdateShape() { case B2ShapeType.b2_circleShape: m_us.circle = new B2Circle(new B2Vec2(0.0f, 0.0f), 0.5f * m_scale); - b2Shape_SetCircle(m_shapeId, ref m_us.circle); + b2Shape_SetCircle(m_shapeId, m_us.circle); break; case B2ShapeType.b2_capsuleShape: diff --git a/src/Box2D.NET/B2Contacts.cs b/src/Box2D.NET/B2Contacts.cs index cde49bf..917ec9c 100644 --- a/src/Box2D.NET/B2Contacts.cs +++ b/src/Box2D.NET/B2Contacts.cs @@ -45,13 +45,13 @@ public static class B2Contacts internal static B2Manifold b2CircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache) { B2_UNUSED(cache); - return b2CollideCircles(ref shapeA.us.circle, xfA, ref shapeB.us.circle, xfB); + return b2CollideCircles(shapeA.us.circle, xfA, shapeB.us.circle, xfB); } internal static B2Manifold b2CapsuleAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache) { B2_UNUSED(cache); - return b2CollideCapsuleAndCircle(shapeA.us.capsule, xfA, ref shapeB.us.circle, xfB); + return b2CollideCapsuleAndCircle(shapeA.us.capsule, xfA, shapeB.us.circle, xfB); } internal static B2Manifold b2CapsuleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache) @@ -63,7 +63,7 @@ internal static B2Manifold b2CapsuleManifold(B2Shape shapeA, in B2Transform xfA, internal static B2Manifold b2PolygonAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache) { B2_UNUSED(cache); - return b2CollidePolygonAndCircle(ref shapeA.us.polygon, xfA, ref shapeB.us.circle, xfB); + return b2CollidePolygonAndCircle(ref shapeA.us.polygon, xfA, shapeB.us.circle, xfB); } internal static B2Manifold b2PolygonAndCapsuleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache) @@ -81,7 +81,7 @@ internal static B2Manifold b2PolygonManifold(B2Shape shapeA, in B2Transform xfA, internal static B2Manifold b2SegmentAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache) { B2_UNUSED(cache); - return b2CollideSegmentAndCircle(shapeA.us.segment, xfA, ref shapeB.us.circle, xfB); + return b2CollideSegmentAndCircle(shapeA.us.segment, xfA, shapeB.us.circle, xfB); } internal static B2Manifold b2SegmentAndCapsuleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache) @@ -99,7 +99,7 @@ internal static B2Manifold b2SegmentAndPolygonManifold(B2Shape shapeA, in B2Tran internal static B2Manifold b2ChainSegmentAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache) { B2_UNUSED(cache); - return b2CollideChainSegmentAndCircle(shapeA.us.chainSegment, xfA, ref shapeB.us.circle, xfB); + return b2CollideChainSegmentAndCircle(shapeA.us.chainSegment, xfA, shapeB.us.circle, xfB); } internal static B2Manifold b2ChainSegmentAndCapsuleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache) diff --git a/src/Box2D.NET/B2Manifolds.cs b/src/Box2D.NET/B2Manifolds.cs index 27b3947..f8c27c9 100644 --- a/src/Box2D.NET/B2Manifolds.cs +++ b/src/Box2D.NET/B2Manifolds.cs @@ -42,7 +42,7 @@ public static B2Polygon b2MakeCapsule(B2Vec2 p1, B2Vec2 p2, float radius) // localAnchorB = qBc * (point - pB) // anchorB = point - pB = qA * localAnchorA + pA - pB // = anchorA + (pA - pB) - public static B2Manifold b2CollideCircles(ref B2Circle circleA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB) + public static B2Manifold b2CollideCircles(in B2Circle circleA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB) { B2Manifold manifold = new B2Manifold(); @@ -80,7 +80,7 @@ public static B2Manifold b2CollideCircles(ref B2Circle circleA, in B2Transform x /// Compute the contact manifold between a capsule and circle /// Compute the collision manifold between a capsule and circle - public static B2Manifold b2CollideCapsuleAndCircle(in B2Capsule capsuleA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB) + public static B2Manifold b2CollideCapsuleAndCircle(in B2Capsule capsuleA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB) { B2Manifold manifold = new B2Manifold(); @@ -145,7 +145,7 @@ public static B2Manifold b2CollideCapsuleAndCircle(in B2Capsule capsuleA, in B2T } /// Compute the contact manifold between a polygon and a circle - public static B2Manifold b2CollidePolygonAndCircle(ref B2Polygon polygonA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB) + public static B2Manifold b2CollidePolygonAndCircle(ref B2Polygon polygonA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB) { B2Manifold manifold = new B2Manifold(); float speculativeDistance = B2_SPECULATIVE_DISTANCE; @@ -1088,10 +1088,10 @@ public static B2Manifold b2CollidePolygons(ref B2Polygon polygonA, in B2Transfor } /// Compute the contact manifold between an segment and a circle - public static B2Manifold b2CollideSegmentAndCircle(in B2Segment segmentA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB) + public static B2Manifold b2CollideSegmentAndCircle(in B2Segment segmentA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB) { B2Capsule capsuleA = new B2Capsule(segmentA.point1, segmentA.point2, 0.0f); - return b2CollideCapsuleAndCircle(capsuleA, xfA, ref circleB, xfB); + return b2CollideCapsuleAndCircle(capsuleA, xfA, circleB, xfB); } /// Compute the contact manifold between an segment and a polygon @@ -1102,7 +1102,7 @@ public static B2Manifold b2CollideSegmentAndPolygon(in B2Segment segmentA, in B2 } /// Compute the contact manifold between a chain segment and a circle - public static B2Manifold b2CollideChainSegmentAndCircle(in B2ChainSegment segmentA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB) + public static B2Manifold b2CollideChainSegmentAndCircle(in B2ChainSegment segmentA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB) { B2Manifold manifold = new B2Manifold(); diff --git a/src/Box2D.NET/B2Shapes.cs b/src/Box2D.NET/B2Shapes.cs index 429667f..b422668 100644 --- a/src/Box2D.NET/B2Shapes.cs +++ b/src/Box2D.NET/B2Shapes.cs @@ -1460,7 +1460,7 @@ public static B2Polygon b2Shape_GetPolygon(in B2ShapeId shapeId) return shape.us.polygon; } - public static void b2Shape_SetCircle(in B2ShapeId shapeId, ref B2Circle circle) + public static void b2Shape_SetCircle(in B2ShapeId shapeId, in B2Circle circle) { B2World world = b2GetWorldLocked(shapeId.world0); if (world == null)