Skip to content

Commit 9579bb9

Browse files
committed
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.
1 parent 31376f3 commit 9579bb9

File tree

6 files changed

+19
-19
lines changed

6 files changed

+19
-19
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public override void Step()
244244
B2Transform transform1 = new B2Transform(offset, b2Rot_identity);
245245
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
246246

247-
B2Manifold m = b2CollideCircles(ref circle1, transform1, ref circle2, transform2);
247+
B2Manifold m = b2CollideCircles(circle1, transform1, circle2, transform2);
248248

249249
DrawSolidCircle(m_draw, new B2Transform(circle1.center, transform1.q), circle1.radius, color1);
250250
DrawSolidCircle(m_draw, new B2Transform(circle2.center, transform2.q), circle2.radius, color2);
@@ -262,7 +262,7 @@ public override void Step()
262262
B2Transform transform1 = new B2Transform(offset, b2Rot_identity);
263263
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
264264

265-
B2Manifold m = b2CollideCapsuleAndCircle(capsule, transform1, ref circle, transform2);
265+
B2Manifold m = b2CollideCapsuleAndCircle(capsule, transform1, circle, transform2);
266266

267267
B2Vec2 v1 = b2TransformPoint(transform1, capsule.center1);
268268
B2Vec2 v2 = b2TransformPoint(transform1, capsule.center2);
@@ -283,7 +283,7 @@ public override void Step()
283283
B2Transform transform1 = new B2Transform(offset, b2Rot_identity);
284284
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
285285

286-
B2Manifold m = b2CollideSegmentAndCircle(segment, transform1, ref circle, transform2);
286+
B2Manifold m = b2CollideSegmentAndCircle(segment, transform1, circle, transform2);
287287

288288
B2Vec2 p1 = b2TransformPoint(transform1, segment.point1);
289289
B2Vec2 p2 = b2TransformPoint(transform1, segment.point2);
@@ -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, ref circle, transform2);
308+
B2Manifold m = b2CollidePolygonAndCircle(ref 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);
@@ -569,7 +569,7 @@ public override void Step()
569569
B2Transform transform1 = new B2Transform(offset, b2Rot_identity);
570570
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
571571

572-
B2Manifold m = b2CollideChainSegmentAndCircle(segment, transform1, ref circle, transform2);
572+
B2Manifold m = b2CollideChainSegmentAndCircle(segment, transform1, circle, transform2);
573573

574574
B2Vec2 g1 = b2TransformPoint(transform1, segment.ghost1);
575575
B2Vec2 g2 = b2TransformPoint(transform1, segment.ghost2);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public override void Draw()
286286
for (int i = 0; i < m_count; ++i)
287287
{
288288
ref readonly B2ChainSegment segment = ref m_segments[i];
289-
B2Manifold m = b2CollideChainSegmentAndCircle(segment, transform1, ref circle, transform2);
289+
B2Manifold m = b2CollideChainSegmentAndCircle(segment, transform1, circle, transform2);
290290
DrawManifold(m);
291291
}
292292
}

src/Box2D.NET.Samples/Samples/Shapes/ModifyGeometry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void UpdateShape()
8181
{
8282
case B2ShapeType.b2_circleShape:
8383
m_us.circle = new B2Circle(new B2Vec2(0.0f, 0.0f), 0.5f * m_scale);
84-
b2Shape_SetCircle(m_shapeId, ref m_us.circle);
84+
b2Shape_SetCircle(m_shapeId, m_us.circle);
8585
break;
8686

8787
case B2ShapeType.b2_capsuleShape:

src/Box2D.NET/B2Contacts.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,13 @@ public static class B2Contacts
4545
internal static B2Manifold b2CircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
4646
{
4747
B2_UNUSED(cache);
48-
return b2CollideCircles(ref shapeA.us.circle, xfA, ref shapeB.us.circle, xfB);
48+
return b2CollideCircles(shapeA.us.circle, xfA, shapeB.us.circle, xfB);
4949
}
5050

5151
internal static B2Manifold b2CapsuleAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
5252
{
5353
B2_UNUSED(cache);
54-
return b2CollideCapsuleAndCircle(shapeA.us.capsule, xfA, ref shapeB.us.circle, xfB);
54+
return b2CollideCapsuleAndCircle(shapeA.us.capsule, xfA, shapeB.us.circle, xfB);
5555
}
5656

5757
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,
6363
internal static B2Manifold b2PolygonAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
6464
{
6565
B2_UNUSED(cache);
66-
return b2CollidePolygonAndCircle(ref shapeA.us.polygon, xfA, ref shapeB.us.circle, xfB);
66+
return b2CollidePolygonAndCircle(ref shapeA.us.polygon, xfA, shapeB.us.circle, xfB);
6767
}
6868

6969
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,
8181
internal static B2Manifold b2SegmentAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
8282
{
8383
B2_UNUSED(cache);
84-
return b2CollideSegmentAndCircle(shapeA.us.segment, xfA, ref shapeB.us.circle, xfB);
84+
return b2CollideSegmentAndCircle(shapeA.us.segment, xfA, shapeB.us.circle, xfB);
8585
}
8686

8787
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
9999
internal static B2Manifold b2ChainSegmentAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
100100
{
101101
B2_UNUSED(cache);
102-
return b2CollideChainSegmentAndCircle(shapeA.us.chainSegment, xfA, ref shapeB.us.circle, xfB);
102+
return b2CollideChainSegmentAndCircle(shapeA.us.chainSegment, xfA, shapeB.us.circle, xfB);
103103
}
104104

105105
internal static B2Manifold b2ChainSegmentAndCapsuleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)

