@series-inc/rundot-syncplay 6.0.0-rc.22 → 6.0.0-rc.23

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.
@@ -0,0 +1,133 @@
1
+ # Migrating from Syncplay 6.0.0-rc.20 to rc.22
2
+
3
+ Syncplay `6.0.0-rc.22` introduces ergonomics improvements for branded units, public state codecs for game-owned state containing `bigint` physics identifiers, generalized multi-wheel vehicle verification, and explicit guidance for Kinematic Character Controller (KCC) Revision 2.
4
+
5
+ ---
6
+
7
+ ## 1. Branded Vector Helpers & Migration Codemod (Item 10)
8
+
9
+ ### What Changed
10
+ In rc.20, unit branding (`Meters`, `Radians`, `Newtons`, etc.) was tightened across all physics APIs to prevent unit mismatch bugs. However, writing `{ x: meters(a), y: meters(b), z: meters(c) }` across hundreds of lines introduces substantial boilerplate.
11
+
12
+ rc.22 exports convenient vector constructors that accept both positional coordinates and existing `{ x, y, z }` record objects:
13
+
14
+ ```ts
15
+ import {
16
+ meters3,
17
+ radians3,
18
+ metersPerSecond3,
19
+ metersPerSecondSquared3,
20
+ newtons3,
21
+ vec3,
22
+ meters2,
23
+ radians2,
24
+ metersPerSecond2,
25
+ vec2,
26
+ } from '@series-inc/rundot-syncplay/physics/3d';
27
+ ```
28
+
29
+ ### Usage
30
+
31
+ ```ts
32
+ // Positional coordinates
33
+ const pos = meters3(0, 1.5, -2);
34
+ const vel = metersPerSecond3(0, 0, 15);
35
+ const force = newtons3(0, 500, 0);
36
+
37
+ // Object coordinates (drop-in wrapper for legacy vector objects)
38
+ const legacyVec = { x: 10, y: 0, z: -5 };
39
+ const brandedPos = meters3(legacyVec);
40
+
41
+ // Custom brands
42
+ const custom = vec3(turns, 0.1, 0.2, 0.3);
43
+ ```
44
+
45
+ ### Regex / Search & Replace Codemod
46
+
47
+ If your codebase has repetitive manual `{ x: meters(...), y: meters(...), z: meters(...) }` calls, you can migrate them with the following regular expressions:
48
+
49
+ - **Find:**
50
+ ```regexp
51
+ \{\s*x:\s*meters\(([^)]+)\),\s*y:\s*meters\(([^)]+)\),\s*z:\s*meters\(([^)]+)\)\s*\}
52
+ ```
53
+ - **Replace:**
54
+ ```ts
55
+ meters3($1, $2, $3)
56
+ ```
57
+
58
+ - **2D Vectors Find:**
59
+ ```regexp
60
+ \{\s*x:\s*meters\(([^)]+)\),\s*y:\s*meters\(([^)]+)\)\s*\}
61
+ ```
62
+ - **Replace:**
63
+ ```ts
64
+ meters2($1, $2)
65
+ ```
66
+
67
+ ---
68
+
69
+ ## 2. KCC Revision 2: Native Step-Up & Platform Support Persistence (Item 11)
70
+
71
+ ### Native Step-Up (No Custom Raycast Hacks)
72
+ In Revision 1, stepping up over vertical edges (curbs, stairs, debris) often required game-side raycasts to manually nudge the character upward before stepping.
73
+
74
+ In **Revision 2 (`behaviorRevision: 2`)**, the solver natively sweeps the bottom spherical cap against vertical steps up to `stepHeight`.
75
+ - Author `stepHeight: 0.3` (or your desired maximum step height in meters).
76
+ - A 0.2 m vertical edge will be climbed smoothly and deterministically without clipping or snagging.
77
+ - **Action Required:** Remove any custom game-side pre-step raycasts or edge-nudging hacks; they are redundant and can interfere with the native solver's ground alignment.
78
+
79
+ ### Crucial: Persist the `support` Field Across Ticks
80
+ Revision 2 introduces physical moving-platform carry and continuous contact resolution via the `support` property on `DeterministicKccState3D` / step result.
81
+
82
+ ```ts
83
+ // Ticking KCC Revision 2:
84
+ const result = stepDeterministicKcc3D(input, {
85
+ ...currentState,
86
+ // CRITICAL: Pass back the support returned from the previous tick!
87
+ support: lastStepResult.support,
88
+ }, world, options);
89
+ ```
90
+
91
+ > [!IMPORTANT]
92
+ > If your game drops `support` or creates a fresh state object each frame without copying `lastStepResult.support`, the character controller will not inherit platform velocity or maintain stable ground contact.
93
+
94
+ ---
95
+
96
+ ## 3. Game-Owned State Codecs for BigInt Body IDs (Item 8)
97
+
98
+ ### What Changed
99
+ `DeterministicVehicleState3D` and `DeterministicKccSupport3D` (when `kind: 'physics'`) carry native 64-bit `PhysicsBodyId3D` values represented as JavaScript `bigint` primitives.
100
+
101
+ Standard `JSON.stringify(state)` throws:
102
+ ```
103
+ TypeError: Do not know how to serialize a BigInt
104
+ ```
105
+
106
+ ### Solution
107
+ Syncplay rc.22 exports canonical serialization and deserialization helpers that encode `bigint` without loss:
108
+
109
+ ```ts
110
+ import {
111
+ encodeDeterministicVehicleState3D,
112
+ decodeDeterministicVehicleState3D,
113
+ encodeDeterministicVehicleState3DBytes,
114
+ decodeDeterministicVehicleState3DBytes,
115
+ encodeDeterministicKccSupport3D,
116
+ decodeDeterministicKccSupport3D,
117
+ encodeDeterministicKccSupport3DBytes,
118
+ decodeDeterministicKccSupport3DBytes,
119
+ } from '@series-inc/rundot-syncplay/physics/3d';
120
+
121
+ // Save vehicle state to JSON string or binary Uint8Array
122
+ const savedJson = encodeDeterministicVehicleState3D(vehicleState);
123
+ const savedBytes = encodeDeterministicVehicleState3DBytes(vehicleState);
124
+
125
+ // Restore vehicle state
126
+ const restoredState = decodeDeterministicVehicleState3D(savedJson);
127
+
128
+ // Save KCC support state
129
+ const savedSupportJson = encodeDeterministicKccSupport3D(kccResult.support);
130
+ const restoredSupport = decodeDeterministicKccSupport3D(savedSupportJson);
131
+ ```
132
+
133
+ These functions use canonical JSON formatting with BigInt sentinels (`{"$rdm":"bigint","value":"..."}`) ensuring bit-identical hashing and portable network snapshots across clients.
@@ -11,7 +11,7 @@ exact_dyadic::Operation& arithmetic(SolverOperation& operation){
11
11
  return *operation.triangleArithmetic;
12
12
  }
