@threenative/physics 0.3.21 → 0.3.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.
package/dist/index.js CHANGED
@@ -1229,6 +1229,24 @@ function nativeBodyShape(shape) {
1229
1229
  z: shape.z
1230
1230
  };
1231
1231
  }
1232
+ if (shape.kind === "convexHull") {
1233
+ if (shape.vertices === void 0)
1234
+ throw new Error("TN_NATIVE_PHYSICS_SHAPE_INVALID: convexHull requires vertices");
1235
+ return {
1236
+ collisionLayer: shape.collisionLayer,
1237
+ collisionMask: shape.collisionMask,
1238
+ kind: shape.kind,
1239
+ sensor: shape.sensor,
1240
+ vertices: shape.vertices,
1241
+ x: shape.x,
1242
+ y: shape.y,
1243
+ z: shape.z
1244
+ };
1245
+ }
1246
+ return primitiveShape(shape);
1247
+ }
1248
+ function nativeQueryShape(shape) {
1249
+ if (shape.kind === "convexHull" || shape.kind === "trimesh") return nativeBodyShape(shape);
1232
1250
  return primitiveShape(shape);
1233
1251
  }
1234
1252
  function opaqueNativeShape(shape) {
@@ -1324,6 +1342,32 @@ function createNativePhysicsSimulation(raw, version) {
1324
1342
  position: finiteQueryVector(hit.position, "query hit position")
1325
1343
  };
1326
1344
  };
1345
+ const rayOutputHit = () => {
1346
+ const bodyId3 = rayOutput[0];
1347
+ const distance = rayOutput[7];
1348
+ if (bodyId3 === void 0 || !Number.isSafeInteger(bodyId3) || bodyId3 < 0 || distance === void 0)
1349
+ throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output is malformed");
1350
+ const body = bodyHandle(bodyId3);
1351
+ const px = rayOutput[1];
1352
+ const py = rayOutput[2];
1353
+ const pz = rayOutput[3];
1354
+ if (!Number.isFinite(px) || !Number.isFinite(py) || !Number.isFinite(pz))
1355
+ throw new Error("TN_NATIVE_PHYSICS_INVALID: query hit position contains a non-finite vector");
1356
+ if (!Number.isFinite(distance) || distance < 0)
1357
+ throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output distance is invalid");
1358
+ const nx = rayOutput[4];
1359
+ const ny = rayOutput[5];
1360
+ const nz = rayOutput[6];
1361
+ if (!Number.isFinite(nx) || !Number.isFinite(ny) || !Number.isFinite(nz))
1362
+ throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output normal contains a non-finite vector");
1363
+ return {
1364
+ body,
1365
+ entity: body.entity,
1366
+ position: { x: px, y: py, z: pz },
1367
+ distance,
1368
+ normal: { x: nx, y: ny, z: nz }
1369
+ };
1370
+ };
1327
1371
  const refreshBodyTransforms = () => {
1328
1372
  if (!bodyTransformsDirty) return;
1329
1373
  const required = Math.max(1, bodyIds.size) * PHYSICS_TRANSFORM_STRIDE;
@@ -1504,6 +1548,7 @@ function createNativePhysicsSimulation(raw, version) {
1504
1548
  if (raw.setBodyTransform === void 0)
1505
1549
  throw new Error("TN_NATIVE_PHYSICS_SET_TRANSFORM_MISSING: runtime ABI is too old");
1506
1550
  raw.setBodyTransform(id, position);
1551
+ bodyTransformsDirty = true;
1507
1552
  },
1508
1553
  // Keep the optional raw members: an older runtime must throw rather than accept a call and
1509
1554
  // drop the actuation, which would make the same game diverge by platform.
@@ -1600,24 +1645,7 @@ function createNativePhysicsSimulation(raw, version) {
1600
1645
  if (hit === 0) return void 0;
1601
1646
  if (hit !== 1)
1602
1647
  throw new Error("TN_NATIVE_PHYSICS_INVALID: ray query returned an invalid status");
1603
- const bodyId3 = rayOutput[0];
1604
- const position = {
1605
- x: rayOutput[1],
1606
- y: rayOutput[2],
1607
- z: rayOutput[3]
1608
- };
1609
- const normal = {
1610
- x: rayOutput[4],
1611
- y: rayOutput[5],
1612
- z: rayOutput[6]
1613
- };
1614
- const distance = rayOutput[7];
1615
- if (bodyId3 === void 0 || !Number.isSafeInteger(bodyId3) || bodyId3 < 0 || distance === void 0)
1616
- throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output is malformed");
1617
- const base2 = nativeHit({ bodyId: bodyId3, position });
1618
- if (!Number.isFinite(distance) || distance < 0)
1619
- throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output distance is invalid");
1620
- return { ...base2, distance, normal: finiteQueryVector(normal, "ray output normal") };
1648
+ return rayOutputHit();
1621
1649
  }
1622
1650
  if (hit === void 0 || hit === null) return void 0;
1623
1651
  if (!Number.isFinite(hit.distance) || hit.distance < 0)
@@ -1637,7 +1665,7 @@ function createNativePhysicsSimulation(raw, version) {
1637
1665
  maxResults: query.maxResults,
1638
1666
  position: query.position,
1639
1667
  rotation: query.rotation,
1640
- shape: primitiveShape(query.shape)
1668
+ shape: nativeQueryShape(query.shape)
1641
1669
  });
