Skip to content

Commit 683dacb

Browse files
committed
refactor: B2Transform usage to use 'in' parameters
- Updated collision, joint, and shape functions (e.g., `b2CollideCircles`, `b2DrawDistanceJoint`) to accept `B2Transform` as `in` parameter. - Optimized internal geometry and rendering helpers to use `in B2Transform` for better performance and read-only safety. - Updated Samples and data structures to align with the API changes. - This refactoring reduces unnecessary struct copying and enforces immutability for transform data in these contexts.
1 parent 39e89df commit 683dacb

File tree

16 files changed

+37
-37
lines changed

16 files changed

+37
-37
lines changed

src/Box2D.NET.Samples/Graphics/Draws.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static void DrawSolidPolygon(Draw draw, in B2Transform transform, ReadOnl
6363
AddPolygon(ref draw.polygons, transform, vertices, vertexCount, radius, color);
6464
}
6565

66-
public static void DrawTransform(Draw draw, B2Transform transform, float scale)
66+
public static void DrawTransform(Draw draw, in B2Transform transform, float scale)
6767
{
6868
B2Vec2 p1 = transform.p;
6969

@@ -103,9 +103,9 @@ public static void DrawCircle(Draw draw, B2Vec2 center, float radius, B2HexColor
103103
AddCircle(ref draw.hollowCircles, center, radius, color);
104104
}
105105

106-
public static void DrawSolidCircle(Draw draw, B2Transform transform, float radius, B2HexColor color)
106+
public static void DrawSolidCircle(Draw draw, in B2Transform transform, float radius, B2HexColor color)
107107
{
108-
AddSolidCircle(ref draw.circles, ref transform, radius, color);
108+
AddSolidCircle(ref draw.circles, transform, radius, color);
109109
}
110110

111111
public static void DrawSolidCapsule(Draw draw, B2Vec2 p1, B2Vec2 p2, float radius, B2HexColor color)

src/Box2D.NET.Samples/Graphics/SolidCircles.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public static void DestroySolidCircles(GL gl, ref SolidCircleRender render)
9696
}
9797
}
9898

