Skip to content

Commit 39e89df

Browse files
committed
refactor: B2Capsule usage to use 'in' parameters
- Updated collision and world functions (e.g., `b2CollideCapsules`, `b2World_CastMover`) to accept `B2Capsule` as `in` parameter instead of `ref`. - Updated `b2Shape_SetCapsule` to use `in B2Capsule`. - Removed `ref` keyword from `B2Capsule` arguments in Samples and internal calls. - This change clarifies that capsule data is read-only in these contexts and improves API usability.
1 parent 9dd5d7c commit 39e89df

File tree

8 files changed

+25
-25
lines changed

8 files changed

+25
-25
lines changed

src/Box2D.NET.Samples/Samples/Characters/Mover.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,12 @@ void SolveMove(float timeStep, float throttle)
440440
mover.center2 = b2TransformPoint(m_transform, m_capsule.center2);
441441
mover.radius = m_capsule.radius;
442442

443-
b2World_CollideMover(m_worldId, ref mover, collideFilter, PlaneResultFcn, this);
443+
b2World_CollideMover(m_worldId, mover, collideFilter, PlaneResultFcn, this);
444444
B2PlaneSolverResult result = b2SolvePlanes(target - m_transform.p, m_planes, m_planeCount);
445445

446446
m_totalIterations += result.iterationCount;
447447

448-
float fraction = b2World_CastMover(m_worldId, ref mover, result.translation, castFilter);
448+
float fraction = b2World_CastMover(m_worldId, mover, result.translation, castFilter);
449449

450450
B2Vec2 delta = fraction * result.translation;
451451
m_transform.p += delta;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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(ref capsule, transform1, ref circle, transform2);
265+
B2Manifold m = b2CollideCapsuleAndCircle(capsule, transform1, ref circle, transform2);
266266

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

326-
B2Manifold m = b2CollideCapsules(ref capsule1, transform1, ref capsule2, transform2);
326+
B2Manifold m = b2CollideCapsules(capsule1, transform1, capsule2, transform2);
327327

328328
B2Vec2 v1 = b2TransformPoint(transform1, capsule1.center1);
329329
B2Vec2 v2 = b2TransformPoint(transform1, capsule1.center2);
@@ -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, ref capsule, transform2);
349+
B2Manifold m = b2CollidePolygonAndCapsule(ref box, transform1, capsule, transform2);
350350

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

@@ -367,7 +367,7 @@ public override void Step()
367367
B2Transform transform1 = new B2Transform(offset, b2Rot_identity);
368368
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
369369

370-
B2Manifold m = b2CollideSegmentAndCapsule(ref segment, transform1, ref capsule, transform2);
370+
B2Manifold m = b2CollideSegmentAndCapsule(ref segment, transform1, capsule, transform2);
371371

372372
B2Vec2 p1 = b2TransformPoint(transform1, segment.point1);
373373
B2Vec2 p2 = b2TransformPoint(transform1, segment.point2);
@@ -666,8 +666,8 @@ public override void Step()
666666
B2Transform transform1 = new B2Transform(offset, b2Rot_identity);
667667
B2Transform transform2 = new B2Transform(b2Add(m_transform.p, offset), m_transform.q);
668668

669-
B2Manifold m1 = b2CollideChainSegmentAndCapsule(ref segment1, transform1, ref capsule, transform2, ref m_smgcapCache1);
670-
B2Manifold m2 = b2CollideChainSegmentAndCapsule(ref segment2, transform1, ref capsule, transform2, ref m_smgcapCache2);
669+
B2Manifold m1 = b2CollideChainSegmentAndCapsule(ref segment1, transform1, capsule, transform2, ref m_smgcapCache1);
670+
B2Manifold m2 = b2CollideChainSegmentAndCapsule(ref segment2, transform1, capsule, transform2, ref m_smgcapCache2);
671671

