@series-inc/rundot-syncplay 6.0.0-rc.2 → 6.0.0-rc.21

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 (329) hide show
  1. package/README.md +133 -0
  2. package/core/deterministic-math.d.ts +75 -1
  3. package/core/native/instance_ledger.hpp +1095 -0
  4. package/core/native/legacy_checkpoint_ids.hpp +107 -0
  5. package/core/native/physics/abi.cpp +39 -211
  6. package/core/native/physics/abi.hpp +2 -6
  7. package/core/native/physics/articulation.cpp +1434 -0
  8. package/core/native/physics/articulation.hpp +279 -0
  9. package/core/native/physics/buoyancy.cpp +1588 -0
  10. package/core/native/physics/buoyancy.hpp +254 -0
  11. package/core/native/physics/capsule_triangle.cpp +168 -0
  12. package/core/native/physics/capsule_triangle.hpp +40 -0
  13. package/core/native/physics/capsule_triangle_native.inc +291 -0
  14. package/core/native/physics/certified_sum.hpp +218 -0
  15. package/core/native/physics/character.cpp +638 -0
  16. package/core/native/physics/character.hpp +169 -0
  17. package/core/native/physics/checkpoint_schema.hpp +379 -0
  18. package/core/native/physics/cloth.cpp +1401 -0
  19. package/core/native/physics/cloth.hpp +357 -0
  20. package/core/native/physics/constraint_rows.cpp +363 -2
  21. package/core/native/physics/constraint_rows.hpp +72 -1
  22. package/core/native/physics/deformable_query.cpp +1630 -0
  23. package/core/native/physics/deformable_query.hpp +578 -0
  24. package/core/native/physics/distance.cpp +1166 -0
  25. package/core/native/physics/distance.hpp +151 -0
  26. package/core/native/physics/exact/exact-applied.cpp +101 -0
  27. package/core/native/physics/exact/exact-applied.hpp +112 -0
  28. package/core/native/physics/exact/exact-dyadic.cpp +691 -0
  29. package/core/native/physics/exact/exact-dyadic.hpp +58 -0
  30. package/core/native/physics/exact/exact-operation.hpp +51 -0
  31. package/core/native/physics/exact/word-core.hpp +262 -0
  32. package/core/native/physics/extended_abi.cpp +1242 -0
  33. package/core/native/physics/extended_abi.hpp +355 -0
  34. package/core/native/physics/extended_abi_characters.cpp +261 -0
  35. package/core/native/physics/extended_abi_characters.hpp +434 -0
  36. package/core/native/physics/extended_abi_core_features.cpp +713 -0
  37. package/core/native/physics/extended_abi_core_features.hpp +226 -0
  38. package/core/native/physics/extended_abi_deformables.cpp +448 -0
  39. package/core/native/physics/extended_abi_deformables.hpp +867 -0
  40. package/core/native/physics/extended_abi_vehicles.cpp +411 -0
  41. package/core/native/physics/extended_abi_vehicles.hpp +222 -0
  42. package/core/native/physics/fracture.cpp +1981 -0
  43. package/core/native/physics/fracture.hpp +460 -0
  44. package/core/native/physics/gear.cpp +563 -0
  45. package/core/native/physics/gear.hpp +158 -0
  46. package/core/native/physics/geometry_memory.hpp +476 -0
  47. package/core/native/physics/geometry_store.cpp +547 -0
  48. package/core/native/physics/geometry_store.hpp +134 -0
  49. package/core/native/physics/lag_compensation.cpp +1626 -0
  50. package/core/native/physics/lag_compensation.hpp +405 -0
  51. package/core/native/physics/mass_properties.cpp +449 -0
  52. package/core/native/physics/mass_properties.hpp +23 -0
  53. package/core/native/physics/material_pose.hpp +988 -0
  54. package/core/native/physics/particles.cpp +1065 -0
  55. package/core/native/physics/particles.hpp +261 -0
  56. package/core/native/physics/path_joint.cpp +781 -0
  57. package/core/native/physics/path_joint.hpp +154 -0
  58. package/core/native/physics/physics.cpp +13043 -1065
  59. package/core/native/physics/physics.hpp +184 -8
  60. package/core/native/physics/planar_deformable.cpp +2433 -0
  61. package/core/native/physics/planar_deformable.hpp +889 -0
  62. package/core/native/physics/platformer_2d.cpp +1353 -0
  63. package/core/native/physics/platformer_2d.hpp +340 -0
  64. package/core/native/physics/powered_ragdoll.cpp +1100 -0
  65. package/core/native/physics/powered_ragdoll.hpp +279 -0
  66. package/core/native/physics/powertrain.cpp +586 -0
  67. package/core/native/physics/powertrain.hpp +180 -0
  68. package/core/native/physics/pulley.cpp +311 -0
  69. package/core/native/physics/pulley.hpp +121 -0
  70. package/core/native/physics/ragdoll.cpp +586 -0
  71. package/core/native/physics/ragdoll.hpp +221 -0
  72. package/core/native/physics/rope.cpp +692 -0
  73. package/core/native/physics/rope.hpp +215 -0
  74. package/core/native/physics/rounded_convex.cpp +1229 -0
  75. package/core/native/physics/rounded_convex.hpp +287 -0
  76. package/core/native/physics/sdf_geometry.cpp +1821 -0
  77. package/core/native/physics/sdf_geometry.hpp +474 -0
  78. package/core/native/physics/soft_body.cpp +1449 -0
  79. package/core/native/physics/soft_body.hpp +328 -0
  80. package/core/native/physics/soft_collision.cpp +1862 -0
  81. package/core/native/physics/soft_collision.hpp +561 -0
  82. package/core/native/physics/surface_velocity.cpp +366 -0
  83. package/core/native/physics/surface_velocity.hpp +243 -0
  84. package/core/native/physics/topology.cpp +357 -39
  85. package/core/native/physics/topology.hpp +97 -6
  86. package/core/native/physics/tracks.cpp +1179 -0
  87. package/core/native/physics/tracks.hpp +404 -0
  88. package/core/native/physics/vehicle.cpp +629 -0
  89. package/core/native/physics/vehicle.hpp +191 -0
  90. package/core/native/physics/voxel_cooking.cpp +1929 -0
  91. package/core/native/physics/voxel_cooking.hpp +395 -0
  92. package/core/native/physics/world.cpp +5290 -854
  93. package/core/native/physics/world.hpp +796 -24
  94. package/core/native/physics/world_abi.cpp +3840 -306
  95. package/core/native/physics/world_abi.hpp +530 -10
  96. package/core/native/physics/world_digest_visitor.hpp +277 -0
  97. package/core/native/physics/world_strong_digest.hpp +557 -0
  98. package/core/native/physics2d/ccd.cpp +1302 -0
  99. package/core/native/physics2d/ccd.hpp +141 -0
  100. package/core/native/physics2d/checkpoint_memory.hpp +349 -0
  101. package/core/native/physics2d/clock_primitives.hpp +138 -0
  102. package/core/native/physics2d/commands.cpp +174 -28
  103. package/core/native/physics2d/commands.hpp +3 -1
  104. package/core/native/physics2d/compound.cpp +581 -0
  105. package/core/native/physics2d/compound.hpp +248 -0
  106. package/core/native/physics2d/contour.cpp +325 -0
  107. package/core/native/physics2d/contour.hpp +143 -0
  108. package/core/native/physics2d/frame_scope.hpp +124 -0
  109. package/core/native/physics2d/joint_phases.hpp +93 -0
  110. package/core/native/physics2d/joints.cpp +587 -150
  111. package/core/native/physics2d/joints.hpp +5 -0
  112. package/core/native/physics2d/memory_private.hpp +125 -0
  113. package/core/native/physics2d/owned_abi.cpp +1354 -879
  114. package/core/native/physics2d/owned_abi.hpp +190 -52
  115. package/core/native/physics2d/phase_test_hooks.hpp +17 -0
  116. package/core/native/physics2d/physics2d.cpp +2073 -120
  117. package/core/native/physics2d/physics2d.hpp +154 -2
  118. package/core/native/physics2d/queries.cpp +1055 -79
  119. package/core/native/physics2d/queries.hpp +71 -5
  120. package/core/native/physics2d/world.cpp +1313 -353
  121. package/core/native/physics2d/world.hpp +124 -13
  122. package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
  123. package/core/native/sha256/sha256_fips_constants.hpp +24 -0
  124. package/core/native/sha256/sha256_length.hpp +56 -0
  125. package/core/native/sha256/sha256_memo.hpp +48 -0
  126. package/core/native/sha256/sha256_sink.hpp +234 -0
  127. package/core/physics2d-owned.d.ts +466 -126
  128. package/core/runtime.d.ts +192 -19
  129. package/core/scripts/build-native.mjs +34 -0
  130. package/core/scripts/build-wasm-physics.mjs +272 -23
  131. package/core/scripts/extended-physics-profile.mjs +634 -0
  132. package/core/scripts/generate-physics2d-vectors.mjs +59 -0
  133. package/core/src/kinetix-deterministic-math.d.ts +2 -0
  134. package/core/src/kinetix-deterministic-math.mjs +513 -27
  135. package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
  136. package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
  137. package/core/src/kinetix-runtime-contract.mjs +55 -9
  138. package/core/src/kinetix-runtime-identity.mjs +12 -1
  139. package/core/src/kinetix-session-config.mjs +7 -2
  140. package/core/src/kinetix-state-report.mjs +36 -0
  141. package/core/src/physics/abi-constants.mjs +5 -4
  142. package/core/src/physics/articulation.d.mts +194 -0
  143. package/core/src/physics/articulation.mjs +1141 -0
  144. package/core/src/physics/buoyancy.d.mts +143 -0
  145. package/core/src/physics/buoyancy.mjs +268 -0
  146. package/core/src/physics/cloth.d.mts +314 -0
  147. package/core/src/physics/cloth.mjs +1156 -0
  148. package/core/src/physics/deformable-query.d.mts +217 -0
  149. package/core/src/physics/deformable-query.mjs +1412 -0
  150. package/core/src/physics/distance.d.mts +160 -0
  151. package/core/src/physics/distance.mjs +175 -0
  152. package/core/src/physics/extended-wasm-instance.d.mts +5 -0
  153. package/core/src/physics/extended-wasm-instance.mjs +48 -0
  154. package/core/src/physics/feature-api.d.mts +74 -0
  155. package/core/src/physics/feature-api.mjs +98 -0
  156. package/core/src/physics/fracture.d.mts +133 -0
  157. package/core/src/physics/fracture.mjs +1324 -0
  158. package/core/src/physics/gear.d.mts +585 -0
  159. package/core/src/physics/gear.mjs +955 -0
  160. package/core/src/physics/lag-compensation.d.mts +101 -0
  161. package/core/src/physics/lag-compensation.mjs +1100 -0
  162. package/core/src/physics/mass-properties.d.mts +114 -0
  163. package/core/src/physics/mass-properties.mjs +137 -468
  164. package/core/src/physics/particles.d.mts +177 -0
  165. package/core/src/physics/particles.mjs +758 -0
  166. package/core/src/physics/path-joint.d.mts +171 -0
  167. package/core/src/physics/path-joint.mjs +1231 -0
  168. package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
  169. package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
  170. package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
  171. package/core/src/physics/powered-ragdoll.d.mts +188 -0
  172. package/core/src/physics/powered-ragdoll.mjs +1043 -0
  173. package/core/src/physics/powertrain.d.mts +260 -0
  174. package/core/src/physics/powertrain.mjs +621 -0
  175. package/core/src/physics/pulley.d.mts +98 -0
  176. package/core/src/physics/pulley.mjs +686 -0
  177. package/core/src/physics/ragdoll.d.mts +289 -0
  178. package/core/src/physics/ragdoll.mjs +1323 -0
  179. package/core/src/physics/rope.d.mts +241 -0
  180. package/core/src/physics/rope.mjs +319 -0
  181. package/core/src/physics/rounded-convex.d.mts +328 -0
  182. package/core/src/physics/rounded-convex.mjs +1404 -0
  183. package/core/src/physics/sdf-geometry.d.mts +117 -0
  184. package/core/src/physics/sdf-geometry.mjs +1204 -0
  185. package/core/src/physics/soft-body.d.mts +144 -0
  186. package/core/src/physics/soft-body.mjs +1285 -0
  187. package/core/src/physics/soft-collision.d.mts +651 -0
  188. package/core/src/physics/soft-collision.mjs +1883 -0
  189. package/core/src/physics/surface-velocity.d.mts +156 -0
  190. package/core/src/physics/surface-velocity.mjs +722 -0
  191. package/core/src/physics/tracks.d.mts +411 -0
  192. package/core/src/physics/tracks.mjs +1074 -0
  193. package/core/src/physics/vehicle.d.mts +3 -0
  194. package/core/src/physics/vehicle.mjs +172 -0
  195. package/core/src/physics/voxel-cooking.d.mts +219 -0
  196. package/core/src/physics/voxel-cooking.mjs +1499 -0
  197. package/core/src/physics/voxel-swept-quad.mjs +110 -0
  198. package/core/src/physics/wasm-instance.mjs +160 -23
  199. package/core/src/physics/world.d.mts +62 -0
  200. package/core/src/physics/world.mjs +1947 -152
  201. package/core/src/physics2d/body-state.mjs +218 -15
  202. package/core/src/physics2d/collision.d.mts +52 -0
  203. package/core/src/physics2d/collision.mjs +234 -0
  204. package/core/src/physics2d/compound.mjs +454 -0
  205. package/core/src/physics2d/contour-state.mjs +771 -0
  206. package/core/src/physics2d/feature-api.d.mts +211 -0
  207. package/core/src/physics2d/feature-api.mjs +3 -0
  208. package/core/src/physics2d/fluid-state.mjs +145 -0
  209. package/core/src/physics2d/joint-state.mjs +1 -1
  210. package/core/src/physics2d/owned-queries.mjs +3 -1
  211. package/core/src/physics2d/owned-transport.mjs +268 -122
  212. package/core/src/physics2d/owned-world.mjs +353 -230
  213. package/core/src/physics2d/particle-fluid.d.mts +152 -0
  214. package/core/src/physics2d/particle-fluid.mjs +233 -0
  215. package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
  216. package/core/src/physics2d/query-codec.mjs +61 -13
  217. package/core/src/physics2d/submerged-geometry.d.mts +51 -0
  218. package/core/src/physics2d/submerged-geometry.mjs +186 -0
  219. package/core/src/runtime.mjs +23 -0
  220. package/core/src/shared/bytes.d.mts +2 -0
  221. package/core/src/shared/bytes.mjs +33 -0
  222. package/core/src/shared/sha256.d.mts +3 -0
  223. package/core/src/shared/sha256.mjs +72 -22
  224. package/core/units.d.ts +50 -0
  225. package/core/world.d.ts +253 -5
  226. package/dist/authority-room.d.ts +9 -0
  227. package/dist/authority-room.js +45 -16
  228. package/dist/browser.d.ts +6 -5
  229. package/dist/browser.js +4 -3
  230. package/dist/cadence.d.ts +7 -0
  231. package/dist/cadence.js +19 -0
  232. package/dist/canonical.d.ts +1 -0
  233. package/dist/canonical.js +3 -0
  234. package/dist/checksum-policy.d.ts +10 -0
  235. package/dist/checksum-policy.js +14 -0
  236. package/dist/collider-cooking-hull.d.ts +18 -0
  237. package/dist/collider-cooking-hull.js +248 -0
  238. package/dist/collider-cooking.d.ts +140 -0
  239. package/dist/collider-cooking.js +794 -0
  240. package/dist/compat/v5.d.ts +121 -0
  241. package/dist/compat/v5.js +479 -0
  242. package/dist/creator.d.ts +7 -6
  243. package/dist/creator.js +4 -3
  244. package/dist/game-byte-section.d.ts +36 -0
  245. package/dist/game-byte-section.js +350 -0
  246. package/dist/index.d.ts +13 -6
  247. package/dist/index.js +9 -4
  248. package/dist/instant-replay.d.ts +27 -2
  249. package/dist/instant-replay.js +91 -16
  250. package/dist/lag-compensation3d.js +11 -10
  251. package/dist/match-log.d.ts +21 -4
  252. package/dist/match-log.js +92 -9
  253. package/dist/math.d.ts +2 -0
  254. package/dist/math.js +1 -0
  255. package/dist/modules/character-3d.d.ts +16 -1
  256. package/dist/modules/character-3d.js +42 -5
  257. package/dist/modules/combat.js +6 -5
  258. package/dist/movement3d-revision1.d.ts +2 -0
  259. package/dist/movement3d-revision1.js +973 -0
  260. package/dist/movement3d.d.ts +53 -3
  261. package/dist/movement3d.js +917 -247
  262. package/dist/networked-client.d.ts +15 -0
  263. package/dist/networked-client.js +378 -105
  264. package/dist/node.d.ts +3 -2
  265. package/dist/node.js +2 -1
  266. package/dist/physics/2d.d.ts +38 -4
  267. package/dist/physics/2d.js +57 -36
  268. package/dist/physics/3d.d.ts +755 -111
  269. package/dist/physics/3d.js +2355 -158
  270. package/dist/physics/edit-result-graph.d.ts +36 -0
  271. package/dist/physics/edit-result-graph.js +141 -0
  272. package/dist/physics/game-codec3d.d.ts +41 -0
  273. package/dist/physics/game-codec3d.js +544 -0
  274. package/dist/physics/installed-checkpoint3d.d.ts +13 -0
  275. package/dist/physics/installed-checkpoint3d.js +120 -0
  276. package/dist/physics/installed-runtime3d.d.ts +51 -0
  277. package/dist/physics/installed-runtime3d.js +372 -0
  278. package/dist/physics/installed-state-commitment3d.d.ts +2 -0
  279. package/dist/physics/installed-state-commitment3d.js +24 -0
  280. package/dist/physics/units.d.ts +38 -0
  281. package/dist/physics/units.js +56 -0
  282. package/dist/physics/vehicle-state3d.d.ts +93 -0
  283. package/dist/physics/vehicle-state3d.js +1191 -0
  284. package/dist/physics/voxel-body-state.d.ts +61 -0
  285. package/dist/physics/voxel-body-state.js +410 -0
  286. package/dist/r3f-render-adapter.d.ts +59 -1
  287. package/dist/r3f-render-adapter.js +137 -2
  288. package/dist/replay.d.ts +13 -0
  289. package/dist/replay.js +95 -70
  290. package/dist/run-room-transport.js +5 -1
  291. package/dist/runner.d.ts +17 -2
  292. package/dist/runner.js +472 -84
  293. package/dist/runtime-cleanup.d.ts +54 -0
  294. package/dist/runtime-cleanup.js +266 -0
  295. package/dist/runtime-lease.d.ts +11 -1
  296. package/dist/runtime-lease.js +23 -10
  297. package/dist/runtime-room-metadata.d.ts +2 -8
  298. package/dist/runtime-room-metadata.js +11 -14
  299. package/dist/runtime-session.d.ts +8 -0
  300. package/dist/runtime-session.js +222 -58
  301. package/dist/schema-codegen.js +3 -0
  302. package/dist/schema-dsl-binary.d.ts +10 -4
  303. package/dist/schema-dsl-binary.js +375 -76
  304. package/dist/scratch-pool.d.ts +32 -0
  305. package/dist/scratch-pool.js +60 -0
  306. package/dist/sdk-session.d.ts +1 -0
  307. package/dist/sdk-session.js +4 -0
  308. package/dist/secret-config.js +2 -60
  309. package/dist/secret-crypto.js +70 -36
  310. package/dist/session-snapshot.d.ts +15 -4
  311. package/dist/session-snapshot.js +35 -33
  312. package/dist/session-wire.js +14 -22
  313. package/dist/sparse-set.d.ts +1 -0
  314. package/dist/sparse-set.js +22 -0
  315. package/dist/spatial/spatial-grid-3d.d.ts +41 -0
  316. package/dist/spatial/spatial-grid-3d.js +115 -0
  317. package/dist/synctest.d.ts +9 -0
  318. package/dist/synctest.js +106 -17
  319. package/dist/testing-network.d.ts +2 -0
  320. package/dist/testing-network.js +122 -18
  321. package/dist/testing.d.ts +27 -6
  322. package/dist/testing.js +297 -56
  323. package/dist/tools/index.d.ts +3 -2
  324. package/dist/tools/index.js +3 -2
  325. package/dist/tools/module-certification.d.ts +6 -0
  326. package/dist/tools/module-certification.js +26 -2
  327. package/package.json +18 -5
  328. package/core/scripts/run-box3d-benchmark.mjs +0 -350
  329. package/core/src/physics/index.mjs +0 -3169
