@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
@@ -0,0 +1,1065 @@
1
+ #include "particles.hpp"
2
+
3
+ #include <algorithm>
4
+ #include <cmath>
5
+ #include <cstring>
6
+
7
+ namespace rundot::kinetix::physics {
8
+
9
+ constexpr double kPi = 3.1415926535897932384626433832795;
10
+ constexpr double kTwoPi = 2.0 * kPi;
11
+
12
+ // ---------------------------------------------------------------------------
13
+ // Deterministic vector and quaternion math
14
+ // ---------------------------------------------------------------------------
15
+
16
+ ParticleVec3 addParticleVec3(const ParticleVec3& a, const ParticleVec3& b) {
17
+ return {a.x + b.x, a.y + b.y, a.z + b.z};
18
+ }
19
+
20
+ ParticleVec3 subParticleVec3(const ParticleVec3& a, const ParticleVec3& b) {
21
+ return {a.x - b.x, a.y - b.y, a.z - b.z};
22
+ }
23
+
24
+ ParticleVec3 scaleParticleVec3(const ParticleVec3& v, double s) {
25
+ return {v.x * s, v.y * s, v.z * s};
26
+ }
27
+
28
+ double dotParticleVec3(const ParticleVec3& a, const ParticleVec3& b) {
29
+ return a.x * b.x + a.y * b.y + a.z * b.z;
30
+ }
31
+
32
+ ParticleVec3 crossParticleVec3(const ParticleVec3& a, const ParticleVec3& b) {
33
+ return {
34
+ a.y * b.z - a.z * b.y,
35
+ a.z * b.x - a.x * b.z,
36
+ a.x * b.y - a.y * b.x,
37
+ };
38
+ }
39
+
40
+ double lengthSquaredParticleVec3(const ParticleVec3& v) {
41
+ return dotParticleVec3(v, v);
42
+ }
43
+
44
+ double lengthParticleVec3(const ParticleVec3& v) {
45
+ const double sq = lengthSquaredParticleVec3(v);
46
+ return sq > 0.0 ? std::sqrt(sq) : 0.0;
47
+ }
48
+
49
+ ParticleVec3 normalizeParticleVec3(const ParticleVec3& v) {
50
+ const double len = lengthParticleVec3(v);
51
+ if (len <= 1e-15) {
52
+ return {0.0, 1.0, 0.0};
53
+ }
54
+ const double inv = 1.0 / len;
55
+ return {v.x * inv, v.y * inv, v.z * inv};
56
+ }
57
+
58
+ ParticleVec3 rotateByQuat(const ParticleQuat& q, const ParticleVec3& v) {
59
+ const ParticleVec3 u{q.x, q.y, q.z};
60
+ const ParticleVec3 uv = crossParticleVec3(u, v);
61
+ const ParticleVec3 uuv = crossParticleVec3(u, uv);
62
+ return addParticleVec3(v, addParticleVec3(scaleParticleVec3(uv, 2.0 * q.w), scaleParticleVec3(uuv, 2.0)));
63
+ }
64
+
65
+ ParticleQuat conjugateQuat(const ParticleQuat& q) {
66
+ return {-q.x, -q.y, -q.z, q.w};
67
+ }
68
+
69
+ double clampDouble(double v, double minVal, double maxVal) {
70
+ if (v < minVal) return minVal;
71
+ if (v > maxVal) return maxVal;
72
+ return v;
73
+ }
74
+
75
+ static ParticleVec3 makeOrthogonal(const ParticleVec3& dir) {
76
+ const ParticleVec3 up = (std::abs(dir.y) < 0.9) ? ParticleVec3{0.0, 1.0, 0.0} : ParticleVec3{1.0, 0.0, 0.0};
77
+ return normalizeParticleVec3(crossParticleVec3(dir, up));
78
+ }
79
+
80
+ // ---------------------------------------------------------------------------
81
+ // Particle System Lifecycle & Initialization
82
+ // ---------------------------------------------------------------------------
83
+
84
+ ParticleStatus initParticleSystem(
85
+ ParticleSystemSimulation& sim,
86
+ const ParticleSystemParams& params,
87
+ std::uint32_t capacity) {
88
+ if (capacity == 0 || capacity > kParticleMaxCapacity) {
89
+ return ParticleStatus::InvalidCapacity;
90
+ }
91
+ sim.params = params;
92
+ sim.capacity = capacity;
93
+ sim.activeCount = 0;
94
+ sim.totalEmitted = 0;
95
+ sim.tick = 0;
96
+ sim.emitterCount = 0;
97
+
98
+ for (std::uint32_t i = 0; i < kParticleMaxCapacity; ++i) {
99
+ sim.particles[i] = Particle{};
100
+ }
101
+ for (std::uint32_t e = 0; e < kMaxEmitters; ++e) {
102
+ sim.emitters[e] = ParticleEmitter{};
103
+ }
104
+ return ParticleStatus::Success;
105
+ }
106
+
107
+ ParticleStatus addEmitter(
108
+ ParticleSystemSimulation& sim,
109
+ const ParticleEmitter& emitter,
110
+ std::uint32_t* emitterIndex) {
111
+ if (sim.emitterCount >= kMaxEmitters) {
112
+ return ParticleStatus::BufferOverflow;
113
+ }
114
+ const std::uint32_t idx = sim.emitterCount;
115
+ sim.emitters[idx] = emitter;
116
+ sim.emitters[idx].id = idx;
117
+ sim.emitterCount += 1;
118
+ if (emitterIndex != nullptr) {
119
+ *emitterIndex = idx;
120
+ }
121
+ return ParticleStatus::Success;
122
+ }
123
+
124
+ // ---------------------------------------------------------------------------
125
+ // Deterministic Emission & Lifetime Ordering
126
+ // ---------------------------------------------------------------------------
127
+
128
+ std::uint32_t emitParticlesFromEmitter(
129
+ ParticleSystemSimulation& sim,
130
+ std::uint32_t emitterIndex,
131
+ std::uint32_t count,
132
+ std::uint64_t tick) {
133
+ if (emitterIndex >= sim.emitterCount || count == 0) {
134
+ return 0;
135
+ }
136
+
137
+ ParticleEmitter& emitter = sim.emitters[emitterIndex];
138
+ if (!emitter.enabled) {
139
+ return 0;
140
+ }
141
+
142
+ const ParticleVec3 baseDir = normalizeParticleVec3(emitter.direction);
143
+ const ParticleVec3 u = makeOrthogonal(baseDir);
144
+ const ParticleVec3 v = crossParticleVec3(baseDir, u);
145
+
146
+ std::uint32_t emittedCount = 0;
147
+
148
+ for (std::uint32_t k = 0; k < count; ++k) {
149
+ // Deterministic PRNG initialized from (seed, tick, k)
150
+ const std::uint64_t subSeed = DeterministicRng::hashCombine(emitter.seed, tick, k, 0);
151
+ DeterministicRng rng(subSeed);
152
+
153
+ // Find particle slot in strictly deterministic order
154
+ std::int32_t targetSlot = -1;
155
+
156
+ // First scan for first inactive slot (index 0 to capacity-1)
157
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
158
+ if (!sim.particles[i].active) {
159
+ targetSlot = static_cast<std::int32_t>(i);
160
+ break;
161
+ }
162
+ }
163
+
164
+ // If at full capacity, handle according to overflow policy
165
+ if (targetSlot < 0) {
166
+ if (sim.params.capacityPolicy == CapacityOverflowPolicy::RecycleOldest) {
167
+ // Find oldest active particle (highest age, lowest index on tie)
168
+ double maxAge = -1.0;
169
+ std::uint32_t oldestIdx = 0;
170
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
171
+ if (sim.particles[i].active && sim.particles[i].age > maxAge) {
172
+ maxAge = sim.particles[i].age;
173
+ oldestIdx = i;
174
+ }
175
+ }
176
+ targetSlot = static_cast<std::int32_t>(oldestIdx);
177
+ } else {
178
+ // Capacity overflow policy is DropNew: stop emitting
179
+ break;
180
+ }
181
+ }
182
+
183
+ // Direction with spread
184
+ ParticleVec3 dir = baseDir;
185
+ if (emitter.spreadAngle > 1e-6) {
186
+ const double phi = kTwoPi * rng.nextDouble();
187
+ const double cosMax = std::cos(emitter.spreadAngle);
188
+ const double cosTheta = 1.0 - rng.nextDouble() * (1.0 - cosMax);
189
+ const double sinTheta = std::sqrt(std::max(0.0, 1.0 - cosTheta * cosTheta));
190
+ const ParticleVec3 perturb = addParticleVec3(
191
+ scaleParticleVec3(u, sinTheta * std::cos(phi)),
192
+ scaleParticleVec3(v, sinTheta * std::sin(phi)));
193
+ dir = normalizeParticleVec3(addParticleVec3(perturb, scaleParticleVec3(baseDir, cosTheta)));
194
+ }
195
+
196
+ // Speed with spread
197
+ double speed = emitter.speed;
198
+ if (emitter.speedSpread > 0.0) {
199
+ speed += rng.nextRange(-emitter.speedSpread, emitter.speedSpread);
200
+ if (speed < 0.0) speed = 0.0;
201
+ }
202
+ const ParticleVec3 vel = scaleParticleVec3(dir, speed);
203
+
204
+ // Position offset according to emitter shape
205
+ ParticleVec3 pos = emitter.position;
206
+ if (emitter.shape == EmitterShape::Sphere) {
207
+ const double r = emitter.shapeSize.x * std::cbrt(rng.nextDouble());
208
+ const double theta = kTwoPi * rng.nextDouble();
209
+ const double z = rng.nextRange(-1.0, 1.0);
210
+ const double s = std::sqrt(std::max(0.0, 1.0 - z * z));
211
+ pos = addParticleVec3(pos, {r * s * std::cos(theta), r * s * std::sin(theta), r * z});
212
+ } else if (emitter.shape == EmitterShape::Box) {
213
+ pos.x += rng.nextRange(-emitter.shapeSize.x, emitter.shapeSize.x);
214
+ pos.y += rng.nextRange(-emitter.shapeSize.y, emitter.shapeSize.y);
215
+ pos.z += rng.nextRange(-emitter.shapeSize.z, emitter.shapeSize.z);
216
+ } else if (emitter.shape == EmitterShape::Disc) {
217
+ const double r = emitter.shapeSize.x * std::sqrt(rng.nextDouble());
218
+ const double theta = kTwoPi * rng.nextDouble();
219
+ const ParticleVec3 discOffset = addParticleVec3(
220
+ scaleParticleVec3(u, r * std::cos(theta)),
221
+ scaleParticleVec3(v, r * std::sin(theta)));
222
+ pos = addParticleVec3(pos, discOffset);
223
+ }
224
+
225
+ // Radius with spread
226
+ double radius = emitter.radius;
227
+ if (emitter.radiusSpread > 0.0) {
228
+ radius += rng.nextRange(-emitter.radiusSpread, emitter.radiusSpread);
229
+ if (radius < 0.001) radius = 0.001;
230
+ }
231
+
232
+ // Lifetime with spread
233
+ double lifetime = emitter.lifetime;
234
+ if (emitter.lifetimeSpread > 0.0) {
235
+ lifetime += rng.nextRange(-emitter.lifetimeSpread, emitter.lifetimeSpread);
236
+ if (lifetime < 0.0) lifetime = 0.0;
237
+ }
238
+
239
+ // Populate slot
240
+ Particle& p = sim.particles[targetSlot];
241
+ p.position = pos;
242
+ p.velocity = vel;
243
+ p.radius = radius;
244
+ p.invMass = emitter.mass > 0.0 ? 1.0 / emitter.mass : 1.0;
245
+ p.age = 0.0;
246
+ p.lifetime = lifetime;
247
+ p.restitution = emitter.restitution;
248
+ p.friction = emitter.friction;
249
+ p.active = true;
250
+ p.materialTag = emitter.materialTag;
251
+
252
+ sim.totalEmitted += 1;
253
+ emittedCount += 1;
254
+ }
255
+
256
+ // Recount active particles
257
+ std::uint32_t active = 0;
258
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
259
+ if (sim.particles[i].active) active += 1;
260
+ }
261
+ sim.activeCount = active;
262
+
263
+ return emittedCount;
264
+ }
265
+
266
+ // ---------------------------------------------------------------------------
267
+ // Aging and Lifetime Reaping
268
+ // ---------------------------------------------------------------------------
269
+
270
+ void ageAndReapParticles(ParticleSystemSimulation& sim, double dt) {
271
+ std::uint32_t active = 0;
272
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
273
+ Particle& p = sim.particles[i];
274
+ if (p.active) {
275
+ p.age += dt;
276
+ if (p.lifetime > 0.0 && p.age >= p.lifetime) {
277
+ p.active = false;
278
+ } else {
279
+ active += 1;
280
+ }
281
+ }
282
+ }
283
+ sim.activeCount = active;
284
+ }
285
+
286
+ // ---------------------------------------------------------------------------
287
+ // Forces & Linear Integration
288
+ // ---------------------------------------------------------------------------
289
+
290
+ void integrateForcesAndVelocities(ParticleSystemSimulation& sim, double dt) {
291
+ const ParticleVec3 grav = scaleParticleVec3(sim.params.gravity, dt);
292
+ const double dragFactor = (sim.params.drag > 0.0)
293
+ ? std::max(0.0, 1.0 - sim.params.drag * dt)
294
+ : 1.0;
295
+
296
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
297
+ Particle& p = sim.particles[i];
298
+ if (p.active && p.invMass > 0.0) {
299
+ p.velocity = addParticleVec3(p.velocity, grav);
300
+ if (dragFactor < 1.0) {
301
+ p.velocity = scaleParticleVec3(p.velocity, dragFactor);
302
+ }
303
+ }
304
+ }
305
+ }
306
+
307
+ // ---------------------------------------------------------------------------
308
+ // Granular Material: Inter-particle contact & shear friction (DEM)
309
+ // ---------------------------------------------------------------------------
310
+
311
+ void solveGranularInteractions(ParticleSystemSimulation& sim, double dt) {
312
+ if (sim.activeCount <= 1 || dt <= 0.0) return;
313
+
314
+ const double restitutionThreshold = sim.params.restitutionThreshold;
315
+ const double granularFriction = sim.params.granularFriction;
316
+ const double rollingResistance = sim.params.rollingResistance;
317
+
318
+ // Strict index-ordered pairwise check (i < j) for 100% deterministic results
319
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
320
+ Particle& pA = sim.particles[i];
321
+ if (!pA.active) continue;
322
+
323
+ for (std::uint32_t j = i + 1; j < sim.capacity; ++j) {
324
+ Particle& pB = sim.particles[j];
325
+ if (!pB.active) continue;
326
+
327
+ const double rSum = pA.radius + pB.radius;
328
+ const ParticleVec3 delta = subParticleVec3(pA.position, pB.position);
329
+ const double distSq = lengthSquaredParticleVec3(delta);
330
+
331
+ if (distSq < rSum * rSum && distSq > 1e-18) {
332
+ const double dist = std::sqrt(distSq);
333
+ const double overlap = rSum - dist;
334
+ const ParticleVec3 normal = scaleParticleVec3(delta, 1.0 / dist);
335
+
336
+ const double invMassSum = pA.invMass + pB.invMass;
337
+ if (invMassSum <= 0.0) continue;
338
+
339
+ const double mEff = 1.0 / invMassSum;
340
+ const double wA = pA.invMass * mEff;
341
+ const double wB = pB.invMass * mEff;
342
+
343
+ // Position depenetration along contact normal
344
+ pA.position = addParticleVec3(pA.position, scaleParticleVec3(normal, overlap * wA));
345
+ pB.position = subParticleVec3(pB.position, scaleParticleVec3(normal, overlap * wB));
346
+
347
+ // Normal velocity resolution
348
+ const ParticleVec3 vRel = subParticleVec3(pA.velocity, pB.velocity);
349
+ const double vn = dotParticleVec3(vRel, normal);
350
+
351
+ if (vn < 0.0) { // Approaching
352
+ double e = std::min(pA.restitution, pB.restitution);
353
+ if (std::abs(vn) < restitutionThreshold) {
354
+ e = 0.0;
355
+ }
356
+
357
+ const double jn = -(1.0 + e) * vn * mEff;
358
+ pA.velocity = addParticleVec3(pA.velocity, scaleParticleVec3(normal, jn * pA.invMass));
359
+ pB.velocity = subParticleVec3(pB.velocity, scaleParticleVec3(normal, jn * pB.invMass));
360
+
361
+ // Tangential relative velocity & shear friction
362
+ const ParticleVec3 vt = subParticleVec3(vRel, scaleParticleVec3(normal, vn));
363
+ const double vtLen = lengthParticleVec3(vt);
364
+
365
+ if (vtLen > 1e-9) {
366
+ const ParticleVec3 tDir = scaleParticleVec3(vt, 1.0 / vtLen);
367
+ const double mu = std::sqrt(pA.friction * pB.friction * granularFriction);
368
+ const double maxFriction = mu * jn;
369
+ const double jt = std::min(maxFriction, vtLen * mEff);
370
+
371
+ pA.velocity = subParticleVec3(pA.velocity, scaleParticleVec3(tDir, jt * pA.invMass));
372
+ pB.velocity = addParticleVec3(pB.velocity, scaleParticleVec3(tDir, jt * pB.invMass));
373
+ }
374
+
375
+ // Rolling resistance damping to prevent endless rolling of beads
376
+ if (rollingResistance > 0.0) {
377
+ pA.velocity = subParticleVec3(pA.velocity, scaleParticleVec3(vt, rollingResistance * wA));
378
+ pB.velocity = addParticleVec3(pB.velocity, scaleParticleVec3(vt, rollingResistance * wB));
379
+ }
380
+ }
381
+ }
382
+ }
383
+ }
384
+ }
385
+
386
+ // ---------------------------------------------------------------------------
387
+ // Rigid Body Collision (Ground, Boxes, Spheres, Capsules)
388
+ // ---------------------------------------------------------------------------
389
+
390
+ void solveRigidBodyCollisions(
391
+ ParticleSystemSimulation& sim,
392
+ std::span<const Body> bodies,
393
+ double dt) {
394
+ const double restitutionThreshold = sim.params.restitutionThreshold;
395
+ const double groundY = sim.params.groundY;
396
+ const bool enableGround = sim.params.enableGround;
397
+ const double groundRestitution = sim.params.groundRestitution;
398
+ const double groundFriction = sim.params.groundFriction;
399
+ const double rollingResistance = sim.params.rollingResistance;
400
+
401
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
402
+ Particle& p = sim.particles[i];
403
+ if (!p.active) continue;
404
+
405
+ // Ground plane collision: y >= groundY
406
+ if (enableGround) {
407
+ const double bottomY = p.position.y - p.radius;
408
+ if (bottomY < groundY) {
409
+ p.position.y = groundY + p.radius;
410
+ const double vn = p.velocity.y;
411
+ if (vn < 0.0) {
412
+ const double e = (std::abs(vn) < restitutionThreshold) ? 0.0 : (groundRestitution * p.restitution);
413
+ const double jn = -(1.0 + e) * vn;
414
+ p.velocity.y += jn;
415
+
416
+ // Ground friction
417
+ const double vHorizSq = p.velocity.x * p.velocity.x + p.velocity.z * p.velocity.z;
418
+ if (vHorizSq > 1e-16) {
419
+ const double vHoriz = std::sqrt(vHorizSq);
420
+ const double mu = groundFriction * p.friction;
421
+ const double maxF = mu * jn;
422
+ const double applied = std::min(maxF, vHoriz);
423
+ const double factor = (vHoriz - applied) / vHoriz;
424
+ p.velocity.x *= factor;
425
+ p.velocity.z *= factor;
426
+ }
427
+
428
+ // Rolling resistance damping on horizontal contact
429
+ if (rollingResistance > 0.0) {
430
+ const double rollDamp = std::max(0.0, 1.0 - rollingResistance);
431
+ p.velocity.x *= rollDamp;
432
+ p.velocity.z *= rollDamp;
433
+ }
434
+ }
435
+ }
436
+ }
437
+
438
+ // Rigid body collisions in index order
439
+ for (std::size_t bIdx = 0; bIdx < bodies.size(); ++bIdx) {
440
+ const Body& body = bodies[bIdx];
441
+ const ParticleVec3 bodyPos{body.x, body.y, body.z};
442
+ const ParticleQuat bodyRot{body.orientationX, body.orientationY, body.orientationZ, body.orientationW};
443
+ const ParticleVec3 bodyLinearVel{body.vx, body.vy, body.vz};
444
+ const ParticleVec3 bodyAngVel{body.angularVelX, body.angularVelY, body.angularVelZ};
445
+
446
+ if (body.shape == ShapeKind::Box) {
447
+ // Box half-extents
448
+ const double hx = body.shape0;
449
+ const double hy = body.shape1;
450
+ const double hz = body.shape2;
451
+
452
+ // Transform particle into body local frame
453
+ const ParticleVec3 pRel = subParticleVec3(p.position, bodyPos);
454
+ const ParticleVec3 pLoc = rotateByQuat(conjugateQuat(bodyRot), pRel);
455
+
456
+ // Closest point on box
457
+ const double cx = clampDouble(pLoc.x, -hx, hx);
458
+ const double cy = clampDouble(pLoc.y, -hy, hy);
459
+ const double cz = clampDouble(pLoc.z, -hz, hz);
460
+
461
+ const ParticleVec3 diffLoc{pLoc.x - cx, pLoc.y - cy, pLoc.z - cz};
462
+ const double distSq = lengthSquaredParticleVec3(diffLoc);
463
+
464
+ ParticleVec3 normLoc{0.0, 1.0, 0.0};
465
+ double dist = 0.0;
466
+
467
+ if (distSq < 1e-16) {
468
+ // Inside the box: push out to closest face
469
+ const double dx = hx - std::abs(pLoc.x);
470
+ const double dy = hy - std::abs(pLoc.y);
471
+ const double dz = hz - std::abs(pLoc.z);
472
+ if (dx <= dy && dx <= dz) {
473
+ normLoc = {pLoc.x >= 0.0 ? 1.0 : -1.0, 0.0, 0.0};
474
+ dist = -dx;
475
+ } else if (dy <= dx && dy <= dz) {
476
+ normLoc = {0.0, pLoc.y >= 0.0 ? 1.0 : -1.0, 0.0};
477
+ dist = -dy;
478
+ } else {
479
+ normLoc = {0.0, 0.0, pLoc.z >= 0.0 ? 1.0 : -1.0};
480
+ dist = -dz;
481
+ }
482
+ } else {
483
+ dist = std::sqrt(distSq);
484
+ normLoc = scaleParticleVec3(diffLoc, 1.0 / dist);
485
+ }
486
+
487
+ if (dist < p.radius) {
488
+ const double penetration = p.radius - dist;
489
+ const ParticleVec3 normalWorld = rotateByQuat(bodyRot, normLoc);
490
+ p.position = addParticleVec3(p.position, scaleParticleVec3(normalWorld, penetration));
491
+
492
+ // Body velocity at contact point
493
+ const ParticleVec3 contactWorld = addParticleVec3(bodyPos, rotateByQuat(bodyRot, {cx, cy, cz}));
494
+ const ParticleVec3 arm = subParticleVec3(contactWorld, {body.centerOfMassX, body.centerOfMassY, body.centerOfMassZ});
495
+ const ParticleVec3 vBodyContact = addParticleVec3(bodyLinearVel, crossParticleVec3(bodyAngVel, arm));
496
+
497
+ const ParticleVec3 vRel = subParticleVec3(p.velocity, vBodyContact);
498
+ const double vn = dotParticleVec3(vRel, normalWorld);
499
+
500
+ if (vn < 0.0) {
501
+ const double e = (std::abs(vn) < restitutionThreshold) ? 0.0 : (body.restitution * p.restitution);
502
+ const double jn = -(1.0 + e) * vn;
503
+ p.velocity = addParticleVec3(p.velocity, scaleParticleVec3(normalWorld, jn));
504
+
505
+ // Tangential friction
506
+ const ParticleVec3 vt = subParticleVec3(vRel, scaleParticleVec3(normalWorld, vn));
507
+ const double vtLen = lengthParticleVec3(vt);
508
+ if (vtLen > 1e-9) {
509
+ const ParticleVec3 tDir = scaleParticleVec3(vt, 1.0 / vtLen);
510
+ const double mu = body.friction * p.friction;
511
+ const double maxF = mu * jn;
512
+ const double jt = std::min(maxF, vtLen);
513
+ p.velocity = subParticleVec3(p.velocity, scaleParticleVec3(tDir, jt));
514
+ }
515
+ }
516
+ }
517
+ } else if (body.shape == ShapeKind::Sphere) {
518
+ const double bodyRadius = body.shape0;
519
+ const ParticleVec3 center = addParticleVec3(bodyPos, rotateByQuat(bodyRot, {body.shapeOffsetX, body.shapeOffsetY, body.shapeOffsetZ}));
520
+ const ParticleVec3 diff = subParticleVec3(p.position, center);
521
+ const double distSq = lengthSquaredParticleVec3(diff);
522
+ const double targetDist = bodyRadius + p.radius;
523
+
524
+ if (distSq < targetDist * targetDist) {
525
+ const double dist = std::sqrt(distSq);
526
+ const ParticleVec3 normal = dist > 1e-9 ? scaleParticleVec3(diff, 1.0 / dist) : ParticleVec3{0.0, 1.0, 0.0};
527
+ const double penetration = targetDist - dist;
528
+ p.position = addParticleVec3(p.position, scaleParticleVec3(normal, penetration));
529
+
530
+ const ParticleVec3 contactWorld = addParticleVec3(center, scaleParticleVec3(normal, bodyRadius));
531
+ const ParticleVec3 arm = subParticleVec3(contactWorld, {body.centerOfMassX, body.centerOfMassY, body.centerOfMassZ});
532
+ const ParticleVec3 vBodyContact = addParticleVec3(bodyLinearVel, crossParticleVec3(bodyAngVel, arm));
533
+
534
+ const ParticleVec3 vRel = subParticleVec3(p.velocity, vBodyContact);
535
+ const double vn = dotParticleVec3(vRel, normal);
536
+
537
+ if (vn < 0.0) {
538
+ const double e = (std::abs(vn) < restitutionThreshold) ? 0.0 : (body.restitution * p.restitution);
539
+ const double jn = -(1.0 + e) * vn;
540
+ p.velocity = addParticleVec3(p.velocity, scaleParticleVec3(normal, jn));
541
+
542
+ const ParticleVec3 vt = subParticleVec3(vRel, scaleParticleVec3(normal, vn));
543
+ const double vtLen = lengthParticleVec3(vt);
544
+ if (vtLen > 1e-9) {
545
+ const ParticleVec3 tDir = scaleParticleVec3(vt, 1.0 / vtLen);
546
+ const double mu = body.friction * p.friction;
547
+ const double maxF = mu * jn;
548
+ const double jt = std::min(maxF, vtLen);
549
+ p.velocity = subParticleVec3(p.velocity, scaleParticleVec3(tDir, jt));
550
+ }
551
+ }
552
+ }
553
+ } else if (body.shape == ShapeKind::Capsule) {
554
+ const double radius = body.shape0;
555
+ const double halfHeight = body.shape1;
556
+
557
+ const ParticleVec3 pRel = subParticleVec3(p.position, bodyPos);
558
+ const ParticleVec3 pLoc = rotateByQuat(conjugateQuat(bodyRot), pRel);
559
+
560
+ // Clamped segment along local Y
561
+ const double clampedY = clampDouble(pLoc.y, -halfHeight, halfHeight);
562
+ const ParticleVec3 segPtLoc{0.0, clampedY, 0.0};
563
+
564
+ const ParticleVec3 diffLoc = subParticleVec3(pLoc, segPtLoc);
565
+ const double distSq = lengthSquaredParticleVec3(diffLoc);
566
+ const double targetDist = radius + p.radius;
567
+
568
+ if (distSq < targetDist * targetDist) {
569
+ const double dist = std::sqrt(distSq);
570
+ const ParticleVec3 normLoc = dist > 1e-9 ? scaleParticleVec3(diffLoc, 1.0 / dist) : ParticleVec3{0.0, 1.0, 0.0};
571
+ const double penetration = targetDist - dist;
572
+ const ParticleVec3 normalWorld = rotateByQuat(bodyRot, normLoc);
573
+ p.position = addParticleVec3(p.position, scaleParticleVec3(normalWorld, penetration));
574
+
575
+ const ParticleVec3 contactLoc = addParticleVec3(segPtLoc, scaleParticleVec3(normLoc, radius));
576
+ const ParticleVec3 contactWorld = addParticleVec3(bodyPos, rotateByQuat(bodyRot, contactLoc));
577
+ const ParticleVec3 arm = subParticleVec3(contactWorld, {body.centerOfMassX, body.centerOfMassY, body.centerOfMassZ});
578
+ const ParticleVec3 vBodyContact = addParticleVec3(bodyLinearVel, crossParticleVec3(bodyAngVel, arm));
579
+
580
+ const ParticleVec3 vRel = subParticleVec3(p.velocity, vBodyContact);
581
+ const double vn = dotParticleVec3(vRel, normalWorld);
582
+
583
+ if (vn < 0.0) {
584
+ const double e = (std::abs(vn) < restitutionThreshold) ? 0.0 : (body.restitution * p.restitution);
585
+ const double jn = -(1.0 + e) * vn;
586
+ p.velocity = addParticleVec3(p.velocity, scaleParticleVec3(normalWorld, jn));
587
+
588
+ const ParticleVec3 vt = subParticleVec3(vRel, scaleParticleVec3(normalWorld, vn));
589
+ const double vtLen = lengthParticleVec3(vt);
590
+ if (vtLen > 1e-9) {
591
+ const ParticleVec3 tDir = scaleParticleVec3(vt, 1.0 / vtLen);
592
+ const double mu = body.friction * p.friction;
593
+ const double maxF = mu * jn;
594
+ const double jt = std::min(maxF, vtLen);
595
+ p.velocity = subParticleVec3(p.velocity, scaleParticleVec3(tDir, jt));
596
+ }
597
+ }
598
+ }
599
+ }
600
+ }
601
+ }
602
+ }
603
+
604
+ void solveRigidBodyCollisionsRaw(
605
+ ParticleSystemSimulation& sim,
606
+ const double* bodyData,
607
+ std::uint32_t bodyCount,
608
+ double dt) {
609
+ if (bodyData == nullptr || bodyCount == 0) {
610
+ solveRigidBodyCollisions(sim, {}, dt);
611
+ return;
612
+ }
613
+
614
+ // Parse Body array from bodyData
615
+ std::vector<Body> bodies;
616
+ bodies.reserve(bodyCount);
617
+
618
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
619
+ const std::uint32_t offset = i * kBodyStride;
620
+ Body b;
621
+ b.x = bodyData[offset + 0];
622
+ b.y = bodyData[offset + 1];
623
+ b.z = bodyData[offset + 2];
624
+ b.vx = bodyData[offset + 3];
625
+ b.vy = bodyData[offset + 4];
626
+ b.vz = bodyData[offset + 5];
627
+ b.shape0 = bodyData[offset + 6];
628
+ b.shape1 = bodyData[offset + 7];
629
+ b.shape2 = bodyData[offset + 8];
630
+ b.shape = static_cast<ShapeKind>(static_cast<std::uint32_t>(bodyData[offset + 10]));
631
+ b.shapeOffsetX = bodyData[offset + 15];
632
+ b.shapeOffsetY = bodyData[offset + 16];
633
+ b.shapeOffsetZ = bodyData[offset + 17];
634
+ b.mass = bodyData[offset + 18];
635
+ b.angularVelX = bodyData[offset + 19];
636
+ b.angularVelY = bodyData[offset + 20];
637
+ b.angularVelZ = bodyData[offset + 21];
638
+ b.orientationX = bodyData[offset + 22];
639
+ b.orientationY = bodyData[offset + 23];
640
+ b.orientationZ = bodyData[offset + 24];
641
+ b.orientationW = bodyData[offset + 25];
642
+ b.friction = bodyData[offset + 27];
643
+ b.restitution = bodyData[offset + 28];
644
+ b.centerOfMassX = bodyData[offset + 37];
645
+ b.centerOfMassY = bodyData[offset + 38];
646
+ b.centerOfMassZ = bodyData[offset + 39];
647
+ bodies.push_back(b);
648
+ }
649
+
650
+ solveRigidBodyCollisions(sim, bodies, dt);
651
+ }
652
+
653
+ // ---------------------------------------------------------------------------
654
+ // Position Integration
655
+ // ---------------------------------------------------------------------------
656
+
657
+ void integratePositions(ParticleSystemSimulation& sim, double dt) {
658
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
659
+ Particle& p = sim.particles[i];
660
+ if (p.active) {
661
+ p.position = addParticleVec3(p.position, scaleParticleVec3(p.velocity, dt));
662
+ }
663
+ }
664
+ }
665
+
666
+ // ---------------------------------------------------------------------------
667
+ // Step Particle System
668
+ // ---------------------------------------------------------------------------
669
+
670
+ ParticleStatus stepParticleSystem(
671
+ ParticleSystemSimulation& sim,
672
+ std::span<const Body> bodies,
673
+ double dt) {
674
+ if (dt <= 0.0) return ParticleStatus::InvalidInput;
675
+
676
+ const std::uint32_t substeps = std::max(1u, sim.params.substeps);
677
+ const double subDt = dt / static_cast<double>(substeps);
678
+
679
+ for (std::uint32_t step = 0; step < substeps; ++step) {
680
+ // 1. Accumulate emitter rate and emit particles
681
+ for (std::uint32_t e = 0; e < sim.emitterCount; ++e) {
682
+ ParticleEmitter& em = sim.emitters[e];
683
+ if (em.enabled && em.emissionRate > 0.0) {
684
+ em.accumulator += em.emissionRate * subDt;
685
+ const auto toEmit = static_cast<std::uint32_t>(std::floor(em.accumulator));
686
+ if (toEmit > 0) {
687
+ em.accumulator -= static_cast<double>(toEmit);
688
+ emitParticlesFromEmitter(sim, e, toEmit, sim.tick);
689
+ }
690
+ }
691
+ }
692
+
693
+ // 2. Age and reap expired particles
694
+ ageAndReapParticles(sim, subDt);
695
+
696
+ // 3. Integrate forces (gravity + drag)
697
+ integrateForcesAndVelocities(sim, subDt);
698
+
699
+ // 4. Granular inter-particle collisions (DEM contact and shear friction)
700
+ solveGranularInteractions(sim, subDt);
701
+
702
+ // 5. Rigid body and ground collisions
703
+ solveRigidBodyCollisions(sim, bodies, subDt);
704
+
705
+ // 6. Integrate positions
706
+ integratePositions(sim, subDt);
707
+ }
708
+
709
+ sim.tick += 1;
710
+ return ParticleStatus::Success;
711
+ }
712
+
713
+ ParticleStatus stepParticleSystemRaw(
714
+ double* particleData,
715
+ std::uint32_t maxParticles,
716
+ std::uint32_t* activeParticleCount,
717
+ double* emitterData,
718
+ std::uint32_t emitterCount,
719
+ const double* bodyData,
720
+ std::uint32_t bodyCount,
721
+ const double* paramsData,
722
+ double dt,
723
+ std::uint64_t tick) {
724
+ if (particleData == nullptr || maxParticles == 0 || maxParticles > kParticleMaxCapacity) {
725
+ return ParticleStatus::InvalidCapacity;
726
+ }
727
+
728
+ ParticleSystemSimulation sim;
729
+ sim.capacity = maxParticles;
730
+ sim.tick = tick;
731
+
732
+ // Import params
733
+ if (paramsData != nullptr) {
734
+ sim.params.gravity.x = paramsData[0];
735
+ sim.params.gravity.y = paramsData[1];
736
+ sim.params.gravity.z = paramsData[2];
737
+ sim.params.drag = paramsData[3];
738
+ sim.params.groundY = paramsData[4];
739
+ sim.params.enableGround = paramsData[5] != 0.0;
740
+ sim.params.groundRestitution = paramsData[6];
741
+ sim.params.groundFriction = paramsData[7];
742
+ sim.params.granularRepulsionStiffness = paramsData[8];
743
+ sim.params.granularDamping = paramsData[9];
744
+ sim.params.granularFriction = paramsData[10];
745
+ sim.params.rollingResistance = paramsData[11];
746
+ sim.params.restitutionThreshold = paramsData[12];
747
+ sim.params.capacityPolicy = static_cast<CapacityOverflowPolicy>(static_cast<std::uint32_t>(paramsData[13]));
748
+ sim.params.substeps = static_cast<std::uint32_t>(paramsData[14]);
749
+ sim.params.maxParticles = static_cast<std::uint32_t>(paramsData[15]);
750
+ }
751
+
752
+ // Import particles
753
+ importParticlesFromBuffer(sim, particleData, maxParticles);
754
+
755
+ // Import emitters
756
+ sim.emitterCount = std::min(emitterCount, kMaxEmitters);
757
+ if (emitterData != nullptr) {
758
+ for (std::uint32_t e = 0; e < sim.emitterCount; ++e) {
759
+ const std::uint32_t off = e * kParticleEmitterStride;
760
+ ParticleEmitter& em = sim.emitters[e];
761
+ em.id = e;
762
+ em.position = {emitterData[off + 0], emitterData[off + 1], emitterData[off + 2]};
763
+ em.direction = {emitterData[off + 3], emitterData[off + 4], emitterData[off + 5]};
764
+ em.speed = emitterData[off + 6];
765
+ em.speedSpread = emitterData[off + 7];
766
+ em.spreadAngle = emitterData[off + 8];
767
+ em.emissionRate = emitterData[off + 9];
768
+ em.accumulator = emitterData[off + 10];
769
+ em.lifetime = emitterData[off + 11];
770
+ em.lifetimeSpread = emitterData[off + 12];
771
+ em.radius = emitterData[off + 13];
772
+ em.radiusSpread = emitterData[off + 14];
773
+ em.mass = emitterData[off + 15];
774
+ em.restitution = emitterData[off + 16];
775
+ em.friction = emitterData[off + 17];
776
+ em.seed = static_cast<std::uint64_t>(emitterData[off + 18]);
777
+ em.enabled = emitterData[off + 19] != 0.0;
778
+ em.shape = static_cast<EmitterShape>(static_cast<std::uint32_t>(emitterData[off + 20]));
779
+ em.shapeSize = {emitterData[off + 21], emitterData[off + 22], emitterData[off + 23]};
780
+ }
781
+ }
782
+
783
+ // Execute step
784
+ std::vector<Body> bodies;
785
+ if (bodyData != nullptr && bodyCount > 0) {
786
+ bodies.reserve(bodyCount);
787
+ for (std::uint32_t i = 0; i < bodyCount; ++i) {
788
+ const std::uint32_t offset = i * kBodyStride;
789
+ Body b;
790
+ b.x = bodyData[offset + 0];
791
+ b.y = bodyData[offset + 1];
792
+ b.z = bodyData[offset + 2];
793
+ b.vx = bodyData[offset + 3];
794
+ b.vy = bodyData[offset + 4];
795
+ b.vz = bodyData[offset + 5];
796
+ b.shape0 = bodyData[offset + 6];
797
+ b.shape1 = bodyData[offset + 7];
798
+ b.shape2 = bodyData[offset + 8];
799
+ b.shape = static_cast<ShapeKind>(static_cast<std::uint32_t>(bodyData[offset + 10]));
800
+ b.shapeOffsetX = bodyData[offset + 15];
801
+ b.shapeOffsetY = bodyData[offset + 16];
802
+ b.shapeOffsetZ = bodyData[offset + 17];
803
+ b.mass = bodyData[offset + 18];
804
+ b.angularVelX = bodyData[offset + 19];
805
+ b.angularVelY = bodyData[offset + 20];
806
+ b.angularVelZ = bodyData[offset + 21];
807
+ b.orientationX = bodyData[offset + 22];
808
+ b.orientationY = bodyData[offset + 23];
809
+ b.orientationZ = bodyData[offset + 24];
810
+ b.orientationW = bodyData[offset + 25];
811
+ b.friction = bodyData[offset + 27];
812
+ b.restitution = bodyData[offset + 28];
813
+ b.centerOfMassX = bodyData[offset + 37];
814
+ b.centerOfMassY = bodyData[offset + 38];
815
+ b.centerOfMassZ = bodyData[offset + 39];
816
+ bodies.push_back(b);
817
+ }
818
+ }
819
+
820
+ const ParticleStatus status = stepParticleSystem(sim, bodies, dt);
821
+ if (status != ParticleStatus::Success) return status;
822
+
823
+ // Export updated particles back to buffer
824
+ exportParticlesToBuffer(sim, particleData);
825
+
826
+ // Write back accumulator to emitterData
827
+ if (emitterData != nullptr) {
828
+ for (std::uint32_t e = 0; e < sim.emitterCount; ++e) {
829
+ emitterData[e * kParticleEmitterStride + 10] = sim.emitters[e].accumulator;
830
+ }
831
+ }
832
+
833
+ if (activeParticleCount != nullptr) {
834
+ *activeParticleCount = sim.activeCount;
835
+ }
836
+
837
+ return ParticleStatus::Success;
838
+ }
839
+
840
+ // ---------------------------------------------------------------------------
841
+ // Granular Metrics: Angle of Repose
842
+ // ---------------------------------------------------------------------------
843
+
844
+ double computeAngleOfRepose(const ParticleSystemSimulation& sim, double minRadius) {
845
+ if (sim.activeCount == 0) return 0.0;
846
+
847
+ double sumX = 0.0;
848
+ double sumZ = 0.0;
849
+ std::uint32_t restingCount = 0;
850
+
851
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
852
+ const Particle& p = sim.particles[i];
853
+ if (p.active) {
854
+ sumX += p.position.x;
855
+ sumZ += p.position.z;
856
+ restingCount += 1;
857
+ }
858
+ }
859
+
860
+ if (restingCount == 0) return 0.0;
861
+
862
+ const double meanX = sumX / static_cast<double>(restingCount);
863
+ const double meanZ = sumZ / static_cast<double>(restingCount);
864
+
865
+ double maxHeight = 0.0;
866
+ double maxRadialDist = 0.0;
867
+
868
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
869
+ const Particle& p = sim.particles[i];
870
+ if (p.active) {
871
+ const double h = p.position.y - sim.params.groundY;
872
+ if (h > maxHeight) maxHeight = h;
873
+
874
+ const double dx = p.position.x - meanX;
875
+ const double dz = p.position.z - meanZ;
876
+ const double r = std::sqrt(dx * dx + dz * dz);
877
+ if (r > maxRadialDist) maxRadialDist = r;
878
+ }
879
+ }
880
+
881
+ if (minRadius > 0.0 && maxRadialDist < minRadius) {
882
+ maxRadialDist = minRadius;
883
+ }
884
+
885
+ if (maxRadialDist <= 1e-6 || maxHeight <= 1e-6) {
886
+ return 0.0;
887
+ }
888
+
889
+ return std::atan2(maxHeight, maxRadialDist);
890
+ }
891
+
892
+ // ---------------------------------------------------------------------------
893
+ // Buffer serialization
894
+ // ---------------------------------------------------------------------------
895
+
896
+ void exportParticlesToBuffer(const ParticleSystemSimulation& sim, double* outputData) {
897
+ if (outputData == nullptr) return;
898
+ for (std::uint32_t i = 0; i < sim.capacity; ++i) {
899
+ const Particle& p = sim.particles[i];
900
+ const std::uint32_t off = i * kParticleStride;
901
+ outputData[off + 0] = p.position.x;
902
+ outputData[off + 1] = p.position.y;
903
+ outputData[off + 2] = p.position.z;
904
+ outputData[off + 3] = p.velocity.x;
905
+ outputData[off + 4] = p.velocity.y;
906
+ outputData[off + 5] = p.velocity.z;
907
+ outputData[off + 6] = p.radius;
908
+ outputData[off + 7] = p.invMass;
909
+ outputData[off + 8] = p.age;
910
+ outputData[off + 9] = p.lifetime;
911
+ outputData[off + 10] = p.restitution;
912
+ outputData[off + 11] = p.friction;
913
+ outputData[off + 12] = p.active ? 1.0 : 0.0;
914
+ outputData[off + 13] = static_cast<double>(p.materialTag);
915
+ }
916
+ }
917
+
918
+ void importParticlesFromBuffer(ParticleSystemSimulation& sim, const double* inputData, std::uint32_t particleCount) {
919
+ if (inputData == nullptr) return;
920
+ const std::uint32_t count = std::min(particleCount, sim.capacity);
921
+ std::uint32_t active = 0;
922
+
923
+ for (std::uint32_t i = 0; i < count; ++i) {
924
+ const std::uint32_t off = i * kParticleStride;
925
+ Particle& p = sim.particles[i];
926
+ p.position = {inputData[off + 0], inputData[off + 1], inputData[off + 2]};
927
+ p.velocity = {inputData[off + 3], inputData[off + 4], inputData[off + 5]};
928
+ p.radius = inputData[off + 6];
929
+ p.invMass = inputData[off + 7];
930
+ p.age = inputData[off + 8];
931
+ p.lifetime = inputData[off + 9];
932
+ p.restitution = inputData[off + 10];
933
+ p.friction = inputData[off + 11];
934
+ p.active = inputData[off + 12] != 0.0;
935
+ p.materialTag = static_cast<std::uint32_t>(inputData[off + 13]);
936
+
937
+ if (p.active) active += 1;
938
+ }
939
+
940
+ sim.activeCount = active;
941
+ }
942
+
943
+ } // namespace rundot::kinetix::physics
944
+
945
+ // ---------------------------------------------------------------------------
946
+ // Extern "C" ABI
947
+ // ---------------------------------------------------------------------------
948
+
949
+ extern "C" {
950
+
951
+ std::uint32_t rundot_kinetix_physics_particles_abi_version() {
952
+ return rundot::kinetix::physics::kParticlesAbiVersion;
953
+ }
954
+
955
+ std::uint32_t rundot_kinetix_physics_particles_particle_stride() {
956
+ return rundot::kinetix::physics::kParticleStride;
957
+ }
958
+
959
+ std::uint32_t rundot_kinetix_physics_particles_emitter_stride() {
960
+ return rundot::kinetix::physics::kParticleEmitterStride;
961
+ }
962
+
963
+ std::uint32_t rundot_kinetix_physics_particles_params_stride() {
964
+ return rundot::kinetix::physics::kParticleParamsStride;
965
+ }
966
+
967
+ std::uint32_t rundot_kinetix_physics_particles_max_capacity() {
968
+ return rundot::kinetix::physics::kParticleMaxCapacity;
969
+ }
970
+
971
+ std::int32_t rundot_kinetix_physics_particles_step(
972
+ double* particleData,
973
+ std::uint32_t maxParticles,
974
+ std::uint32_t* activeParticleCount,
975
+ double* emitterData,
976
+ std::uint32_t emitterCount,
977
+ const double* bodyData,
978
+ std::uint32_t bodyCount,
979
+ const double* paramsData,
980
+ double dt,
981
+ std::uint64_t tick) {
982
+ return static_cast<std::int32_t>(rundot::kinetix::physics::stepParticleSystemRaw(
983
+ particleData,
984
+ maxParticles,
985
+ activeParticleCount,
986
+ emitterData,
987
+ emitterCount,
988
+ bodyData,
989
+ bodyCount,
990
+ paramsData,
991
+ dt,
992
+ tick));
993
+ }
994
+
995
+ std::int32_t rundot_kinetix_physics_particles_emit(
996
+ double* particleData,
997
+ std::uint32_t maxParticles,
998
+ std::uint32_t* activeParticleCount,
999
+ const double* emitterData,
1000
+ std::uint32_t emitterIndex,
1001
+ std::uint32_t count,
1002
+ std::uint64_t tick,
1003
+ std::uint64_t seed) {
1004
+ if (particleData == nullptr || maxParticles == 0 || maxParticles > rundot::kinetix::physics::kParticleMaxCapacity) {
1005
+ return static_cast<std::int32_t>(rundot::kinetix::physics::ParticleStatus::InvalidCapacity);
1006
+ }
1007
+ if (emitterData == nullptr) {
1008
+ return static_cast<std::int32_t>(rundot::kinetix::physics::ParticleStatus::InvalidInput);
1009
+ }
1010
+
1011
+ rundot::kinetix::physics::ParticleSystemSimulation sim;
1012
+ sim.capacity = maxParticles;
1013
+ rundot::kinetix::physics::importParticlesFromBuffer(sim, particleData, maxParticles);
1014
+
1015
+ const std::uint32_t off = emitterIndex * rundot::kinetix::physics::kParticleEmitterStride;
1016
+ rundot::kinetix::physics::ParticleEmitter em;
1017
+ em.id = emitterIndex;
1018
+ em.position = {emitterData[off + 0], emitterData[off + 1], emitterData[off + 2]};
1019
+ em.direction = {emitterData[off + 3], emitterData[off + 4], emitterData[off + 5]};
1020
+ em.speed = emitterData[off + 6];
1021
+ em.speedSpread = emitterData[off + 7];
1022
+ em.spreadAngle = emitterData[off + 8];
1023
+ em.emissionRate = emitterData[off + 9];
1024
+ em.accumulator = emitterData[off + 10];
1025
+ em.lifetime = emitterData[off + 11];
1026
+ em.lifetimeSpread = emitterData[off + 12];
1027
+ em.radius = emitterData[off + 13];
1028
+ em.radiusSpread = emitterData[off + 14];
1029
+ em.mass = emitterData[off + 15];
1030
+ em.restitution = emitterData[off + 16];
1031
+ em.friction = emitterData[off + 17];
1032
+ em.seed = seed != 0 ? seed : static_cast<std::uint64_t>(emitterData[off + 18]);
1033
+ em.enabled = emitterData[off + 19] != 0.0;
1034
+ em.shape = static_cast<rundot::kinetix::physics::EmitterShape>(static_cast<std::uint32_t>(emitterData[off + 20]));
1035
+ em.shapeSize = {emitterData[off + 21], emitterData[off + 22], emitterData[off + 23]};
1036
+
1037
+ sim.emitters[0] = em;
1038
+ sim.emitterCount = 1;
1039
+
1040
+ const std::uint32_t emitted = rundot::kinetix::physics::emitParticlesFromEmitter(sim, 0, count, tick);
1041
+ rundot::kinetix::physics::exportParticlesToBuffer(sim, particleData);
1042
+
1043
+ if (activeParticleCount != nullptr) {
1044
+ *activeParticleCount = sim.activeCount;
1045
+ }
1046
+
1047
+ return static_cast<std::int32_t>(emitted);
1048
+ }
1049
+
1050
+ std::int32_t rundot_kinetix_physics_particles_reset(
1051
+ double* particleData,
1052
+ std::uint32_t maxParticles,
1053
+ std::uint32_t* activeParticleCount) {
1054
+ if (particleData == nullptr || maxParticles == 0 || maxParticles > rundot::kinetix::physics::kParticleMaxCapacity) {
1055
+ return static_cast<std::int32_t>(rundot::kinetix::physics::ParticleStatus::InvalidCapacity);
1056
+ }
1057
+
1058
+ std::memset(particleData, 0, maxParticles * rundot::kinetix::physics::kParticleStride * sizeof(double));
1059
+ if (activeParticleCount != nullptr) {
1060
+ *activeParticleCount = 0;
1061
+ }
1062
+ return 0;
1063
+ }
1064
+
1065
+ }