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

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.
Files changed (40) hide show
  1. package/README.md +3 -0
  2. package/core/native/physics/physics.cpp +29 -5
  3. package/core/native/physics2d/ccd.cpp +79 -1
  4. package/core/native/physics2d/commands.cpp +134 -17
  5. package/core/native/physics2d/commands.hpp +2 -0
  6. package/core/native/physics2d/contour.cpp +263 -0
  7. package/core/native/physics2d/contour.hpp +141 -0
  8. package/core/native/physics2d/owned_abi.cpp +47 -7
  9. package/core/native/physics2d/owned_abi.hpp +17 -5
  10. package/core/native/physics2d/physics2d.cpp +862 -2
  11. package/core/native/physics2d/physics2d.hpp +28 -1
  12. package/core/native/physics2d/queries.cpp +544 -10
  13. package/core/native/physics2d/queries.hpp +13 -0
  14. package/core/native/physics2d/world.cpp +181 -18
  15. package/core/native/physics2d/world.hpp +18 -5
  16. package/core/physics2d-owned.d.ts +15 -13
  17. package/core/scripts/build-native.mjs +2 -0
  18. package/core/scripts/build-wasm-physics.mjs +6 -3
  19. package/core/scripts/generate-physics2d-vectors.mjs +59 -0
  20. package/core/src/physics/physics-wasm-module.generated.mjs +3 -3
  21. package/core/src/physics2d/body-state.mjs +30 -2
  22. package/core/src/physics2d/collision.mjs +39 -3
  23. package/core/src/physics2d/contour-state.mjs +202 -1
  24. package/core/src/physics2d/owned-transport.mjs +45 -6
  25. package/core/src/physics2d/owned-world.mjs +138 -20
  26. package/core/src/physics2d/physics2d-wasm-module.generated.mjs +7 -7
  27. package/core/src/physics2d/query-codec.mjs +2 -2
  28. package/core/src/shared/sha256.mjs +81 -43
  29. package/dist/authority-room.d.ts +9 -0
  30. package/dist/authority-room.js +22 -3
  31. package/dist/physics/2d.js +1 -0
  32. package/dist/physics/3d.d.ts +58 -0
  33. package/dist/physics/3d.js +115 -0
  34. package/dist/r3f-render-adapter.d.ts +41 -0
  35. package/dist/r3f-render-adapter.js +67 -0
  36. package/dist/runner.js +20 -2
  37. package/dist/secret-config.js +2 -60
  38. package/dist/secret-crypto.js +70 -36
  39. package/dist/testing.js +8 -2
  40. package/package.json +2 -2
package/README.md CHANGED
@@ -459,6 +459,9 @@ Call `initPhysics2D()` or `initPhysics3D()` before world creation. Create an
459
459
  owned world with `createPhysicsWorld2D()` or `createPhysicsWorld3D()`. Each
460
460
  world provides fixed stepping, atomic edits, retained queries, opaque native
461
461
  checkpoints, portable checkpoint bytes, a state digest, and explicit disposal.
462
+ The 2D engine supports circle, box, capsule, polygon, segment, chain, piecewise
463
+ contour loops, and circular quadrant arcs with the free-side collision rule and
464
+ strict deterministic query tie-ordering.
462
465
 
463
466
  The TypeScript facade does not contain a second solver. Both dimensions run in
464
467
  the canonical C++ core compiled to WASM. The internal solver algorithm, state