672672
{
673673
B2Vec2 g2 = b2TransformPoint(transform1, segment1.ghost2);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ void UpdateShape()
8686

8787
case B2ShapeType.b2_capsuleShape:
8888
m_us.capsule = new B2Capsule(new B2Vec2(-0.5f * m_scale, 0.0f), new B2Vec2(0.0f, 0.5f * m_scale), 0.5f * m_scale);
89-
b2Shape_SetCapsule(m_shapeId, ref m_us.capsule);
89+
b2Shape_SetCapsule(m_shapeId, m_us.capsule);
9090
break;
9191

9292
case B2ShapeType.b2_segmentShape:

src/Box2D.NET.Shared/Humans.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ public static void Human_SetScale(ref Human human, float scale)
686686
capsule.center1 = b2MulSV(ratio, capsule.center1);
687687
capsule.center2 = b2MulSV(ratio, capsule.center2);
688688
capsule.radius *= ratio;
689-
b2Shape_SetCapsule(shapeIds[shapeIndex], ref capsule);
689+
b2Shape_SetCapsule(shapeIds[shapeIndex], capsule);
690690
}
691691
else if (type == B2ShapeType.b2_polygonShape)
692692
{

src/Box2D.NET/B2Contacts.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ internal static B2Manifold b2CircleManifold(B2Shape shapeA, in B2Transform xfA,
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(ref shapeA.us.capsule, xfA, ref shapeB.us.circle, xfB);
54+
return b2CollideCapsuleAndCircle(shapeA.us.capsule, xfA, ref shapeB.us.circle, xfB);
5555
}
5656

5757
internal static B2Manifold b2CapsuleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
5858
{
5959
B2_UNUSED(cache);
60-
return b2CollideCapsules(ref shapeA.us.capsule, xfA, ref shapeB.us.capsule, xfB);
60+
return b2CollideCapsules(shapeA.us.capsule, xfA, shapeB.us.capsule, xfB);
6161
}
6262

6363
internal static B2Manifold b2PolygonAndCircleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
@@ -69,7 +69,7 @@ internal static B2Manifold b2PolygonAndCircleManifold(B2Shape shapeA, in B2Trans
6969
internal static B2Manifold b2PolygonAndCapsuleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
7070
{
7171
B2_UNUSED(cache);
72-
return b2CollidePolygonAndCapsule(ref shapeA.us.polygon, xfA, ref shapeB.us.capsule, xfB);
72+
return b2CollidePolygonAndCapsule(ref shapeA.us.polygon, xfA, shapeB.us.capsule, xfB);
7373
}
7474

7575
internal static B2Manifold b2PolygonManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
@@ -87,7 +87,7 @@ internal static B2Manifold b2SegmentAndCircleManifold(B2Shape shapeA, in B2Trans
8787
internal static B2Manifold b2SegmentAndCapsuleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
8888
{
8989
B2_UNUSED(cache);
90-
return b2CollideSegmentAndCapsule(ref shapeA.us.segment, xfA, ref shapeB.us.capsule, xfB);
90+
return b2CollideSegmentAndCapsule(ref shapeA.us.segment, xfA, shapeB.us.capsule, xfB);
9191
}
9292

9393
internal static B2Manifold b2SegmentAndPolygonManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
@@ -104,7 +104,7 @@ internal static B2Manifold b2ChainSegmentAndCircleManifold(B2Shape shapeA, in B2
104104

105105
internal static B2Manifold b2ChainSegmentAndCapsuleManifold(B2Shape shapeA, in B2Transform xfA, B2Shape shapeB, in B2Transform xfB, ref B2SimplexCache cache)
106106
{
107-
return b2CollideChainSegmentAndCapsule(ref shapeA.us.chainSegment, xfA, ref shapeB.us.capsule, xfB, ref cache);
107+
return b2CollideChainSegmentAndCapsule(ref shapeA.us.chainSegment, xfA, shapeB.us.capsule, xfB, ref cache);
108108
}
109109

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

src/Box2D.NET/B2Manifolds.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public static B2Manifold b2CollideCircles(ref B2Circle circleA, B2Transform xfA,
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(ref B2Capsule capsuleA, B2Transform xfA, ref B2Circle circleB, B2Transform xfB)
83+
public static B2Manifold b2CollideCapsuleAndCircle(in B2Capsule capsuleA, B2Transform xfA, ref B2Circle circleB, B2Transform xfB)
8484
{
8585
B2Manifold manifold = new B2Manifold();
8686

@@ -266,7 +266,7 @@ public static B2Manifold b2CollidePolygonAndCircle(ref B2Polygon polygonA, B2Tra
266266
/// Compute the contact manifold between a capsule and circle
267267
// Follows Ericson 5.1.9 Closest Points of Two Line Segments
268268
// Adds some logic to support clipping to get two contact points
269-
public static B2Manifold b2CollideCapsules(ref B2Capsule capsuleA, B2Transform xfA, ref B2Capsule capsuleB, B2Transform xfB)
269+
public static B2Manifold b2CollideCapsules(in B2Capsule capsuleA, B2Transform xfA, in B2Capsule capsuleB, B2Transform xfB)
270270
{
271271
B2Vec2 origin = capsuleA.center1;
272272

@@ -537,14 +537,14 @@ public static B2Manifold b2CollideCapsules(ref B2Capsule capsuleA, B2Transform x
537537
}
538538

539539
/// Compute the contact manifold between an segment and a capsule
540-
public static B2Manifold b2CollideSegmentAndCapsule(ref B2Segment segmentA, B2Transform xfA, ref B2Capsule capsuleB, B2Transform xfB)
540+
public static B2Manifold b2CollideSegmentAndCapsule(ref B2Segment segmentA, B2Transform xfA, in B2Capsule capsuleB, B2Transform xfB)
541541
{
542542
B2Capsule capsuleA = new B2Capsule(segmentA.point1, segmentA.point2, 0.0f);
543-
return b2CollideCapsules(ref capsuleA, xfA, ref capsuleB, xfB);
543+
return b2CollideCapsules(capsuleA, xfA, capsuleB, xfB);
544544
}
545545

546546
/// Compute the contact manifold between a polygon and capsule
547-
public static B2Manifold b2CollidePolygonAndCapsule(ref B2Polygon polygonA, B2Transform xfA, ref B2Capsule capsuleB, B2Transform xfB)
547+
public static B2Manifold b2CollidePolygonAndCapsule(ref B2Polygon polygonA, B2Transform xfA, in B2Capsule capsuleB, B2Transform xfB)
548548
{
549549
B2Polygon polyB = b2MakeCapsule(capsuleB.center1, capsuleB.center2, capsuleB.radius);
550550
return b2CollidePolygons(ref polygonA, xfA, ref polyB, xfB);
@@ -1091,7 +1091,7 @@ public static B2Manifold b2CollidePolygons(ref B2Polygon polygonA, B2Transform x
10911091
public static B2Manifold b2CollideSegmentAndCircle(ref B2Segment segmentA, B2Transform xfA, ref B2Circle circleB, B2Transform xfB)
10921092
{
10931093
B2Capsule capsuleA = new B2Capsule(segmentA.point1, segmentA.point2, 0.0f);
1094-
return b2CollideCapsuleAndCircle(ref capsuleA, xfA, ref circleB, xfB);
1094+
return b2CollideCapsuleAndCircle(capsuleA, xfA, ref circleB, xfB);
10951095
}
10961096

10971097
/// Compute the contact manifold between an segment and a polygon
@@ -1190,7 +1190,7 @@ public static B2Manifold b2CollideChainSegmentAndCircle(ref B2ChainSegment segme
11901190
}
11911191

11921192
/// Compute the contact manifold between a chain segment and a capsule
1193-
public static B2Manifold b2CollideChainSegmentAndCapsule(ref B2ChainSegment segmentA, B2Transform xfA, ref B2Capsule capsuleB, B2Transform xfB, ref B2SimplexCache cache)
1193+
public static B2Manifold b2CollideChainSegmentAndCapsule(ref B2ChainSegment segmentA, B2Transform xfA, in B2Capsule capsuleB, B2Transform xfB, ref B2SimplexCache cache)
11941194
{
11951195
B2Polygon polyB = b2MakeCapsule(capsuleB.center1, capsuleB.center2, capsuleB.radius);
11961196
return b2CollideChainSegmentAndPolygon(ref segmentA, xfA, ref polyB, xfB, ref cache);

src/Box2D.NET/B2Shapes.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1478,7 +1478,7 @@ public static void b2Shape_SetCircle(in B2ShapeId shapeId, ref B2Circle circle)
14781478
b2ResetProxy(world, shape, wakeBodies, destroyProxy);
14791479
}
14801480

1481-
public static void b2Shape_SetCapsule(in B2ShapeId shapeId, ref B2Capsule capsule)
1481+
public static void b2Shape_SetCapsule(in B2ShapeId shapeId, in B2Capsule capsule)
14821482
{
14831483
B2World world = b2GetWorldLocked(shapeId.world0);
14841484
if (world == null)

src/Box2D.NET/B2Worlds.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,7 +2177,7 @@ internal static float MoverCastCallback(in B2ShapeCastInput input, int proxyId,
21772177

21782178
/// Cast a capsule mover through the world. This is a special shape cast that handles sliding along other shapes while reducing
21792179
/// clipping.
2180-
public static float b2World_CastMover(B2WorldId worldId, ref B2Capsule mover, B2Vec2 translation, B2QueryFilter filter)
2180+
public static float b2World_CastMover(B2WorldId worldId, in B2Capsule mover, B2Vec2 translation, B2QueryFilter filter)
21812181
{
21822182
B2_ASSERT(b2IsValidVec2(translation));
21832183
B2_ASSERT(mover.radius > 2.0f * B2_LINEAR_SLOP);
@@ -2251,7 +2251,7 @@ internal static bool TreeCollideCallback(int proxyId, ulong userData, ref B2Worl
22512251
/// kinematic character movement
22522252
// It is tempting to use a shape proxy for the mover, but this makes handling deep overlap difficult and the generality may
22532253
// not be worth it.
2254-
public static void b2World_CollideMover(B2WorldId worldId, ref B2Capsule mover, B2QueryFilter filter, b2PlaneResultFcn fcn, object context)
2254+
public static void b2World_CollideMover(B2WorldId worldId, in B2Capsule mover, B2QueryFilter filter, b2PlaneResultFcn fcn, object context)
22552255
{
22562256
B2World world = b2GetWorldFromId(worldId);
22572257
B2_ASSERT(world.locked == false);

0 commit comments

Comments
 (0)