13
13
  void work(SolverOperation& operation,std::uint64_t amount=1){if(!operation.triangleQuota.charge(amount))throw TriangleNativeFailure{tp::Failure::WorkExhausted};}
14
- void stage(SolverOperation& operation,std::uint64_t bytes){if(bytes>operation.triangleStagingRemaining)throw TriangleNativeFailure{tp::Failure::ArithmeticExhausted};operation.triangleStagingRemaining-=bytes;}
14
+ void stage(SolverOperation& operation,std::uint64_t bytes){if(bytes>operation.triangleStagingRemaining)throw TriangleNativeFailure{tp::Failure::ArithmeticExhausted};operation.triangleStagingRemaining-=bytes;}\
15
15
  template<class T>void append(SolverOperation& operation,std::vector<T>& list,T value){
16
16
  work(operation);
17
17
  if(list.size()==list.capacity()){
@@ -174,10 +174,16 @@ Collision observeGeometry(SolverOperation& operation,const Body& capsule,const B
174
174
  work(operation);
175
175
  arithmetic(operation);
176
176
  if(!orientation_private::validUnit(capsule.orientationX,capsule.orientationY,capsule.orientationZ,capsule.orientationW)
177
- ||!orientation_private::validUnit(geometry.orientationX,geometry.orientationY,geometry.orientationZ,geometry.orientationW))throw TriangleNativeFailure{tp::Failure::InvalidInput};
177
+ ||!orientation_private::validUnit(geometry.orientationX,geometry.orientationY,geometry.orientationZ,geometry.orientationW))throw TriangleNativeFailure{tp::Failure::InvalidInput};\
178
178
  const auto payload=geometryForBody(operation,geometry);
179
- if(payload.size()>12600000u||payload.size()%3)throw TriangleNativeFailure{tp::Failure::FeatureCapacity};
179
+ if(payload.size()>12600000u||payload.size()%3)throw TriangleNativeFailure{tp::Failure::FeatureCapacity};\
180
180
  const auto capsuleAxis=rotateByOrientation(capsule,{0.,1.,0.});
181
+ const double capLatX = capsule.shape0 + axisDelta(capsuleAxis.x) * capsule.shape1;
182
+ const double capLatY = capsule.shape0 + axisDelta(capsuleAxis.y) * capsule.shape1;
183
+ const double capLatZ = capsule.shape0 + axisDelta(capsuleAxis.z) * capsule.shape1;
184
+ const Vec3 rotX = rotateByOrientation(geometry, {1.0, 0.0, 0.0});
185
+ const Vec3 rotY = rotateByOrientation(geometry, {0.0, 1.0, 0.0});
186
+ const Vec3 rotZ = rotateByOrientation(geometry, {0.0, 0.0, 1.0});
181
187
  tp::Input input;input.radius=capsule.shape0;input.projection=tp::ProjectionAssessment::RoundedNativePose;
182
188
  input.segment={tp::Point{-capsuleAxis.x*capsule.shape1,-capsuleAxis.y*capsule.shape1,-capsuleAxis.z*capsule.shape1},tp::Point{capsuleAxis.x*capsule.shape1,capsuleAxis.y*capsule.shape1,capsuleAxis.z*capsule.shape1}};
183
189
  Collision best;
@@ -221,7 +227,7 @@ Collision observeGeometry(SolverOperation& operation,const Body& capsule,const B
221
227
  triangle(candidate.points[0], candidate.points[1], candidate.points[2], candidate.ordinal);
222
228
  }
223
229
  }
224
- };
230
+ };\
225
231
  visit(visit, segment->root);