src/Box2D.NET/B2Manifolds.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static B2Polygon b2MakeCapsule(B2Vec2 p1, B2Vec2 p2, float radius)
4242
// localAnchorB = qBc * (point - pB)
4343
// anchorB = point - pB = qA * localAnchorA + pA - pB
4444
// = anchorA + (pA - pB)
45-
public static B2Manifold b2CollideCircles(ref B2Circle circleA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB)
45+
public static B2Manifold b2CollideCircles(in B2Circle circleA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB)
4646
{
4747
B2Manifold manifold = new B2Manifold();
4848

@@ -80,7 +80,7 @@ public static B2Manifold b2CollideCircles(ref B2Circle circleA, in B2Transform x
8080

8181
/// Compute the contact manifold between a capsule and circle
8282
/// Compute the collision manifold between a capsule and circle
83-
public static B2Manifold b2CollideCapsuleAndCircle(in B2Capsule capsuleA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB)
83+
public static B2Manifold b2CollideCapsuleAndCircle(in B2Capsule capsuleA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB)
8484
{
8585
B2Manifold manifold = new B2Manifold();
8686

@@ -145,7 +145,7 @@ public static B2Manifold b2CollideCapsuleAndCircle(in B2Capsule capsuleA, in B2T
145145
}
146146

147147
/// Compute the contact manifold between a polygon and a circle
148-
public static B2Manifold b2CollidePolygonAndCircle(ref B2Polygon polygonA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB)
148+
public static B2Manifold b2CollidePolygonAndCircle(ref B2Polygon polygonA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB)
149149
{
150150
B2Manifold manifold = new B2Manifold();
151151
float speculativeDistance = B2_SPECULATIVE_DISTANCE;
@@ -1088,10 +1088,10 @@ public static B2Manifold b2CollidePolygons(ref B2Polygon polygonA, in B2Transfor
10881088
}
10891089

10901090
/// Compute the contact manifold between an segment and a circle
1091-
public static B2Manifold b2CollideSegmentAndCircle(in B2Segment segmentA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB)
1091+
public static B2Manifold b2CollideSegmentAndCircle(in B2Segment segmentA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB)
10921092
{
10931093
B2Capsule capsuleA = new B2Capsule(segmentA.point1, segmentA.point2, 0.0f);
1094-
return b2CollideCapsuleAndCircle(capsuleA, xfA, ref circleB, xfB);
1094+
return b2CollideCapsuleAndCircle(capsuleA, xfA, circleB, xfB);
10951095
}
10961096

10971097
/// Compute the contact manifold between an segment and a polygon
@@ -1102,7 +1102,7 @@ public static B2Manifold b2CollideSegmentAndPolygon(in B2Segment segmentA, in B2
11021102
}
11031103

11041104
/// Compute the contact manifold between a chain segment and a circle
1105-
public static B2Manifold b2CollideChainSegmentAndCircle(in B2ChainSegment segmentA, in B2Transform xfA, ref B2Circle circleB, in B2Transform xfB)
1105+
public static B2Manifold b2CollideChainSegmentAndCircle(in B2ChainSegment segmentA, in B2Transform xfA, in B2Circle circleB, in B2Transform xfB)
11061106
{
11071107
B2Manifold manifold = new B2Manifold();
11081108

src/Box2D.NET/B2Shapes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ public static B2Polygon b2Shape_GetPolygon(in B2ShapeId shapeId)
14601460
return shape.us.polygon;
14611461
}
14621462

1463-
public static void b2Shape_SetCircle(in B2ShapeId shapeId, ref B2Circle circle)
1463+
public static void b2Shape_SetCircle(in B2ShapeId shapeId, in B2Circle circle)
14641464
{
14651465
B2World world = b2GetWorldLocked(shapeId.world0);
14661466
if (world == null)

0 commit comments

Comments
 (0)