diff --git a/Physics2D.Diagnostics/Diagnostics/DebugView.cs b/Physics2D.Diagnostics/Diagnostics/DebugView.cs index 3c4d6a01..95478522 100644 --- a/Physics2D.Diagnostics/Diagnostics/DebugView.cs +++ b/Physics2D.Diagnostics/Diagnostics/DebugView.cs @@ -516,13 +516,13 @@ public override void DrawPolygon(Vector2[] vertices, int count, Color color, boo for (int i = 0; i < count - 1; i++) { - _primitiveBatch.AddVertex(vertices[i], color, PrimitiveType.LineList); - _primitiveBatch.AddVertex(vertices[i + 1], color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref vertices[i], color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref vertices[i + 1], color, PrimitiveType.LineList); } if (closed) { - _primitiveBatch.AddVertex(vertices[count - 1], color, PrimitiveType.LineList); - _primitiveBatch.AddVertex(vertices[0], color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref vertices[count - 1], color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref vertices[0], color, PrimitiveType.LineList); } } @@ -546,9 +546,9 @@ public void DrawSolidPolygon(Vector2[] vertices, int count, Color color, bool ou for (int i = 1; i < count - 1; i++) { - _primitiveBatch.AddVertex(vertices[0], colorFill, PrimitiveType.TriangleList); - _primitiveBatch.AddVertex(vertices[i], colorFill, PrimitiveType.TriangleList); - _primitiveBatch.AddVertex(vertices[i + 1], colorFill, PrimitiveType.TriangleList); + _primitiveBatch.AddVertex(ref vertices[0], colorFill, PrimitiveType.TriangleList); + _primitiveBatch.AddVertex(ref vertices[i], colorFill, PrimitiveType.TriangleList); + _primitiveBatch.AddVertex(ref vertices[i + 1], colorFill, PrimitiveType.TriangleList); } if (outline) @@ -561,19 +561,22 @@ public override void DrawCircle(Vector2 center, float radius, Color color) throw new InvalidOperationException("BeginCustomDraw must be called before drawing anything."); Vector2 v2 = new Vector2(radius, 0); - var center_vS = center + v2; + var center_v2 = center + v2; + var center_vS = center_v2; for (int i = 0; i < CircleSegments - 1; i++) { Vector2 v1 = v2; - v2 = Complex.Multiply(ref v1, ref circleSegmentRotation); + var center_v1 = center_v2; + Complex.Multiply(ref v1, ref circleSegmentRotation, out v2); + Vector2.Add(ref center, ref v2, out center_v2); - _primitiveBatch.AddVertex(center + v1, color, PrimitiveType.LineList); - _primitiveBatch.AddVertex(center + v2, color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref center_v1, color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref center_v2, color, PrimitiveType.LineList); } // Close Circle - _primitiveBatch.AddVertex(center+v2, color, PrimitiveType.LineList); - _primitiveBatch.AddVertex(center_vS, color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref center_v2, color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref center_vS, color, PrimitiveType.LineList); } public override void DrawSolidCircle(Vector2 center, float radius, Vector2 axis, Color color) @@ -582,30 +585,33 @@ public override void DrawSolidCircle(Vector2 center, float radius, Vector2 axis, throw new InvalidOperationException("BeginCustomDraw must be called before drawing anything."); Vector2 v2 = new Vector2(radius, 0); - var center_vS = center + v2; + var center_v2 = center + v2; + var center_vS = center_v2; Color colorFill = color * 0.5f; for (int i = 0; i < CircleSegments-1; i++) { Vector2 v1 = v2; - v2 = Complex.Multiply(ref v1, ref circleSegmentRotation); + var center_v1 = center_v2; + Complex.Multiply(ref v1, ref circleSegmentRotation, out v2); + Vector2.Add(ref center, ref v2, out center_v2); // Draw Circle - _primitiveBatch.AddVertex(center + v1, color, PrimitiveType.LineList); - _primitiveBatch.AddVertex(center + v2, color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref center_v1, color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref center_v2, color, PrimitiveType.LineList); // Draw Solid Circle if (i > 0) { - _primitiveBatch.AddVertex(center_vS, colorFill, PrimitiveType.TriangleList); - _primitiveBatch.AddVertex(center+v1, colorFill, PrimitiveType.TriangleList); - _primitiveBatch.AddVertex(center+v2, colorFill, PrimitiveType.TriangleList); + _primitiveBatch.AddVertex(ref center_vS, colorFill, PrimitiveType.TriangleList); + _primitiveBatch.AddVertex(ref center_v1, colorFill, PrimitiveType.TriangleList); + _primitiveBatch.AddVertex(ref center_v2, colorFill, PrimitiveType.TriangleList); } } // Close Circle - _primitiveBatch.AddVertex(center+v2, color, PrimitiveType.LineList); - _primitiveBatch.AddVertex(center_vS, color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref center_v2, color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref center_vS, color, PrimitiveType.LineList); DrawSegment(center, center + axis * radius, color); } @@ -615,8 +621,8 @@ public override void DrawSegment(Vector2 start, Vector2 end, Color color) if (!_primitiveBatch.IsReady()) throw new InvalidOperationException("BeginCustomDraw must be called before drawing anything."); - _primitiveBatch.AddVertex(start, color, PrimitiveType.LineList); - _primitiveBatch.AddVertex(end, color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref start, color, PrimitiveType.LineList); + _primitiveBatch.AddVertex(ref end, color, PrimitiveType.LineList); } public override void DrawTransform(ref Transform transform) diff --git a/Physics2D.Diagnostics/Diagnostics/IPrimitiveBatch.cs b/Physics2D.Diagnostics/Diagnostics/IPrimitiveBatch.cs index 908a0a0e..e57a7359 100644 --- a/Physics2D.Diagnostics/Diagnostics/IPrimitiveBatch.cs +++ b/Physics2D.Diagnostics/Diagnostics/IPrimitiveBatch.cs @@ -13,5 +13,7 @@ public interface IPrimitiveBatch bool IsReady(); int AddVertex(Vector3 position, Color color, PrimitiveType primitiveType); int AddVertex(Vector2 position, Color color, PrimitiveType primitiveType); + int AddVertex(ref Vector2 position, Color color, PrimitiveType primitiveType); + int AddVertex(ref Vector3 position, Color color, PrimitiveType primitiveType); } } diff --git a/Physics2D.Diagnostics/Diagnostics/PrimitiveBatch.cs b/Physics2D.Diagnostics/Diagnostics/PrimitiveBatch.cs index 4d1546f1..01eaccef 100644 --- a/Physics2D.Diagnostics/Diagnostics/PrimitiveBatch.cs +++ b/Physics2D.Diagnostics/Diagnostics/PrimitiveBatch.cs @@ -118,6 +118,11 @@ public bool IsReady() } public int AddVertex(Vector3 position, Color color, PrimitiveType primitiveType) + { + return AddVertex(ref position, color, primitiveType); + } + + public int AddVertex(ref Vector3 position, Color color, PrimitiveType primitiveType) { if (!_hasBegun) throw new InvalidOperationException("Begin must be called before AddVertex can be called."); @@ -144,8 +149,14 @@ public int AddVertex(Vector3 position, Color color, PrimitiveType primitiveType) throw new NotSupportedException("The specified primitiveType is not supported by PrimitiveBatch."); } } - + + public int AddVertex(Vector2 position, Color color, PrimitiveType primitiveType) + { + return AddVertex(ref position, color, primitiveType); + } + + public int AddVertex(ref Vector2 position, Color color, PrimitiveType primitiveType) { if (!_hasBegun) throw new InvalidOperationException("Begin must be called before AddVertex can be called."); @@ -156,7 +167,9 @@ public int AddVertex(Vector2 position, Color color, PrimitiveType primitiveType) if (_triangleVertsCount >= _triangleVertices.Length) FlushTriangles(); - _triangleVertices[_triangleVertsCount].Position = new Vector3(position,DefaultTriangleListDepth); + _triangleVertices[_triangleVertsCount].Position.X = position.X; + _triangleVertices[_triangleVertsCount].Position.Y = position.Y; + _triangleVertices[_triangleVertsCount].Position.Z = DefaultTriangleListDepth; _triangleVertices[_triangleVertsCount].Color = color; return _triangleVertsCount++; @@ -164,7 +177,9 @@ public int AddVertex(Vector2 position, Color color, PrimitiveType primitiveType) if (_lineVertsCount >= _lineVertices.Length) FlushLines(); - _lineVertices[_lineVertsCount].Position = new Vector3(position,DefaultLineListDepth); + _lineVertices[_lineVertsCount].Position.X = position.X; + _lineVertices[_lineVertsCount].Position.Y = position.Y; + _lineVertices[_lineVertsCount].Position.Z = DefaultLineListDepth; _lineVertices[_lineVertsCount].Color = color; return _lineVertsCount++;