226
232
  return true;
227
233
  };
@@ -256,10 +262,17 @@ Collision observeGeometry(SolverOperation& operation,const Body& capsule,const B
256
262
  const double cY=geometry.y+geometry.shapeOffsetY+translated.y-(capsule.y+capsule.shapeOffsetY);
257
263
  const double cZ=geometry.z+geometry.shapeOffsetZ+translated.z-(capsule.z+capsule.shapeOffsetZ);
258
264
  const double p0=payload[at+4],p1=payload[at+5],p2=payload[at+6];
259
- const double childRadius=kind==static_cast<std::uint32_t>(ShapeKind::Box)?std::sqrt(p0*p0+p1*p1+p2*p2):(kind==static_cast<std::uint32_t>(ShapeKind::Sphere)?p0:(p0+p1));
260
- const double capsuleRadius=capsule.shape0+capsule.shape1;
261
- const double maxDist=childRadius+capsuleRadius;
262
- if(cX*cX+cY*cY+cZ*cZ>maxDist*maxDist)continue;
265
+ if (kind == static_cast<std::uint32_t>(ShapeKind::Box)) {
266
+ const double boxHalfX = axisDelta(rotX.x) * p0 + axisDelta(rotY.x) * p1 + axisDelta(rotZ.x) * p2;
267
+ const double boxHalfY = axisDelta(rotX.y) * p0 + axisDelta(rotY.y) * p1 + axisDelta(rotZ.y) * p2;
268
+ const double boxHalfZ = axisDelta(rotX.z) * p0 + axisDelta(rotY.z) * p1 + axisDelta(rotZ.z) * p2;
269
+ if (std::abs(cX) > boxHalfX + capLatX || std::abs(cY) > boxHalfY + capLatY || std::abs(cZ) > boxHalfZ + capLatZ) continue;
270
+ } else {
271
+ const double childRadius=kind==static_cast<std::uint32_t>(ShapeKind::Sphere)?p0:(p0+p1);
272
+ const double capsuleRadius=capsule.shape0+capsule.shape1;
273
+ const double maxDist=childRadius+capsuleRadius;
274
+ if(cX*cX+cY*cY+cZ*cZ>maxDist*maxDist)continue;
275
+ }
263
276
  }
264
277
  work(operation);
265
278
  if(child>std::uint32_t(std::numeric_limits<std::int32_t>::max()))throw TriangleNativeFailure{tp::Failure::FeatureCapacity};