99-
public static void AddSolidCircle(ref SolidCircleRender render, ref B2Transform transform, float radius, B2HexColor color)
99+
public static void AddSolidCircle(ref SolidCircleRender render, in B2Transform transform, float radius, B2HexColor color)
100100
{
101101
RGBA8 rgba = RGBA8.MakeRGBA8(color, 1.0f);
102102
render.circles.Add(new SolidCircleData(transform, radius, rgba));

src/Box2D.NET.Samples/Primitives/CapsuleData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public struct CapsuleData
1111
public float length;
1212
public RGBA8 rgba;
1313

14-
public CapsuleData(B2Transform transform, float radius, float length, RGBA8 rgba)
14+
public CapsuleData(in B2Transform transform, float radius, float length, RGBA8 rgba)
1515
{
1616
this.transform = transform;
1717
this.radius = radius;

src/Box2D.NET.Samples/Primitives/SolidCircleData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public struct SolidCircleData
1010
public float radius;
1111
public RGBA8 rgba;
1212

13-
public SolidCircleData(B2Transform transform, float radius, RGBA8 rgba)
13+
public SolidCircleData(in B2Transform transform, float radius, RGBA8 rgba)
1414
{
1515
this.transform = transform;
1616
this.radius = radius;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ private B2ShapeProxy MakeProxy(ShapeType type, float radius)
189189
return proxy;
190190
}
191191

192-
private void DrawShape(ShapeType type, B2Transform transform, float radius, B2HexColor color)
192+
private void DrawShape(ShapeType type, in B2Transform transform, float radius, B2HexColor color)
193193
{
194194
switch (type)
195195
{

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ B2ShapeProxy MakeProxy(ShapeType type, float radius)
164164
return proxy;
165165
}
166166

167-
void DrawShape(ShapeType type, ref B2Transform transform, float radius, B2HexColor color)
167+
void DrawShape(ShapeType type, in B2Transform transform, float radius, B2HexColor color)
168168
{
169169
switch (type)
170170
{
@@ -389,8 +389,8 @@ public override void Draw()
389389
base.Draw();
390390

391391
var empty = b2Transform_identity;
392-
DrawShape(m_typeA, ref empty, m_radiusA, B2HexColor.b2_colorCyan);
393-
DrawShape(m_typeB, ref m_transform, m_radiusB, B2HexColor.b2_colorBisque);
392+
DrawShape(m_typeA, empty, m_radiusA, B2HexColor.b2_colorCyan);
393+
DrawShape(m_typeB, m_transform, m_radiusB, B2HexColor.b2_colorBisque);
394394

395395
if (m_drawSimplex)
396396
{

src/Box2D.NET.Samples/Samples/Issues/ShapeCastChain.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ private bool ShapeCastSingle(ref PhysicsHitQueryResult2D outResult, B2Vec2 start
216216
start,
217217
b2MakeRot(rotation)
218218
);
219-
B2ShapeProxy transformedShape = TransformShapeProxy(ref transform, ref shape);
219+
B2ShapeProxy transformedShape = TransformShapeProxy(transform, ref shape);
220220

221221
B2Vec2 translation = new B2Vec2(end.X - start.X, end.Y - start.Y);
222222
B2QueryFilter filter = new B2QueryFilter(0x1, 0x1);
@@ -252,7 +252,7 @@ private static float b2CastResult_Closest(in B2ShapeId shapeId, B2Vec2 point, B2
252252
return fraction;
253253
}
254254

255-
private static B2ShapeProxy TransformShapeProxy(ref B2Transform t, ref B2ShapeProxy proxy)
255+
private static B2ShapeProxy TransformShapeProxy(in B2Transform t, ref B2ShapeProxy proxy)
256256
{
257257
B2ShapeProxy ret = new B2ShapeProxy();
258258
ret.count = proxy.count;

src/Box2D.NET/B2DistanceJoints.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ internal static void b2SolveDistanceJoint(B2JointSim @base, B2StepContext contex
547547
}
548548
#endif
549549

550-
internal static void b2DrawDistanceJoint(B2DebugDraw draw, B2JointSim @base, B2Transform transformA, B2Transform transformB)
550+
internal static void b2DrawDistanceJoint(B2DebugDraw draw, B2JointSim @base, in B2Transform transformA, in B2Transform transformB)
551551
{
552552
B2_ASSERT(@base.type == B2JointType.b2_distanceJoint);
553553

src/Box2D.NET/B2Geometries.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ public static B2AABB b2ComputePolygonAABB(in B2Polygon shape, in B2Transform xf)
477477
}
478478

479479
/// Compute the bounding box of a transformed line segment
480-
public static B2AABB b2ComputeSegmentAABB(in B2Segment shape, B2Transform xf)
480+
public static B2AABB b2ComputeSegmentAABB(in B2Segment shape, in B2Transform xf)
481481
{
482482
B2Vec2 v1 = b2TransformPoint(xf, shape.point1);
483483
B2Vec2 v2 = b2TransformPoint(xf, shape.point2);

src/Box2D.NET/B2Joints.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ public static B2WorldId b2Joint_GetWorld(B2JointId jointId)
790790
}
791791

792792
/// Set the local frame on bodyA
793-
public static void b2Joint_SetLocalFrameA(B2JointId jointId, B2Transform localFrame)
793+
public static void b2Joint_SetLocalFrameA(B2JointId jointId, in B2Transform localFrame)
794794
{
795795
B2_ASSERT(b2IsValidTransform(localFrame));
796796

@@ -810,7 +810,7 @@ public static B2Transform b2Joint_GetLocalFrameA(B2JointId jointId)
810810
}
811811

812812
/// Set the local frame on bodyB
813-
public static void b2Joint_SetLocalFrameB(B2JointId jointId, B2Transform localFrame)
813+
public static void b2Joint_SetLocalFrameB(B2JointId jointId, in B2Transform localFrame)
814814
{
815815
B2_ASSERT(b2IsValidTransform(localFrame));
816816

0 commit comments

Comments
 (0)