@@ -2717,6 +2717,7 @@ struct SolverStorage {
2717
2717
  std::vector<std::uint8_t> sleepCanSleep;
2718
2718
  std::vector<std::uint8_t> sleepHasKinematic;
2719
2719
  std::vector<std::uint8_t> sleepHasStaticAnchor;
2720
+ std::vector<std::uint8_t> bodyJointAffected;
2720
2721
  std::unique_ptr<FixedDynamicAabbTree> broadphaseTree;
2721
2722
  bool dynamicTreeEnabled = true;
2722
2723
  std::vector<PreparedJointSharedGeometry> preparedJointGeometry;
@@ -3402,6 +3403,7 @@ void prepareStepStorage(SolverStorage& storage, std::uint32_t bodyCount, std::ui
3402
3403
  storage.sleepCanSleep.resize(bodyCount);
3403
3404
  storage.sleepHasKinematic.resize(bodyCount);
3404
3405
  storage.sleepHasStaticAnchor.resize(bodyCount);
3406
+ storage.bodyJointAffected.assign(bodyCount, 0);
3405
3407
  storage.broadphaseTree->prepare(bodyCount, pairCapacity);
3406
3408
  }
3407
3409
 
@@ -3606,14 +3608,24 @@ Collision capsuleBox(const Body& capsule, const Body& box) {
3606
3608
  capsule.y + capsule.shapeOffsetY,
3607
3609
  capsule.z + capsule.shapeOffsetZ,
3608
3610
  };
3609
- const Vec3 capsuleAxis = rotateByOrientation(capsule, {0.0, 1.0, 0.0});
3610
- const Vec3 startWorld = subtractVec3(capsuleCenter, scaleVec3(capsuleAxis, capsule.shape1));
3611
- const Vec3 segmentWorld = scaleVec3(capsuleAxis, capsule.shape1 * 2.0);
3612
3611
  const Vec3 boxCenter{
3613
3612
  box.x + box.shapeOffsetX,
3614
3613
  box.y + box.shapeOffsetY,
3615
3614
  box.z + box.shapeOffsetZ,
3616
3615
  };
3616
+ const double capRadius = capsule.shape0;
3617
+ const double capBound = capsule.shape1 + capRadius;
3618
+ const double boxBoundSq = box.shape0 * box.shape0 + box.shape1 * box.shape1 + box.shape2 * box.shape2;
3619
+ const double maxDist = capBound + std::sqrt(boxBoundSq);
3620
+ const double dx = capsuleCenter.x - boxCenter.x;
3621
+ const double dy = capsuleCenter.y - boxCenter.y;
3622
+ const double dz = capsuleCenter.z - boxCenter.z;
3623
+ if (dx * dx + dy * dy + dz * dz > maxDist * maxDist) {
3624
+ return {};
3625
+ }
3626
+ const Vec3 capsuleAxis = rotateByOrientation(capsule, {0.0, 1.0, 0.0});
3627
+ const Vec3 startWorld = subtractVec3(capsuleCenter, scaleVec3(capsuleAxis, capsule.shape1));
3628
+ const Vec3 segmentWorld = scaleVec3(capsuleAxis, capsule.shape1 * 2.0);
3617
3629
  const Vec3 start = inverseRotateByOrientation(box, subtractVec3(startWorld, boxCenter));
3618
3630
  const Vec3 segment = inverseRotateByOrientation(box, segmentWorld);
3619
3631
  const Vec3 half{box.shape0, box.shape1, box.shape2};
@@ -3744,8 +3756,7 @@ Collision capsuleBox(const Body& capsule, const Body& box) {
3744
3756
  Collision collide(SolverOperation& operation, const Body& a, const Body& b) {
3745
3757
 
3746
3758
  if (operation.materialMotionEpochs != nullptr) return collideMaterialCurrent(operation, a, b);
3747
- const auto triangleGeometry=[](const Body& body){return body.shape==ShapeKind::Mesh||body.shape==ShapeKind::Heightfield
3748
- ||(body.shape==ShapeKind::Compound&&body.geometryFormat==5);};
3759
+ const auto triangleGeometry = [](const Body& body) { return body.shape == ShapeKind::Mesh || body.shape == ShapeKind::Heightfield || (body.shape == ShapeKind::Compound && body.geometryFormat == 5); };
3749
3760
  if(a.shape==ShapeKind::Capsule&&triangleGeometry(b))return triangle_adapter_private::observeGeometry(operation,a,b);
3750
3761
  if(b.shape==ShapeKind::Capsule&&triangleGeometry(a))return flipCollision(triangle_adapter_private::observeGeometry(operation,b,a));
3751
3762
  if (a.shape == ShapeKind::Compound && (a.geometryFormat == 4 || a.geometryFormat == 5 || a.geometryFormat == 6)) {
@@ -7681,6 +7692,9 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
7681
7692
  std::uint32_t preparedEnd) {
7682
7693
  if (initialize && collisionDisabled(operation, first, second)) return;
7683
7694
  if (initialize && !bodies[first].dynamic && !bodies[first].kinematic && !bodies[second].dynamic && !bodies[second].kinematic) return;
7695
+ const bool pairHasJoint = first < operation.storage.bodyJointAffected.size()
7696
+ && second < operation.storage.bodyJointAffected.size()
7697
+ && (operation.storage.bodyJointAffected[first] != 0 || operation.storage.bodyJointAffected[second] != 0);
7684
7698
  if (!solveVelocity) {
7685
7699
  if (resolveContactPair(operation, bodies[first], bodies[second], options,
7686
7700
  nullptr, nullptr, nullptr, nullptr, nullptr, true,
@@ -18985,6 +18999,16 @@ std::int32_t solveStartingConstraints(SolverOperation& operation,
18985
18999
  if ((warmState != nullptr && jointCount != 0) || hook.fn != nullptr
18986
19000
  || operation.materialMotionEpochs != nullptr) {
18987
19001
  operation.jointSubstepDuration = substepOptions.dtTicks;
19002
+ operation.storage.bodyJointAffected.assign(bodyCount, 0);
19003
+ for (std::uint32_t j = 0; j < jointCount; ++j) {
19004
+ const Joint& joint = operation.storage.soaJoints[j];
19005
+ if (joint.bodyA < bodyCount) operation.storage.bodyJointAffected[joint.bodyA] = 1;
19006
+ if (joint.bodyB < bodyCount) operation.storage.bodyJointAffected[joint.bodyB] = 1;
19007
+ }
19008
+ for (const auto& connection : hook.connections) {
19009
+ if (connection[0] < bodyCount) operation.storage.bodyJointAffected[connection[0]] = 1;
19010
+ if (connection[1] < bodyCount) operation.storage.bodyJointAffected[connection[1]] = 1;
19011
+ }
18988
19012
  resolveSoAContacts(operation, operation.storage.soaBodies.data(), bodyCount,
18989
19013
  1, substepOptions, ContactPass::OwnedPositionCorrection);
18990
19014
  if (operation.storage.contactConstraintOverflow) return 5;
@@ -1,4 +1,5 @@
1
1
  #include "ccd.hpp"
2
+ #include "contour.hpp"
2
3
  #include <algorithm>
3
4
  #include <cmath>
4
5
  #include <cstdint>
@@ -346,8 +347,10 @@ double bodySeparation(const GeometrySource2D& source, const Body& bodyA, const B
346
347
  Vec2& closestA, Vec2& closestB, Vec2& normalFromBToA) {
347
348
  const bool aChain = (bodyA.shape == ShapeType::Segment || bodyA.shape == ShapeType::Chain);
348
349
  const bool bChain = (bodyB.shape == ShapeType::Segment || bodyB.shape == ShapeType::Chain);
350
+ const bool aContour = (bodyA.shape == ShapeType::Contour);
351
+ const bool bContour = (bodyB.shape == ShapeType::Contour);
349
352
 
350
- if (!aChain && !bChain) {
353
+ if (!aChain && !bChain && !aContour && !bContour) {
351
354
  ConvexGeometry2D geomA{}, geomB{};
352
355
  if (!prepareCcdGeometry(source, bodyA, geomA) ||
353
356
  !prepareCcdGeometry(source, bodyB, geomB)) {
@@ -441,6 +444,81 @@ double bodySeparation(const GeometrySource2D& source, const Body& bodyA, const B
441
444
  return sep;
442
445
  }
443
446
 
447
+ if (bContour && !aContour) {
448
+ const PhysicsContext2D* ctx = source.context != nullptr ? source.context : &detail::legacyPhysicsContext();
449
+ const ContourHeader2D* header = contourHeaderFor(*ctx, bodyB);
450
+ if (header == nullptr || header->edgeCount == 0) return 0.0;
451
+ const ContourEdge2D* edges = contourEdgesFor(*ctx, *header);
452
+ if (edges == nullptr) return 0.0;
453
+
454
+ ConvexGeometry2D geomA{};
455
+ if (!prepareCcdGeometry(source, bodyA, geomA)) return 0.0;
456
+
457
+ double minSep = std::numeric_limits<double>::infinity();
458
+ Vec2 bestA = {bodyA.x, bodyA.y};
459
+ Vec2 bestB = {bodyB.x, bodyB.y};
460
+ Vec2 bestNorm = {1.0, 0.0};
461
+
462
+ for (std::uint32_t i = 0; i < header->edgeCount; ++i) {
463
+ const auto& edge = edges[i];
464
+ if (edge.kind == 2) continue;
465
+ if (!onFreeSideContour(*ctx, bodyA, edge)) continue;
466
+
467
+ if (edge.kind == 0) {
468
+ ConvexGeometry2D segGeom{};
469
+ segGeom.sourceShape = ShapeType::Segment;
470
+ segGeom.coreCount = 2;
471
+ segGeom.coreVertices[0] = edge.from;
472
+ segGeom.coreVertices[1] = edge.to;
473
+ segGeom.radius = header->skin;
474
+
475
+ Vec2 cA, cB;
476
+ const double dCore = coreDistance(geomA, segGeom, cA, cB);
477
+ const double sep = dCore - geomA.radius - segGeom.radius;
478
+ if (sep < minSep) {
479
+ minSep = sep;
480
+ const Vec2 norm = dCore > 1e-12 ? scale(sub(cA, cB), 1.0 / dCore) : Vec2{0.0, 1.0};
481
+ bestA = sub(cA, scale(norm, geomA.radius));
482
+ bestB = add(cB, scale(norm, segGeom.radius));
483
+ bestNorm = norm;
484
+ }
485
+ } else if (edge.kind == 1) {
486
+ const Vec2 centerA = {bodyA.x, bodyA.y};
487
+ const Vec2 cp = closestPointOnArc(centerA, edge.center, edge.arcRadius, edge.quadrant, edge.from, edge.to);
488
+
489
+ ConvexGeometry2D ptGeom{};
490
+ ptGeom.sourceShape = ShapeType::Circle;
491
+ ptGeom.coreCount = 1;
492
+ ptGeom.coreVertices[0] = cp;
493
+ ptGeom.radius = header->skin;
494
+
495
+ Vec2 cA, cB;
496
+ const double dCore = coreDistance(geomA, ptGeom, cA, cB);
497
+ const double sep = dCore - geomA.radius - ptGeom.radius;
498
+ if (sep < minSep) {
499
+ minSep = sep;
500
+ const Vec2 norm = dCore > 1e-12 ? scale(sub(cA, cB), 1.0 / dCore) : Vec2{0.0, 1.0};
501
+ bestA = sub(cA, scale(norm, geomA.radius));
502
+ bestB = add(cB, scale(norm, ptGeom.radius));
503
+ bestNorm = norm;
504
+ }
505
+ }
506
+ }
507
+ closestA = bestA;
508
+ closestB = bestB;
509
+ normalFromBToA = bestNorm;
510
+ return minSep;
511
+ }
512
+
513
+ if (aContour && !bContour) {
514
+ Vec2 cB, cA, normAtoB;
515
+ const double sep = bodySeparation(source, bodyB, bodyA, cB, cA, normAtoB);
516
+ closestA = cA;
517
+ closestB = cB;
518
+ normalFromBToA = scale(normAtoB, -1.0);
519
+ return sep;
520
+ }
521
+
444
522
  // Chain to chain
445
523
  closestA = {bodyA.x, bodyA.y};
446
524
  closestB = {bodyB.x, bodyB.y};
@@ -1,4 +1,5 @@
1
1
  #include "world.hpp"
2
+ #include "contour.hpp"
2
3
 
3
4
  #include <algorithm>
4
5
  #include <cmath>
@@ -27,7 +28,8 @@ EditResult2D editResult(
27
28
  bool hasStoredGeometry(ShapeType shape) {
28
29
  return shape == ShapeType::Polygon
29
30
  || shape == ShapeType::Segment
30
- || shape == ShapeType::Chain;
31
+ || shape == ShapeType::Chain
32
+ || shape == ShapeType::Contour;
31
33
  }
32
34
 
33
35
  void* editAllocate(std::size_t size) {
@@ -183,10 +185,9 @@ bool isRemove(TopologyCommandKind2D kind) {
183
185
  }
184
186
 
185
187
  bool validBodyTags(const Body& body) {
186
- // Segment (5) and Chain (6) are implemented, and the retained world already
187
- // accepts them at initial creation and through snapshot capture/restore. Only
188
- // this live-edit path still stopped at Polygon, so a chain could exist in a
189
- // world but never be added to one by an edit.
188
+ if (body.shape == ShapeType::Contour) {
189
+ return body.type == BodyType::Static;
190
+ }
190
191
  return body.type >= BodyType::Static && body.type <= BodyType::Dynamic
191
192
  && body.shape >= ShapeType::Circle && body.shape <= ShapeType::Chain;
192
193
  }
@@ -369,6 +370,42 @@ void addRemoval(
369
370
  removed[removedCount++] = {kind, id};
370
371
  }
371
372
 
373
+ void eraseContourEdgesForBody(
374
+ ContourEdge2D* contourEdges,
375
+ std::uint32_t& contourEdgeCount,
376
+ Body* bodies,
377
+ PolygonGeometry* geometries,
378
+ std::uint32_t bodyCount,
379
+ std::uint32_t bodyIndex) {
380
+ if (bodies[bodyIndex].shape != ShapeType::Contour) return;
381
+ ContourHeader2D header{};
382
+ std::memcpy(&header, &geometries[bodyIndex], sizeof(header));
383
+ if (header.tag != kContourHeaderTag) return;
384
+ const std::uint32_t totalRecords = header.reserved != 0 ? header.reserved : header.edgeCount;
385
+ const std::uint32_t removedFirst = header.firstEdge;
386
+ if (totalRecords == 0 || removedFirst >= contourEdgeCount) return;
387
+
388
+ const std::uint32_t tailEdges = contourEdgeCount - (removedFirst + totalRecords);
389
+ if (tailEdges > 0) {
390
+ std::memmove(contourEdges + removedFirst,
391
+ contourEdges + removedFirst + totalRecords,
392
+ static_cast<std::size_t>(tailEdges) * sizeof(ContourEdge2D));
393
+ }
394
+ contourEdgeCount -= totalRecords;
395
+
396
+ for (std::uint32_t b = 0; b < bodyCount; b += 1) {
397
+ if (b == bodyIndex) continue;
398
+ if (bodies[b].shape == ShapeType::Contour) {
399
+ ContourHeader2D otherHeader{};
400
+ std::memcpy(&otherHeader, &geometries[b], sizeof(otherHeader));
401
+ if (otherHeader.tag == kContourHeaderTag && otherHeader.firstEdge > removedFirst) {
402
+ otherHeader.firstEdge -= totalRecords;
403
+ std::memcpy(&geometries[b], &otherHeader, sizeof(otherHeader));
404
+ }
405
+ }
406
+ }
407
+ }
408
+
372
409
  void eraseBody(
373
410
  Body* bodies,
374
411
  PolygonGeometry* geometries,
@@ -379,6 +416,11 @@ void eraseBody(
379
416
  static_cast<std::size_t>(count - index - 1) * sizeof(Body));
380
417
  std::memmove(geometries + index, geometries + index + 1,
381
418
  static_cast<std::size_t>(count - index - 1) * sizeof(PolygonGeometry));
419
+ for (std::uint32_t b = index; b < count - 1; b += 1) {
420
+ if (hasStoredGeometry(bodies[b].shape)) {
421
+ bodies[b].reserved = b;
422
+ }
423
+ }
382
424
  }
383
425
  count -= 1;
384
426
  }
@@ -554,7 +596,9 @@ EditResult2D World2D::edit(
554
596
  std::uint32_t* handleCount,
555
597
  RemovedIdentity2D* removed,
556
598
  std::uint32_t removedCapacity,
557
- std::uint32_t* removedCount) {
599
+ std::uint32_t* removedCount,
600
+ const ContourEdge2D* contourEdgePayloads,
601
+ std::uint32_t contourEdgePayloadCount) {
558
602
  if (!ready()) return editResult(EditStatus2D::NotReady, EditErrorDomain2D::Envelope);
559
603
  const std::uint64_t configuredLimit = 2ull * (static_cast<std::uint64_t>(config_.bodyCapacity)
560
604
  + config_.jointCapacity + config_.fluidCapacity);
@@ -571,30 +615,32 @@ EditResult2D World2D::edit(
571
615
  std::size_t requiredBytes = 0;
572
616
  std::size_t handleBytes = 0;
573
617
  std::size_t removedBytes = 0;
618
+ std::size_t contourEdgeBytes = 0;
574
619
  if (!checkedBytes(commandCount, sizeof(TopologyCommand2D), commandBytes)
575
620
  || !checkedBytes(bodyPayloadCount, sizeof(BodyCommandPayload2D), bodyPayloadBytes)
576
621
  || !checkedBytes(jointPayloadCount, sizeof(JointRecord2D), jointPayloadBytes)
577
622
  || !checkedBytes(fluidPayloadCount, sizeof(FluidRegion2D), fluidPayloadBytes)
578
623
  || !checkedBytes(requiredResultCount, sizeof(EntityReference2D), requiredBytes)
579
624
  || !checkedBytes(handleCapacity, sizeof(EntityHandle2D), handleBytes)
580
- || !checkedBytes(removedCapacity, sizeof(RemovedIdentity2D), removedBytes)) {
625
+ || !checkedBytes(removedCapacity, sizeof(RemovedIdentity2D), removedBytes)
626
+ || !checkedBytes(contourEdgePayloadCount, sizeof(ContourEdge2D), contourEdgeBytes)) {
581
627
  return editResult(EditStatus2D::InvalidArguments, EditErrorDomain2D::Envelope);
582
628
  }
583
629
  const void* pointers[] = {
584
630
  commands, bodyPayloads, jointPayloads, fluidPayloads, requiredResults,
585
- handles, handleCount, removed, removedCount,
631
+ handles, handleCount, removed, removedCount, contourEdgePayloads,
586
632
  };
587
633
  const std::size_t sizes[] = {
588
634
  commandBytes, bodyPayloadBytes, jointPayloadBytes, fluidPayloadBytes,
589
635
  requiredBytes, handleBytes, sizeof(*handleCount), removedBytes,
590
- sizeof(*removedCount),
636
+ sizeof(*removedCount), contourEdgeBytes,
591
637
  };
592
638
  const std::size_t alignments[] = {
593
639
  alignof(TopologyCommand2D), alignof(BodyCommandPayload2D),
594
640
  alignof(JointRecord2D), alignof(FluidRegion2D),
595
641
  alignof(EntityReference2D), alignof(EntityHandle2D),
596
642
  alignof(std::uint32_t), alignof(RemovedIdentity2D),
597
- alignof(std::uint32_t),
643
+ alignof(std::uint32_t), alignof(ContourEdge2D),
598
644
  };
599
645
  for (std::size_t index = 0; index < std::size(pointers); index += 1) {
600
646
  if (!validRange(pointers[index], sizes[index], alignments[index])) {
@@ -660,13 +706,31 @@ EditResult2D World2D::edit(
660
706
  // Same shape of bug as validBodyTags above, one field over.
661
707
  const bool extendedGeometry = payload.body.shape == ShapeType::Segment
662
708
  || payload.body.shape == ShapeType::Chain;
663
- const bool geometryTagValid = extendedGeometry
664
- ? payload.geometry.reserved != 0
665
- : payload.geometry.reserved == 0;
666
- if (payload.body.id != 0 || payload.body.reserved != 0
667
- || !geometryTagValid || !validBodyTags(payload.body)) {
668
- return editResult(EditStatus2D::InvalidPayload, EditErrorDomain2D::Command, index);
709
+ const bool isContour = payload.body.shape == ShapeType::Contour;
710
+ bool geometryTagValid = false;
711
+ if (isContour) {
712
+ ContourHeader2D header{};
713
+ std::memcpy(&header, &payload.geometry, sizeof(header));
714
+ const std::uint32_t totalRecords = header.reserved != 0 ? header.reserved : header.edgeCount;
715
+ if (!validateContourHeader(header, kMaxWorldContourEdges)) {
716
+ return editResult(EditStatus2D::InvalidContour, EditErrorDomain2D::Command, index);
717
+ }
718
+ if (header.firstEdge + totalRecords > contourEdgePayloadCount) {
719
+ return editResult(EditStatus2D::InvalidContour, EditErrorDomain2D::Command, index);
720
+ }
721
+ if (!validateContourEdges(header, contourEdgePayloads + header.firstEdge, totalRecords)) {
722
+ return editResult(EditStatus2D::InvalidContour, EditErrorDomain2D::Command, index);
723
+ }
724
+ geometryTagValid = true;
725
+ } else if (extendedGeometry) {
726
+ geometryTagValid = payload.geometry.reserved != 0;
727
+ } else {
728
+ geometryTagValid = payload.geometry.reserved == 0;
669
729
  }
730
+ if (payload.body.id != 0) return editResult(isContour ? EditStatus2D::InvalidContour : EditStatus2D::InvalidPayload, EditErrorDomain2D::Command, index);
731
+ if (payload.body.reserved != 0) return editResult(isContour ? EditStatus2D::InvalidContour : EditStatus2D::InvalidPayload, EditErrorDomain2D::Command, index);
732
+ if (!geometryTagValid) return editResult(isContour ? EditStatus2D::InvalidContour : EditStatus2D::InvalidPayload, EditErrorDomain2D::Command, index);
733
+ if (!validBodyTags(payload.body)) return editResult(isContour ? EditStatus2D::InvalidContour : EditStatus2D::InvalidPayload, EditErrorDomain2D::Command, index);
670
734
  if (command.kind == TopologyCommandKind2D::CreateBody) createBodies += 1;
671
735
  } else if (command.kind == TopologyCommandKind2D::CreateJoint
672
736
  || command.kind == TopologyCommandKind2D::ReplaceJoint) {
@@ -735,17 +799,20 @@ EditResult2D World2D::edit(
735
799
  const std::uint64_t bodyStageWide = static_cast<std::uint64_t>(current.bodyCount) + createBodies;
736
800
  const std::uint64_t jointStageWide = static_cast<std::uint64_t>(current.jointCount) + createJoints;
737
801
  const std::uint64_t fluidStageWide = static_cast<std::uint64_t>(current.fluidCount) + createFluids;
802
+ const std::uint64_t contourStageWide = static_cast<std::uint64_t>(current.contourEdgeCount) + contourEdgePayloadCount;
738
803
  const std::uint64_t removalWide = static_cast<std::uint64_t>(current.bodyCount)
739
804
  + current.jointCount + current.fluidCount + createBodies + createJoints + createFluids;
740
805
  if (bodyStageWide > std::numeric_limits<std::uint32_t>::max()
741
806
  || jointStageWide > std::numeric_limits<std::uint32_t>::max()
742
807
  || fluidStageWide > std::numeric_limits<std::uint32_t>::max()
808
+ || contourStageWide > std::numeric_limits<std::uint32_t>::max()
743
809
  || removalWide > std::numeric_limits<std::uint32_t>::max()) {
744
810
  return editResult(EditStatus2D::CommandLimit, EditErrorDomain2D::Envelope);
745
811
  }
746
812
  const std::uint32_t bodyStageCapacity = static_cast<std::uint32_t>(bodyStageWide);
747
813
  const std::uint32_t jointStageCapacity = static_cast<std::uint32_t>(jointStageWide);
748
814
  const std::uint32_t fluidStageCapacity = static_cast<std::uint32_t>(fluidStageWide);
815
+ const std::uint32_t contourStageCapacity = static_cast<std::uint32_t>(contourStageWide);
749
816
  const std::uint32_t removalCapacity = static_cast<std::uint32_t>(removalWide);
750
817
 
751
818
  EditLayout layout;
@@ -754,6 +821,7 @@ EditResult2D World2D::edit(
754
821
  || !layout.add<JointRecord2D>(jointStageCapacity)
755
822
  || !layout.add<JointState2D>(jointStageCapacity)
756
823
  || !layout.add<FluidRegion2D>(fluidStageCapacity)
824
+ || !layout.add<ContourEdge2D>(contourStageCapacity)
757
825
  || !layout.add<std::uint32_t>(commandCount)
758
826
  || !layout.add<RemovedIdentity2D>(removalCapacity)
759
827
  || !layout.add<EntityHandle2D>(requiredResultCount)
@@ -771,6 +839,7 @@ EditResult2D World2D::edit(
771
839
  JointRecord2D* joints = arena.take<JointRecord2D>(jointStageCapacity);
772
840
  JointState2D* jointStates = arena.take<JointState2D>(jointStageCapacity);
773
841
  FluidRegion2D* fluids = arena.take<FluidRegion2D>(fluidStageCapacity);
842
+ ContourEdge2D* stagedContourEdges = arena.take<ContourEdge2D>(contourStageCapacity);
774
843
  std::uint32_t* createdIds = arena.take<std::uint32_t>(commandCount);
775
844
  RemovedIdentity2D* stagedRemoved = arena.take<RemovedIdentity2D>(removalCapacity);
776
845
  EntityHandle2D* stagedHandles = arena.take<EntityHandle2D>(requiredResultCount);
@@ -788,6 +857,11 @@ EditResult2D World2D::edit(
788
857
  if (current.fluidCount > 0) {
789
858
  std::memcpy(fluids, current.fluids, sizeof(FluidRegion2D) * current.fluidCount);
790
859
  }
860
+ std::uint32_t stagedContourEdgeCount = current.contourEdgeCount;
861
+ if (current.contourEdgeCount > 0) {
862
+ std::memcpy(stagedContourEdges, current.contourEdges,
863
+ sizeof(ContourEdge2D) * current.contourEdgeCount);
864
+ }
791
865
  std::uint32_t bodyCount = current.bodyCount;
792
866
  std::uint32_t jointCount = current.jointCount;
793
867
  std::uint32_t fluidCount = current.fluidCount;
@@ -810,6 +884,22 @@ EditResult2D World2D::edit(
810
884
  PolygonGeometry geometry = bodyPayloads[command.payloadIndex].geometry;
811
885
  body.id = nextBodyId++;
812
886
  body.reserved = hasStoredGeometry(body.shape) ? bodyCount : 0;
887
+ if (body.shape == ShapeType::Contour) {
888
+ ContourHeader2D header{};
889
+ std::memcpy(&header, &geometry, sizeof(header));
890
+ const std::uint32_t payloadOffset = header.firstEdge;
891
+ const std::uint32_t totalRecords = header.reserved != 0 ? header.reserved : header.edgeCount;
892
+ if (stagedContourEdgeCount + totalRecords > config_.contourEdgeCapacity) {
893
+ return editResult(EditStatus2D::ContourCapacity, EditErrorDomain2D::Command,
894
+ commandIndex, kStepOk, layout.size());
895
+ }
896
+ header.firstEdge = stagedContourEdgeCount;
897
+ std::memcpy(stagedContourEdges + header.firstEdge,
898
+ contourEdgePayloads + payloadOffset,
899
+ static_cast<std::size_t>(totalRecords) * sizeof(ContourEdge2D));
900
+ stagedContourEdgeCount += totalRecords;
901
+ std::memcpy(&geometry, &header, sizeof(header));
902
+ }
813
903
  bodies[bodyCount] = body;
814
904
  geometries[bodyCount] = geometry;
815
905
  bodyCount += 1;
@@ -820,17 +910,42 @@ EditResult2D World2D::edit(
820
910
  const std::uint32_t index = findId(bodies, bodyCount, subjectId);
821
911
  if (index == kEditNoIndex2D) return editResult(EditStatus2D::InvalidReference,
822
912
  EditErrorDomain2D::Command, commandIndex, kStepOk, layout.size());
913
+ if (bodies[index].shape == ShapeType::Contour) {
914
+ eraseContourEdgesForBody(stagedContourEdges, stagedContourEdgeCount,
915
+ bodies, geometries, bodyCount, index);
916
+ }
823
917
  Body body = bodyPayloads[command.payloadIndex].body;
918
+ PolygonGeometry geometry = bodyPayloads[command.payloadIndex].geometry;
824
919
  body.id = subjectId;
825
920
  body.reserved = hasStoredGeometry(body.shape) ? index : 0;
921
+ if (body.shape == ShapeType::Contour) {
922
+ ContourHeader2D header{};
923
+ std::memcpy(&header, &geometry, sizeof(header));
924
+ const std::uint32_t payloadOffset = header.firstEdge;
925
+ const std::uint32_t totalRecords = header.reserved != 0 ? header.reserved : header.edgeCount;
926
+ if (stagedContourEdgeCount + totalRecords > config_.contourEdgeCapacity) {
927
+ return editResult(EditStatus2D::ContourCapacity, EditErrorDomain2D::Command,
928
+ commandIndex, kStepOk, layout.size());
929
+ }
930
+ header.firstEdge = stagedContourEdgeCount;
931
+ std::memcpy(stagedContourEdges + header.firstEdge,
932
+ contourEdgePayloads + payloadOffset,
933
+ static_cast<std::size_t>(totalRecords) * sizeof(ContourEdge2D));
934
+ stagedContourEdgeCount += totalRecords;
935
+ std::memcpy(&geometry, &header, sizeof(header));
936
+ }
826
937
  bodies[index] = body;
827
- geometries[index] = bodyPayloads[command.payloadIndex].geometry;
938
+ geometries[index] = geometry;
828
939
  break;
829
940
  }
830
941
  case TopologyCommandKind2D::RemoveBody: {
831
942
  const std::uint32_t index = findId(bodies, bodyCount, subjectId);
832
943
  if (index == kEditNoIndex2D) return editResult(EditStatus2D::InvalidReference,
833
944
  EditErrorDomain2D::Command, commandIndex, kStepOk, layout.size());
945
+ if (bodies[index].shape == ShapeType::Contour) {
946
+ eraseContourEdgesForBody(stagedContourEdges, stagedContourEdgeCount,
947
+ bodies, geometries, bodyCount, index);
948
+ }
834
949
  eraseBody(bodies, geometries, bodyCount, index);
835
950
  addRemoval(stagedRemoved, stagedRemovedCount, EntityKind2D::Body, subjectId);
836
951
  removeBodyJointCascade(joints, jointStates, jointCount, subjectId,
@@ -956,6 +1071,8 @@ EditResult2D World2D::edit(
956
1071
  staged.jointCount = jointCount;
957
1072
  staged.fluids = fluids;
958
1073
  staged.fluidCount = fluidCount;
1074
+ staged.contourEdges = stagedContourEdges;
1075
+ staged.contourEdgeCount = stagedContourEdgeCount;
959
1076
  staged.nextBodyId = nextBodyId;
960
1077
  staged.nextJointId = nextJointId;
961
1078
  staged.nextFluidId = nextFluidId;
@@ -88,6 +88,8 @@ enum class EditStatus2D : std::int32_t {
88
88
  AllocationFailure = -209,
89
89
  InvalidState = -210,
90
90
  OutputCapacity = -211,
91
+ ContourCapacity = -212,
92
+ InvalidContour = -213,
91
93
  };
92
94
 
93
95
  enum class EditErrorDomain2D : std::uint32_t {