1642
1670
  if (!Array.isArray(hits) || hits.length > query.maxResults)
1643
1671
  throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed shape query result");
@@ -1815,6 +1843,7 @@ installPhysicsSimulationBackend(createNativePhysicsBackend(nativePhysicsHost));
1815
1843
  // src/native/wasm-host.ts
1816
1844
  var EXPORTS = [
1817
1845
  "tn_physics_add_body",
1846
+ "tn_physics_add_convex_hull_body",
1818
1847
  "tn_physics_add_heightfield_body",
1819
1848
  "tn_physics_add_trimesh_body",
1820
1849
  "tn_physics_alloc",
@@ -1828,6 +1857,7 @@ var EXPORTS = [
1828
1857
  "tn_physics_destroy",
1829
1858
  "tn_physics_drain_collision_events",
1830
1859
  "tn_physics_free",
1860
+ "tn_physics_intersect_mesh_shape",
1831
1861
  "tn_physics_intersect_point",
1832
1862
  "tn_physics_intersect_ray",
1833
1863
  "tn_physics_intersect_shape",
@@ -1849,6 +1879,13 @@ var EXPORTS = [
1849
1879
  "tn_physics_step",
1850
1880
  "tn_physics_version"
1851
1881
  ];
1882
+ var QUERY_SHAPE_TYPES = {
1883
+ box: 0,
1884
+ capsule: 2,
1885
+ convexHull: 5,
1886
+ sphere: 1,
1887
+ trimesh: 3
1888
+ };
1852
1889
  var MAX_EXACT_FLOAT_ID = 16777215;
1853
1890
  var F32_MAX = 34028234663852886e22;
1854
1891
  var ACTUATION_OK = 1;
@@ -1990,6 +2027,7 @@ function createWasmPhysicsHost(exports) {
1990
2027
  let nextVehicleId = 0;
1991
2028
  const first = { bytes: 0, ptr: 0 };
1992
2029
  const second = { bytes: 0, ptr: 0 };
2030
+ const third = { bytes: 0, ptr: 0 };
1993
2031
  const ensure = (scratch, bytes) => {
1994
2032
  if (scratch.bytes >= bytes) return scratch.ptr;
1995
2033
  const size = Math.max(64, bytes, scratch.bytes * 2);
@@ -2000,6 +2038,19 @@ function createWasmPhysicsHost(exports) {
2000
2038
  scratch.bytes = size;
2001
2039
  return scratch.ptr;
2002
2040
  };
2041
+ const meshBuffers = (shape, trimesh) => {
2042
+ const { vertices, indices } = shape;
2043
+ if (!(vertices instanceof Float32Array)) throw INVALID;
2044
+ if (!trimesh) return [copyIn(second, vertices), vertices.length, 0, 0];
2045
+ if (!(indices instanceof Uint32Array)) throw INVALID;
2046
+ return [copyIn(second, vertices), vertices.length, copyIn(third, indices), indices.length];
2047
+ };
2048
+ const queryVolume = (shape) => {
2049
+ const type = QUERY_SHAPE_TYPES[shape.kind];
2050
+ if (type === void 0 || !Object.hasOwn(QUERY_SHAPE_TYPES, shape.kind))
2051
+ throw INVALID;
2052
+ return type === 3 || type === 5 ? { mesh: meshBuffers(shape, type === 3), type } : { type };
2053
+ };
2003
2054
  const live = () => {
2004
2055
  if (simulation === 0) throw new Error("physics simulation is disposed");
2005
2056
  };
@@ -2065,9 +2116,14 @@ function createWasmPhysicsHost(exports) {
2065
2116
  value.type
2066
2117
  );
2067
2118
  const shape = fields(value.shape);
2068
- const shapeType = ["box", "sphere", "capsule", "trimesh", "heightfield"].indexOf(
2069
- shape.kind
2070
- );
2119
+ const shapeType = [
2120
+ "box",
2121
+ "sphere",
2122
+ "capsule",
2123
+ "trimesh",
2124
+ "heightfield",
2125
+ "convexHull"
2126
+ ].indexOf(shape.kind);
2071
2127
  if (typeof value.type !== "string" || bodyType < 0) throw INVALID;
2072
2128
  if (typeof shape.kind !== "string" || shapeType < 0) throw INVALID;
2073
2129
  u32[r2] = nextId;
@@ -2117,6 +2173,15 @@ function createWasmPhysicsHost(exports) {
2117
2173
  sy,
2118
2174
  sz
2119
2175
  );
2176
+ } else if (shapeType === 5) {
2177
+ const { vertices } = shape;
2178
+ if (!(vertices instanceof Float32Array)) throw INVALID;
2179
+ added = wasm.tn_physics_add_convex_hull_body(
2180
+ simulation,
2181
+ record,
2182
+ copyIn(first, vertices),
2183
+ vertices.length
2184
+ );
2120
2185
  } else added = wasm.tn_physics_add_body(simulation, record);
2121
2186
  } catch (error) {
2122
2187
  throw fail(error, "physics body options are invalid");
@@ -2580,11 +2645,13 @@ function createWasmPhysicsHost(exports) {
2580
2645
  if (query === void 0) throw new Error("intersectShape requires query options");
2581
2646
  let pointer;
2582
2647
  let limit;
2648
+ let mesh;
2583
2649
  try {
2584
2650
  const value = fields(query);
2585
2651
  const shape = fields(value.shape);
2586
- const shapeType = ["box", "sphere", "capsule"].indexOf(shape.kind);
2587
- if (typeof shape.kind !== "string" || shapeType < 0) throw INVALID;
2652
+ const volume = queryVolume(shape);
2653
+ const shapeType = volume.type;
2654
+ mesh = volume.mesh;
2588
2655
  limit = maxResults(value);
2589
2656
  pointer = ensure(first, limit * QUERY_HIT_BYTES);
2590
2657
  views();
@@ -2600,7 +2667,7 @@ function createWasmPhysicsHost(exports) {
2600
2667
  } catch (error) {
2601
2668
  throw fail(error, "intersectShape query options are invalid");
2602
2669
  }
2603
- const count = wasm.tn_physics_intersect_shape(simulation, record, pointer, limit);
2670
+ const count = mesh === void 0 ? wasm.tn_physics_intersect_shape(simulation, record, pointer, limit) : wasm.tn_physics_intersect_mesh_shape(simulation, record, ...mesh, pointer, limit);
2604
2671
  if (count < 0) throw new Error("intersectShape query was rejected");
2605
2672
  return queryHits(pointer, count);
2606
2673
  },
@@ -2629,7 +2696,7 @@ function createWasmPhysicsHost(exports) {
2629
2696
  if (simulation === 0) return;
2630
2697
  wasm.tn_physics_destroy(simulation);
2631
2698
  simulation = 0;
2632
- for (const scratch of [first, second]) {
2699
+ for (const scratch of [first, second, third]) {
2633
2700
  if (scratch.bytes > 0) wasm.tn_physics_free(scratch.ptr, scratch.bytes);
2634
2701
  scratch.bytes = 0;
2635
2702
  scratch.ptr = 0;
@@ -7,11 +7,11 @@ import 'recast-navigation';
7
7
  import '@threenative/core/world';
8
8
 
9
9
  interface INativeShapeDescriptor {
10
- readonly kind: "box" | "capsule" | "heightfield" | "sphere" | "trimesh";
10
+ readonly kind: "box" | "capsule" | "convexHull" | "heightfield" | "sphere" | "trimesh";
11
11
  readonly x: number;
12
12
  readonly y: number;
13
13
  readonly z: number;
14
- /** Present for `trimesh` only: a flat xyz vertex buffer and a flat triangle-index buffer. */
14
+ /** `trimesh` and `convexHull`: a flat xyz vertex buffer; `trimesh` adds a flat triangle-index buffer. */
15
15
  readonly vertices?: Float32Array;
16
16
  readonly indices?: Uint32Array;
17
17
  /** Present for `heightfield` only: column-major samples, the grid and its full extent. */
@@ -349,6 +349,24 @@ function nativeBodyShape(shape) {
349
349
  z: shape.z
350
350
  };
351
351
  }
352
+ if (shape.kind === "convexHull") {
353
+ if (shape.vertices === void 0)
354
+ throw new Error("TN_NATIVE_PHYSICS_SHAPE_INVALID: convexHull requires vertices");
355
+ return {
356
+ collisionLayer: shape.collisionLayer,
357
+ collisionMask: shape.collisionMask,
358
+ kind: shape.kind,
359
+ sensor: shape.sensor,
360
+ vertices: shape.vertices,
361
+ x: shape.x,
362
+ y: shape.y,
363
+ z: shape.z
364
+ };
365
+ }
366
+ return primitiveShape(shape);
367
+ }
368
+ function nativeQueryShape(shape) {
369
+ if (shape.kind === "convexHull" || shape.kind === "trimesh") return nativeBodyShape(shape);
352
370
  return primitiveShape(shape);
353
371
  }
354
372
  function opaqueNativeShape(shape) {
@@ -444,6 +462,32 @@ function createNativePhysicsSimulation(raw, version) {
444
462
  position: finiteQueryVector(hit.position, "query hit position")
445
463
  };
446
464
  };
465
+ const rayOutputHit = () => {
466
+ const bodyId2 = rayOutput[0];
467
+ const distance = rayOutput[7];
468
+ if (bodyId2 === void 0 || !Number.isSafeInteger(bodyId2) || bodyId2 < 0 || distance === void 0)
469
+ throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output is malformed");
470
+ const body = bodyHandle(bodyId2);
471
+ const px = rayOutput[1];
472
+ const py = rayOutput[2];
473
+ const pz = rayOutput[3];
474
+ if (!Number.isFinite(px) || !Number.isFinite(py) || !Number.isFinite(pz))
475
+ throw new Error("TN_NATIVE_PHYSICS_INVALID: query hit position contains a non-finite vector");
476
+ if (!Number.isFinite(distance) || distance < 0)
477
+ throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output distance is invalid");
478
+ const nx = rayOutput[4];
479
+ const ny = rayOutput[5];
480
+ const nz = rayOutput[6];
481
+ if (!Number.isFinite(nx) || !Number.isFinite(ny) || !Number.isFinite(nz))
482
+ throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output normal contains a non-finite vector");
483
+ return {
484
+ body,
485
+ entity: body.entity,
486
+ position: { x: px, y: py, z: pz },
487
+ distance,
488
+ normal: { x: nx, y: ny, z: nz }
489
+ };
490
+ };
447
491
  const refreshBodyTransforms = () => {
448
492
  if (!bodyTransformsDirty) return;
449
493
  const required = Math.max(1, bodyIds.size) * PHYSICS_TRANSFORM_STRIDE;
@@ -624,6 +668,7 @@ function createNativePhysicsSimulation(raw, version) {
624
668
  if (raw.setBodyTransform === void 0)
625
669
  throw new Error("TN_NATIVE_PHYSICS_SET_TRANSFORM_MISSING: runtime ABI is too old");
626
670
  raw.setBodyTransform(id, position);
671
+ bodyTransformsDirty = true;
627
672
  },
628
673
  // Keep the optional raw members: an older runtime must throw rather than accept a call and
629
674
  // drop the actuation, which would make the same game diverge by platform.
@@ -720,24 +765,7 @@ function createNativePhysicsSimulation(raw, version) {
720
765
  if (hit === 0) return void 0;
721
766
  if (hit !== 1)
722
767
  throw new Error("TN_NATIVE_PHYSICS_INVALID: ray query returned an invalid status");
723
- const bodyId2 = rayOutput[0];
724
- const position = {
725
- x: rayOutput[1],
726
- y: rayOutput[2],
727
- z: rayOutput[3]
728
- };
729
- const normal = {
730
- x: rayOutput[4],
731
- y: rayOutput[5],
732
- z: rayOutput[6]
733
- };
734
- const distance = rayOutput[7];
735
- if (bodyId2 === void 0 || !Number.isSafeInteger(bodyId2) || bodyId2 < 0 || distance === void 0)
736
- throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output is malformed");
737
- const base2 = nativeHit({ bodyId: bodyId2, position });
738
- if (!Number.isFinite(distance) || distance < 0)
739
- throw new Error("TN_NATIVE_PHYSICS_INVALID: ray output distance is invalid");
740
- return { ...base2, distance, normal: finiteQueryVector(normal, "ray output normal") };
768
+ return rayOutputHit();
741
769
  }
742
770
  if (hit === void 0 || hit === null) return void 0;
743
771
  if (!Number.isFinite(hit.distance) || hit.distance < 0)
@@ -757,7 +785,7 @@ function createNativePhysicsSimulation(raw, version) {
757
785
  maxResults: query.maxResults,
758
786
  position: query.position,
759
787
  rotation: query.rotation,
760
- shape: primitiveShape(query.shape)
788
+ shape: nativeQueryShape(query.shape)
761
789
  });
762
790
  if (!Array.isArray(hits) || hits.length > query.maxResults)
763
791
  throw new Error("TN_NATIVE_PHYSICS_INVALID: malformed shape query result");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@threenative/physics",
3
- "version": "0.3.21",
3
+ "version": "0.3.23",
4
4
  "license": "MIT",
5
5
  "description": "Thin Rapier physics bindings for ThreeNative games.",
6
6
  "keywords": [
@@ -56,7 +56,7 @@
56
56
  "recast-navigation": "0.43.1"
57
57
  },
58
58
  "peerDependencies": {
59
- "@threenative/core": ">=0.3.21 <0.4.0",
59
+ "@threenative/core": ">=0.3.23 <0.4.0",
60
60
  "three": ">=0.187.0 <0.188.0"
61
61
  },
62
62
  "devDependencies": {
@@ -66,7 +66,7 @@
66
66
  "tsup": "8.5.1",
67
67
  "typescript": "npm:@typescript/typescript6@6.0.2",
68
68
  "@typescript/native": "npm:typescript@7.0.2",
69
- "@threenative/core": "0.3.21"
69
+ "@threenative/core": "0.3.23"
70
70
  },
71
71
  "scripts": {
72
72
  "build": "tsup --config tsup.config.ts && publint",
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "rustc": "rustc 1.96.0 (ac68faa20 2026-05-25)",
3
3
  "files": {
4
- "threenative_native_physics.wasm": "917c1c510b43f102576acd477062e7863688c3c8200978372bf9b359a15284b2"
4
+ "threenative_native_physics.wasm": "cfd0f82671aa3802bb3e1589492077d2a71ee0fd7cc23cf18f7dd22b57dc3dd3"
5
5
  }
6
6
  }