@series-inc/rundot-syncplay 6.0.0-rc.17 → 6.0.0-rc.18

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -127,6 +127,52 @@ Vec2 closestPointOnEdge(const Vec2& p, const ContourEdge2D& edge) noexcept {
127
127
  return closestPointOnArc(p, edge.center, edge.arcRadius, edge.quadrant, edge.from, edge.to);
128
128
  }
129
129
 
130
+
131
+ double segmentArcDistance(const Vec2& p1, const Vec2& p2, const ContourEdge2D& edge, double skin) noexcept {
132
+ const Vec2 cpFrom = contourClosestPointOnSegment(edge.from, p1, p2);
133
+ double minDist = length(subtract(cpFrom, edge.from));
134
+
135
+ const Vec2 cpTo = contourClosestPointOnSegment(edge.to, p1, p2);
136
+ const double distTo = length(subtract(cpTo, edge.to));
137
+ if (distTo < minDist) minDist = distTo;
138
+
139
+ const Vec2 cpCenter = contourClosestPointOnSegment(edge.center, p1, p2);
140
+ const Vec2 dCenter = subtract(cpCenter, edge.center);
141
+ if (inSector(dCenter, edge.quadrant)) {
142
+ const double d = std::abs(length(dCenter) - edge.arcRadius);
143
+ if (d < minDist) minDist = d;
144
+ }
145
+
146
+ const Vec2 dir = subtract(p2, p1);
147
+ const double L2 = lengthSquared(dir);
148
+ if (L2 > 1e-12) {
149
+ const Vec2 f = subtract(p1, edge.center);
150
+ const double a = L2;
151
+ const double b = 2.0 * dot(f, dir);
152
+ const double c = lengthSquared(f) - edge.arcRadius * edge.arcRadius;
153
+ const double disc = b * b - 4.0 * a * c;
154
+ if (disc >= 0.0) {
155
+ const double sqrtDisc = deterministicSqrt(disc);
156
+ const double t1 = (-b - sqrtDisc) / (2.0 * a);
157
+ const double t2 = (-b + sqrtDisc) / (2.0 * a);
158
+ if (t1 >= 0.0 && t1 <= 1.0) {
159
+ const Vec2 pt1 = add(p1, scale(dir, t1));
160
+ if (inSector(subtract(pt1, edge.center), edge.quadrant)) {
161
+ minDist = 0.0;
162
+ }
163
+ }
164
+ if (minDist > 0.0 && t2 >= 0.0 && t2 <= 1.0) {
165
+ const Vec2 pt2 = add(p1, scale(dir, t2));
166
+ if (inSector(subtract(pt2, edge.center), edge.quadrant)) {
167
+ minDist = 0.0;
168
+ }
169
+ }
170
+ }
171
+ }
172
+
173
+ return minDist - skin;
174
+ }
175
+
130
176
  double edgeDistance(
131
177
  const Vec2& center,
132
178
  double radius,
@@ -172,15 +218,28 @@ bool rayEdge(
172
218
  const double disc = b * b - c;
173
219
  if (disc < 0.0) return false;
174
220
  const double sqrtDisc = deterministicSqrt(disc);
175
- double t_hit = -b - sqrtDisc;
176
- if (t_hit < 0.0) t_hit = -b + sqrtDisc;
177
- if (t_hit < 0.0 || t_hit > maxT) return false;
178
- const Vec2 hitP = add(origin, scale(dir, t_hit));
179
- const Vec2 d = subtract(hitP, edge.center);
180
- if (!inSector(d, edge.quadrant)) return false;
181
- if (hitT != nullptr) *hitT = t_hit;
221
+ const double t1 = -b - sqrtDisc;
222
+ const double t2 = -b + sqrtDisc;
223
+ double hitT_val = -1.0;
224
+ Vec2 hitD{};
225
+ if (t1 >= 0.0 && t1 <= maxT) {
226
+ const Vec2 d1 = subtract(add(origin, scale(dir, t1)), edge.center);
227
+ if (inSector(d1, edge.quadrant)) {
228
+ hitT_val = t1;
229
+ hitD = d1;
230
+ }
231
+ }
232
+ if (hitT_val < 0.0 && t2 >= 0.0 && t2 <= maxT) {
233
+ const Vec2 d2 = subtract(add(origin, scale(dir, t2)), edge.center);
234
+ if (inSector(d2, edge.quadrant)) {
235
+ hitT_val = t2;
236
+ hitD = d2;
237
+ }
238
+ }
239
+ if (hitT_val < 0.0) return false;
240
+ if (hitT != nullptr) *hitT = hitT_val;
182
241
  if (hitNormal != nullptr) {
183
- const Vec2 rad = normalize(d, {1.0, 0.0});
242
+ const Vec2 rad = normalize(hitD, {1.0, 0.0});
184
243
  *hitNormal = isCcw ? rad : scale(rad, -1.0);
185
244
  }
186
245
  return true;
@@ -244,7 +303,9 @@ void computeEdgeVertexIndices(
244
303
  for (std::uint32_t loopIdx = 0; loopIdx < header.loopCount; ++loopIdx) {
245
304
  const std::uint32_t expectedLoopNum = loopIdx + 1;
246
305
  const std::uint32_t loopStartEdge = edgeIdx;
247
- while (edgeIdx < header.edgeCount && edges[edgeIdx].loopIndex == expectedLoopNum) {
306
+ const std::uint32_t totalEdges = header.reserved > 0 ? header.reserved : header.edgeCount;
307
+ while (edgeIdx < totalEdges && edges[edgeIdx].loopIndex == expectedLoopNum) {
308
+ if (edgeIdx >= kMaxContourTotalEdges) break;
248
309
  edgeIdx++;
249
310
  }
250
311
  const std::uint32_t k = edgeIdx - loopStartEdge;
@@ -253,6 +314,7 @@ void computeEdgeVertexIndices(
253
314
  const std::uint32_t loopStartVertex = currentGlobalVertex;
254
315
  for (std::uint32_t j = 0; j < k; ++j) {
255
316
  const std::uint32_t e = loopStartEdge + j;
317
+ if (e >= kMaxContourTotalEdges) break;
256
318
  indices[e].startVertex = loopStartVertex + j;
257
319
  indices[e].endVertex = (closed && j == k - 1) ? loopStartVertex : (loopStartVertex + j + 1);
258
320
  }
@@ -50,6 +50,8 @@ Vec2 closestPointOnArc(
50
50
 
51
51
  Vec2 closestPointOnEdge(const Vec2& p, const ContourEdge2D& edge) noexcept;
52
52
 
53
+ double segmentArcDistance(const Vec2& p1, const Vec2& p2, const ContourEdge2D& edge, double skin) noexcept;
54
+
53
55
  double edgeDistance(
54
56
  const Vec2& center,
55
57
  double radius,
@@ -2187,6 +2187,7 @@ bool onFreeSideContourInternal(
2187
2187
  const double d1 = dot(subtract(endpoints[1], edge.from), n);
2188
2188
  return d0 >= -1e-4 || d1 >= -1e-4;
2189
2189
  }
2190
+
2190
2191
  const Vec2 c = center(context, shapeBody);
2191
2192
  return dot(subtract(c, edge.from), n) >= -1e-4;
2192
2193
  }
@@ -2196,11 +2197,14 @@ bool onFreeSideContourInternal(
2196
2197
  const double R = edge.arcRadius;
2197
2198
  if (shapeBody.shape == ShapeType::Capsule) {
2198
2199
  const auto endpoints = capsuleEndpoints(shapeBody);
2200
+ const Vec2 cp = closestPointOnSegment(edge.center, endpoints[0], endpoints[1]);
2201
+ const double d_cp = length(subtract(cp, edge.center));
2199
2202
  const double d0 = length(subtract(endpoints[0], edge.center));
2200
2203
  const double d1 = length(subtract(endpoints[1], edge.center));
2201
- return isCcw ? (d0 >= R - 1e-4 || d1 >= R - 1e-4)
2202
- : (d0 <= R + 1e-4 || d1 <= R + 1e-4);
2204
+ return isCcw ? (d_cp >= R - 1e-4 || d0 >= R - 1e-4 || d1 >= R - 1e-4)
2205
+ : (d_cp <= R + 1e-4 || d0 <= R + 1e-4 || d1 <= R + 1e-4);
2203
2206
  }
2207
+
2204
2208
  const Vec2 c = center(context, shapeBody);
2205
2209
  const double d = length(subtract(c, edge.center));
2206
2210
  return isCcw ? (d >= R - 1e-4) : (d <= R + 1e-4);
@@ -2230,10 +2234,12 @@ bool onFreeSideContourInternal(
2230
2234
  const double R = edge.arcRadius;
2231
2235
  if (shapeBody.shape == ShapeType::Capsule) {
2232
2236
  const auto endpoints = capsuleEndpoints(shapeBody);
2237
+ const Vec2 cp = closestPointOnSegment(edge.center, endpoints[0], endpoints[1]);
2238
+ const double d_cp = length(subtract(cp, edge.center));
2233
2239
  const double d0 = length(subtract(endpoints[0], edge.center));
2234
2240
  const double d1 = length(subtract(endpoints[1], edge.center));
2235
- return isCcw ? (d0 >= R - 1e-4 || d1 >= R - 1e-4)
2236
- : (d0 <= R + 1e-4 || d1 <= R + 1e-4);
2241
+ return isCcw ? (d_cp >= R - 1e-4 || d0 >= R - 1e-4 || d1 >= R - 1e-4)
2242
+ : (d_cp <= R + 1e-4 || d0 <= R + 1e-4 || d1 <= R + 1e-4);
2237
2243
  }
2238
2244
  const Vec2 c = {shapeBody.x, shapeBody.y};
2239
2245
  const double d = length(subtract(c, edge.center));
@@ -2507,11 +2513,35 @@ Manifold collideShapeWithContourInternal(
2507
2513
  const ContourEdge2D* edges = contourEdgesForInternal(context, *header);
2508
2514
  if (edges == nullptr) return Manifold{};
2509
2515
 
2510
- EdgeVertexIndices indices[kMaxContourEdges];
2516
+ const Vec2 contourOrigin{contourBody.x, contourBody.y};
2517
+ const double contourAngle = contourBody.angle;
2518
+ Body transformedShape = shapeBody;
2519
+ if (contourOrigin.x != 0.0 || contourOrigin.y != 0.0 || contourAngle != 0.0) {
2520
+ const Vec2 rel = subtract({shapeBody.x, shapeBody.y}, contourOrigin);
2521
+ const Vec2 rot = inverseRotate(rel, contourAngle);
2522
+ transformedShape.x = rot.x;
2523
+ transformedShape.y = rot.y;
2524
+ transformedShape.angle = shapeBody.angle - contourAngle;
2525
+ }
2526
+ const Body& effShapeBody = transformedShape;
2527
+
2528
+ auto transformManifoldToWorld = [&](Manifold m) -> Manifold {
2529
+ if (contourOrigin.x == 0.0 && contourOrigin.y == 0.0 && contourAngle == 0.0) return m;
2530
+ if (m.hit) {
2531
+ m.normal = rotate(m.normal, contourAngle);
2532
+ for (std::uint32_t i = 0; i < m.pointCount; ++i) {
2533
+ m.points[i].position = add(contourOrigin, rotate(m.points[i].position, contourAngle));
2534
+ }
2535
+ }
2536
+ return m;
2537
+ };
2538
+
2539
+ const std::uint32_t totalEdges = header->reserved > 0 ? header->reserved : header->edgeCount;
2540
+ EdgeVertexIndices indices[kMaxContourTotalEdges];
2511
2541
  computeEdgeVertexIndices(*header, edges, indices);
2512
2542
 
2513
- TangentPair tangents[kMaxContourEdges];
2514
- for (std::uint32_t i = 0; i < header->edgeCount; ++i) {
2543
+ TangentPair tangents[kMaxContourTotalEdges];
2544
+ for (std::uint32_t i = 0; i < totalEdges; ++i) {
2515
2545
  tangents[i] = getEdgeTangents(edges[i]);
2516
2546
  }
2517
2547
 
@@ -2521,14 +2551,14 @@ Manifold collideShapeWithContourInternal(
2521
2551
 
2522
2552
  const double skin = header->skin;
2523
2553
 
2524
- if (shapeBody.shape == ShapeType::Circle) {
2525
- const Vec2 C = {shapeBody.x, shapeBody.y};
2526
- const double r = shapeBody.radius;
2554
+ if (effShapeBody.shape == ShapeType::Circle) {
2555
+ const Vec2 C = {effShapeBody.x, effShapeBody.y};
2556
+ const double r = effShapeBody.radius;
2527
2557
 
2528
- for (std::uint32_t e = 0; e < header->edgeCount; ++e) {
2558
+ for (std::uint32_t e = 0; e < totalEdges; ++e) {
2529
2559
  const auto& edge = edges[e];
2530
2560
  if (edge.kind == 2) continue;
2531
- if (!onFreeSideContourInternal(context, shapeBody, edge)) continue;
2561
+ if (!onFreeSideContourInternal(context, effShapeBody, edge)) continue;
2532
2562
 
2533
2563
  if (edge.kind == 0) { // Line
2534
2564
  const Vec2 e_vec = subtract(edge.to, edge.from);
@@ -2569,6 +2599,24 @@ Manifold collideShapeWithContourInternal(
2569
2599
  }
2570
2600
  }
2571
2601
  }
2602
+ } else if (s > L) {
2603
+ const bool isClosed = (edge.flags & 1) != 0;
2604
+ const bool isLastInOpenLoop = !isClosed && (e + 1 >= totalEdges || edges[e + 1].loopIndex != edge.loopIndex || edges[e + 1].kind == 2);
2605
+ if (isLastInOpenLoop) {
2606
+ const Vec2 d_to = subtract(C, edge.to);
2607
+ const double dist_to = length(d_to);
2608
+ const double pen_to = (r + skin) - dist_to;
2609
+ if (pen_to > 0.0 && dist_to > 1e-12) {
2610
+ const Vec2 norm_to = scale(d_to, 1.0 / dist_to);
2611
+ const Vec2 pt = subtract(C, scale(norm_to, r - 0.5 * pen_to));
2612
+ const std::uint32_t featId = makeContactFeatureId(
2613
+ makeContactFeature(kFeatureTypeCurve, 0),
2614
+ makeContactFeature(kFeatureTypeVertex, indices[e].endVertex));
2615
+ if (candidateCount < kMaxCandidates) {
2616
+ candidates[candidateCount++] = {pt, C, r, norm_to, pen_to, featId};
2617
+ }
2618
+ }
2619
+ }
2572
2620
  }
2573
2621
  } else if (edge.kind == 1) { // Arc
2574
2622
  const ArcEndpoints ends = arcEndpoints(edge.center, edge.arcRadius, edge.quadrant);
@@ -2611,6 +2659,23 @@ Manifold collideShapeWithContourInternal(
2611
2659
  }
2612
2660
  }
2613
2661
  }
2662
+ const bool isClosed = (edge.flags & 1) != 0;
2663
+ const bool isLastInOpenLoop = !isClosed && (e + 1 >= totalEdges || edges[e + 1].loopIndex != edge.loopIndex || edges[e + 1].kind == 2);
2664
+ if (isLastInOpenLoop) {
2665
+ const Vec2 d_to = subtract(C, edge.to);
2666
+ const double dist_to = length(d_to);
2667
+ const double pen_to = (r + skin) - dist_to;
2668
+ if (pen_to > 0.0 && dist_to > 1e-12) {
2669
+ const Vec2 norm_to = scale(d_to, 1.0 / dist_to);
2670
+ const Vec2 pt = subtract(C, scale(norm_to, r - 0.5 * pen_to));
2671
+ const std::uint32_t featId = makeContactFeatureId(
2672
+ makeContactFeature(kFeatureTypeCurve, 0),
2673
+ makeContactFeature(kFeatureTypeVertex, indices[e].endVertex));
2674
+ if (candidateCount < kMaxCandidates) {
2675
+ candidates[candidateCount++] = {pt, C, r, norm_to, pen_to, featId};
2676
+ }
2677
+ }
2678
+ }
2614
2679
  }
2615
2680
  }
2616
2681
  } else { // Concave
@@ -2631,13 +2696,13 @@ Manifold collideShapeWithContourInternal(
2631
2696
  }
2632
2697
  }
2633
2698
  }
2634
- } else if (shapeBody.shape == ShapeType::Box || shapeBody.shape == ShapeType::Polygon) {
2699
+ } else if (effShapeBody.shape == ShapeType::Box || effShapeBody.shape == ShapeType::Polygon) {
2635
2700
  ConvexVertices verts;
2636
- if (verticesForBody(context, shapeBody, verts)) {
2637
- for (std::uint32_t e = 0; e < header->edgeCount; ++e) {
2701
+ if (verticesForBody(context, effShapeBody, verts)) {
2702
+ for (std::uint32_t e = 0; e < totalEdges; ++e) {
2638
2703
  const auto& edge = edges[e];
2639
2704
  if (edge.kind == 2) continue;
2640
- if (!onFreeSideContourInternal(context, shapeBody, edge)) continue;
2705
+ if (!onFreeSideContourInternal(context, effShapeBody, edge)) continue;
2641
2706
 
2642
2707
  if (edge.kind == 0) { // Line
2643
2708
  const Vec2 e_vec = subtract(edge.to, edge.from);
@@ -2698,19 +2763,42 @@ Manifold collideShapeWithContourInternal(
2698
2763
  }
2699
2764
  }
2700
2765
  }
2766
+
2767
+ // Also test polygon edges against convex arc (e.g. wide box face across arc apex)
2768
+ if (convex) {
2769
+ const double R_eff = edge.arcRadius + skin;
2770
+ for (std::uint32_t v = 0; v < verts.count; ++v) {
2771
+ const Vec2 p1 = verts.points[v];
2772
+ const Vec2 p2 = verts.points[(v + 1) % verts.count];
2773
+ const Vec2 cp = closestPointOnSegment(edge.center, p1, p2);
2774
+ const Vec2 d_vec = subtract(cp, edge.center);
2775
+ const double dist = length(d_vec);
2776
+ if (dist <= R_eff && inSector(d_vec, edge.quadrant) && dist > 1e-12) {
2777
+ const double pen = R_eff - dist;
2778
+ const Vec2 norm = scale(d_vec, 1.0 / dist);
2779
+ const Vec2 cPt = add(cp, scale(norm, 0.5 * pen));
2780
+ const std::uint32_t featId = makeContactFeatureId(
2781
+ makeContactFeature(kFeatureTypeEdge, v),
2782
+ makeContactFeature(kFeatureTypeCurve, e));
2783
+ if (candidateCount < kMaxCandidates) {
2784
+ candidates[candidateCount++] = {cPt, cp, 0.0, norm, pen, featId};
2785
+ }
2786
+ }
2787
+ }
2788
+ }
2701
2789
  }
2702
2790
  }
2703
2791
  }
2704
- } else if (shapeBody.shape == ShapeType::Capsule) {
2705
- const auto endpoints = capsuleEndpoints(shapeBody);
2706
- const double r = shapeBody.radius;
2792
+ } else if (effShapeBody.shape == ShapeType::Capsule) {
2793
+ const auto endpoints = capsuleEndpoints(effShapeBody);
2794
+ const double r = effShapeBody.radius;
2707
2795
 
2708
2796
  for (std::uint32_t endIdx = 0; endIdx < 2; ++endIdx) {
2709
2797
  const Vec2 C = endpoints[endIdx];
2710
- for (std::uint32_t e = 0; e < header->edgeCount; ++e) {
2798
+ for (std::uint32_t e = 0; e < totalEdges; ++e) {
2711
2799
  const auto& edge = edges[e];
2712
2800
  if (edge.kind == 2) continue;
2713
- if (!onFreeSideContourInternal(context, shapeBody, edge)) continue;
2801
+ if (!onFreeSideContourInternal(context, effShapeBody, edge)) continue;
2714
2802
 
2715
2803
  if (edge.kind == 0) {
2716
2804
  const Vec2 e_vec = subtract(edge.to, edge.from);
@@ -2730,6 +2818,46 @@ Manifold collideShapeWithContourInternal(
2730
2818
  if (candidateCount < kMaxCandidates) {
2731
2819
  candidates[candidateCount++] = {pt, C, r, n, pen, featId};
2732
2820
  }
2821
+ } else if (s < 0.0) {
2822
+ const Vec2 d_from = subtract(C, edge.from);
2823
+ const double dist_from = length(d_from);
2824
+ const double pen_from = (r + skin) - dist_from;
2825
+ if (pen_from > 0.0 && dist_from > 1e-12) {
2826
+ const Vec2 norm_from = scale(d_from, 1.0 / dist_from);
2827
+ bool allow = true;
2828
+ if (e > 0 && edges[e - 1].loopIndex == edge.loopIndex) {
2829
+ const Vec2 t_prev = tangents[e - 1].t_end;
2830
+ const Vec2 t_curr = tangents[e].t_start;
2831
+ if (cross(t_prev, t_curr) <= 1e-6) allow = false;
2832
+ }
2833
+ if (allow) {
2834
+ const Vec2 pt = subtract(C, scale(norm_from, r - 0.5 * pen_from));
2835
+ const std::uint32_t featId = makeContactFeatureId(
2836
+ makeContactFeature(kFeatureTypeCurve, endIdx),
2837
+ makeContactFeature(kFeatureTypeVertex, indices[e].startVertex));
2838
+ if (candidateCount < kMaxCandidates) {
2839
+ candidates[candidateCount++] = {pt, C, r, norm_from, pen_from, featId};
2840
+ }
2841
+ }
2842
+ }
2843
+ } else if (s > L) {
2844
+ const bool isClosed = (edge.flags & 1) != 0;
2845
+ const bool isLastInOpenLoop = !isClosed && (e + 1 >= totalEdges || edges[e + 1].loopIndex != edge.loopIndex || edges[e + 1].kind == 2);
2846
+ if (isLastInOpenLoop) {
2847
+ const Vec2 d_to = subtract(C, edge.to);
2848
+ const double dist_to = length(d_to);
2849
+ const double pen_to = (r + skin) - dist_to;
2850
+ if (pen_to > 0.0 && dist_to > 1e-12) {
2851
+ const Vec2 norm_to = scale(d_to, 1.0 / dist_to);
2852
+ const Vec2 pt = subtract(C, scale(norm_to, r - 0.5 * pen_to));
2853
+ const std::uint32_t featId = makeContactFeatureId(
2854
+ makeContactFeature(kFeatureTypeCurve, endIdx),
2855
+ makeContactFeature(kFeatureTypeVertex, indices[e].endVertex));
2856
+ if (candidateCount < kMaxCandidates) {
2857
+ candidates[candidateCount++] = {pt, C, r, norm_to, pen_to, featId};
2858
+ }
2859
+ }
2860
+ }
2733
2861
  }
2734
2862
  } else if (edge.kind == 1) {
2735
2863
  const ArcEndpoints ends = arcEndpoints(edge.center, edge.arcRadius, edge.quadrant);
@@ -2740,14 +2868,55 @@ Manifold collideShapeWithContourInternal(
2740
2868
  if (convex) {
2741
2869
  const double R_eff = edge.arcRadius + skin;
2742
2870
  const double pen = (R_eff + r) - dist_center;
2743
- if (pen > 0.0 && inSector(d_center, edge.quadrant) && dist_center > 1e-12) {
2744
- const Vec2 norm = scale(d_center, 1.0 / dist_center);
2745
- const Vec2 pt = subtract(C, scale(norm, r - 0.5 * pen));
2746
- const std::uint32_t featId = makeContactFeatureId(
2747
- makeContactFeature(kFeatureTypeCurve, endIdx),
2748
- makeContactFeature(kFeatureTypeCurve, e));
2749
- if (candidateCount < kMaxCandidates) {
2750
- candidates[candidateCount++] = {pt, C, r, norm, pen, featId};
2871
+ if (pen > 0.0) {
2872
+ if (inSector(d_center, edge.quadrant) && dist_center > 1e-12) {
2873
+ const Vec2 norm = scale(d_center, 1.0 / dist_center);
2874
+ const Vec2 pt = subtract(C, scale(norm, r - 0.5 * pen));
2875
+ const std::uint32_t featId = makeContactFeatureId(
2876
+ makeContactFeature(kFeatureTypeCurve, endIdx),
2877
+ makeContactFeature(kFeatureTypeCurve, e));
2878
+ if (candidateCount < kMaxCandidates) {
2879
+ candidates[candidateCount++] = {pt, C, r, norm, pen, featId};
2880
+ }
2881
+ } else {
2882
+ const Vec2 d_from = subtract(C, edge.from);
2883
+ const double dist_from = length(d_from);
2884
+ const double pen_from = (r + skin) - dist_from;
2885
+ if (pen_from > 0.0 && dist_from > 1e-12) {
2886
+ const Vec2 norm_from = scale(d_from, 1.0 / dist_from);
2887
+ bool allow = true;
2888
+ if (e > 0 && edges[e - 1].loopIndex == edge.loopIndex) {
2889
+ const Vec2 t_prev = tangents[e - 1].t_end;
2890
+ const Vec2 t_curr = tangents[e].t_start;
2891
+ if (cross(t_prev, t_curr) <= 1e-6) allow = false;
2892
+ }
2893
+ if (allow) {
2894
+ const Vec2 pt = subtract(C, scale(norm_from, r - 0.5 * pen_from));
2895
+ const std::uint32_t featId = makeContactFeatureId(
2896
+ makeContactFeature(kFeatureTypeCurve, endIdx),
2897
+ makeContactFeature(kFeatureTypeVertex, indices[e].startVertex));
2898
+ if (candidateCount < kMaxCandidates) {
2899
+ candidates[candidateCount++] = {pt, C, r, norm_from, pen_from, featId};
2900
+ }
2901
+ }
2902
+ }
2903
+ const bool isClosed = (edge.flags & 1) != 0;
2904
+ const bool isLastInOpenLoop = !isClosed && (e + 1 >= totalEdges || edges[e + 1].loopIndex != edge.loopIndex || edges[e + 1].kind == 2);
2905
+ if (isLastInOpenLoop) {
2906
+ const Vec2 d_to = subtract(C, edge.to);
2907
+ const double dist_to = length(d_to);
2908
+ const double pen_to = (r + skin) - dist_to;
2909
+ if (pen_to > 0.0 && dist_to > 1e-12) {
2910
+ const Vec2 norm_to = scale(d_to, 1.0 / dist_to);
2911
+ const Vec2 pt = subtract(C, scale(norm_to, r - 0.5 * pen_to));
2912
+ const std::uint32_t featId = makeContactFeatureId(
2913
+ makeContactFeature(kFeatureTypeCurve, endIdx),
2914
+ makeContactFeature(kFeatureTypeVertex, indices[e].endVertex));
2915
+ if (candidateCount < kMaxCandidates) {
2916
+ candidates[candidateCount++] = {pt, C, r, norm_to, pen_to, featId};
2917
+ }
2918
+ }
2919
+ }
2751
2920
  }
2752
2921
  }
2753
2922
  } else {
@@ -2767,14 +2936,39 @@ Manifold collideShapeWithContourInternal(
2767
2936
  }
2768
2937
  }
2769
2938
  }
2939
+
2940
+ // Also test capsule central segment against convex arc (e.g. capsule horizontal across apex)
2941
+ for (std::uint32_t e = 0; e < totalEdges; ++e) {
2942
+ const auto& edge = edges[e];
2943
+ if (edge.kind != 1) continue;
2944
+ if (!onFreeSideContourInternal(context, effShapeBody, edge)) continue;
2945
+ const ArcEndpoints ends = arcEndpoints(edge.center, edge.arcRadius, edge.quadrant);
2946
+ const bool convex = pointsEqual(edge.from, ends.A);
2947
+ if (!convex) continue;
2948
+ const Vec2 segCp = closestPointOnSegment(edge.center, endpoints[0], endpoints[1]);
2949
+ const Vec2 d_center = subtract(segCp, edge.center);
2950
+ const double dist_center = length(d_center);
2951
+ const double R_eff = edge.arcRadius + skin;
2952
+ const double pen = (R_eff + r) - dist_center;
2953
+ if (pen > 0.0 && inSector(d_center, edge.quadrant) && dist_center > 1e-12) {
2954
+ const Vec2 norm = scale(d_center, 1.0 / dist_center);
2955
+ const Vec2 pt = subtract(segCp, scale(norm, r - 0.5 * pen));
2956
+ const std::uint32_t featId = makeContactFeatureId(
2957
+ makeContactFeature(kFeatureTypeCurve, 0),
2958
+ makeContactFeature(kFeatureTypeCurve, e));
2959
+ if (candidateCount < kMaxCandidates) {
2960
+ candidates[candidateCount++] = {pt, segCp, r, norm, pen, featId};
2961
+ }
2962
+ }
2963
+ }
2770
2964
  }
2771
2965
 
2772
2966
  if (candidateCount > 0) {
2773
- return reduceContourCandidatesInternal(candidates, candidateCount, edges, skin);
2967
+ return transformManifoldToWorld(reduceContourCandidatesInternal(candidates, candidateCount, edges, skin));
2774
2968
  }
2775
2969
 
2776
2970
  if (header->fill == 1) {
2777
- return computeSolidContourMTVInternal(context, shapeBody, contourBody, *header, edges);
2971
+ return transformManifoldToWorld(computeSolidContourMTVInternal(context, effShapeBody, contourBody, *header, edges));
2778
2972
  }
2779
2973
 
2780
2974
  return Manifold{};
@@ -160,6 +160,7 @@ struct ChainSegmentList {
160
160
  constexpr std::uint32_t kContourHeaderTag = 0x434f4e54; // "CONT"
161
161
  constexpr std::size_t kMaxContourLoops = 32;
162
162
  constexpr std::size_t kMaxContourEdges = 256;
163
+ constexpr std::size_t kMaxContourTotalEdges = kMaxContourEdges + 2 * kMaxContourLoops;
163
164
 
164
165
  struct ContourHeader2D { // 32 bytes, static_assert-ed
165
166
  std::uint32_t firstEdge = 0; // index into the world contour edge pool