@@ -273,10 +286,17 @@ Collision observeGeometry(SolverOperation& operation,const Body& capsule,const B
273
286
  child.x=sum(operation,{geometry.x,geometry.shapeOffsetX,translated.x,-capsule.x,-capsule.shapeOffsetX},certified_sum::SumSite::ChildPose);child.y=sum(operation,{geometry.y,geometry.shapeOffsetY,translated.y,-capsule.y,-capsule.shapeOffsetY},certified_sum::SumSite::ChildPose);child.z=sum(operation,{geometry.z,geometry.shapeOffsetZ,translated.z,-capsule.z,-capsule.shapeOffsetZ},certified_sum::SumSite::ChildPose);
274
287
  child.shapeOffsetX=child.shapeOffsetY=child.shapeOffsetZ=0.;
275
288
  const double p0=child.shape0,p1=child.shape1,p2=child.shape2;
276
- const double childRadius=child.shape==ShapeKind::Box?std::sqrt(p0*p0+p1*p1+p2*p2):(child.shape==ShapeKind::Sphere?p0:(p0+p1));
277
- const double capsuleRadius=capsule.shape0+capsule.shape1;
278
- const double maxDist=childRadius+capsuleRadius;
279
- if(child.x*child.x+child.y*child.y+child.z*child.z>maxDist*maxDist)continue;
289
+ if (node.kind == ShapeKind::Box) {
290
+ const double boxHalfX = axisDelta(rotX.x) * p0 + axisDelta(rotY.x) * p1 + axisDelta(rotZ.x) * p2;
291
+ const double boxHalfY = axisDelta(rotX.y) * p0 + axisDelta(rotY.y) * p1 + axisDelta(rotZ.y) * p2;
292
+ const double boxHalfZ = axisDelta(rotX.z) * p0 + axisDelta(rotY.z) * p1 + axisDelta(rotZ.z) * p2;
293
+ if (std::abs(child.x) > boxHalfX + capLatX || std::abs(child.y) > boxHalfY + capLatY || std::abs(child.z) > boxHalfZ + capLatZ) continue;
294
+ } else {
295
+ const double childRadius=child.shape==ShapeKind::Sphere?p0:(p0+p1);
296
+ const double capsuleRadius=capsule.shape0+capsule.shape1;
297
+ const double maxDist=childRadius+capsuleRadius;
298
+ if(child.x*child.x+child.y*child.y+child.z*child.z>maxDist*maxDist)continue;
299
+ }
280
300
  auto collision=node.kind==ShapeKind::Sphere?capsuleSphere(localCapsule,child):node.kind==ShapeKind::Box?capsuleBox(localCapsule,child):capsuleCapsule(localCapsule,child);
281
301
  if(collision.hit){if(!collision.pointCount){collision.pointCount=1;collision.points[0]={collision.pointX,collision.pointY,collision.pointZ};}
282
302
  auto& basis=collision.coordinates.emplace<TriangleManifoldBasis>();collision.geometryChildIndex=node.outerChild;basis.anchor={capsule.x,capsule.y,capsule.z};basis.shapeOffset={capsule.shapeOffsetX,capsule.shapeOffsetY,capsule.shapeOffsetZ};
@@ -290,9 +290,130 @@ GeometryRecord::GeometryRecord(ConstructionKey, std::shared_ptr<AllocationLedger
290
290
  triangleSegments_(LedgerAllocator<GeometryTriangleSegment>{std::move(ledger)}),
291
291
  id(recordId), descriptor(shape), fingerprint(hash),
292
292
  strongLeaf_(digestGeometryRecord(shape, values)) {
293
+ buildBounds();
293
294
  buildTriangleIndex();
294
295
  }
295
296
 
297
+ void GeometryRecord::buildBounds() {
298
+ if (values_.empty() || descriptor.count == 0) return;
299
+ if (descriptor.kind == ShapeKind::Compound && descriptor.format == 4) {
300
+ constexpr std::uint32_t childStride = 15;
301
+ const std::uint32_t childCount = descriptor.count / 5;
302
+ if (childCount == 0 || childCount * 5 != descriptor.count) return;
303
+ constexpr std::uint32_t first = 0;
304
+ if (first + 12 >= values_.size()) return;
305
+ minimum_[0] = values_[first + 1] + values_[first + 7];
306
+ minimum_[1] = values_[first + 2] + values_[first + 8];
307
+ minimum_[2] = values_[first + 3] + values_[first + 9];
308
+ maximum_[0] = values_[first + 1] + values_[first + 10];
309
+ maximum_[1] = values_[first + 2] + values_[first + 11];
310
+ maximum_[2] = values_[first + 3] + values_[first + 12];
311
+ for (std::uint32_t child = 1; child < childCount; child += 1) {
312
+ const std::uint32_t offset = first + child * childStride;
313
+ if (offset + 12 >= values_.size()) return;
314
+ minimum_[0] = std::min(minimum_[0], values_[offset + 1] + values_[offset + 7]);
315
+ minimum_[1] = std::min(minimum_[1], values_[offset + 2] + values_[offset + 8]);
316
+ minimum_[2] = std::min(minimum_[2], values_[offset + 3] + values_[offset + 9]);
317
+ maximum_[0] = std::max(maximum_[0], values_[offset + 1] + values_[offset + 10]);
318
+ maximum_[1] = std::max(maximum_[1], values_[offset + 2] + values_[offset + 11]);
319
+ maximum_[2] = std::max(maximum_[2], values_[offset + 3] + values_[offset + 12]);
320
+ }
321
+ hasBounds_ = true;
322
+ return;
323
+ }
324
+ if (descriptor.kind == ShapeKind::Compound && descriptor.format == 5) {
325
+ constexpr std::uint32_t childStride = 18;
326
+ constexpr std::uint32_t first = 0;
327
+ if (first + 17 >= values_.size()) return;
328
+ const std::uint32_t childCount = static_cast<std::uint32_t>(values_[first + 17]);
329
+ if (childCount == 0 || childCount * 6 > descriptor.count) return;
330
+ if (first + 12 >= values_.size()) return;
331
+ minimum_[0] = values_[first + 1] + values_[first + 7];
332
+ minimum_[1] = values_[first + 2] + values_[first + 8];
333
+ minimum_[2] = values_[first + 3] + values_[first + 9];
334
+ maximum_[0] = values_[first + 1] + values_[first + 10];
335
+ maximum_[1] = values_[first + 2] + values_[first + 11];
336
+ maximum_[2] = values_[first + 3] + values_[first + 12];
337
+ for (std::uint32_t child = 1; child < childCount; child += 1) {
338
+ const std::uint32_t offset = first + child * childStride;
339
+ if (offset + 12 >= values_.size()) return;
340
+ minimum_[0] = std::min(minimum_[0], values_[offset + 1] + values_[offset + 7]);
341
+ minimum_[1] = std::min(minimum_[1], values_[offset + 2] + values_[offset + 8]);
342
+ minimum_[2] = std::min(minimum_[2], values_[offset + 3] + values_[offset + 9]);
343
+ maximum_[0] = std::max(maximum_[0], values_[offset + 1] + values_[offset + 10]);
344
+ maximum_[1] = std::max(maximum_[1], values_[offset + 2] + values_[offset + 11]);
345
+ maximum_[2] = std::max(maximum_[2], values_[offset + 3] + values_[offset + 12]);
346
+ }
347
+ hasBounds_ = true;
348
+ return;
349
+ }
350
+ if (descriptor.kind == ShapeKind::Compound && descriptor.format == 6) {
351
+ constexpr std::uint32_t childStride = 24;
352
+ constexpr std::uint32_t first = 0;
353
+ if (first + 21 >= values_.size()) return;
354
+ const std::uint32_t childCount = static_cast<std::uint32_t>(values_[first + 21]);
355
+ if (childCount == 0 || childCount * 8 > descriptor.count + 1) return;
356
+ auto calcFmt6 = [&](std::uint32_t offset, double& minX, double& minY, double& minZ, double& maxX, double& maxY, double& maxZ) {
357
+ if (offset + 16 >= values_.size()) return false;
358
+ const double px = values_[offset + 1];
359
+ const double py = values_[offset + 2];
360
+ const double pz = values_[offset + 3];
361
+ const double cMinX = values_[offset + 7];
362
+ const double cMinY = values_[offset + 8];
363
+ const double cMinZ = values_[offset + 9];
364
+ const double cMaxX = values_[offset + 10];
365
+ const double cMaxY = values_[offset + 11];
366
+ const double cMaxZ = values_[offset + 12];
367
+ const double qx = values_[offset + 13];
368
+ const double qy = values_[offset + 14];
369
+ const double qz = values_[offset + 15];
370
+ const double qw = values_[offset + 16];
371
+ if (qx == 0.0 && qy == 0.0 && qz == 0.0 && qw == 1.0) {
372
+ minX = px + cMinX; minY = py + cMinY; minZ = pz + cMinZ;
373
+ maxX = px + cMaxX; maxY = py + cMaxY; maxZ = pz + cMaxZ;
374
+ return true;
375
+ }
376
+ const double r00 = 1.0 - 2.0 * (qy * qy + qz * qz);
377
+ const double r01 = 2.0 * (qx * qy - qw * qz);
378
+ const double r02 = 2.0 * (qx * qz + qw * qy);
379
+ const double r10 = 2.0 * (qx * qy + qw * qz);
380
+ const double r11 = 1.0 - 2.0 * (qx * qx + qz * qz);
381
+ const double r12 = 2.0 * (qy * qz - qw * qx);
382
+ const double r20 = 2.0 * (qx * qz - qw * qy);
383
+ const double r21 = 2.0 * (qy * qz + qw * qx);
384
+ const double r22 = 1.0 - 2.0 * (qx * qx + qy * qy);
385
+ const double cx = 0.5 * (cMinX + cMaxX);
386
+ const double cy = 0.5 * (cMinY + cMaxY);
387
+ const double cz = 0.5 * (cMinZ + cMaxZ);
388
+ const double hx = 0.5 * (cMaxX - cMinX);
389
+ const double hy = 0.5 * (cMaxY - cMinY);
390
+ const double hz = 0.5 * (cMaxZ - cMinZ);
391
+ const double rotCx = px + r00 * cx + r01 * cy + r02 * cz;
392
+ const double rotCy = py + r10 * cx + r11 * cy + r12 * cz;
393
+ const double rotCz = pz + r20 * cx + r21 * cy + r22 * cz;
394
+ const double rotHx = std::abs(r00) * hx + std::abs(r01) * hy + std::abs(r02) * hz;
395
+ const double rotHy = std::abs(r10) * hx + std::abs(r11) * hy + std::abs(r12) * hz;
396
+ const double rotHz = std::abs(r20) * hx + std::abs(r21) * hy + std::abs(r22) * hz;
397
+ minX = rotCx - rotHx; minY = rotCy - rotHy; minZ = rotCz - rotHz;
398
+ maxX = rotCx + rotHx; maxY = rotCy + rotHy; maxZ = rotCz + rotHz;
399
+ return true;
400
+ };
401
+ if (!calcFmt6(first, minimum_[0], minimum_[1], minimum_[2], maximum_[0], maximum_[1], maximum_[2])) return;
402
+ for (std::uint32_t child = 1; child < childCount; child += 1) {
403
+ double cMinX, cMinY, cMinZ, cMaxX, cMaxY, cMaxZ;
404
+ if (!calcFmt6(first + child * childStride, cMinX, cMinY, cMinZ, cMaxX, cMaxY, cMaxZ)) return;
405
+ minimum_[0] = std::min(minimum_[0], cMinX);
406
+ minimum_[1] = std::min(minimum_[1], cMinY);
407
+ minimum_[2] = std::min(minimum_[2], cMinZ);
408
+ maximum_[0] = std::max(maximum_[0], cMaxX);
409
+ maximum_[1] = std::max(maximum_[1], cMaxY);
410
+ maximum_[2] = std::max(maximum_[2], cMaxZ);
411
+ }
412
+ hasBounds_ = true;
413
+ return;
414
+ }
415
+ }
416
+
296
417
  const GeometryTriangleSegment* GeometryRecord::triangleSegment(ShapeKind kind,
297
418
  std::uint32_t offset, std::uint32_t count, std::uint32_t format) const noexcept {
298
419
  const auto found = std::lower_bound(triangleSegments_.begin(), triangleSegments_.end(), offset,
@@ -44,6 +44,10 @@ class GeometryRecord : public std::enable_shared_from_this<GeometryRecord> {
44
44
  std::vector<GeometryTriangle, LedgerAllocator<GeometryTriangle>> triangles_;
45
45
  std::vector<GeometryTriangleNode, LedgerAllocator<GeometryTriangleNode>> triangleNodes_;
46
46
  std::vector<GeometryTriangleSegment, LedgerAllocator<GeometryTriangleSegment>> triangleSegments_;
47
+ bool hasBounds_ = false;
48
+ std::array<double, 3> minimum_{};
49
+ std::array<double, 3> maximum_{};
50
+ void buildBounds();
47
51
  void buildTriangleIndex();
48
52
  std::uint32_t buildTriangleNode(std::uint32_t offset, std::uint32_t first, std::uint32_t count);
49
53
  public:
@@ -61,6 +65,9 @@ class GeometryRecord : public std::enable_shared_from_this<GeometryRecord> {
61
65
  std::span<const GeometryTriangleNode> triangleNodes() const noexcept { return triangleNodes_; }
62
66
  const GeometryTriangleSegment* triangleSegment(ShapeKind kind, std::uint32_t offset,
63
67
  std::uint32_t count, std::uint32_t format) const noexcept;
68
+ bool hasBounds() const noexcept { return hasBounds_; }
69
+ const std::array<double, 3>& minimum() const noexcept { return minimum_; }
70
+ const std::array<double, 3>& maximum() const noexcept { return maximum_; }
64
71
  };
65
72
  using GeometryRecordOwner = std::shared_ptr<const GeometryRecord>;
66
73
  struct GeometryBinding {
@@ -1272,6 +1272,15 @@ inline void calculateChildBoundsFormat6(
1272
1272
  }
1273
1273
 
1274
1274
  bool geometryBounds(SolverOperation& operation, const Body& body, GeometryBounds& bounds) {
1275
+ if (body.geometryRecord != nullptr && body.geometryRecord->hasBounds()) {
1276
+ bounds.minX = body.geometryRecord->minimum()[0];
1277
+ bounds.minY = body.geometryRecord->minimum()[1];
1278
+ bounds.minZ = body.geometryRecord->minimum()[2];
1279
+ bounds.maxX = body.geometryRecord->maximum()[0];
1280
+ bounds.maxY = body.geometryRecord->maximum()[1];
1281
+ bounds.maxZ = body.geometryRecord->maximum()[2];
1282
+ return true;
1283
+ }
1275
1284
  if (geometryForBody(operation, body).data() == nullptr || body.geometryCount == 0) return false;
1276
1285
  if (body.shape == ShapeKind::Compound && body.geometryFormat == 4) {
1277
1286
  constexpr std::uint32_t childStride = 15;
@@ -6934,6 +6943,9 @@ RayHit rayBody(SolverOperation& operation, const Body& body, const Vec3& origin,
6934
6943
  }
6935
6944
  if (body.shape == ShapeKind::Compound
6936
6945
  && (body.geometryFormat == 4 || body.geometryFormat == 5 || body.geometryFormat == 6)) {
6946
+ const BroadphaseBounds bounds = queryProxyBounds(operation, body);
6947
+ if (!rayAabb(bounds, origin, direction, maxDistance).hit) return {};
6948
+
6937
6949
  const Vec3 center{body.x + body.shapeOffsetX, body.y + body.shapeOffsetY, body.z + body.shapeOffsetZ};
6938
6950
  const Vec3 localOrigin = inverseRotateByOrientation(body, subtractVec3(origin, center));
6939
6951
  const Vec3 localDirection = inverseRotateByOrientation(body, direction);
@@ -6942,9 +6954,21 @@ RayHit rayBody(SolverOperation& operation, const Body& body, const Vec3& origin,
6942
6954
  localParent.shapeOffsetX = localParent.shapeOffsetY = localParent.shapeOffsetZ = 0.0;
6943
6955
  localParent.orientationX = localParent.orientationY = localParent.orientationZ = 0.0;
6944
6956
  localParent.orientationW = 1.0;
6957
+ const Vec3 localEnd = addVec3(localOrigin, scaleVec3(localDirection, maxDistance));
6958
+ const BroadphaseBounds localRayBounds{
6959
+ std::min(localOrigin.x, localEnd.x),
6960
+ std::min(localOrigin.y, localEnd.y),
6961
+ std::min(localOrigin.z, localEnd.z),
6962
+ std::max(localOrigin.x, localEnd.x),
6963
+ std::max(localOrigin.y, localEnd.y),
6964
+ std::max(localOrigin.z, localEnd.z),
6965
+ };
6945
6966
  RayHit best;
6946
6967
  for (std::uint32_t child = 0; child < compoundChildCount(operation, body); child += 1) {
6947
- RayHit candidate = rayBody(operation, compoundChildBody(operation, localParent, child), localOrigin, localDirection, maxDistance);
6968
+ const Body childBody = compoundChildBody(operation, localParent, child);
6969
+ const BroadphaseBounds childBounds = queryProxyBounds(operation, childBody);
6970
+ if (!boundsOverlap(localRayBounds, childBounds)) continue;
6971
+ RayHit candidate = rayBody(operation, childBody, localOrigin, localDirection, maxDistance);
6948
6972
  if (candidate.hit && (!best.hit || candidate.distance < best.distance)) {
6949
6973
  candidate.childIndex = static_cast<std::int32_t>(child);
6950
6974
  best = candidate;
@@ -7107,10 +7131,26 @@ std::int32_t runRaycastQuery(SolverOperation& operation,
7107
7131
  const double length = solverSqrt(dotVec3(rawDirection, rawDirection));
7108
7132
  const Vec3 direction = scaleVec3(rawDirection, 1.0 / length);
7109
7133
  const Vec3 origin{queryData[7], queryData[8], queryData[9]};
7134
+ const double maxDistance = queryData[17];
7135
+ const Vec3 rayEnd = addVec3(origin, scaleVec3(direction, maxDistance));
7136
+ const BroadphaseBounds rayBounds{
7137
+ std::min(origin.x, rayEnd.x),
7138
+ std::min(origin.y, rayEnd.y),
7139
+ std::min(origin.z, rayEnd.z),
7140
+ std::max(origin.x, rayEnd.x),
7141
+ std::max(origin.y, rayEnd.y),
7142
+ std::max(origin.z, rayEnd.z),
7143
+ };
7110
7144
  for (std::uint32_t index = 0; index < bodyCount; index += 1) {
7111
7145
  if (static_cast<std::int32_t>(queryData[20]) == static_cast<std::int32_t>(index)
7112
7146
  || !queryBodyAllowed(operation.storage.soaBodies[index], queryData)) continue;
7113
- appendQueryHit(operation, index, rayBody(operation, operation.storage.soaBodies[index], origin, direction, queryData[17]));
7147
+ const Body& target = operation.storage.soaBodies[index];
7148
+ if (target.shape != ShapeKind::Mesh && target.shape != ShapeKind::Hull
7149
+ && target.shape != ShapeKind::Heightfield) {
7150
+ const BroadphaseBounds targetBounds = queryProxyBounds(operation, target);
7151
+ if (!boundsOverlap(rayBounds, targetBounds)) continue;
7152
+ }
7153
+ appendQueryHit(operation, index, rayBody(operation, target, origin, direction, maxDistance));
7114
7154
  }
7115
7155
  finalizeQueryResults(operation, false);
7116
7156
  return 0;
@@ -7156,7 +7196,42 @@ std::int32_t runOverlapQuery(SolverOperation& operation,
7156
7196
  const bool useGeometryProxy = (isGeometryShape(queryBody.shape) && isGeometryShape(target.shape))
7157
7197
  || (cast && (target.shape == ShapeKind::Mesh && target.geometryCount % 3 != 0))
7158
7198
  || (cast && (queryBody.shape == ShapeKind::Mesh && queryBody.geometryCount % 3 != 0));
7159
- auto collisionAt = [&](const Body& moved) {
7199
+ struct ActiveCompoundChild {
7200
+ std::uint32_t index;
7201
+ Body body;
7202
+ BroadphaseBounds bounds;
7203
+ };
7204
+ std::vector<ActiveCompoundChild> activeCompoundChildren;
7205
+ if (cast && target.shape == ShapeKind::Compound) {
7206
+ for (std::uint32_t child = 0; child < compoundChildCount(operation, target); child += 1) {
7207
+ const Body childBody = compoundChildBody(operation, target, child);
7208
+ const BroadphaseBounds childBounds = queryProxyBounds(operation, childBody);
7209
+ if (boundsOverlap(queryBounds, childBounds)) {
7210
+ activeCompoundChildren.push_back({child, childBody, childBounds});
7211
+ }
7212
+ }
7213
+ if (activeCompoundChildren.empty()) continue;
7214
+ }
7215
+ auto collisionAt = [&](const Body& moved) -> Collision {
7216
+ if (cast && target.shape == ShapeKind::Compound) {
7217
+ const BroadphaseBounds boundsB = queryProxyBounds(operation, moved);
7218
+ Collision best;
7219
+ std::int32_t bestChild = -1;
7220
+ for (const auto& item : activeCompoundChildren) {
7221
+ if (!boundsOverlap(item.bounds, boundsB)) continue;
7222
+ const Collision candidate = collide(operation, moved, item.body);
7223
+ if (candidate.hit) {
7224
+ if (!best.hit || candidate.penetration > best.penetration) {
7225
+ best = candidate;
7226
+ bestChild = static_cast<std::int32_t>(item.index);
7227
+ }
7228
+ }
7229
+ }
7230
+ if (best.hit && bestChild >= 0) {
7231
+ best.geometryChildIndex = bestChild;
7232
+ }
7233
+ return best;
7234
+ }
7160
7235
  return useGeometryProxy ? queryProxyCollision(operation, moved, target) : collide(operation, moved, target);
7161
7236
  };
7162
7237
  if (!cast) {