@@ -1,5 +1,6 @@
1
+ import { normalizeContourShape, normalizeArcShape, writeContourHeader, serializeContourShape } from "./contour-state.mjs";
1
2
  export const bodyAbi = Object.freeze({
2
- bodyBytes: 144,
3
+ bodyBytes: 160,
3
4
  polygonGeometryBytes: 136,
4
5
  polygonMassPropertiesBytes: 32,
5
6
  maxBodies: 4096,
@@ -8,7 +9,16 @@ export const bodyAbi = Object.freeze({
8
9
  });
9
10
 
10
11
  export const bodyKinds = Object.freeze({ static: 0, kinematic: 1, dynamic: 2 });
11
- export const shapeCodes = Object.freeze({ circle: 1, box: 2, capsule: 3, polygon: 4 });
12
+ export const shapeCodes = Object.freeze({
13
+ circle: 1,
14
+ box: 2,
15
+ capsule: 3,
16
+ polygon: 4,
17
+ segment: 5,
18
+ chain: 6,
19
+ contour: 7,
20
+ arc: 8,
21
+ });
12
22
  export const bodyFields = Object.freeze([
13
23
  'x', 'y', 'angle', 'vx', 'vy', 'angularVelocity', 'mass', 'inertia',
14
24
  'forceX', 'forceY', 'torque', 'halfX', 'halfY', 'radius', 'friction', 'restitution',
@@ -86,6 +96,77 @@ function normalizePolygonVertices(input) {
86
96
  return Object.freeze(vertices);
87
97
  }
88
98
 
99
+ function normalizeVertices(input, minimum, maximum, shapeName) {
100
+ if (!Array.isArray(input) || input.length < minimum || input.length > maximum) {
101
+ fail(`SHAPE_INVALID: ${shapeName} needs ${minimum}-${maximum} vertices`);
102
+ }
103
+ return Object.freeze(input.map((vertex, index) => {
104
+ if (!vertex || typeof vertex !== 'object' || Array.isArray(vertex)) {
105
+ fail(`SHAPE_INVALID: ${shapeName} vertex ${index} is invalid`);
106
+ }
107
+ const x = finiteNumber(vertex.x, undefined, `vertices[${index}].x`);
108
+ const y = finiteNumber(vertex.y, undefined, `vertices[${index}].y`);
109
+ if (Math.abs(x) > bodyAbi.maxPolygonCoordinate || Math.abs(y) > bodyAbi.maxPolygonCoordinate) {
110
+ fail(`SHAPE_INVALID: ${shapeName} vertex ${index} exceeds the coordinate limit`);
111
+ }
112
+ return Object.freeze({ x, y });
113
+ }));
114
+ }
115
+
116
+ function ensureDistinctAdjacentVertices(vertices, loop, shapeName) {
117
+ const count = loop ? vertices.length : vertices.length - 1;
118
+ for (let index = 0; index < count; index += 1) {
119
+ const next = vertices[(index + 1) % vertices.length];
120
+ const dx = next.x - vertices[index].x;
121
+ const dy = next.y - vertices[index].y;
122
+ if (dx * dx + dy * dy <= 1e-12) {
123
+ fail(`SHAPE_INVALID: ${shapeName} has coincident adjacent vertices`);
124
+ }
125
+ }
126
+ }
127
+
128
+ function normalizeSegmentShape(shape) {
129
+ let point1 = shape.point1;
130
+ let point2 = shape.point2;
131
+ let ghost1 = shape.ghost1;
132
+ let ghost2 = shape.ghost2;
133
+ if (Array.isArray(shape.vertices)) {
134
+ const vertices = normalizeVertices(shape.vertices, 2, 4, 'segment');
135
+ if (vertices.length === 4) {
136
+ [ghost1, point1, point2, ghost2] = vertices;
137
+ } else if (vertices.length === 3 && (shape.hasGhost1 === true || shape.ghost1 !== undefined)) {
138
+ [ghost1, point1, point2] = vertices;
139
+ } else if (vertices.length === 3) {
140
+ [point1, point2, ghost2] = vertices;
141
+ } else {
142
+ [point1, point2] = vertices;
143
+ }
144
+ }
145
+ const vertices = normalizeVertices([point1, point2], 2, 2, 'segment');
146
+ ensureDistinctAdjacentVertices(vertices, false, 'segment');
147
+ const normalized = {
148
+ type: 'segment',
149
+ point1: vertices[0],
150
+ point2: vertices[1],
151
+ radius: finiteNumber(shape.radius, 0, 'radius'),
152
+ };
153
+ if (normalized.radius < 0) fail('SHAPE_INVALID: segment radius must be non-negative');
154
+ if (ghost1 !== undefined) normalized.ghost1 = normalizeVertices([ghost1], 1, 1, 'segment')[0];
155
+ if (ghost2 !== undefined) normalized.ghost2 = normalizeVertices([ghost2], 1, 1, 'segment')[0];
156
+ return Object.freeze(normalized);
157
+ }
158
+
159
+ function normalizeChainShape(shape) {
160
+ const vertices = normalizeVertices(shape.vertices, 2, bodyAbi.maxPolygonVertices, 'chain');
161
+ const loop = shape.loop ?? shape.isLoop ?? false;
162
+ if (typeof loop !== 'boolean') fail('SHAPE_INVALID: chain.loop must be a boolean');
163
+ if (loop && vertices.length < 3) fail('SHAPE_INVALID: a chain loop needs at least 3 vertices');
164
+ ensureDistinctAdjacentVertices(vertices, loop, 'chain');
165
+ const radius = finiteNumber(shape.radius, 0, 'radius');
166
+ if (radius < 0) fail('SHAPE_INVALID: chain radius must be non-negative');
167
+ return Object.freeze({ type: 'chain', vertices, loop, radius });
168
+ }
169
+
89
170
  function capsuleDefaultInertia(mass, halfLength, radius) {
90
171
  const pi = 3.141592653589793;
91
172
  const area = 4 * halfLength * radius + pi * radius * radius;
@@ -97,7 +178,9 @@ function capsuleDefaultInertia(mass, halfLength, radius) {
97
178
  }
98
179
 
99
180
  export function normalizeShape(shape, calculateMassProperties = true, polygonMassPropertiesFor) {
100
- if (!shape || !Object.hasOwn(shapeCodes, shape.type)) fail('SHAPE_INVALID: use circle, box, capsule, or polygon');
181
+ if (!shape || !Object.hasOwn(shapeCodes, shape.type)) {
182
+ fail('SHAPE_INVALID: use circle, box, capsule, polygon, segment, or chain');
183
+ }
101
184
  let normalizedShape;
102
185
  let polygonMassProperties;
103
186
  if (shape.type === 'circle') {
@@ -117,9 +200,17 @@ export function normalizeShape(shape, calculateMassProperties = true, polygonMas
117
200
  radius: finiteNumber(shape.radius, undefined, 'radius'),
118
201
  };
119
202
  if (normalizedShape.halfLength < 0 || normalizedShape.radius <= 0) fail('SHAPE_INVALID: dimensions must be positive');
120
- } else {
203
+ } else if (shape.type === 'polygon') {
121
204
  normalizedShape = { type: 'polygon', vertices: normalizePolygonVertices(shape.vertices) };
122
205
  if (calculateMassProperties) polygonMassProperties = polygonMassPropertiesFor(normalizedShape.vertices);
206
+ } else if (shape.type === 'segment') {
207
+ normalizedShape = normalizeSegmentShape(shape);
208
+ } else if (shape.type === 'chain') {
209
+ normalizedShape = normalizeChainShape(shape);
210
+ } else if (shape.type === 'contour') {
211
+ normalizedShape = normalizeContourShape(shape);
212
+ } else if (shape.type === 'arc') {
213
+ normalizedShape = normalizeArcShape(shape);
123
214
  }
124
215
  return { normalizedShape, polygonMassProperties };
125
216
  }
@@ -128,8 +219,15 @@ export function normalizeBodyFields(input, polygonMassPropertiesFor) {
128
219
  if (!input || typeof input !== 'object' || Array.isArray(input)) {
129
220
  fail('BODY_INVALID: supply a body object');
130
221
  }
131
- const kind = input.kind ?? 'dynamic';
222
+ const isTerrain = input.shape?.type === 'segment'
223
+ || input.shape?.type === 'chain'
224
+ || input.shape?.type === 'contour'
225
+ || input.shape?.type === 'arc';
226
+ const kind = input.kind ?? (isTerrain ? 'static' : 'dynamic');
132
227
  if (!Object.hasOwn(bodyKinds, kind)) fail(`BODY_INVALID: unsupported kind ${String(kind)}`);
228
+ if ((input.shape?.type === 'contour' || input.shape?.type === 'arc') && kind !== 'static') {
229
+ fail('CONTOUR_INVALID: contour bodies must be static');
230
+ }
133
231
  const { normalizedShape, polygonMassProperties } = normalizeShape(
134
232
  input.shape,
135
233
  true,
@@ -142,7 +240,9 @@ export function normalizeBodyFields(input, polygonMassPropertiesFor) {
142
240
  ? mass * (normalizedShape.halfX ** 2 + normalizedShape.halfY ** 2) / 3
143
241
  : normalizedShape.type === 'capsule'
144
242
  ? capsuleDefaultInertia(mass, normalizedShape.halfLength, normalizedShape.radius)
145
- : mass * polygonMassProperties.unitInertiaAboutCentroid;
243
+ : normalizedShape.type === 'polygon'
244
+ ? mass * polygonMassProperties.unitInertiaAboutCentroid
245
+ : 1;
146
246
  const inertia = finiteNumber(input.inertia, defaultInertia, 'inertia');
147
247
  const friction = finiteNumber(input.friction, 0.5, 'friction');
148
248
  const restitution = finiteNumber(input.restitution, 0, 'restitution');
@@ -150,7 +250,22 @@ export function normalizeBodyFields(input, polygonMassPropertiesFor) {
150
250
  || friction < 0 || restitution < 0 || restitution > 1) {
151
251
  fail('BODY_INVALID: check mass, inertia, friction, and restitution');
152
252
  }
153
- const body = { kind, shape: Object.freeze(normalizedShape), mass, inertia, friction, restitution };
253
+ const layer = input.layer === undefined ? 1 : input.layer;
254
+ const mask = input.mask === undefined ? 1 : input.mask;
255
+ for (const [name, value] of [['layer', layer], ['mask', mask]]) {
256
+ if (!Number.isInteger(value) || value < 0 || value > 0xffffffff) {
257
+ fail(`BODY_INVALID: ${name} must be an unsigned 32-bit integer`);
258
+ }
259
+ }
260
+ const trigger = input.trigger === undefined ? false : input.trigger;
261
+ const sensor = input.sensor === undefined ? false : input.sensor;
262
+ if (typeof trigger !== 'boolean' || typeof sensor !== 'boolean') {
263
+ fail('BODY_INVALID: trigger and sensor must be Boolean values');
264
+ }
265
+ const ccd = input.ccd === undefined ? false : input.ccd;
266
+ if (typeof ccd !== 'boolean') fail('BODY_INVALID: body.ccd must be a boolean');
267
+ const body = { kind, shape: Object.freeze(normalizedShape), mass, inertia, friction, restitution,
268
+ layer, mask, trigger, sensor, ccd };
154
269
  for (const field of bodyMotionFields) body[field] = finiteNumber(input[field], 0, field);
155
270
  return Object.freeze(body);
156
271
  }
@@ -179,7 +294,11 @@ export function writeBody(view, offset, body, numericId, geometrySlot) {
179
294
  view.setUint32(offset, numericId, true);
180
295
  view.setUint32(offset + 4, bodyKinds[body.kind], true);
181
296
  view.setUint32(offset + 8, shapeCodes[body.shape.type], true);
182
- view.setUint32(offset + 12, body.shape.type === 'polygon' ? geometrySlot : 0, true);
297
+ const storedGeometry = body.shape.type === 'polygon'
298
+ || body.shape.type === 'segment'
299
+ || body.shape.type === 'chain'
300
+ || body.shape.type === 'contour';
301
+ view.setUint32(offset + 12, storedGeometry ? geometrySlot : 0, true);
183
302
  const record = { ...body, halfX: 0.5, halfY: 0.5, radius: 0.5 };
184
303
  if (body.shape.type === 'circle') record.radius = body.shape.radius;
185
304
  if (body.shape.type === 'box') {
@@ -190,12 +309,19 @@ export function writeBody(view, offset, body, numericId, geometrySlot) {
190
309
  record.halfX = body.shape.halfLength;
191
310
  record.radius = body.shape.radius;
192
311
  }
312
+ if (body.shape.type === 'segment' || body.shape.type === 'chain') {
313
+ record.radius = body.shape.radius;
314
+ }
193
315
  bodyFields.forEach((field, index) => view.setFloat64(offset + 16 + index * 8, record[field], true));
316
+ view.setUint32(offset + 144, body.layer, true);
317
+ view.setUint32(offset + 148, body.mask, true);
318
+ view.setUint32(offset + 152, (body.trigger ? 1 : 0) | (body.sensor ? 2 : 0), true);
319
+ view.setUint32(offset + 156, body.ccd ? 1 : 0, true);
194
320
  }
195
321
 
196
- export function writePolygonGeometryVertices(view, offset, vertices) {
322
+ export function writePolygonGeometryVertices(view, offset, vertices, reserved = 0) {
197
323
  view.setUint32(offset, vertices.length, true);
198
- view.setUint32(offset + 4, 0, true);
324
+ view.setUint32(offset + 4, reserved, true);
199
325
  for (let index = 0; index < bodyAbi.maxPolygonVertices; index += 1) {
200
326
  const vertex = vertices[index] ?? { x: 0, y: 0 };
201
327
  view.setFloat64(offset + 8 + index * 16, vertex.x, true);
@@ -204,15 +330,50 @@ export function writePolygonGeometryVertices(view, offset, vertices) {
204
330
  }
205
331
 
206
332
  export function writePolygonGeometry(view, offset, body) {
207
- writePolygonGeometryVertices(view, offset, body.shape.type === 'polygon' ? body.shape.vertices : []);
333
+ if (body.shape.type === 'polygon') {
334
+ writePolygonGeometryVertices(view, offset, body.shape.vertices);
335
+ return;
336
+ }
337
+ if (body.shape.type === 'segment') {
338
+ const vertices = body.shape.ghost1 !== undefined && body.shape.ghost2 !== undefined
339
+ ? [body.shape.ghost1, body.shape.point1, body.shape.point2, body.shape.ghost2]
340
+ : body.shape.ghost1 !== undefined
341
+ ? [body.shape.ghost1, body.shape.point1, body.shape.point2]
342
+ : body.shape.ghost2 !== undefined
343
+ ? [body.shape.point1, body.shape.point2, body.shape.ghost2]
344
+ : [body.shape.point1, body.shape.point2];
345
+ writePolygonGeometryVertices(view, offset, vertices, body.shape.ghost1 !== undefined ? 1 : 2);
346
+ return;
347
+ }
348
+ if (body.shape.type === 'chain') {
349
+ writePolygonGeometryVertices(view, offset, body.shape.vertices, body.shape.loop ? 1 : 2);
350
+ return;
351
+ }
352
+ if (body.shape.type === 'contour') {
353
+ if (body.shape.header !== undefined) {
354
+ writeContourHeader(view, offset, body.shape.header);
355
+ return;
356
+ }
357
+ const { header } = serializeContourShape(body.shape);
358
+ writeContourHeader(view, offset, header);
359
+ return;
360
+ }
361
+ writePolygonGeometryVertices(view, offset, []);
208
362
  }
209
363
 
210
364
  export function readBodyRecord(bodyBytes, geometryBytes) {
211
365
  const bv = new DataView(bodyBytes.buffer, bodyBytes.byteOffset, bodyBytes.byteLength);
212
366
  const id = bv.getUint32(0, true);
213
367
  const kindCode = bv.getUint32(4, true);
214
- const kind = ['static', 'kinematic', 'dynamic'][kindCode] ?? 'dynamic';
368
+ const kind = ['static', 'kinematic', 'dynamic'][kindCode];
369
+ if (kind === undefined) fail(`NATIVE_OUTPUT_INVALID: unknown body kind code ${kindCode}`);
215
370
  const shapeCode = bv.getUint32(8, true);
371
+ const layer = bv.getUint32(144, true);
372
+ const mask = bv.getUint32(148, true);
373
+ const flags = bv.getUint32(152, true);
374
+ if ((flags & ~3) !== 0) fail('NATIVE_OUTPUT_INVALID: invalid filter flags');
375
+ const ccd = bv.getUint32(156, true);
376
+ if (ccd > 1) fail('NATIVE_OUTPUT_INVALID: invalid CCD setting');
216
377
 
217
378
  const rawValues = {};
218
379
  bodyFields.forEach((field, index) => {
@@ -226,9 +387,10 @@ export function readBodyRecord(bodyBytes, geometryBytes) {
226
387
  shape = Object.freeze({ type: 'box', halfX: rawValues.halfX, halfY: rawValues.halfY });
227
388
  } else if (shapeCode === 3) {
228
389
  shape = Object.freeze({ type: 'capsule', halfLength: rawValues.halfX, radius: rawValues.radius });
229
- } else if (shapeCode === 4) {
390
+ } else if (shapeCode >= 4 && shapeCode <= 6) {
230
391
  const gv = new DataView(geometryBytes.buffer, geometryBytes.byteOffset, geometryBytes.byteLength);
231
392
  const vertexCount = gv.getUint32(0, true);
393
+ const reserved = gv.getUint32(4, true);
232
394
  const vertices = [];
233
395
  for (let i = 0; i < vertexCount; i += 1) {
234
396
  vertices.push(Object.freeze({
@@ -236,12 +398,53 @@ export function readBodyRecord(bodyBytes, geometryBytes) {
236
398
  y: gv.getFloat64(16 + i * 16, true),
237
399
  }));
238
400
  }
239
- shape = Object.freeze({ type: 'polygon', vertices: Object.freeze(vertices) });
401
+ const frozenVertices = Object.freeze(vertices);
402
+ if (shapeCode === 4) {
403
+ shape = Object.freeze({ type: 'polygon', vertices: frozenVertices });
404
+ } else if (shapeCode === 5) {
405
+ const hasGhost1 = vertexCount === 4 || (vertexCount === 3 && (reserved & 1) !== 0);
406
+ const hasGhost2 = vertexCount === 4 || (vertexCount === 3 && (reserved & 1) === 0);
407
+ const pointOffset = hasGhost1 ? 1 : 0;
408
+ shape = Object.freeze({
409
+ type: 'segment',
410
+ point1: frozenVertices[pointOffset],
411
+ point2: frozenVertices[pointOffset + 1],
412
+ ...(hasGhost1 ? { ghost1: frozenVertices[0] } : {}),
413
+ ...(hasGhost2 ? { ghost2: frozenVertices[vertexCount - 1] } : {}),
414
+ radius: rawValues.radius,
415
+ });
416
+ } else {
417
+ shape = Object.freeze({
418
+ type: 'chain',
419
+ vertices: frozenVertices,
420
+ loop: (reserved & 1) !== 0,
421
+ radius: rawValues.radius,
422
+ });
423
+ }
424
+ } else if (shapeCode === 7) {
425
+ const gv = new DataView(geometryBytes.buffer, geometryBytes.byteOffset, geometryBytes.byteLength);
426
+ const firstEdge = gv.getUint32(0, true);
427
+ const edgeCount = gv.getUint32(4, true);
428
+ const loopCount = gv.getUint32(8, true);
429
+ const fill = gv.getUint32(12, true);
430
+ const tag = gv.getUint32(16, true);
431
+ const reserved = gv.getUint32(20, true);
432
+ const skin = gv.getFloat64(24, true);
433
+ shape = Object.freeze({
434
+ type: "contour",
435
+ firstEdge,
436
+ edgeCount,
437
+ loopCount,
438
+ fill: fill === 1 ? "solid" : "boundary",
439
+ reserved,
440
+ radius: skin,
441
+ });
240
442
  } else {
241
443
  throw new Error(`NATIVE_OUTPUT_INVALID: unknown body shape code ${shapeCode}.`);
242
444
  }
243
445
 
244
- const result = { id, kind, shape };
446
+ const result = { id, kind, shape, layer, mask, trigger: (flags & 1) !== 0,
447
+ sensor: (flags & 2) !== 0, ccd: ccd !== 0 };
245
448
  bodyOutputFields.forEach(({ field }) => {
246
449
  result[field] = rawValues[field];
247
450
  });
@@ -0,0 +1,52 @@
1
+ import type {
2
+ Dimensionless, Meters, Seconds } from '../../units.js';
3
+ import type { PhysicsBodyInput2D, PhysicsVertex2D } from '../../physics2d-owned.js';
4
+
5
+ export interface PhysicsContactFeature2D {
6
+ readonly type: 'vertex' | 'edge' | 'curve' | 'reserved';
7
+ readonly index: number;
8
+ }
9
+
10
+ export interface PhysicsContactFeaturePair2D {
11
+ readonly a: PhysicsContactFeature2D;
12
+ readonly b: PhysicsContactFeature2D;
13
+ }
14
+
15
+ export interface PhysicsContactPoint2D extends PhysicsVertex2D<Meters> {
16
+ readonly separation: Meters;
17
+ readonly featureId: number;
18
+ }
19
+
20
+ export interface PhysicsCollisionManifold2D {
21
+ readonly normal: PhysicsVertex2D<Dimensionless>;
22
+ readonly penetration: Meters;
23
+ readonly points: readonly PhysicsContactPoint2D[];
24
+ }
25
+
26
+ export interface PhysicsTimeOfImpactOptions2D {
27
+ readonly dt?: Seconds;
28
+ readonly targetSeparation?: Meters;
29
+ readonly maxIterations?: number;
30
+ }
31
+
32
+ export interface PhysicsTimeOfImpactResult2D {
33
+ readonly hit: boolean;
34
+ readonly toi: Dimensionless;
35
+ readonly time: Seconds;
36
+ readonly normal: PhysicsVertex2D<Dimensionless>;
37
+ readonly point: PhysicsVertex2D<Meters>;
38
+ readonly separation: Meters;
39
+ readonly iterations: number;
40
+ }
41
+
42
+ export function decodeContactFeatureId2D(featureId: number): PhysicsContactFeaturePair2D;
43
+ export function mirrorContactFeatureId2D(featureId: number): number;
44
+ export function collidePhysics2D(
45
+ first: PhysicsBodyInput2D,
46
+ second: PhysicsBodyInput2D,
47
+ ): PhysicsCollisionManifold2D | null;
48
+ export function computeTimeOfImpact2D(
49
+ first: PhysicsBodyInput2D,
50
+ second: PhysicsBodyInput2D,
51
+ options?: PhysicsTimeOfImpactOptions2D,
52
+ ): PhysicsTimeOfImpactResult2D;
@@ -0,0 +1,234 @@
1
+ import {
2
+ bodyAbi,
3
+ normalizeBodyFields,
4
+ writeBody,
5
+ writePolygonGeometry,
6
+ writePolygonGeometryVertices,
7
+ } from './body-state.mjs';
8
+ import {
9
+ writeContourEdge,
10
+ serializeContourShape,
11
+ } from './contour-state.mjs';
12
+ import { ready } from './wasm-instance.mjs';
13
+
14
+ const manifoldBytes = 104;
15
+ const contactPointBytes = 32;
16
+ const contactFeatureTypeNames = Object.freeze({
17
+ 0: 'vertex',
18
+ 1: 'edge',
19
+ 2: 'curve',
20
+ 3: 'reserved',
21
+ });
22
+
23
+ function fail(detail) {
24
+ throw new Error(`KINETIX_PHYSICS2D_${detail}`);
25
+ }
26
+
27
+ function decodeContactFeature(value) {
28
+ return Object.freeze({
29
+ type: contactFeatureTypeNames[(value >>> 14) & 0x3],
30
+ index: value & 0x3fff,
31
+ });
32
+ }
33
+
34
+ function validateFeatureId(featureId) {
35
+ if (!Number.isInteger(featureId) || featureId < 0 || featureId > 0xffffffff) {
36
+ fail('CONTACT_FEATURE_INVALID: featureId must be a 32-bit unsigned integer');
37
+ }
38
+ }
39
+
40
+ export function decodeContactFeatureId2D(featureId) {
41
+ validateFeatureId(featureId);
42
+ return Object.freeze({
43
+ a: decodeContactFeature((featureId >>> 16) & 0xffff),
44
+ b: decodeContactFeature(featureId & 0xffff),
45
+ });
46
+ }
47
+
48
+ export function mirrorContactFeatureId2D(featureId) {
49
+ validateFeatureId(featureId);
50
+ return (((featureId & 0xffff) << 16) | ((featureId >>> 16) & 0xffff)) >>> 0;
51
+ }
52
+
53
+ function withBuffer(length, action) {
54
+ const exports = ready();
55
+ const pointer = exports.malloc(length);
56
+ if (!pointer) fail('ALLOCATION_FAILED');
57
+ try {
58
+ return action(exports, pointer);
59
+ } finally {
60
+ exports.free(pointer);
61
+ }
62
+ }
63
+
64
+ function polygonMassPropertiesFor(vertices) {
65
+ return withBuffer(bodyAbi.polygonGeometryBytes + bodyAbi.polygonMassPropertiesBytes,
66
+ (exports, pointer) => {
67
+ const view = new DataView(exports.memory.buffer);
68
+ writePolygonGeometryVertices(view, pointer, vertices);
69
+ const status = exports.rundot_physics2d_polygon_mass_properties(
70
+ pointer,
71
+ 1,
72
+ pointer + bodyAbi.polygonGeometryBytes,
73
+ );
74
+ if (status === -6) fail('GEOMETRY_ALLOCATION_FAILED');
75
+ if (status !== 0) fail(`GEOMETRY_INVALID: native status ${status}`);
76
+ return Object.freeze({
77
+ area: view.getFloat64(pointer + bodyAbi.polygonGeometryBytes, true),
78
+ centroidX: view.getFloat64(pointer + bodyAbi.polygonGeometryBytes + 8, true),
79
+ centroidY: view.getFloat64(pointer + bodyAbi.polygonGeometryBytes + 16, true),
80
+ unitInertiaAboutCentroid: view.getFloat64(pointer + bodyAbi.polygonGeometryBytes + 24, true),
81
+ });
82
+ });
83
+ }
84
+
85
+ function hasGeometry(body) {
86
+ return body.shape.type === 'polygon'
87
+ || body.shape.type === 'segment'
88
+ || body.shape.type === 'chain'
89
+ || body.shape.type === 'contour';
90
+ }
91
+
92
+ function withEncodedPair(first, second, tailBytes, action) {
93
+ const bodies = [
94
+ normalizeBodyFields(first, polygonMassPropertiesFor),
95
+ normalizeBodyFields(second, polygonMassPropertiesFor),
96
+ ];
97
+ const geometryCount = bodies.some(hasGeometry) ? bodies.length : 0;
98
+ const bodiesBytes = bodies.length * bodyAbi.bodyBytes;
99
+ const geometryBytes = geometryCount * bodyAbi.polygonGeometryBytes;
100
+
101
+ const contourHeaders = [];
102
+ const contourEdges = [];
103
+ let edgeOffset = 0;
104
+ for (let i = 0; i < bodies.length; i += 1) {
105
+ if (bodies[i].shape.type === 'contour') {
106
+ const { header, edges } = serializeContourShape(bodies[i].shape, edgeOffset);
107
+ contourHeaders.push(header);
108
+ contourEdges.push(...edges);
109
+ edgeOffset += edges.length;
110
+ }
111
+ }
112
+ const contourEdgesBytes = contourEdges.length * 72;
113
+
114
+ return withBuffer(bodiesBytes + geometryBytes + contourEdgesBytes + tailBytes, (exports, pointer) => {
115
+ const geometryPointer = pointer + bodiesBytes;
116
+ const contourEdgesPointer = geometryPointer + geometryBytes;
117
+ const tailPointer = contourEdgesPointer + contourEdgesBytes;
118
+ const view = new DataView(exports.memory.buffer);
119
+ bodies.forEach((body, index) => {
120
+ writeBody(view, pointer + index * bodyAbi.bodyBytes, body, index, index);
121
+ if (geometryCount > 0) {
122
+ writePolygonGeometry(view, geometryPointer + index * bodyAbi.polygonGeometryBytes, body);
123
+ }
124
+ });
125
+ const geometryStatus = exports.rundot_physics2d_set_polygon_geometry(
126
+ geometryCount === 0 ? 0 : geometryPointer,
127
+ geometryCount,
128
+ );
129
+ if (geometryStatus === -6) fail('GEOMETRY_ALLOCATION_FAILED');
130
+ if (geometryStatus !== 0) fail(`GEOMETRY_INVALID: native status ${geometryStatus}`);
131
+
132
+ if (contourHeaders.length > 0) {
133
+ for (let i = 0; i < contourEdges.length; i += 1) {
134
+ writeContourEdge(view, contourEdgesPointer + i * 72, contourEdges[i]);
135
+ }
136
+ exports.rundot_physics2d_set_contour_geometry(
137
+ geometryPointer,
138
+ geometryCount,
139
+ contourEdgesPointer,
140
+ contourEdges.length,
141
+ );
142
+ }
143
+
144
+ try {
145
+ return action(exports, pointer, tailPointer);
146
+ } finally {
147
+ if (contourHeaders.length > 0) {
148
+ exports.rundot_physics2d_set_contour_geometry(0, 0, 0, 0);
149
+ }
150
+ exports.rundot_physics2d_set_polygon_geometry(0, 0);
151
+ }
152
+ });
153
+ }
154
+
155
+ export function collidePhysics2D(first, second) {
156
+ return withEncodedPair(first, second, manifoldBytes, (exports, pointer, manifoldPointer) => {
157
+ const status = exports.rundot_physics2d_collide(
158
+ pointer,
159
+ pointer + bodyAbi.bodyBytes,
160
+ manifoldPointer,
161
+ );
162
+ if (status === -4) fail('COLLISION_NON_FINITE: reduce dimensions, speeds, or coordinate magnitudes');
163
+ if (status !== 0) fail(`COLLISION_FAILED: native status ${status}`);
164
+ const output = new DataView(exports.memory.buffer);
165
+ if (!output.getUint8(manifoldPointer)) return null;
166
+ const pointCount = output.getUint32(manifoldPointer + 32, true);
167
+ if (pointCount > 2) fail('MANIFOLD_INVALID');
168
+ return Object.freeze({
169
+ normal: Object.freeze({
170
+ x: output.getFloat64(manifoldPointer + 8, true),
171
+ y: output.getFloat64(manifoldPointer + 16, true),
172
+ }),
173
+ penetration: output.getFloat64(manifoldPointer + 24, true),
174
+ points: Object.freeze(Array.from({ length: pointCount }, (_, index) => {
175
+ const point = manifoldPointer + 40 + index * contactPointBytes;
176
+ return Object.freeze({
177
+ x: output.getFloat64(point, true),
178
+ y: output.getFloat64(point + 8, true),
179
+ separation: output.getFloat64(point + 16, true),
180
+ featureId: output.getUint32(point + 24, true),
181
+ });
182
+ })),
183
+ });
184
+ });
185
+ }
186
+
187
+ export function computeTimeOfImpact2D(first, second, options = {}) {
188
+ if (!options || typeof options !== 'object' || Array.isArray(options)) {
189
+ fail('CCD_INVALID: options must be an object');
190
+ }
191
+ const dt = options.dt ?? 1 / 60;
192
+ const targetSeparation = options.targetSeparation ?? 1e-6;
193
+ const maxIterations = options.maxIterations ?? 32;
194
+ if (!Number.isFinite(dt) || dt <= 0
195
+ || !Number.isFinite(targetSeparation) || targetSeparation < 0
196
+ || !Number.isInteger(maxIterations) || maxIterations <= 0 || maxIterations > 256) {
197
+ fail('CCD_INVALID: check dt, targetSeparation, and maxIterations');
198
+ }
199
+ const outputBytes = 64;
200
+ return withEncodedPair(first, second, outputBytes, (exports, pointer, outputPointer) => {
201
+ const status = exports.rundot_physics2d_ccd_time_of_impact_ex(
202
+ pointer,
203
+ pointer + bodyAbi.bodyBytes,
204
+ dt,
205
+ targetSeparation,
206
+ maxIterations,
207
+ outputPointer,
208
+ outputPointer + 8,
209
+ outputPointer + 16,
210
+ outputPointer + 24,
211
+ outputPointer + 32,
212
+ outputPointer + 40,
213
+ outputPointer + 48,
214
+ outputPointer + 56,
215
+ );
216
+ if (status !== 0 && status !== 1) fail(`CCD_FAILED: native status ${status}`);
217
+ const output = new DataView(exports.memory.buffer);
218
+ return Object.freeze({
219
+ hit: status === 1,
220
+ toi: output.getFloat64(outputPointer, true),
221
+ time: output.getFloat64(outputPointer + 8, true),
222
+ normal: Object.freeze({
223
+ x: output.getFloat64(outputPointer + 16, true),
224
+ y: output.getFloat64(outputPointer + 24, true),
225
+ }),
226
+ point: Object.freeze({
227
+ x: output.getFloat64(outputPointer + 32, true),
228
+ y: output.getFloat64(outputPointer + 40, true),
229
+ }),
230
+ separation: output.getFloat64(outputPointer + 48, true),
231
+ iterations: output.getUint32(outputPointer + 56, true),
232
+ });
233
+ });
234
+ }