@series-inc/rundot-syncplay 6.0.0-rc.61 → 6.0.0-rc.62

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.
@@ -3933,6 +3933,11 @@ struct SolverStorage {
3933
3933
  std::vector<ContactConstraintBlock> contactConstraints;
3934
3934
  std::vector<double> contactCacheOutput;
3935
3935
  std::vector<std::uint8_t> contactSupported;
3936
+ // Support contacts (lower << 32 | upper) seen by the collecting iteration of this substep. The
3937
+ // first frozen pass marks every body that rests on support through a chain of them
3938
+ // (propagateContactSupport); the collecting order alone missed crates visited before their support.
3939
+ std::vector<std::uint64_t> supportEdges;
3940
+ bool supportPropagated = false;
3936
3941
  std::vector<std::uint8_t> movingFoundation;
3937
3942
  // Dynamic-dynamic pairs that the collecting contact iteration solves after the support pairs.
3938
3943
  std::vector<std::uint64_t> deferredDynamicPairs;
@@ -4808,6 +4813,8 @@ void SolverStorage::invalidate(bool keepBroadphaseTree) noexcept {
4808
4813
  contactConstraints.clear();
4809
4814
  contactCacheOutput.clear();
4810
4815
  contactSupported.clear();
4816
+ supportEdges.clear();
4817
+ supportPropagated = false;
4811
4818
  movingFoundation.clear();
4812
4819
  deferredDynamicPairs.clear();
4813
4820
  collideMemo.clear();
@@ -4869,6 +4876,7 @@ std::uint64_t SolverStorage::retainedBytes() const noexcept {
4869
4876
  + retainedVectorBytes(contactConstraints)
4870
4877
  + retainedVectorBytes(contactCacheOutput)
4871
4878
  + retainedVectorBytes(contactSupported)
4879
+ + retainedVectorBytes(supportEdges)
4872
4880
  + retainedVectorBytes(movingFoundation)
4873
4881
  + retainedVectorBytes(deferredDynamicPairs)
4874
4882
  + retainedVectorBytes(collideMemo)
@@ -5623,9 +5631,10 @@ Collision finalizeCompoundManifold(
5623
5631
 
5624
5632
  bool isSmallDynamicWorld(const SolverOperation& operation);
5625
5633
 
5626
- // The stack solver path. Above 360 bodies it is always on. A world with few dynamic bodies also
5627
- // takes it while the dynamic tree runs its broadphase: the tree visits pairs in body-index order,
5628
- // and the small-world path is not stable in that order (a ten-box column drifts and never sleeps).
5634
+ // The stack solver path. Above 360 bodies it is always on, and it is on while the dynamic tree
5635
+ // runs the broadphase: the tree visits pairs in body-index order, and the small-world path is not
5636
+ // stable in that order (a ten-box column drifts and never sleeps). The number of dynamic bodies
5637
+ // does not select a path, so a distant body that changes kind cannot change a stack elsewhere.
5629
5638
  bool usesStackSolverPath(const SolverOperation& operation, std::uint32_t bodyCount) {
5630
5639
  if (operation.stepSubsteps <= 1) return false;
5631
5640
  if (bodyCount > 360) return true;
@@ -5633,17 +5642,16 @@ bool usesStackSolverPath(const SolverOperation& operation, std::uint32_t bodyCou
5633
5642
  && isSmallDynamicWorld(operation);
5634
5643
  }
5635
5644
 
5645
+ // True for every world that can move a body. Before rc.62 a world with more than 16 dynamic bodies
5646
+ // and at most 360 bodies returned false here and took a third solver path without cross-substep
5647
+ // constraint retention, where a ten-box column collapses (sweep broadphase) or never sleeps (tree).
5636
5648
  bool computeSmallDynamicWorld(const SolverOperation& operation) {
5637
5649
  if (operation.storage.soaBodies.empty()) return false;
5638
5650
  if (operation.storage.soaBodies.size() <= 16) return true;
5639
- std::uint32_t dynamicCount = 0;
5640
5651
  for (const Body& body : operation.storage.soaBodies) {
5641
- if (body.dynamic) {
5642
- dynamicCount += 1;
5643
- if (dynamicCount > 16) return false;
5644
- }
5652
+ if (body.dynamic) return true;
5645
5653
  }
5646
- return dynamicCount > 0;
5654
+ return false;
5647
5655
  }
5648
5656
 
5649
5657
  // The narrowphase asks this once per box pair, compound children included, and the scan walks the
@@ -8451,6 +8459,17 @@ inline bool solvePreparedContact(
8451
8459
  applyContactImpulse(
8452
8460
  a, b, mobilityA, mobilityB, movableA, movableB, rA, rB, impulse);
8453
8461
  };
8462
+ // A frozen support body takes no normal impulse, so a stack cannot press it down, but friction
8463
+ // acts on both bodies. Friction against a frozen body moved the upper body with the lower one and
8464
+ // gave nothing back: a knocked or torch-cut crate stack kept moving at about 0.1 m/s forever.
8465
+ const bool frictionMovableA = a.dynamic && !a.sleeping;
8466
+ const bool frictionMovableB = b.dynamic && !b.sleeping;
8467
+ const auto applyFrictionImpulse = [&](const Vec3& impulse) {
8468
+ captureStartingContactApplication(operation, &constraint, rA, rB, impulse);
8469
+ captureContactApplication(applications, rA, rB, impulse);
8470
+ applyContactImpulse(
8471
+ a, b, mobilityA, mobilityB, frictionMovableA, frictionMovableB, rA, rB, impulse);
8472
+ };
8454
8473
 
8455
8474
  const Vec3 S{constraint.materialVelocity[0], constraint.materialVelocity[1], constraint.materialVelocity[2]};
8456
8475
  const Vec3 relative = subtractVec3(contactRelativeVelocity(a, b, rA, rB), S);
@@ -8470,7 +8489,7 @@ inline bool solvePreparedContact(
8470
8489
  contactTangentBasis(normal, tangent1, tangent2);
8471
8490
  if (normalTotal <= 0.0) {
8472
8491
  if (constraint.impulses[1] != 0.0 || constraint.impulses[2] != 0.0) {
8473
- applyImpulse(addVec3(
8492
+ applyFrictionImpulse(addVec3(
8474
8493
  scaleVec3(tangent1, -constraint.impulses[1]),
8475
8494
  scaleVec3(tangent2, -constraint.impulses[2])));
8476
8495
  constraint.impulses[1] = 0.0;
@@ -8485,8 +8504,8 @@ inline bool solvePreparedContact(
8485
8504
  const double postNormalVelocity = dotVec3(postRelative, normal);
8486
8505
  const Vec3 tangent = subtractVec3(
8487
8506
  postRelative, scaleVec3(normal, postNormalVelocity));
8488
- const double tangentMass1 = effectiveMass(tangent1, constraint.tangentMass1);
8489
- const double tangentMass2 = effectiveMass(tangent2, constraint.tangentMass2);
8507
+ const double tangentMass1 = constraint.tangentMass1;
8508
+ const double tangentMass2 = constraint.tangentMass2;
8490
8509
  const double oldTangent1 = constraint.impulses[1];
8491
8510
  const double oldTangent2 = constraint.impulses[2];
8492
8511
  double tangentTotal1 = oldTangent1
@@ -8505,7 +8524,7 @@ inline bool solvePreparedContact(
8505
8524
  constraint.impulses[2] = tangentTotal2;
8506
8525
  tangentTotal1Output = tangentTotal1;
8507
8526
  tangentTotal2Output = tangentTotal2;
8508
- applyImpulse(addVec3(
8527
+ applyFrictionImpulse(addVec3(
8509
8528
  scaleVec3(tangent1, tangentTotal1 - oldTangent1),
8510
8529
  scaleVec3(tangent2, tangentTotal2 - oldTangent2)));
8511
8530
  if (phase == ContactResponsePhase::Fresh) {
@@ -11244,6 +11263,8 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
11244
11263
  operation.storage.contactSolvePairCount = 0;
11245
11264
  std::fill(operation.storage.contactSupported.begin(),
11246
11265
  operation.storage.contactSupported.end(), 0);
11266
+ operation.storage.supportEdges.clear();
11267
+ operation.storage.supportPropagated = false;
11247
11268
  operation.storage.firstSubstepOfTick = firstTickSubstep;
11248
11269
  if (firstTickSubstep) {
11249
11270
  operation.storage.restingSubstepPairCount = 0;
@@ -11702,6 +11723,9 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
11702
11723
  Vec3 tangent1;
11703
11724
  Vec3 tangent2;
11704
11725
  contactTangentBasis(normal, tangent1, tangent2);
11726
+ // Friction acts on both bodies even when the support body is frozen (see solvePreparedContact).
11727
+ const bool frictionMovableA = a.dynamic && !a.sleeping;
11728
+ const bool frictionMovableB = b.dynamic && !b.sleeping;
11705
11729
  std::array<double, 4> errT1List{};
11706
11730
  std::array<double, 4> errT2List{};
11707
11731
  double errT1Avg = 0.0;
@@ -11722,9 +11746,9 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
11722
11746
  maxErrTDiff = std::max(maxErrTDiff, std::max(std::abs(errT1List[i] - errT1Avg), std::abs(errT2List[i] - errT2Avg)));
11723
11747
  }
11724
11748
  const double centroidT1Mass = effectiveContactMass(
11725
- mobilityA, mobilityB, movableA, movableB, rAAvg, rBAvg, tangent1);
11749
+ mobilityA, mobilityB, frictionMovableA, frictionMovableB, rAAvg, rBAvg, tangent1);
11726
11750
  const double centroidT2Mass = effectiveContactMass(
11727
- mobilityA, mobilityB, movableA, movableB, rAAvg, rBAvg, tangent2);
11751
+ mobilityA, mobilityB, frictionMovableA, frictionMovableB, rAAvg, rBAvg, tangent2);
11728
11752
  const double symDeltaT1 = centroidT1Mass > 0.0 ? (-errT1Avg / centroidT1Mass) * invCount : 0.0;
11729
11753
  const double symDeltaT2 = centroidT2Mass > 0.0 ? (-errT2Avg / centroidT2Mass) * invCount : 0.0;
11730
11754
  if (symDeltaT1 != 0.0 || symDeltaT2 != 0.0) {
@@ -11767,12 +11791,12 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
11767
11791
  sumTorqueA = snapSmallVec(sumTorqueA);
11768
11792
  sumTorqueB = snapSmallVec(sumTorqueB);
11769
11793
  }
11770
- const Vec3 linA = inverseLinearMass(mobilityA, movableA);
11771
- const Vec3 linB = inverseLinearMass(mobilityB, movableB);
11794
+ const Vec3 linA = inverseLinearMass(mobilityA, frictionMovableA);
11795
+ const Vec3 linB = inverseLinearMass(mobilityB, frictionMovableB);
11772
11796
  a.vx -= sumImpT.x * linA.x; a.vy -= sumImpT.y * linA.y; a.vz -= sumImpT.z * linA.z;
11773
11797
  b.vx += sumImpT.x * linB.x; b.vy += sumImpT.y * linB.y; b.vz += sumImpT.z * linB.z;
11774
- const Vec3 angA = inverseAngularMass(mobilityA, movableA, sumTorqueA);
11775
- const Vec3 angB = inverseAngularMass(mobilityB, movableB, sumTorqueB);
11798
+ const Vec3 angA = inverseAngularMass(mobilityA, frictionMovableA, sumTorqueA);
11799
+ const Vec3 angB = inverseAngularMass(mobilityB, frictionMovableB, sumTorqueB);
11776
11800
  a.angularVelX -= angA.x; a.angularVelY -= angA.y; a.angularVelZ -= angA.z;
11777
11801
  b.angularVelX += angB.x; b.angularVelY += angB.y; b.angularVelZ += angB.z;
11778
11802
  outTangent1Delta += sumDT1;
@@ -11941,6 +11965,10 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
11941
11965
  && bodies[matched->upper].dynamic) {
11942
11966
  operation.storage.contactSupported[supportIndex(matched->upper)] = 1;
11943
11967
  }
11968
+ if (bodies[matched->upper].dynamic) {
11969
+ operation.storage.supportEdges.push_back(
11970
+ (static_cast<std::uint64_t>(matched->lower) << 32) | matched->upper);
11971
+ }
11944
11972
  }
11945
11973
  if (operation.storage.contactConstraintCount + matched->newConstraintCount
11946
11974
  > operation.storage.maximumContactConstraintCount) {
@@ -12017,6 +12045,10 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
12017
12045
  && bodies[supportUpper].dynamic) {
12018
12046
  operation.storage.contactSupported[supportIndex(supportUpper)] = 1;
12019
12047
  }
12048
+ if (bodies[supportUpper].dynamic) {
12049
+ operation.storage.supportEdges.push_back(
12050
+ (static_cast<std::uint64_t>(supportLower) << 32) | supportUpper);
12051
+ }
12020
12052
  }
12021
12053
  }
12022
12054
  const std::uint32_t pointCount = contactPointCount(contact);
@@ -12196,7 +12228,27 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
12196
12228
  operation.storage.contactSolvePairCount = writeOrder;
12197
12229
  }
12198
12230
  };
12231
+ const auto propagateContactSupport = [&]() {
12232
+ if (operation.storage.supportPropagated) return;
12233
+ operation.storage.supportPropagated = true;
12234
+ bool changed = true;
12235
+ while (changed) {
12236
+ changed = false;
12237
+ for (const std::uint64_t edge : operation.storage.supportEdges) {
12238
+ const auto lower = static_cast<std::uint32_t>(edge >> 32);
12239
+ const auto upper = static_cast<std::uint32_t>(edge);
12240
+ auto& upperSupported = operation.storage.contactSupported[supportIndex(upper)];
12241
+ if (upperSupported != 0) continue;
12242
+ if (!bodies[lower].dynamic || bodies[lower].kinematic
12243
+ || operation.storage.contactSupported[supportIndex(lower)] != 0) {
12244
+ upperSupported = 1;
12245
+ changed = true;
12246
+ }
12247
+ }
12248
+ }
12249
+ };
12199
12250
  const auto solvePreparedConstraints = [&](bool stabilizeSupported) {
12251
+ if (stabilizeSupported) propagateContactSupport();
12200
12252
  for (std::uint32_t order = operation.storage.contactSolvePairCount;
12201
12253
  order > 0; order -= 1) {
12202
12254
  const std::uint32_t begin = operation.storage.contactSolvePairBegin[order - 1];
@@ -12219,6 +12271,12 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
12219
12271
  // direct pushes already differ (review 10). The owned (joint and portal) path runs one iteration per call, so
12220
12272
  // its loop marks the last call, and the extra passes come before that iteration's joint and
12221
12273
  // portal work, as on the standalone path.
12274
+ //
12275
+ // A pair inside a supported structure (each body static, kinematic, or resting on support, the
12276
+ // structure the frozen-support rule acts on, side contacts included) gets the same floor. With
12277
+ // friction acting on both bodies, a 400-crate stack left out of line by a torch cut or a knock
12278
+ // settles at 24 iterations; at 6, 12, 16 and 18 a few crates kept creeping above the sleep
12279
+ // threshold, so the stack never slept.
12222
12280
  constexpr std::uint32_t kCylinderMinimumVelocityIterations = 24;
12223
12281
  const std::uint32_t cylinderExtraVelocityIterations =
12224
12282
  options.velocityIterations < kCylinderMinimumVelocityIterations
@@ -12227,7 +12285,14 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
12227
12285
  && cylinderExtraVelocityIterations != 0
12228
12286
  && (pass == ContactPass::StandaloneFirstTick || pass == ContactPass::StandaloneLaterSubstep
12229
12287
  || (lastOwnedVelocityIteration && pass == ContactPass::OwnedContinueVelocity));
12288
+ const auto isSupportPair = [&](std::uint32_t first, std::uint32_t second) {
12289
+ const auto supported = [&](std::uint32_t body) {
12290
+ return !bodies[body].dynamic || operation.storage.contactSupported[supportIndex(body)] != 0;
12291
+ };
12292
+ return (bodies[first].dynamic || bodies[second].dynamic) && supported(first) && supported(second);
12293
+ };
12230
12294
  const auto solvePreparedCylinderConstraints = [&]() {
12295
+ propagateContactSupport();
12231
12296
  for (std::uint32_t order = operation.storage.contactSolvePairCount;
12232
12297
  order > 0; order -= 1) {
12233
12298
  const std::uint32_t begin = operation.storage.contactSolvePairBegin[order - 1];
@@ -12237,7 +12302,8 @@ void resolveSoAContacts(SolverOperation& operation, Body* bodies, std::uint32_t
12237
12302
  operation.storage.contactConstraints[begin];
12238
12303
  const std::uint32_t first = firstConstraint.bodyA;
12239
12304
  const std::uint32_t second = firstConstraint.bodyB;
12240
- if (bodies[first].shape != ShapeKind::Cylinder && bodies[second].shape != ShapeKind::Cylinder) continue;
12305
+ if (bodies[first].shape != ShapeKind::Cylinder && bodies[second].shape != ShapeKind::Cylinder
12306
+ && !isSupportPair(first, second)) continue;
12241
12307
  solvePair(first, second, false, true, begin, end);
12242
12308
  }
12243
12309
  };
@@ -20504,6 +20570,58 @@ bool sameMotionSearchBody(const Body& a, const Body& b) {
20504
20570
  && a.geometryOffset == b.geometryOffset && a.geometryCount == b.geometryCount;
20505
20571
  }
20506
20572
 
20573
+ // The contact skin of the native motion clock. It is equal to the contact slop of the solver.
20574
+ // The clock used the same zero-distance test to start and to end a contact. A pair that rests,
20575
+ // slides or grazes then touched and separated again after a few units in the last place of the clock
20576
+ // time, and a search for a contact that stays near zero distance divided the substep into slabs as
20577
+ // narrow as the gap. The skin bounds both: a pair keeps its contact episodes until its gap is larger
20578
+ // than the skin, and a search stops dividing a slab when the bodies move less than a quarter of the
20579
+ // skin across it.
20580
+ constexpr double kMotionContactSkin = 0.005;
20581
+
20582
+ // Returns true when the shapes of `a` and `b` are not certainly farther apart than the skin. The test
20583
+ // grows one primitive body of the pair (sphere, capsule or box) by the skin and then uses collide().
20584
+ // A grown box contains every point within the skin of the box, so a miss proves a gap larger than
20585
+ // the skin. A pair without a primitive body returns true: its episodes stay until the substep ends.
20586
+ bool withinMotionContactSkin(SolverOperation& operation, const Body& a, const Body& b) {
20587
+ const auto grown = [](Body body) -> std::optional<Body> {
20588
+ switch (body.shape) {
20589
+ case ShapeKind::Sphere:
20590
+ case ShapeKind::Capsule:
20591
+ body.shape0 += kMotionContactSkin;
20592
+ return body;
20593
+ case ShapeKind::Box:
20594
+ body.shape0 += kMotionContactSkin;
20595
+ body.shape1 += kMotionContactSkin;
20596
+ body.shape2 += kMotionContactSkin;
20597
+ return body;
20598
+ default: return std::nullopt;
20599
+ }
20600
+ };
20601
+ triangle_adapter_private::work(operation);
20602
+ if (const auto first = grown(a)) return collide(operation, *first, b).hit;
20603
+ if (const auto second = grown(b)) return collide(operation, a, *second).hit;
20604
+ return true;
20605
+ }
20606
+
20607
+ // An upper bound of the speed of every point of a primitive body (sphere, capsule or box) that
20608
+ // follows its motion epoch. The center of mass moves at the linear velocity, and every point turns
20609
+ // about it at most at |angular velocity| times its distance. The bound adds the offsets and the full
20610
+ // shape radius to that distance, so it holds for each shape-offset convention.
20611
+ double motionPointSpeedBound(const BodyMotionEpoch& epoch, const Body& body) {
20612
+ if ((!epoch.dynamic && !epoch.kinematic) || (epoch.dynamic && epoch.sleeping)) return 0.0;
20613
+ const auto length = [](double x, double y, double z) { return solverSqrt(x * x + y * y + z * z); };
20614
+ const double linear = length((epoch.motionLocks & 1u) == 0 ? epoch.linearVelocity[0] : 0.0,
20615
+ (epoch.motionLocks & 2u) == 0 ? epoch.linearVelocity[1] : 0.0,
20616
+ (epoch.motionLocks & 4u) == 0 ? epoch.linearVelocity[2] : 0.0);
20617
+ const double angular = length(epoch.angularVelocity[0], epoch.angularVelocity[1], epoch.angularVelocity[2]);
20618
+ const double extent = body.shape == ShapeKind::Box ? length(body.shape0, body.shape1, body.shape2)
20619
+ : body.shape == ShapeKind::Capsule ? body.shape0 + body.shape1 : body.shape0;
20620
+ const double radius = extent + length(body.shapeOffsetX, body.shapeOffsetY, body.shapeOffsetZ)
20621
+ + length(epoch.centerOfMass[0], epoch.centerOfMass[1], epoch.centerOfMass[2]);
20622
+ return linear + angular * radius;
20623
+ }
20624
+
20507
20625
  class MotionContactSearch {
20508
20626
  public:
20509
20627
  MotionContactSearch(SolverOperation& operation, std::uint32_t first, std::uint32_t second,
@@ -20591,6 +20709,15 @@ class MotionContactSearch {
20591
20709
  using Slab = std::array<double, 2>;
20592
20710
  std::vector<Slab> pending;
20593
20711
  if (now_ != end_) triangle_adapter_private::append(operation_, pending, Slab{now_, end_});
20712
+ // A native slab that the classifier cannot resolve is a leaf when no point of the pair moves
20713
+ // more than a quarter of the contact skin across it (the factor 2 covers the error of the
20714
+ // orientation step). A leaf is decided by the point test at its ends. A contact that starts
20715
+ // and ends inside a leaf penetrates less than the contact slop, so the search may ignore it,
20716
+ // and the number of leaves is at most about 8 * duration * speed / skin.
20717
+ const double speed = std::holds_alternative<NativeSearchGeometry>(geometry_)
20718
+ ? motionPointSpeedBound(epochs_[0], bodies_[0]) + motionPointSpeedBound(epochs_[1], bodies_[1]) : 0.0;
20719
+ if (!finite(speed)) return fail(Status::Unresolved);
20720
+ const double grazeSlab = speed > 0.0 ? kMotionContactSkin * 0.25 / speed : 0.0;
20594
20721
  while (!pending.empty()) {
20595
20722
  triangle_adapter_private::work(operation_);
20596
20723
  const Slab slab = pending.back();
@@ -20621,14 +20748,27 @@ class MotionContactSearch {
20621
20748
  case MotionContactIntervalStatus::Unresolved: break;
20622
20749
  }
20623
20750
  triangle_adapter_private::work(operation_);
20624
- if (slab[0] == slab[1] || std::nextafter(slab[0], slab[1]) == slab[1]) {
20625
- const auto low = captureContact(slab[0]);
20751
+ const bool adjacent = slab[0] == slab[1] || std::nextafter(slab[0], slab[1]) == slab[1];
20752
+ if (adjacent || (material == nullptr && slab[1] - slab[0] <= grazeSlab)) {
20753
+ auto low = captureContact(slab[0]);
20626
20754
  if (const auto failure = std::get_if<Status>(&low)) return fail(*failure);
20627
20755
  if (std::get<Collision>(low).hit != departing) return fail(Status::Unresolved);
20628
- const auto high = captureContact(slab[1]);
20756
+ auto high = captureContact(slab[1]);
20629
20757
  if (const auto failure = std::get_if<Status>(&high)) return fail(*failure);
20758
+ // Divide a leaf that changes state by the point test only, down to adjacent times.
20759
+ Slab bracket = slab;
20760
+ while (std::get<Collision>(high).hit != departing && std::nextafter(bracket[0], bracket[1]) != bracket[1]) {
20761
+ triangle_adapter_private::work(operation_);
20762
+ const double middle = bracket[0] + (bracket[1] - bracket[0]) * 0.5;
20763
+ if (!(bracket[0] < middle && middle < bracket[1])) return fail(Status::Unresolved);
20764
+ auto sample = captureContact(middle);
20765
+ if (const auto failure = std::get_if<Status>(&sample)) return fail(*failure);
20766
+ if (std::get<Collision>(sample).hit != departing) { bracket[1] = middle; high = std::move(sample); }
20767
+ else { bracket[0] = middle; low = std::move(sample); }
20768
+ }
20630
20769
  if (const auto& collision = std::get<Collision>(high); collision.hit != departing) {
20631
- result_.bracket = slab;
20770
+ result_.certifiedEnd = bracket[0];
20771
+ result_.bracket = bracket;
20632
20772
  result_.collision = departing ? std::get<Collision>(low) : collision;
20633
20773
  if (material != nullptr && slab[0] < slab[1]
20634
20774
  && !hasMaterialAngularMotion(material->sources[0]->body().origin)
@@ -20856,9 +20996,10 @@ enum class MotionContactEventBatchStatus : std::uint8_t {
20856
20996
  Selected, Complete, InvalidInput, StaleInput, Unresolved, ResourceExhausted,
20857
20997
  AllocationFailure, UnsupportedGeometry, Consumed,
20858
20998
  };
20999
+ // A native batch event is always an entry: a pair that is not engaged touches at `bracket[1]`.
21000
+ // A pair leaves contact through the contact skin (see withinMotionContactSkin), not through an event.
20859
21001
  struct MotionContactBatchEvent {
20860
21002
  std::array<std::uint32_t, 2> pair;
20861
- MotionContactTransition transition;
20862
21003
  std::array<double, 2> bracket;
20863
21004
  Collision collision;
20864
21005
  };
@@ -20977,6 +21118,12 @@ class MotionContactEventBatch {
20977
21118
  MotionContactEventBatch& operator=(MotionContactEventBatch&&) = delete;
20978
21119
  Status retireDepartures(ContactEventPhaseOwner& phase);
20979
21120
  Status activateEntries(ContactEventPhaseOwner& phase);
21121
+ // Selects the earliest entry events. On the native path a bullet pair is engaged when it touches
21122
+ // or when it has contact episodes (in operation.contactPhaseOwner) and stays within the contact
21123
+ // skin. An engaged pair is not searched, so it causes no event. An entry needs a pair that is not
21124
+ // engaged, and a pair stops being engaged only when its gap is larger than the skin. So each event
21125
+ // after the first event of a pair needs a closing motion of the full skin, and the number of
21126
+ // events in a substep is finite.
20980
21127
  Status run() {
20981
21128
  if (ran_) return Status::Consumed;
20982
21129
  ran_ = true;
@@ -21022,14 +21169,15 @@ class MotionContactEventBatch {
21022
21169
  if ((!a.bullet && !b.bullet) || !bodiesCanCollide(a, b)
21023
21170
  || collisionDisabled(operation_, first, second)) continue;
21024
21171
  triangle_adapter_private::work(operation_);
21025
- const auto transition = collide(operation_, a, b).hit
21026
- ? MotionContactTransition::Departure : MotionContactTransition::Entry;
21027
- MotionContactSearch search(operation_, first, second, transition);
21172
+ if (collide(operation_, a, b).hit) continue;
21173
+ if (hasEpisodes(operation_, first, second)) {
21174
+ if (withinMotionContactSkin(operation_, a, b)) continue;
21175
+ triangle_adapter_private::append(operation_, native.separated, std::array<std::uint32_t, 2>{first, second});
21176
+ }
21177
+ MotionContactSearch search(operation_, first, second, MotionContactTransition::Entry);
21028
21178
  const auto result = search.run();
21029
- if (result == MotionContactSearchStatus::Clear
21030
- || result == MotionContactSearchStatus::ContactThroughEnd) continue;
21031
- if (result != MotionContactSearchStatus::Entry && result != MotionContactSearchStatus::Departure)
21032
- return fail(searchFailure(result));
21179
+ if (result == MotionContactSearchStatus::Clear) continue;
21180
+ if (result != MotionContactSearchStatus::Entry) return fail(searchFailure(result));
21033
21181
  MotionContactSearchView view;
21034
21182
  const auto checked = search.read(operation_, view);
21035
21183
  if (checked != result) return fail(searchFailure(checked));
@@ -21037,7 +21185,7 @@ class MotionContactEventBatch {
21037
21185
  if (view.bracket[1] > native.time) continue;
21038
21186
  if (view.bracket[1] < native.time) { native.events.clear(); native.time = view.bracket[1]; }
21039
21187
  triangle_adapter_private::append(operation_, native.events,
21040
- MotionContactBatchEvent{{first, second}, transition, view.bracket, view.collision});
21188
+ MotionContactBatchEvent{{first, second}, view.bracket, view.collision});
21041
21189
  }
21042
21190
  }
21043
21191
  const auto checked = validateSource();
@@ -21074,6 +21222,7 @@ class MotionContactEventBatch {
21074
21222
  return readMaterialAtPhase(operation, output, true);
21075
21223
  }
21076
21224
  private:
21225
+ static bool hasEpisodes(SolverOperation& operation, std::uint32_t first, std::uint32_t second);
21077
21226
  struct Source { Body body; BodyMotionEpoch epoch; };
21078
21227
  struct NativeSelection {
21079
21228
  const Body* bodyData = nullptr;
@@ -21084,6 +21233,8 @@ class MotionContactEventBatch {
21084
21233
  std::vector<Source> sources;
21085
21234
  std::vector<std::array<std::uint32_t, 2>> disabled;
21086
21235
  std::vector<MotionContactBatchEvent> events;
21236
+ // Pairs that have episodes but are farther apart than the contact skin at `now`.
21237
+ std::vector<std::array<std::uint32_t, 2>> separated;
21087
21238
  };
21088
21239
  using TimeOwner = std::shared_ptr<const MaterialTime>;
21089
21240
  using MotionOwner = std::shared_ptr<const MaterialMotionSource>;
@@ -22628,6 +22779,15 @@ MaterialBoundsStatus adoptMaterialContactResponses(SolverOperation& operation) {
22628
22779
  std::terminate();
22629
22780
  }
22630
22781
 
22782
+ // The contact episodes of a step live in its phase owner. Without a phase owner, no pair has episodes.
22783
+ bool MotionContactEventBatch::hasEpisodes(SolverOperation& operation, std::uint32_t first, std::uint32_t second) {
22784
+ auto* phase = operation.contactPhaseOwner;
22785
+ if (phase == nullptr) return false;
22786
+ const auto index = phase->findEpisode(first, second, 0);
22787
+ return index < phase->episodes_.size() && std::get<0>(phase->episodes_[index].key) == first
22788
+ && std::get<1>(phase->episodes_[index].key) == second;
22789
+ }
22790
+
22631
22791
  MotionContactEventBatchStatus MotionContactEventBatch::retireDepartures(ContactEventPhaseOwner& phase) {
22632
22792
  const auto reject = [&](Status status) {
22633
22793
  transfer_ = FailedEpisodeTransfer{status};
@@ -22675,15 +22835,17 @@ MotionContactEventBatchStatus MotionContactEventBatch::retireDepartures(ContactE
22675
22835
  transfer_ = RetiredEpisodes{&phase};
22676
22836
  return Status::Selected;
22677
22837
  }
22838
+ // A pair that was farther apart than the contact skin when the batch ran leaves contact here.
22839
+ // Its entry search proved that it does not touch before this event time. If it touches at this
22840
+ // time, activateEntries gives it new episodes.
22678
22841
  std::vector<std::array<std::size_t, 2>> ranges;
22679
22842
  std::uint64_t eraseWork = 0;
22680
- for (const auto& event : std::get<NativeSelection>(selection_).events) {
22681
- if (event.transition != MotionContactTransition::Departure) continue;
22682
- const auto first = phase.findEpisode(event.pair[0], event.pair[1], 0);
22843
+ for (const auto& pair : std::get<NativeSelection>(selection_).separated) {
22844
+ const auto first = phase.findEpisode(pair[0], pair[1], 0);
22683
22845
  std::size_t last = first;
22684
22846
  while (last < phase.episodes_.size()
22685
- && std::get<0>(phase.episodes_[last].key) == event.pair[0]
22686
- && std::get<1>(phase.episodes_[last].key) == event.pair[1]) {
22847
+ && std::get<0>(phase.episodes_[last].key) == pair[0]
22848
+ && std::get<1>(phase.episodes_[last].key) == pair[1]) {
22687
22849
  triangle_adapter_private::work(operation_);
22688
22850
  ++last;
22689
22851
  }
@@ -22757,7 +22919,6 @@ MotionContactEventBatchStatus MotionContactEventBatch::activateEntries(ContactEv
22757
22919
  }
22758
22920
  }
22759
22921
  } else for (const auto& event : std::get<NativeSelection>(selection_).events) {
22760
- if (event.transition != MotionContactTransition::Entry) continue;
22761
22922
  const auto& a = operation_.storage.soaBodies[event.pair[0]];
22762
22923
  const auto& b = operation_.storage.soaBodies[event.pair[1]];
22763
22924
  const auto& collision = event.collision;
@@ -24881,32 +25042,13 @@ std::int32_t stepSoAWithJointsInternal(
24881
25042
  }
24882
25043
  }
24883
25044
  }
25045
+ // A world with a bullet body always steps on the native motion clock. The material motion clock
25046
+ // has no contact skin, so a bullet that rests or rolls on it uses all the geometry work of a step,
25047
+ // and it moves bodies only by translation, so a bullet that starts to rotate stops its next search.
25048
+ // A world without a bullet keeps its material pose.
24884
25049
  const auto materialMotionPairsSupported = [&] {
24885
- bool hasBullet = false;
24886
- for (std::uint32_t index = 0; index < bodyCount; ++index) {
24887
- if (operation.storage.soaBodies[index].bullet) {
24888
- hasBullet = true;
24889
- break;
24890
- }
24891
- }
24892
- if (!hasBullet) return true;
24893
- // A pure predicate over pairs that contain a bullet, so the visit order does not matter.
24894
- for (std::uint32_t bullet = 0; bullet < bodyCount; ++bullet) {
24895
- if (!operation.storage.soaBodies[bullet].bullet) continue;
24896
- for (std::uint32_t other = 0; other < bodyCount; ++other) {
24897
- if (other == bullet || (operation.storage.soaBodies[other].bullet && other < bullet)) continue;
24898
- const std::uint32_t first = std::min(bullet, other);
24899
- const std::uint32_t second = std::max(bullet, other);
24900
- const auto& a = operation.storage.soaBodies[first];
24901
- const auto& b = operation.storage.soaBodies[second];
24902
- if ((!a.bullet && !b.bullet) || !bodiesCanCollide(a, b)
24903
- || collisionDisabled(operation, first, second)) continue;
24904
- if ((a.bullet && a.angularState && a.shape != ShapeKind::Sphere)
24905
- || (b.bullet && b.angularState && b.shape != ShapeKind::Sphere)) return false;
24906
- if (motionContactPairKind(a, b) == MotionContactPairKind::Unsupported) return false;
24907
- }
24908
- }
24909
- return true;
25050
+ return std::none_of(operation.storage.soaBodies.begin(), operation.storage.soaBodies.begin() + bodyCount,
25051
+ [](const Body& body) { return body.bullet; });
24910
25052
  };
24911
25053
  WorldPose admittedPose = pose;
24912
25054
  if (!materialMotionPairsSupported()) {