@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,1862 @@
1
+ #include "soft_collision.hpp"
2
+
3
+ #include <algorithm>
4
+ #include <array>
5
+ #include <cmath>
6
+ #include <cstdint>
7
+ #include <limits>
8
+ #include <span>
9
+
10
+ #include "physics.hpp"
11
+ #include "constraint_rows.hpp"
12
+
13
+ namespace rundot::kinetix::physics {
14
+
15
+ namespace {
16
+
17
+ // ---------------------------------------------------------------------------
18
+ // Internal Unpack / Pack Helpers
19
+ // ---------------------------------------------------------------------------
20
+
21
+ SoftNode unpackSoftNode(const double* data, std::uint32_t index) {
22
+ const std::uint32_t offset = index * kSoftNodeStride;
23
+ SoftNode node;
24
+ node.position = {data[offset + 0], data[offset + 1], data[offset + 2]};
25
+ node.previousPosition = {data[offset + 3], data[offset + 4], data[offset + 5]};
26
+ node.velocity = {data[offset + 6], data[offset + 7], data[offset + 8]};
27
+ node.inverseMass = data[offset + 9];
28
+ node.radius = data[offset + 10];
29
+ node.thickness = data[offset + 11];
30
+ node.friction = data[offset + 12];
31
+ node.restitution = data[offset + 13];
32
+ node.flags = static_cast<std::uint32_t>(data[offset + 14]);
33
+ node.softBodyId = static_cast<std::uint32_t>(data[offset + 15]);
34
+ return node;
35
+ }
36
+
37
+ void packSoftNode(double* data, std::uint32_t index, const SoftNode& node) {
38
+ const std::uint32_t offset = index * kSoftNodeStride;
39
+ data[offset + 0] = node.position.x;
40
+ data[offset + 1] = node.position.y;
41
+ data[offset + 2] = node.position.z;
42
+ data[offset + 3] = node.previousPosition.x;
43
+ data[offset + 4] = node.previousPosition.y;
44
+ data[offset + 5] = node.previousPosition.z;
45
+ data[offset + 6] = node.velocity.x;
46
+ data[offset + 7] = node.velocity.y;
47
+ data[offset + 8] = node.velocity.z;
48
+ data[offset + 9] = node.inverseMass;
49
+ data[offset + 10] = node.radius;
50
+ data[offset + 11] = node.thickness;
51
+ data[offset + 12] = node.friction;
52
+ data[offset + 13] = node.restitution;
53
+ data[offset + 14] = static_cast<double>(node.flags);
54
+ data[offset + 15] = static_cast<double>(node.softBodyId);
55
+ }
56
+
57
+ SoftFace unpackSoftFace(const double* data, std::uint32_t index) {
58
+ const std::uint32_t offset = index * kSoftFaceStride;
59
+ SoftFace face;
60
+ face.nodeA = static_cast<std::uint32_t>(data[offset + 0]);
61
+ face.nodeB = static_cast<std::uint32_t>(data[offset + 1]);
62
+ face.nodeC = static_cast<std::uint32_t>(data[offset + 2]);
63
+ face.thickness = data[offset + 3];
64
+ face.softBodyId = static_cast<std::uint32_t>(data[offset + 4]);
65
+ face.flags = static_cast<std::uint32_t>(data[offset + 5]);
66
+ return face;
67
+ }
68
+
69
+ SoftBodyDef unpackSoftBodyDef(const double* data, std::uint32_t index) {
70
+ const std::uint32_t offset = index * kSoftBodyStride;
71
+ SoftBodyDef def;
72
+ def.softBodyId = static_cast<std::uint32_t>(data[offset + 0]);
73
+ def.layer = static_cast<std::uint32_t>(data[offset + 1]);
74
+ def.mask = static_cast<std::uint32_t>(data[offset + 2]);
75
+ def.selfPolicy = static_cast<SoftSelfCollisionPolicy>(static_cast<std::uint32_t>(data[offset + 3]));
76
+ def.policy = static_cast<SoftCollisionPolicy>(static_cast<std::uint32_t>(data[offset + 4]));
77
+ def.defaultThickness = data[offset + 5];
78
+ def.friction = data[offset + 6];
79
+ def.restitution = data[offset + 7];
80
+ def.isSensor = (data[offset + 8] != 0.0);
81
+ def.firstNode = static_cast<std::uint32_t>(data[offset + 9]);
82
+ def.nodeCount = static_cast<std::uint32_t>(data[offset + 10]);
83
+ def.firstFace = static_cast<std::uint32_t>(data[offset + 11]);
84
+ return def;
85
+ }
86
+
87
+ SoftCollisionParams unpackSoftCollisionParams(const double* data) {
88
+ SoftCollisionParams p;
89
+ p.enableGround = (data[0] != 0.0);
90
+ p.groundY = data[1];
91
+ p.groundFriction = data[2];
92
+ p.groundRestitution = data[3];
93
+ p.groundThickness = data[4];
94
+ p.restitutionThreshold = data[5];
95
+ p.positionDamping = data[6];
96
+ p.velocityDamping = data[7];
97
+ p.solverIterations = static_cast<std::uint32_t>(data[8]);
98
+ p.maxPenetrationCorrection = data[9];
99
+ p.selfCollisionExclusionHops = data[10];
100
+ p.twoWayRigidCoupling = data[11];
101
+ return p;
102
+ }
103
+
104
+ void packSoftRigidContact(double* data, std::uint32_t index, const SoftRigidContact& contact) {
105
+ const std::uint32_t offset = index * kSoftRigidContactStride;
106
+ data[offset + 0] = static_cast<double>(contact.softBodyId);
107
+ data[offset + 1] = static_cast<double>(contact.nodeIndex);
108
+ data[offset + 2] = static_cast<double>(contact.rigidBodyIndex);
109
+ data[offset + 3] = contact.point.x;
110
+ data[offset + 4] = contact.point.y;
111
+ data[offset + 5] = contact.point.z;
112
+ data[offset + 6] = contact.normal.x;
113
+ data[offset + 7] = contact.normal.y;
114
+ data[offset + 8] = contact.normal.z;
115
+ data[offset + 9] = contact.penetration;
116
+ data[offset + 10] = contact.normalImpulse;
117
+ data[offset + 11] = contact.frictionImpulse;
118
+ }
119
+
120
+ void packSoftSoftContact(double* data, std::uint32_t index, const SoftSoftContact& contact) {
121
+ const std::uint32_t offset = index * kSoftSoftContactStride;
122
+ data[offset + 0] = static_cast<double>(contact.softBodyIdA);
123
+ data[offset + 1] = static_cast<double>(contact.elementIndexA);
124
+ data[offset + 2] = static_cast<double>(contact.softBodyIdB);
125
+ data[offset + 3] = static_cast<double>(contact.elementIndexB);
126
+ data[offset + 4] = contact.isSelfCollision ? 1.0 : 0.0;
127
+ data[offset + 5] = contact.point.x;
128
+ data[offset + 6] = contact.point.y;
129
+ data[offset + 7] = contact.point.z;
130
+ data[offset + 8] = contact.normal.x;
131
+ data[offset + 9] = contact.normal.y;
132
+ data[offset + 10] = contact.normal.z;
133
+ data[offset + 11] = contact.penetration;
134
+ data[offset + 12] = contact.normalImpulse;
135
+ data[offset + 13] = contact.frictionImpulse;
136
+ }
137
+
138
+ void packSoftSensorEvent(double* data, std::uint32_t index, const SoftSensorEvent& evt) {
139
+ const std::uint32_t offset = index * kSoftSensorEventStride;
140
+ data[offset + 0] = static_cast<double>(evt.softBodyId);
141
+ data[offset + 1] = static_cast<double>(evt.nodeIndex);
142
+ data[offset + 2] = static_cast<double>(static_cast<std::uint32_t>(evt.otherKind));
143
+ data[offset + 3] = static_cast<double>(evt.otherId);
144
+ data[offset + 4] = evt.point.x;
145
+ data[offset + 5] = evt.point.y;
146
+ data[offset + 6] = evt.point.z;
147
+ data[offset + 7] = evt.normal.x;
148
+ data[offset + 8] = evt.normal.y;
149
+ data[offset + 9] = evt.normal.z;
150
+ }
151
+
152
+ Body unpackRigidBody(const double* bodyData, std::uint32_t b) {
153
+ const std::uint32_t offset = b * kBodyStride;
154
+ Body body;
155
+ body.x = bodyData[offset + 0];
156
+ body.y = bodyData[offset + 1];
157
+ body.z = bodyData[offset + 2];
158
+ body.vx = bodyData[offset + 3];
159
+ body.vy = bodyData[offset + 4];
160
+ body.vz = bodyData[offset + 5];
161
+
162
+ const std::uint32_t motionType = static_cast<std::uint32_t>(bodyData[offset + 9]);
163
+ const std::uint32_t kind = motionType & kMotionKindMask;
164
+ body.dynamic = (kind == 1);
165
+ body.kinematic = (kind == 2);
166
+ body.trigger = (motionType & kMotionTrigger) != 0;
167
+ body.sensor = (motionType & kMotionSensor) != 0;
168
+ body.sensorLike = body.trigger || body.sensor;
169
+ body.sleeping = body.dynamic && (motionType & kMotionSleeping) != 0;
170
+ body.angularState = (motionType & kMotionAngularState) != 0;
171
+
172
+ body.shape = static_cast<ShapeKind>(static_cast<std::uint32_t>(bodyData[offset + 10]));
173
+
174
+ // Collision layer and mask live at offsets 11 and 12. Skipping them left every
175
+ // rigid body on the default layer, so soft-rigid contacts ignored collision
176
+ // filtering entirely. Mirrors the canonical unpack in physics.cpp.
177
+ const double layerValue = bodyData[offset + 11];
178
+ const double maskValue = bodyData[offset + 12];
179
+ const auto validMask = [](double value) {
180
+ return std::isfinite(value) && value >= 0.0 && value <= 4294967295.0
181
+ && std::floor(value) == value;
182
+ };
183
+ body.masksValid = validMask(layerValue) && validMask(maskValue);
184
+ if (body.masksValid) {
185
+ body.layer = static_cast<std::uint32_t>(layerValue);
186
+ body.mask = static_cast<std::uint32_t>(maskValue);
187
+ } else {
188
+ body.layer = 1;
189
+ body.mask = 1;
190
+ }
191
+
192
+ body.gravityScale = bodyData[offset + 13];
193
+ body.linearDamping = bodyData[offset + 14];
194
+ body.mass = bodyData[offset + 18] > 0.0 ? bodyData[offset + 18] : 1.0;
195
+
196
+ body.angularVelX = bodyData[offset + 19];
197
+ body.angularVelY = bodyData[offset + 20];
198
+ body.angularVelZ = bodyData[offset + 21];
199
+
200
+ const double qx = bodyData[offset + 22];
201
+ const double qy = bodyData[offset + 23];
202
+ const double qz = bodyData[offset + 24];
203
+ const double qw = bodyData[offset + 25];
204
+ if (qx * qx + qy * qy + qz * qz + qw * qw > 1e-12) {
205
+ body.orientationX = qx;
206
+ body.orientationY = qy;
207
+ body.orientationZ = qz;
208
+ body.orientationW = qw;
209
+ } else {
210
+ body.orientationW = 1.0;
211
+ }
212
+
213
+ body.angularDamping = bodyData[offset + 26];
214
+ body.friction = bodyData[offset + 27];
215
+ body.restitution = bodyData[offset + 28];
216
+ body.motionLocks = static_cast<std::uint32_t>(bodyData[offset + 29]);
217
+ body.bullet = (bodyData[offset + 30] != 0.0);
218
+ body.inertiaXX = bodyData[offset + 31] > 0.0 ? bodyData[offset + 31] : 1.0;
219
+ body.inertiaYY = bodyData[offset + 32] > 0.0 ? bodyData[offset + 32] : 1.0;
220
+ body.inertiaZZ = bodyData[offset + 33] > 0.0 ? bodyData[offset + 33] : 1.0;
221
+ body.inertiaXY = bodyData[offset + 34];
222
+ body.inertiaXZ = bodyData[offset + 35];
223
+ body.inertiaYZ = bodyData[offset + 36];
224
+ body.centerOfMassX = bodyData[offset + 37];
225
+ body.centerOfMassY = bodyData[offset + 38];
226
+ body.centerOfMassZ = bodyData[offset + 39];
227
+ body.sleepThreshold = bodyData[offset + 40];
228
+ body.sleepEnabled = (bodyData[offset + 41] != 0.0);
229
+ body.sleepTime = bodyData[offset + 42];
230
+ body.rollingResistance = bodyData[offset + 43];
231
+ return body;
232
+ }
233
+
234
+ void packRigidBody(double* bodyData, std::uint32_t b, const Body& body) {
235
+ const std::uint32_t offset = b * kBodyStride;
236
+ bodyData[offset + 0] = body.x;
237
+ bodyData[offset + 1] = body.y;
238
+ bodyData[offset + 2] = body.z;
239
+ bodyData[offset + 3] = body.vx;
240
+ bodyData[offset + 4] = body.vy;
241
+ bodyData[offset + 5] = body.vz;
242
+ bodyData[offset + 19] = body.angularVelX;
243
+ bodyData[offset + 20] = body.angularVelY;
244
+ bodyData[offset + 21] = body.angularVelZ;
245
+ bodyData[offset + 15] = body.angularVelX;
246
+ bodyData[offset + 16] = body.angularVelY;
247
+ bodyData[offset + 17] = body.angularVelZ;
248
+ }
249
+
250
+ bool isPairExcluded(
251
+ std::uint32_t i,
252
+ std::uint32_t j,
253
+ std::span<const SoftExclusionPair> exclusions) {
254
+ for (std::size_t k = 0; k < exclusions.size(); ++k) {
255
+ const auto& ex = exclusions[k];
256
+ if ((ex.nodeA == i && ex.nodeB == j) || (ex.nodeA == j && ex.nodeB == i)) {
257
+ return true;
258
+ }
259
+ }
260
+ return false;
261
+ }
262
+
263
+ const SoftBodyDef* findBodyDef(std::uint32_t bodyId, std::span<const SoftBodyDef> bodyDefs) {
264
+ for (std::size_t b = 0; b < bodyDefs.size(); ++b) {
265
+ if (bodyDefs[b].softBodyId == bodyId) {
266
+ return &bodyDefs[b];
267
+ }
268
+ }
269
+ return nullptr;
270
+ }
271
+
272
+ } // namespace
273
+
274
+ // ---------------------------------------------------------------------------
275
+ // Geometry Utilities
276
+ // ---------------------------------------------------------------------------
277
+
278
+ SoftVec3 closestPointOnSegment(
279
+ const SoftVec3& p,
280
+ const SoftVec3& a,
281
+ const SoftVec3& b,
282
+ double& outT) {
283
+ const SoftVec3 ab = subSoftVec3(b, a);
284
+ const double abLenSq = lengthSquaredSoftVec3(ab);
285
+ if (abLenSq <= 1e-15) {
286
+ outT = 0.0;
287
+ return a;
288
+ }
289
+ const double t = clampDouble(dotSoftVec3(subSoftVec3(p, a), ab) / abLenSq, 0.0, 1.0);
290
+ outT = t;
291
+ return addSoftVec3(a, scaleSoftVec3(ab, t));
292
+ }
293
+
294
+ SoftVec3 closestPointOnTriangle(
295
+ const SoftVec3& p,
296
+ const SoftVec3& a,
297
+ const SoftVec3& b,
298
+ const SoftVec3& c,
299
+ double& outU,
300
+ double& outV,
301
+ double& outW) {
302
+ const SoftVec3 ab = subSoftVec3(b, a);
303
+ const SoftVec3 ac = subSoftVec3(c, a);
304
+ const SoftVec3 ap = subSoftVec3(p, a);
305
+
306
+ const double d1 = dotSoftVec3(ab, ap);
307
+ const double d2 = dotSoftVec3(ac, ap);
308
+ if (d1 <= 0.0 && d2 <= 0.0) {
309
+ outU = 1.0;
310
+ outV = 0.0;
311
+ outW = 0.0;
312
+ return a;
313
+ }
314
+
315
+ const SoftVec3 bp = subSoftVec3(p, b);
316
+ const double d3 = dotSoftVec3(ab, bp);
317
+ const double d4 = dotSoftVec3(ac, bp);
318
+ if (d3 >= 0.0 && d4 <= d3) {
319
+ outU = 0.0;
320
+ outV = 1.0;
321
+ outW = 0.0;
322
+ return b;
323
+ }
324
+
325
+ const double vc = d1 * d4 - d3 * d2;
326
+ if (vc <= 0.0 && d1 >= 0.0 && d3 <= 0.0) {
327
+ const double v = d1 / (d1 - d3);
328
+ outU = 1.0 - v;
329
+ outV = v;
330
+ outW = 0.0;
331
+ return addSoftVec3(a, scaleSoftVec3(ab, v));
332
+ }
333
+
334
+ const SoftVec3 cp = subSoftVec3(p, c);
335
+ const double d5 = dotSoftVec3(ab, cp);
336
+ const double d6 = dotSoftVec3(ac, cp);
337
+ if (d6 >= 0.0 && d5 <= d6) {
338
+ outU = 0.0;
339
+ outV = 0.0;
340
+ outW = 1.0;
341
+ return c;
342
+ }
343
+
344
+ const double vb = d5 * d2 - d1 * d6;
345
+ if (vb <= 0.0 && d2 >= 0.0 && d6 <= 0.0) {
346
+ const double w = d2 / (d2 - d6);
347
+ outU = 1.0 - w;
348
+ outV = 0.0;
349
+ outW = w;
350
+ return addSoftVec3(a, scaleSoftVec3(ac, w));
351
+ }
352
+
353
+ const double va = d3 * d6 - d5 * d4;
354
+ if (va <= 0.0 && (d4 - d3) >= 0.0 && (d5 - d6) >= 0.0) {
355
+ const double denom = (d4 - d3) + (d5 - d6);
356
+ const double w = (denom > 1e-15) ? ((d4 - d3) / denom) : 0.0;
357
+ outU = 0.0;
358
+ outV = 1.0 - w;
359
+ outW = w;
360
+ return addSoftVec3(b, scaleSoftVec3(subSoftVec3(c, b), w));
361
+ }
362
+
363
+ const double denom = va + vb + vc;
364
+ if (denom <= 1e-15) {
365
+ outU = 0.3333333333333333;
366
+ outV = 0.3333333333333333;
367
+ outW = 0.3333333333333333;
368
+ return scaleSoftVec3(addSoftVec3(addSoftVec3(a, b), c), 1.0 / 3.0);
369
+ }
370
+ const double invDenom = 1.0 / denom;
371
+ const double v = vb * invDenom;
372
+ const double w = vc * invDenom;
373
+ const double u = 1.0 - v - w;
374
+ outU = u;
375
+ outV = v;
376
+ outW = w;
377
+ return addSoftVec3(a, addSoftVec3(scaleSoftVec3(ab, v), scaleSoftVec3(ac, w)));
378
+ }
379
+
380
+ // ---------------------------------------------------------------------------
381
+ // Soft-Rigid Collision Detection & Two-Way Response
382
+ // ---------------------------------------------------------------------------
383
+
384
+ SoftCollisionStatus solveSoftRigid(
385
+ std::span<SoftNode> nodes,
386
+ std::span<const SoftBodyDef> bodyDefs,
387
+ std::span<Body> rigidBodies,
388
+ const SoftCollisionParams& params,
389
+ double dt,
390
+ std::span<SoftRigidContact> outContacts,
391
+ std::uint32_t& outContactCount,
392
+ std::span<SoftSensorEvent> outSensorEvents,
393
+ std::uint32_t& outSensorEventCount) {
394
+ outContactCount = 0;
395
+ outSensorEventCount = 0;
396
+
397
+ if (dt <= 0.0) {
398
+ return SoftCollisionStatus::InvalidParams;
399
+ }
400
+
401
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
402
+ SoftNode& node = nodes[i];
403
+ const SoftBodyDef* bDef = findBodyDef(node.softBodyId, bodyDefs);
404
+ if (bDef != nullptr && bDef->policy == SoftCollisionPolicy::IgnoreSoftRigid) {
405
+ continue;
406
+ }
407
+
408
+ const bool nodeIsSensor = (node.flags & 2u) != 0 || (bDef != nullptr && bDef->isSensor);
409
+
410
+ // 1. Ground plane collision: y >= groundY
411
+ if (params.enableGround) {
412
+ const double targetDist = node.radius + node.thickness + params.groundThickness;
413
+ const double floorY = params.groundY + targetDist;
414
+ if (node.position.y < floorY) {
415
+ const double penetration = floorY - node.position.y;
416
+ const SoftVec3 normal{0.0, 1.0, 0.0};
417
+ const SoftVec3 contactPoint{node.position.x, params.groundY, node.position.z};
418
+
419
+ if (nodeIsSensor) {
420
+ if (outSensorEventCount < outSensorEvents.size()) {
421
+ outSensorEvents[outSensorEventCount++] = {
422
+ .softBodyId = node.softBodyId,
423
+ .nodeIndex = static_cast<std::uint32_t>(i),
424
+ .otherKind = SoftTargetKind::Ground,
425
+ .otherId = 0,
426
+ .otherElementIndex = 0,
427
+ .point = contactPoint,
428
+ .normal = normal,
429
+ .penetration = penetration,
430
+ .relativeVelocity = node.velocity.y,
431
+ };
432
+ }
433
+ } else {
434
+ // Position penetration resolution
435
+ node.position.y = floorY;
436
+
437
+ // Normal velocity impulse
438
+ double normalImpulse = 0.0;
439
+ double frictionImpulse = 0.0;
440
+
441
+ if (node.inverseMass > 0.0) {
442
+ const double vn = node.velocity.y;
443
+ if (vn < 0.0) {
444
+ const double e = (std::abs(vn) < params.restitutionThreshold)
445
+ ? 0.0 : (params.groundRestitution * node.restitution);
446
+ const double jn = -(1.0 + e) * vn;
447
+ node.velocity.y += jn;
448
+ normalImpulse = jn / node.inverseMass;
449
+
450
+ // Ground friction
451
+ const double vHorizSq = node.velocity.x * node.velocity.x + node.velocity.z * node.velocity.z;
452
+ if (vHorizSq > 1e-16) {
453
+ const double vHoriz = std::sqrt(vHorizSq);
454
+ const double mu = params.groundFriction * node.friction;
455
+ const double maxF = mu * jn;
456
+ const double applied = std::min(maxF, vHoriz);
457
+ const double factor = (vHoriz - applied) / vHoriz;
458
+ node.velocity.x *= factor;
459
+ node.velocity.z *= factor;
460
+ frictionImpulse = applied / node.inverseMass;
461
+ }
462
+ }
463
+ }
464
+
465
+ if (outContactCount < outContacts.size()) {
466
+ outContacts[outContactCount++] = {
467
+ .softBodyId = node.softBodyId,
468
+ .nodeIndex = static_cast<std::uint32_t>(i),
469
+ .rigidBodyIndex = 0xffffffff,
470
+ .point = contactPoint,
471
+ .normal = normal,
472
+ .penetration = penetration,
473
+ .normalImpulse = normalImpulse,
474
+ .frictionImpulse = frictionImpulse,
475
+ .isSensor = false,
476
+ };
477
+ }
478
+ }
479
+ }
480
+ }
481
+
482
+ // 2. Rigid Body Collisions (Iterate strictly in index order)
483
+ for (std::size_t b = 0; b < rigidBodies.size(); ++b) {
484
+ Body& body = rigidBodies[b];
485
+
486
+ // Layer/mask filtering
487
+ const std::uint32_t nodeLayer = (bDef != nullptr) ? bDef->layer : 1u;
488
+ const std::uint32_t nodeMask = (bDef != nullptr) ? bDef->mask : 0xffffffffu;
489
+ if ((nodeMask & body.layer) == 0 || (body.mask & nodeLayer) == 0) {
490
+ continue;
491
+ }
492
+
493
+ const bool isSensorPair = nodeIsSensor || body.sensorLike || body.sensor || body.trigger;
494
+
495
+ // Transform soft node to rigid body local frame
496
+ const SoftVec3 bodyPos{body.x, body.y, body.z};
497
+ const SoftQuat bodyRot{body.orientationX, body.orientationY, body.orientationZ, body.orientationW};
498
+ const SoftVec3 shapeOffset{body.shapeOffsetX, body.shapeOffsetY, body.shapeOffsetZ};
499
+
500
+ const SoftVec3 pRel = subSoftVec3(node.position, bodyPos);
501
+ const SoftVec3 pLoc = subSoftVec3(invRotateByQuat(bodyRot, pRel), shapeOffset);
502
+
503
+ SoftVec3 normLoc{0.0, 1.0, 0.0};
504
+ SoftVec3 cLoc{0.0, 0.0, 0.0};
505
+ double dist = 0.0;
506
+
507
+ if (body.shape == ShapeKind::Box) {
508
+ const double hx = body.shape0 > 0.0 ? body.shape0 : 0.5;
509
+ const double hy = body.shape1 > 0.0 ? body.shape1 : 0.5;
510
+ const double hz = body.shape2 > 0.0 ? body.shape2 : 0.5;
511
+
512
+ const double cx = clampDouble(pLoc.x, -hx, hx);
513
+ const double cy = clampDouble(pLoc.y, -hy, hy);
514
+ const double cz = clampDouble(pLoc.z, -hz, hz);
515
+ cLoc = {cx, cy, cz};
516
+
517
+ const SoftVec3 diffLoc = subSoftVec3(pLoc, cLoc);
518
+ const double distSq = lengthSquaredSoftVec3(diffLoc);
519
+
520
+ if (distSq < 1e-16) {
521
+ // Inside the box: push out to closest face
522
+ const double dx = hx - std::abs(pLoc.x);
523
+ const double dy = hy - std::abs(pLoc.y);
524
+ const double dz = hz - std::abs(pLoc.z);
525
+ if (dx <= dy && dx <= dz) {
526
+ normLoc = {pLoc.x >= 0.0 ? 1.0 : -1.0, 0.0, 0.0};
527
+ dist = -dx;
528
+ cLoc.x = pLoc.x >= 0.0 ? hx : -hx;
529
+ } else if (dy <= dx && dy <= dz) {
530
+ normLoc = {0.0, pLoc.y >= 0.0 ? 1.0 : -1.0, 0.0};
531
+ dist = -dy;
532
+ cLoc.y = pLoc.y >= 0.0 ? hy : -hy;
533
+ } else {
534
+ normLoc = {0.0, 0.0, pLoc.z >= 0.0 ? 1.0 : -1.0};
535
+ dist = -dz;
536
+ cLoc.z = pLoc.z >= 0.0 ? hz : -hz;
537
+ }
538
+ } else {
539
+ dist = std::sqrt(distSq);
540
+ normLoc = scaleSoftVec3(diffLoc, 1.0 / dist);
541
+ }
542
+ } else if (body.shape == ShapeKind::Sphere) {
543
+ const double radius = body.shape0 > 0.0 ? body.shape0 : 0.5;
544
+ const double distSq = lengthSquaredSoftVec3(pLoc);
545
+ if (distSq > 1e-16) {
546
+ dist = std::sqrt(distSq);
547
+ normLoc = scaleSoftVec3(pLoc, 1.0 / dist);
548
+ cLoc = scaleSoftVec3(normLoc, radius);
549
+ } else {
550
+ dist = 0.0;
551
+ normLoc = {0.0, 1.0, 0.0};
552
+ cLoc = {0.0, radius, 0.0};
553
+ }
554
+ dist -= radius;
555
+ } else if (body.shape == ShapeKind::Capsule) {
556
+ const double radius = body.shape0 > 0.0 ? body.shape0 : 0.5;
557
+ const double hh = body.shape1 > 0.0 ? body.shape1 : 0.5;
558
+ const double cy = clampDouble(pLoc.y, -hh, hh);
559
+ const SoftVec3 segLoc{0.0, cy, 0.0};
560
+ const SoftVec3 diffLoc = subSoftVec3(pLoc, segLoc);
561
+ const double distSq = lengthSquaredSoftVec3(diffLoc);
562
+ if (distSq > 1e-16) {
563
+ const double d = std::sqrt(distSq);
564
+ normLoc = scaleSoftVec3(diffLoc, 1.0 / d);
565
+ cLoc = addSoftVec3(segLoc, scaleSoftVec3(normLoc, radius));
566
+ dist = d - radius;
567
+ } else {
568
+ normLoc = {0.0, 1.0, 0.0};
569
+ cLoc = addSoftVec3(segLoc, {0.0, radius, 0.0});
570
+ dist = -radius;
571
+ }
572
+ } else if (body.shape == ShapeKind::Cylinder) {
573
+ const double radius = body.shape0 > 0.0 ? body.shape0 : 0.5;
574
+ const double hh = body.shape1 > 0.0 ? body.shape1 : 0.5;
575
+ const double r = std::sqrt(pLoc.x * pLoc.x + pLoc.z * pLoc.z);
576
+ const double cy = clampDouble(pLoc.y, -hh, hh);
577
+
578
+ if (r <= radius && std::abs(pLoc.y) <= hh) {
579
+ // Inside cylinder
580
+ const double dr = radius - r;
581
+ const double dy = hh - std::abs(pLoc.y);
582
+ if (dy < dr) {
583
+ normLoc = {0.0, pLoc.y >= 0.0 ? 1.0 : -1.0, 0.0};
584
+ dist = -dy;
585
+ cLoc = {pLoc.x, pLoc.y >= 0.0 ? hh : -hh, pLoc.z};
586
+ } else {
587
+ normLoc = r > 1e-12 ? SoftVec3{pLoc.x / r, 0.0, pLoc.z / r} : SoftVec3{1.0, 0.0, 0.0};
588
+ dist = -dr;
589
+ cLoc = {normLoc.x * radius, pLoc.y, normLoc.z * radius};
590
+ }
591
+ } else {
592
+ // Outside cylinder
593
+ const double clampedR = r > 1e-12 ? (radius / std::max(r, radius)) : 0.0;
594
+ cLoc = {pLoc.x * clampedR, cy, pLoc.z * clampedR};
595
+ const SoftVec3 diffLoc = subSoftVec3(pLoc, cLoc);
596
+ const double d = lengthSoftVec3(diffLoc);
597
+ normLoc = d > 1e-12 ? scaleSoftVec3(diffLoc, 1.0 / d) : SoftVec3{0.0, 1.0, 0.0};
598
+ dist = d;
599
+ }
600
+ } else {
601
+ // Fallback bounding sphere
602
+ const double radius = body.shape0 > 0.0 ? body.shape0 : 0.5;
603
+ const double d = lengthSoftVec3(pLoc);
604
+ normLoc = d > 1e-12 ? scaleSoftVec3(pLoc, 1.0 / d) : SoftVec3{0.0, 1.0, 0.0};
605
+ cLoc = scaleSoftVec3(normLoc, radius);
606
+ dist = d - radius;
607
+ }
608
+
609
+ // Check contact against soft node radius and thickness
610
+ const double targetDist = node.radius + node.thickness;
611
+ if (dist < targetDist) {
612
+ const double penetration = targetDist - dist;
613
+ const SoftVec3 normalWorld = rotateByQuat(bodyRot, normLoc);
614
+ const SoftVec3 contactWorld = addSoftVec3(bodyPos, rotateByQuat(bodyRot, addSoftVec3(cLoc, shapeOffset)));
615
+
616
+ // Arm from rigid body center of mass
617
+ const SoftVec3 comWorld{body.centerOfMassX, body.centerOfMassY, body.centerOfMassZ};
618
+ const SoftVec3 arm = subSoftVec3(contactWorld, comWorld);
619
+
620
+ // Rigid body linear and angular velocities
621
+ const SoftVec3 bodyLinearVel{body.vx, body.vy, body.vz};
622
+ const SoftVec3 bodyAngVel{body.angularVelX, body.angularVelY, body.angularVelZ};
623
+ const SoftVec3 vBodyContact = addSoftVec3(bodyLinearVel, crossSoftVec3(bodyAngVel, arm));
624
+
625
+ // Relative velocity
626
+ const SoftVec3 vRel = subSoftVec3(node.velocity, vBodyContact);
627
+ const double vn = dotSoftVec3(vRel, normalWorld);
628
+
629
+ if (isSensorPair) {
630
+ if (outSensorEventCount < outSensorEvents.size()) {
631
+ outSensorEvents[outSensorEventCount++] = {
632
+ .softBodyId = node.softBodyId,
633
+ .nodeIndex = static_cast<std::uint32_t>(i),
634
+ .otherKind = SoftTargetKind::RigidBody,
635
+ .otherId = static_cast<std::uint32_t>(b),
636
+ .otherElementIndex = 0,
637
+ .point = contactWorld,
638
+ .normal = normalWorld,
639
+ .penetration = penetration,
640
+ .relativeVelocity = vn,
641
+ };
642
+ }
643
+ continue;
644
+ }
645
+
646
+ // Rigorous Two-Way Rigid Dynamics Response
647
+ const ConstraintBodyMobility mobility = constraintBodyMobility(body);
648
+ const double wrx = mobility.inverseLinear[0];
649
+ const double wry = mobility.inverseLinear[1];
650
+ const double wrz = mobility.inverseLinear[2];
651
+ const double wnLinear = normalWorld.x * normalWorld.x * wrx
652
+ + normalWorld.y * normalWorld.y * wry
653
+ + normalWorld.z * normalWorld.z * wrz;
654
+
655
+ // Angular mobility along normal: u_n = arm x normal
656
+ const SoftVec3 un = crossSoftVec3(arm, normalWorld);
657
+ const SoftVec3 an{
658
+ mobility.inverseAngular[0] * un.x + mobility.inverseAngular[1] * un.y + mobility.inverseAngular[2] * un.z,
659
+ mobility.inverseAngular[1] * un.x + mobility.inverseAngular[3] * un.y + mobility.inverseAngular[4] * un.z,
660
+ mobility.inverseAngular[2] * un.x + mobility.inverseAngular[4] * un.y + mobility.inverseAngular[5] * un.z,
661
+ };
662
+ const double wnRot = dotSoftVec3(un, an);
663
+
664
+ const double wnRigid = (params.twoWayRigidCoupling != 0.0) ? (wnLinear + wnRot) : 0.0;
665
+ const double wnSoft = node.inverseMass;
666
+ const double wnTotal = wnSoft + wnRigid;
667
+
668
+ if (wnTotal <= 1e-14) {
669
+ continue;
670
+ }
671
+
672
+ // Position penetration correction
673
+ const double corr = std::min(penetration, params.maxPenetrationCorrection);
674
+ node.position = addSoftVec3(node.position, scaleSoftVec3(normalWorld, corr * (wnSoft / wnTotal)));
675
+ if (body.dynamic && params.twoWayRigidCoupling != 0.0 && wnLinear > 0.0) {
676
+ const double rigidCorr = corr * (wnLinear / wnTotal);
677
+ body.x -= normalWorld.x * rigidCorr;
678
+ body.y -= normalWorld.y * rigidCorr;
679
+ body.z -= normalWorld.z * rigidCorr;
680
+ }
681
+
682
+ // Normal velocity impulse
683
+ const double e = (std::abs(vn) < params.restitutionThreshold) ? 0.0 : (body.restitution * node.restitution);
684
+ double jn = 0.0;
685
+ if (vn < 0.0) {
686
+ jn = -(1.0 + e) * vn / wnTotal;
687
+ } else if (penetration > 0.0) {
688
+ jn = (penetration / dt) * 0.1 / wnTotal;
689
+ }
690
+
691
+ double normalImpulse = 0.0;
692
+ double frictionImpulse = 0.0;
693
+
694
+ if (jn > 0.0) {
695
+ normalImpulse = jn;
696
+ // Apply normal impulse two-way
697
+ node.velocity = addSoftVec3(node.velocity, scaleSoftVec3(normalWorld, jn * wnSoft));
698
+ if (body.dynamic && params.twoWayRigidCoupling != 0.0) {
699
+ body.vx -= normalWorld.x * jn * wrx;
700
+ body.vy -= normalWorld.y * jn * wry;
701
+ body.vz -= normalWorld.z * jn * wrz;
702
+ body.angularVelX -= an.x * jn;
703
+ body.angularVelY -= an.y * jn;
704
+ body.angularVelZ -= an.z * jn;
705
+ }
706
+
707
+ // Tangential friction (Two-Way!)
708
+ const SoftVec3 vBodyNew{
709
+ body.vx + (body.angularVelY * arm.z - body.angularVelZ * arm.y),
710
+ body.vy + (body.angularVelZ * arm.x - body.angularVelX * arm.z),
711
+ body.vz + (body.angularVelX * arm.y - body.angularVelY * arm.x),
712
+ };
713
+ const SoftVec3 vRelNew = subSoftVec3(node.velocity, vBodyNew);
714
+ const SoftVec3 vt = subSoftVec3(vRelNew, scaleSoftVec3(normalWorld, dotSoftVec3(vRelNew, normalWorld)));
715
+ const double vtLen = lengthSoftVec3(vt);
716
+
717
+ if (vtLen > 1e-12) {
718
+ const SoftVec3 t = scaleSoftVec3(vt, 1.0 / vtLen);
719
+ const SoftVec3 ut = crossSoftVec3(arm, t);
720
+ const SoftVec3 at{
721
+ mobility.inverseAngular[0] * ut.x + mobility.inverseAngular[1] * ut.y + mobility.inverseAngular[2] * ut.z,
722
+ mobility.inverseAngular[1] * ut.x + mobility.inverseAngular[3] * ut.y + mobility.inverseAngular[4] * ut.z,
723
+ mobility.inverseAngular[2] * ut.x + mobility.inverseAngular[4] * ut.y + mobility.inverseAngular[5] * ut.z,
724
+ };
725
+ const double wtRot = dotSoftVec3(ut, at);
726
+ const double wtLinear = t.x * t.x * wrx + t.y * t.y * wry + t.z * t.z * wrz;
727
+ const double wtRigid = (params.twoWayRigidCoupling != 0.0) ? (wtLinear + wtRot) : 0.0;
728
+ const double wtTotal = wnSoft + wtRigid;
729
+
730
+ if (wtTotal > 1e-14) {
731
+ const double mu = body.friction * node.friction;
732
+ const double maxF = mu * jn;
733
+ const double jt = std::min(maxF, vtLen / wtTotal);
734
+ frictionImpulse = jt;
735
+
736
+ node.velocity = subSoftVec3(node.velocity, scaleSoftVec3(t, jt * wnSoft));
737
+ if (body.dynamic && params.twoWayRigidCoupling != 0.0) {
738
+ body.vx += t.x * jt * wrx;
739
+ body.vy += t.y * jt * wry;
740
+ body.vz += t.z * jt * wrz;
741
+ body.angularVelX += at.x * jt;
742
+ body.angularVelY += at.y * jt;
743
+ body.angularVelZ += at.z * jt;
744
+ }
745
+ }
746
+ }
747
+ }
748
+
749
+ if (outContactCount < outContacts.size()) {
750
+ outContacts[outContactCount++] = {
751
+ .softBodyId = node.softBodyId,
752
+ .nodeIndex = static_cast<std::uint32_t>(i),
753
+ .rigidBodyIndex = static_cast<std::uint32_t>(b),
754
+ .point = contactWorld,
755
+ .normal = normalWorld,
756
+ .penetration = penetration,
757
+ .normalImpulse = normalImpulse,
758
+ .frictionImpulse = frictionImpulse,
759
+ .isSensor = false,
760
+ };
761
+ }
762
+ }
763
+ }
764
+ }
765
+
766
+ return SoftCollisionStatus::Success;
767
+ }
768
+
769
+ // ---------------------------------------------------------------------------
770
+ // Soft-Soft and Self Collision Detection & Resolution
771
+ // ---------------------------------------------------------------------------
772
+
773
+ SoftCollisionStatus solveSoftSoft(
774
+ std::span<SoftNode> nodes,
775
+ std::span<const SoftFace> faces,
776
+ std::span<const SoftBodyDef> bodyDefs,
777
+ std::span<const SoftExclusionPair> exclusions,
778
+ const SoftCollisionParams& params,
779
+ double dt,
780
+ std::span<SoftSoftContact> outContacts,
781
+ std::uint32_t& outContactCount,
782
+ std::span<SoftSensorEvent> outSensorEvents,
783
+ std::uint32_t& outSensorEventCount) {
784
+ outContactCount = 0;
785
+ outSensorEventCount = 0;
786
+
787
+ if (dt <= 0.0) {
788
+ return SoftCollisionStatus::InvalidParams;
789
+ }
790
+
791
+ const std::size_t nodeCount = nodes.size();
792
+
793
+ // 1. Node-Node Collision (Self and Cross-Soft)
794
+ for (std::size_t i = 0; i < nodeCount; ++i) {
795
+ SoftNode& nodeA = nodes[i];
796
+ const SoftBodyDef* defA = findBodyDef(nodeA.softBodyId, bodyDefs);
797
+
798
+ for (std::size_t j = i + 1; j < nodeCount; ++j) {
799
+ SoftNode& nodeB = nodes[j];
800
+ const bool isSameBody = (nodeA.softBodyId == nodeB.softBodyId);
801
+
802
+ // Filtering
803
+ if (isSameBody) {
804
+ if (defA != nullptr && defA->selfPolicy == SoftSelfCollisionPolicy::Disabled) {
805
+ continue;
806
+ }
807
+ if (isPairExcluded(static_cast<std::uint32_t>(i), static_cast<std::uint32_t>(j), exclusions)) {
808
+ continue;
809
+ }
810
+ } else {
811
+ const SoftBodyDef* defB = findBodyDef(nodeB.softBodyId, bodyDefs);
812
+ if (defA != nullptr && defA->policy == SoftCollisionPolicy::IgnoreSoftSoft) continue;
813
+ if (defB != nullptr && defB->policy == SoftCollisionPolicy::IgnoreSoftSoft) continue;
814
+
815
+ const std::uint32_t layerA = defA != nullptr ? defA->layer : 1u;
816
+ const std::uint32_t maskA = defA != nullptr ? defA->mask : 0xffffffffu;
817
+ const std::uint32_t layerB = defB != nullptr ? defB->layer : 1u;
818
+ const std::uint32_t maskB = defB != nullptr ? defB->mask : 0xffffffffu;
819
+ if ((maskA & layerB) == 0 || (maskB & layerA) == 0) {
820
+ continue;
821
+ }
822
+ }
823
+
824
+ const double targetDist = nodeA.radius + nodeA.thickness + nodeB.radius + nodeB.thickness;
825
+ const SoftVec3 diff = subSoftVec3(nodeA.position, nodeB.position);
826
+ const double distSq = lengthSquaredSoftVec3(diff);
827
+
828
+ if (distSq < targetDist * targetDist) {
829
+ const double dist = std::sqrt(distSq);
830
+ const SoftVec3 normal = dist > 1e-12 ? scaleSoftVec3(diff, 1.0 / dist) : SoftVec3{0.0, 1.0, 0.0};
831
+ const double penetration = targetDist - dist;
832
+ const SoftVec3 contactPoint = addSoftVec3(nodeB.position, scaleSoftVec3(normal, nodeB.radius + nodeB.thickness));
833
+
834
+ const bool isSensor = (nodeA.flags & 2u) != 0 || (nodeB.flags & 2u) != 0
835
+ || (defA != nullptr && defA->isSensor);
836
+
837
+ const SoftVec3 vRel = subSoftVec3(nodeA.velocity, nodeB.velocity);
838
+ const double vn = dotSoftVec3(vRel, normal);
839
+
840
+ if (isSensor) {
841
+ if (outSensorEventCount < outSensorEvents.size()) {
842
+ outSensorEvents[outSensorEventCount++] = {
843
+ .softBodyId = nodeA.softBodyId,
844
+ .nodeIndex = static_cast<std::uint32_t>(i),
845
+ .otherKind = SoftTargetKind::SoftBody,
846
+ .otherId = nodeB.softBodyId,
847
+ .otherElementIndex = static_cast<std::uint32_t>(j),
848
+ .point = contactPoint,
849
+ .normal = normal,
850
+ .penetration = penetration,
851
+ .relativeVelocity = vn,
852
+ };
853
+ }
854
+ continue;
855
+ }
856
+
857
+ const double wTotal = nodeA.inverseMass + nodeB.inverseMass;
858
+ if (wTotal <= 1e-14) {
859
+ continue;
860
+ }
861
+
862
+ // Position resolution
863
+ const double corr = std::min(penetration, params.maxPenetrationCorrection);
864
+ nodeA.position = addSoftVec3(nodeA.position, scaleSoftVec3(normal, corr * (nodeA.inverseMass / wTotal)));
865
+ nodeB.position = subSoftVec3(nodeB.position, scaleSoftVec3(normal, corr * (nodeB.inverseMass / wTotal)));
866
+
867
+ // Normal velocity impulse
868
+ const double e = (std::abs(vn) < params.restitutionThreshold)
869
+ ? 0.0 : (nodeA.restitution * nodeB.restitution);
870
+ double jn = 0.0;
871
+ if (vn < 0.0) {
872
+ jn = -(1.0 + e) * vn / wTotal;
873
+ } else if (penetration > 0.0) {
874
+ jn = (penetration / dt) * 0.1 / wTotal;
875
+ }
876
+
877
+ double normalImpulse = 0.0;
878
+ double frictionImpulse = 0.0;
879
+
880
+ if (jn > 0.0) {
881
+ normalImpulse = jn;
882
+ nodeA.velocity = addSoftVec3(nodeA.velocity, scaleSoftVec3(normal, jn * nodeA.inverseMass));
883
+ nodeB.velocity = subSoftVec3(nodeB.velocity, scaleSoftVec3(normal, jn * nodeB.inverseMass));
884
+
885
+ // Tangential friction
886
+ const SoftVec3 vRelNew = subSoftVec3(nodeA.velocity, nodeB.velocity);
887
+ const SoftVec3 vt = subSoftVec3(vRelNew, scaleSoftVec3(normal, dotSoftVec3(vRelNew, normal)));
888
+ const double vtLen = lengthSoftVec3(vt);
889
+ if (vtLen > 1e-12) {
890
+ const SoftVec3 t = scaleSoftVec3(vt, 1.0 / vtLen);
891
+ const double mu = nodeA.friction * nodeB.friction;
892
+ const double maxF = mu * jn;
893
+ const double jt = std::min(maxF, vtLen / wTotal);
894
+ frictionImpulse = jt;
895
+
896
+ nodeA.velocity = subSoftVec3(nodeA.velocity, scaleSoftVec3(t, jt * nodeA.inverseMass));
897
+ nodeB.velocity = addSoftVec3(nodeB.velocity, scaleSoftVec3(t, jt * nodeB.inverseMass));
898
+ }
899
+ }
900
+
901
+ if (outContactCount < outContacts.size()) {
902
+ outContacts[outContactCount++] = {
903
+ .softBodyIdA = nodeA.softBodyId,
904
+ .elementIndexA = static_cast<std::uint32_t>(i),
905
+ .softBodyIdB = nodeB.softBodyId,
906
+ .elementIndexB = static_cast<std::uint32_t>(j),
907
+ .isSelfCollision = isSameBody,
908
+ .point = contactPoint,
909
+ .normal = normal,
910
+ .penetration = penetration,
911
+ .normalImpulse = normalImpulse,
912
+ .frictionImpulse = frictionImpulse,
913
+ .isSensor = false,
914
+ };
915
+ }
916
+ }
917
+ }
918
+ }
919
+
920
+ // 2. Node-Face Collision (Vertex-Face Proximity and Repulsion)
921
+ for (std::size_t f = 0; f < faces.size(); ++f) {
922
+ const SoftFace& face = faces[f];
923
+ if (face.nodeA >= nodeCount || face.nodeB >= nodeCount || face.nodeC >= nodeCount) {
924
+ continue;
925
+ }
926
+ SoftNode& nA = nodes[face.nodeA];
927
+ SoftNode& nB = nodes[face.nodeB];
928
+ SoftNode& nC = nodes[face.nodeC];
929
+
930
+ for (std::size_t i = 0; i < nodeCount; ++i) {
931
+ // Don't collide face with its own constituent vertices
932
+ if (i == face.nodeA || i == face.nodeB || i == face.nodeC) {
933
+ continue;
934
+ }
935
+ SoftNode& nodeP = nodes[i];
936
+ const bool isSameBody = (nodeP.softBodyId == face.softBodyId);
937
+
938
+ // Check self-collision policy
939
+ if (isSameBody) {
940
+ const SoftBodyDef* def = findBodyDef(face.softBodyId, bodyDefs);
941
+ if (def != nullptr && (def->selfPolicy != SoftSelfCollisionPolicy::VertexFace &&
942
+ def->selfPolicy != SoftSelfCollisionPolicy::Full)) {
943
+ continue;
944
+ }
945
+ }
946
+
947
+ double u = 0.0, v = 0.0, w = 0.0;
948
+ const SoftVec3 closestOnTri = closestPointOnTriangle(nodeP.position, nA.position, nB.position, nC.position, u, v, w);
949
+ const SoftVec3 diff = subSoftVec3(nodeP.position, closestOnTri);
950
+ const double distSq = lengthSquaredSoftVec3(diff);
951
+ const double targetDist = nodeP.radius + nodeP.thickness + face.thickness;
952
+
953
+ if (distSq < targetDist * targetDist) {
954
+ const double dist = std::sqrt(distSq);
955
+ const SoftVec3 normal = dist > 1e-12 ? scaleSoftVec3(diff, 1.0 / dist) : SoftVec3{0.0, 1.0, 0.0};
956
+ const double penetration = targetDist - dist;
957
+
958
+ const SoftVec3 vTri = addSoftVec3(scaleSoftVec3(nA.velocity, u),
959
+ addSoftVec3(scaleSoftVec3(nB.velocity, v),
960
+ scaleSoftVec3(nC.velocity, w)));
961
+ const SoftVec3 vRel = subSoftVec3(nodeP.velocity, vTri);
962
+ const double vn = dotSoftVec3(vRel, normal);
963
+
964
+ // Face virtual inverse mass
965
+ const double wTri = u * u * nA.inverseMass + v * v * nB.inverseMass + w * w * nC.inverseMass;
966
+ const double wTotal = nodeP.inverseMass + wTri;
967
+
968
+ if (wTotal <= 1e-14) {
969
+ continue;
970
+ }
971
+
972
+ // Position resolution
973
+ const double corr = std::min(penetration, params.maxPenetrationCorrection);
974
+ nodeP.position = addSoftVec3(nodeP.position, scaleSoftVec3(normal, corr * (nodeP.inverseMass / wTotal)));
975
+ const SoftVec3 triCorr = scaleSoftVec3(normal, -corr / wTotal);
976
+ nA.position = addSoftVec3(nA.position, scaleSoftVec3(triCorr, u * nA.inverseMass));
977
+ nB.position = addSoftVec3(nB.position, scaleSoftVec3(triCorr, v * nB.inverseMass));
978
+ nC.position = addSoftVec3(nC.position, scaleSoftVec3(triCorr, w * nC.inverseMass));
979
+
980
+ // Velocity impulse
981
+ if (vn < 0.0) {
982
+ const double jn = -vn / wTotal;
983
+ nodeP.velocity = addSoftVec3(nodeP.velocity, scaleSoftVec3(normal, jn * nodeP.inverseMass));
984
+ const SoftVec3 triImp = scaleSoftVec3(normal, -jn);
985
+ nA.velocity = addSoftVec3(nA.velocity, scaleSoftVec3(triImp, u * nA.inverseMass));
986
+ nB.velocity = addSoftVec3(nB.velocity, scaleSoftVec3(triImp, v * nB.inverseMass));
987
+ nC.velocity = addSoftVec3(nC.velocity, scaleSoftVec3(triImp, w * nC.inverseMass));
988
+ }
989
+
990
+ if (outContactCount < outContacts.size()) {
991
+ outContacts[outContactCount++] = {
992
+ .softBodyIdA = nodeP.softBodyId,
993
+ .elementIndexA = static_cast<std::uint32_t>(i),
994
+ .softBodyIdB = face.softBodyId,
995
+ .elementIndexB = static_cast<std::uint32_t>(f),
996
+ .isSelfCollision = isSameBody,
997
+ .point = closestOnTri,
998
+ .normal = normal,
999
+ .penetration = penetration,
1000
+ .normalImpulse = 0.0,
1001
+ .frictionImpulse = 0.0,
1002
+ .isSensor = false,
1003
+ };
1004
+ }
1005
+ }
1006
+ }
1007
+ }
1008
+
1009
+ return SoftCollisionStatus::Success;
1010
+ }
1011
+
1012
+ // ---------------------------------------------------------------------------
1013
+ // Combined Step Function
1014
+ // ---------------------------------------------------------------------------
1015
+
1016
+ SoftCollisionStatus stepSoftCollision(
1017
+ std::span<SoftNode> nodes,
1018
+ std::span<const SoftFace> faces,
1019
+ std::span<const SoftBodyDef> bodyDefs,
1020
+ std::span<const SoftExclusionPair> exclusions,
1021
+ std::span<Body> rigidBodies,
1022
+ const SoftCollisionParams& params,
1023
+ double dt,
1024
+ std::span<SoftRigidContact> outRigidContacts,
1025
+ std::uint32_t& outRigidContactCount,
1026
+ std::span<SoftSoftContact> outSoftContacts,
1027
+ std::uint32_t& outSoftContactCount,
1028
+ std::span<SoftSensorEvent> outSensorEvents,
1029
+ std::uint32_t& outSensorEventCount) {
1030
+ outRigidContactCount = 0;
1031
+ outSoftContactCount = 0;
1032
+ outSensorEventCount = 0;
1033
+
1034
+ const std::uint32_t iters = std::max(1u, params.solverIterations);
1035
+ const double subDt = dt / static_cast<double>(iters);
1036
+
1037
+ for (std::uint32_t it = 0; it < iters; ++it) {
1038
+ std::uint32_t subRigidContacts = 0;
1039
+ std::uint32_t subSensorEvents1 = 0;
1040
+ solveSoftRigid(
1041
+ nodes,
1042
+ bodyDefs,
1043
+ rigidBodies,
1044
+ params,
1045
+ subDt,
1046
+ outRigidContacts.subspan(outRigidContactCount),
1047
+ subRigidContacts,
1048
+ outSensorEvents.subspan(outSensorEventCount),
1049
+ subSensorEvents1);
1050
+ outRigidContactCount += subRigidContacts;
1051
+ outSensorEventCount += subSensorEvents1;
1052
+
1053
+ std::uint32_t subSoftContacts = 0;
1054
+ std::uint32_t subSensorEvents2 = 0;
1055
+ solveSoftSoft(
1056
+ nodes,
1057
+ faces,
1058
+ bodyDefs,
1059
+ exclusions,
1060
+ params,
1061
+ subDt,
1062
+ outSoftContacts.subspan(outSoftContactCount),
1063
+ subSoftContacts,
1064
+ outSensorEvents.subspan(outSensorEventCount),
1065
+ subSensorEvents2);
1066
+ outSoftContactCount += subSoftContacts;
1067
+ outSensorEventCount += subSensorEvents2;
1068
+ }
1069
+
1070
+ return SoftCollisionStatus::Success;
1071
+ }
1072
+
1073
+ // ---------------------------------------------------------------------------
1074
+ // Queries
1075
+ // ---------------------------------------------------------------------------
1076
+
1077
+ SoftRayHit querySoftRaycast(
1078
+ std::span<const SoftNode> nodes,
1079
+ std::span<const SoftFace> faces,
1080
+ std::span<const SoftBodyDef> bodyDefs,
1081
+ const SoftRay& ray) {
1082
+ SoftRayHit bestHit;
1083
+ bestHit.hit = false;
1084
+ bestHit.distance = ray.maxDistance;
1085
+
1086
+ const double rayDirLenSq = lengthSquaredSoftVec3(ray.direction);
1087
+ if (rayDirLenSq <= 1e-15) {
1088
+ return bestHit;
1089
+ }
1090
+ const SoftVec3 rDir = normalizeSoftVec3(ray.direction);
1091
+
1092
+ // 1. Ray vs Nodes (as bounding spheres with radius + thickness + ray.radius)
1093
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
1094
+ const SoftNode& node = nodes[i];
1095
+ const SoftBodyDef* def = findBodyDef(node.softBodyId, bodyDefs);
1096
+ if (def != nullptr && (def->layer & ray.mask) == 0) {
1097
+ continue;
1098
+ }
1099
+
1100
+ const double R = node.radius + node.thickness + ray.radius;
1101
+ const SoftVec3 m = subSoftVec3(ray.origin, node.position);
1102
+ const double b = dotSoftVec3(m, rDir);
1103
+ const double c = lengthSquaredSoftVec3(m) - R * R;
1104
+
1105
+ if (c > 0.0 && b > 0.0) continue;
1106
+ const double discr = b * b - c;
1107
+ if (discr < 0.0) continue;
1108
+
1109
+ double t = -b - std::sqrt(discr);
1110
+ if (t < 0.0) t = 0.0; // Origin inside sphere
1111
+
1112
+ if (t <= bestHit.distance) {
1113
+ bestHit.hit = true;
1114
+ bestHit.distance = t;
1115
+ bestHit.point = addSoftVec3(ray.origin, scaleSoftVec3(rDir, t));
1116
+ const SoftVec3 norm = subSoftVec3(bestHit.point, node.position);
1117
+ bestHit.normal = lengthSquaredSoftVec3(norm) > 1e-12 ? normalizeSoftVec3(norm) : scaleSoftVec3(rDir, -1.0);
1118
+ bestHit.softBodyId = node.softBodyId;
1119
+ bestHit.elementIndex = static_cast<std::uint32_t>(i);
1120
+ bestHit.primitiveKind = SoftPrimitiveKind::Node;
1121
+ }
1122
+ }
1123
+
1124
+ // 2. Ray vs Faces (Triangles with thickness)
1125
+ for (std::size_t f = 0; f < faces.size(); ++f) {
1126
+ const SoftFace& face = faces[f];
1127
+ if (face.nodeA >= nodes.size() || face.nodeB >= nodes.size() || face.nodeC >= nodes.size()) {
1128
+ continue;
1129
+ }
1130
+ const SoftNode& nA = nodes[face.nodeA];
1131
+ const SoftNode& nB = nodes[face.nodeB];
1132
+ const SoftNode& nC = nodes[face.nodeC];
1133
+
1134
+ const SoftVec3 edge1 = subSoftVec3(nB.position, nA.position);
1135
+ const SoftVec3 edge2 = subSoftVec3(nC.position, nA.position);
1136
+ const SoftVec3 pvec = crossSoftVec3(rDir, edge2);
1137
+ const double det = dotSoftVec3(edge1, pvec);
1138
+
1139
+ if (std::abs(det) < 1e-15) continue;
1140
+ const double invDet = 1.0 / det;
1141
+ const SoftVec3 tvec = subSoftVec3(ray.origin, nA.position);
1142
+ const double u = dotSoftVec3(tvec, pvec) * invDet;
1143
+ if (u < -0.05 || u > 1.05) continue;
1144
+
1145
+ const SoftVec3 qvec = crossSoftVec3(tvec, edge1);
1146
+ const double v = dotSoftVec3(rDir, qvec) * invDet;
1147
+ if (v < -0.05 || u + v > 1.05) continue;
1148
+
1149
+ const double t = dotSoftVec3(edge2, qvec) * invDet;
1150
+ if (t >= 0.0 && t <= bestHit.distance) {
1151
+ bestHit.hit = true;
1152
+ bestHit.distance = t;
1153
+ bestHit.point = addSoftVec3(ray.origin, scaleSoftVec3(rDir, t));
1154
+ const SoftVec3 triNorm = normalizeSoftVec3(crossSoftVec3(edge1, edge2));
1155
+ bestHit.normal = (det > 0.0) ? triNorm : scaleSoftVec3(triNorm, -1.0);
1156
+ bestHit.softBodyId = face.softBodyId;
1157
+ bestHit.elementIndex = static_cast<std::uint32_t>(f);
1158
+ bestHit.primitiveKind = SoftPrimitiveKind::Face;
1159
+ }
1160
+ }
1161
+
1162
+ return bestHit;
1163
+ }
1164
+
1165
+ std::uint32_t querySoftOverlapSphere(
1166
+ std::span<const SoftNode> nodes,
1167
+ std::span<const SoftBodyDef> bodyDefs,
1168
+ const SoftOverlapSphereQuery& query,
1169
+ std::span<SoftOverlapHit> outHits) {
1170
+ std::uint32_t count = 0;
1171
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
1172
+ if (count >= outHits.size() || count >= query.maxHits) break;
1173
+ const SoftNode& node = nodes[i];
1174
+ const SoftBodyDef* def = findBodyDef(node.softBodyId, bodyDefs);
1175
+ if (def != nullptr && (def->layer & query.mask) == 0) continue;
1176
+
1177
+ const double targetDist = query.radius + node.radius + node.thickness;
1178
+ const double distSq = lengthSquaredSoftVec3(subSoftVec3(node.position, query.center));
1179
+ if (distSq <= targetDist * targetDist) {
1180
+ outHits[count++] = {
1181
+ .softBodyId = node.softBodyId,
1182
+ .nodeIndex = static_cast<std::uint32_t>(i),
1183
+ .point = node.position,
1184
+ .distance = std::sqrt(distSq),
1185
+ };
1186
+ }
1187
+ }
1188
+ return count;
1189
+ }
1190
+
1191
+ std::uint32_t querySoftOverlapAABB(
1192
+ std::span<const SoftNode> nodes,
1193
+ std::span<const SoftBodyDef> bodyDefs,
1194
+ const SoftVec3& minBounds,
1195
+ const SoftVec3& maxBounds,
1196
+ std::uint32_t mask,
1197
+ std::span<std::uint32_t> outNodeIndices) {
1198
+ std::uint32_t count = 0;
1199
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
1200
+ if (count >= outNodeIndices.size()) break;
1201
+ const SoftNode& node = nodes[i];
1202
+ const SoftBodyDef* def = findBodyDef(node.softBodyId, bodyDefs);
1203
+ if (def != nullptr && (def->layer & mask) == 0) continue;
1204
+
1205
+ const double R = node.radius + node.thickness;
1206
+ if (node.position.x + R >= minBounds.x && node.position.x - R <= maxBounds.x &&
1207
+ node.position.y + R >= minBounds.y && node.position.y - R <= maxBounds.y &&
1208
+ node.position.z + R >= minBounds.z && node.position.z - R <= maxBounds.z) {
1209
+ outNodeIndices[count++] = static_cast<std::uint32_t>(i);
1210
+ }
1211
+ }
1212
+ return count;
1213
+ }
1214
+
1215
+ SoftClosestPointResult querySoftClosestPoint(
1216
+ std::span<const SoftNode> nodes,
1217
+ std::span<const SoftFace> faces,
1218
+ std::span<const SoftBodyDef> bodyDefs,
1219
+ const SoftClosestPointQuery& query) {
1220
+ SoftClosestPointResult res;
1221
+ res.found = false;
1222
+ res.distance = query.maxDistance;
1223
+
1224
+ for (std::size_t i = 0; i < nodes.size(); ++i) {
1225
+ const SoftNode& node = nodes[i];
1226
+ const SoftBodyDef* def = findBodyDef(node.softBodyId, bodyDefs);
1227
+ if (def != nullptr && (def->layer & query.mask) == 0) continue;
1228
+
1229
+ const SoftVec3 diff = subSoftVec3(node.position, query.point);
1230
+ const double d = lengthSoftVec3(diff);
1231
+ if (d < res.distance) {
1232
+ res.found = true;
1233
+ res.distance = d;
1234
+ res.closestPoint = node.position;
1235
+ res.normal = d > 1e-12 ? scaleSoftVec3(diff, 1.0 / d) : SoftVec3{0.0, 1.0, 0.0};
1236
+ res.softBodyId = node.softBodyId;
1237
+ res.elementIndex = static_cast<std::uint32_t>(i);
1238
+ }
1239
+ }
1240
+
1241
+ for (std::size_t f = 0; f < faces.size(); ++f) {
1242
+ const SoftFace& face = faces[f];
1243
+ if (face.nodeA >= nodes.size() || face.nodeB >= nodes.size() || face.nodeC >= nodes.size()) continue;
1244
+ double u = 0.0, v = 0.0, w = 0.0;
1245
+ const SoftVec3 pt = closestPointOnTriangle(
1246
+ query.point, nodes[face.nodeA].position, nodes[face.nodeB].position, nodes[face.nodeC].position, u, v, w);
1247
+ const SoftVec3 diff = subSoftVec3(pt, query.point);
1248
+ const double d = lengthSoftVec3(diff);
1249
+ if (d < res.distance) {
1250
+ res.found = true;
1251
+ res.distance = d;
1252
+ res.closestPoint = pt;
1253
+ res.normal = d > 1e-12 ? scaleSoftVec3(diff, 1.0 / d) : SoftVec3{0.0, 1.0, 0.0};
1254
+ res.softBodyId = face.softBodyId;
1255
+ res.elementIndex = static_cast<std::uint32_t>(f);
1256
+ }
1257
+ }
1258
+
1259
+ return res;
1260
+ }
1261
+
1262
+ } // namespace rundot::kinetix::physics
1263
+
1264
+ // ---------------------------------------------------------------------------
1265
+ // C ABI Export Definitions
1266
+ // ---------------------------------------------------------------------------
1267
+
1268
+ extern "C" {
1269
+
1270
+ using namespace rundot::kinetix::physics;
1271
+
1272
+ std::uint32_t rundot_kinetix_ext_soft_collision_abi_version() {
1273
+ return kSoftCollisionAbiVersion;
1274
+ }
1275
+
1276
+ std::uint32_t rundot_kinetix_ext_soft_node_stride() {
1277
+ return kSoftNodeStride;
1278
+ }
1279
+
1280
+ std::uint32_t rundot_kinetix_ext_soft_face_stride() {
1281
+ return kSoftFaceStride;
1282
+ }
1283
+
1284
+ std::uint32_t rundot_kinetix_ext_soft_body_stride() {
1285
+ return kSoftBodyStride;
1286
+ }
1287
+
1288
+ std::uint32_t rundot_kinetix_ext_soft_params_stride() {
1289
+ return kSoftParamsStride;
1290
+ }
1291
+
1292
+ std::uint32_t rundot_kinetix_ext_soft_rigid_contact_stride() {
1293
+ return kSoftRigidContactStride;
1294
+ }
1295
+
1296
+ std::uint32_t rundot_kinetix_ext_soft_soft_contact_stride() {
1297
+ return kSoftSoftContactStride;
1298
+ }
1299
+
1300
+ std::uint32_t rundot_kinetix_ext_soft_sensor_event_stride() {
1301
+ return kSoftSensorEventStride;
1302
+ }
1303
+
1304
+ std::uint32_t rundot_kinetix_ext_soft_raycast_query_stride() {
1305
+ return kSoftRaycastQueryStride;
1306
+ }
1307
+
1308
+ std::uint32_t rundot_kinetix_ext_soft_raycast_hit_stride() {
1309
+ return kSoftRaycastHitStride;
1310
+ }
1311
+
1312
+ std::uint32_t rundot_kinetix_ext_soft_overlap_query_stride() {
1313
+ return kSoftOverlapQueryStride;
1314
+ }
1315
+
1316
+ std::uint32_t rundot_kinetix_ext_soft_overlap_hit_stride() {
1317
+ return kSoftOverlapHitStride;
1318
+ }
1319
+
1320
+ std::uint32_t rundot_kinetix_ext_soft_closest_point_query_stride() {
1321
+ return kSoftClosestPointQueryStride;
1322
+ }
1323
+
1324
+ std::uint32_t rundot_kinetix_ext_soft_closest_point_result_stride() {
1325
+ return kSoftClosestPointResultStride;
1326
+ }
1327
+
1328
+ std::uint32_t rundot_kinetix_ext_soft_max_nodes() {
1329
+ return kSoftMaxNodes;
1330
+ }
1331
+
1332
+ std::uint32_t rundot_kinetix_ext_soft_max_faces() {
1333
+ return kSoftMaxFaces;
1334
+ }
1335
+
1336
+ std::uint32_t rundot_kinetix_ext_soft_max_bodies() {
1337
+ return kSoftMaxBodies;
1338
+ }
1339
+
1340
+ std::uint32_t rundot_kinetix_ext_soft_max_contacts() {
1341
+ return kSoftMaxContacts;
1342
+ }
1343
+
1344
+ std::uint32_t rundot_kinetix_ext_soft_max_sensor_events() {
1345
+ return kSoftMaxSensorEvents;
1346
+ }
1347
+
1348
+ std::uint32_t rundot_kinetix_ext_soft_max_exclusions() {
1349
+ return kSoftMaxExclusions;
1350
+ }
1351
+
1352
+ std::int32_t rundot_kinetix_ext_soft_rigid_solve(
1353
+ double* nodeData,
1354
+ std::uint32_t nodeCount,
1355
+ const double* bodyDefData,
1356
+ std::uint32_t bodyDefCount,
1357
+ double* rigidBodyData,
1358
+ std::uint32_t rigidBodyCount,
1359
+ const double* paramsData,
1360
+ double dt,
1361
+ double* outContactData,
1362
+ std::uint32_t maxContacts,
1363
+ std::uint32_t* outContactCount,
1364
+ double* outSensorEventData,
1365
+ std::uint32_t maxSensorEvents,
1366
+ std::uint32_t* outSensorEventCount) {
1367
+ if (nodeData == nullptr || nodeCount == 0 || paramsData == nullptr) {
1368
+ return static_cast<std::int32_t>(SoftCollisionStatus::InvalidInput);
1369
+ }
1370
+ if (nodeCount > kSoftMaxNodes || bodyDefCount > kSoftMaxBodies) {
1371
+ return static_cast<std::int32_t>(SoftCollisionStatus::InvalidCapacity);
1372
+ }
1373
+
1374
+ std::array<SoftNode, kSoftMaxNodes> nodesBuffer;
1375
+ for (std::uint32_t i = 0; i < nodeCount; ++i) {
1376
+ nodesBuffer[i] = unpackSoftNode(nodeData, i);
1377
+ }
1378
+
1379
+ std::array<SoftBodyDef, kSoftMaxBodies> bodyDefsBuffer;
1380
+ for (std::uint32_t b = 0; b < bodyDefCount; ++b) {
1381
+ bodyDefsBuffer[b] = unpackSoftBodyDef(bodyDefData, b);
1382
+ }
1383
+
1384
+ std::array<Body, 128> rigidBodiesBuffer;
1385
+ const std::uint32_t clampedRigidCount = std::min(rigidBodyCount, 128u);
1386
+ if (rigidBodyData != nullptr) {
1387
+ for (std::uint32_t r = 0; r < clampedRigidCount; ++r) {
1388
+ rigidBodiesBuffer[r] = unpackRigidBody(rigidBodyData, r);
1389
+ }
1390
+ }
1391
+
1392
+ const SoftCollisionParams params = unpackSoftCollisionParams(paramsData);
1393
+ std::array<SoftRigidContact, kSoftMaxContacts> contactsBuffer;
1394
+ std::array<SoftSensorEvent, kSoftMaxSensorEvents> sensorBuffer;
1395
+ std::uint32_t contactCount = 0;
1396
+ std::uint32_t sensorCount = 0;
1397
+
1398
+ const SoftCollisionStatus status = solveSoftRigid(
1399
+ std::span<SoftNode>(nodesBuffer.data(), nodeCount),
1400
+ std::span<const SoftBodyDef>(bodyDefsBuffer.data(), bodyDefCount),
1401
+ std::span<Body>(rigidBodiesBuffer.data(), clampedRigidCount),
1402
+ params,
1403
+ dt,
1404
+ std::span<SoftRigidContact>(contactsBuffer.data(), std::min(maxContacts, kSoftMaxContacts)),
1405
+ contactCount,
1406
+ std::span<SoftSensorEvent>(sensorBuffer.data(), std::min(maxSensorEvents, kSoftMaxSensorEvents)),
1407
+ sensorCount);
1408
+
1409
+ for (std::uint32_t i = 0; i < nodeCount; ++i) {
1410
+ packSoftNode(nodeData, i, nodesBuffer[i]);
1411
+ }
1412
+
1413
+ if (rigidBodyData != nullptr) {
1414
+ for (std::uint32_t r = 0; r < clampedRigidCount; ++r) {
1415
+ packRigidBody(rigidBodyData, r, rigidBodiesBuffer[r]);
1416
+ }
1417
+ }
1418
+
1419
+ if (outContactData != nullptr && outContactCount != nullptr) {
1420
+ *outContactCount = contactCount;
1421
+ for (std::uint32_t c = 0; c < contactCount; ++c) {
1422
+ packSoftRigidContact(outContactData, c, contactsBuffer[c]);
1423
+ }
1424
+ }
1425
+
1426
+ if (outSensorEventData != nullptr && outSensorEventCount != nullptr) {
1427
+ *outSensorEventCount = sensorCount;
1428
+ for (std::uint32_t s = 0; s < sensorCount; ++s) {
1429
+ packSoftSensorEvent(outSensorEventData, s, sensorBuffer[s]);
1430
+ }
1431
+ }
1432
+
1433
+ return static_cast<std::int32_t>(status);
1434
+ }
1435
+
1436
+ std::int32_t rundot_kinetix_ext_soft_soft_solve(
1437
+ double* nodeData,
1438
+ std::uint32_t nodeCount,
1439
+ const double* faceData,
1440
+ std::uint32_t faceCount,
1441
+ const double* bodyDefData,
1442
+ std::uint32_t bodyDefCount,
1443
+ const std::uint32_t* exclusionData,
1444
+ std::uint32_t exclusionCount,
1445
+ const double* paramsData,
1446
+ double dt,
1447
+ double* outContactData,
1448
+ std::uint32_t maxContacts,
1449
+ std::uint32_t* outContactCount,
1450
+ double* outSensorEventData,
1451
+ std::uint32_t maxSensorEvents,
1452
+ std::uint32_t* outSensorEventCount) {
1453
+ if (nodeData == nullptr || nodeCount == 0 || paramsData == nullptr) {
1454
+ return static_cast<std::int32_t>(SoftCollisionStatus::InvalidInput);
1455
+ }
1456
+ if (nodeCount > kSoftMaxNodes || faceCount > kSoftMaxFaces || bodyDefCount > kSoftMaxBodies) {
1457
+ return static_cast<std::int32_t>(SoftCollisionStatus::InvalidCapacity);
1458
+ }
1459
+
1460
+ std::array<SoftNode, kSoftMaxNodes> nodesBuffer;
1461
+ for (std::uint32_t i = 0; i < nodeCount; ++i) {
1462
+ nodesBuffer[i] = unpackSoftNode(nodeData, i);
1463
+ }
1464
+
1465
+ std::array<SoftFace, kSoftMaxFaces> facesBuffer;
1466
+ if (faceData != nullptr) {
1467
+ for (std::uint32_t f = 0; f < faceCount; ++f) {
1468
+ facesBuffer[f] = unpackSoftFace(faceData, f);
1469
+ }
1470
+ }
1471
+
1472
+ std::array<SoftBodyDef, kSoftMaxBodies> bodyDefsBuffer;
1473
+ if (bodyDefData != nullptr) {
1474
+ for (std::uint32_t b = 0; b < bodyDefCount; ++b) {
1475
+ bodyDefsBuffer[b] = unpackSoftBodyDef(bodyDefData, b);
1476
+ }
1477
+ }
1478
+
1479
+ std::array<SoftExclusionPair, kSoftMaxExclusions> exclusionsBuffer;
1480
+ const std::uint32_t clampedExclusions = std::min(exclusionCount, kSoftMaxExclusions);
1481
+ if (exclusionData != nullptr) {
1482
+ for (std::uint32_t k = 0; k < clampedExclusions; ++k) {
1483
+ exclusionsBuffer[k] = SoftExclusionPair{exclusionData[k * 2 + 0], exclusionData[k * 2 + 1]};
1484
+ }
1485
+ }
1486
+
1487
+ const SoftCollisionParams params = unpackSoftCollisionParams(paramsData);
1488
+ std::array<SoftSoftContact, kSoftMaxContacts> contactsBuffer;
1489
+ std::array<SoftSensorEvent, kSoftMaxSensorEvents> sensorBuffer;
1490
+ std::uint32_t contactCount = 0;
1491
+ std::uint32_t sensorCount = 0;
1492
+
1493
+ const SoftCollisionStatus status = solveSoftSoft(
1494
+ std::span<SoftNode>(nodesBuffer.data(), nodeCount),
1495
+ std::span<const SoftFace>(facesBuffer.data(), faceCount),
1496
+ std::span<const SoftBodyDef>(bodyDefsBuffer.data(), bodyDefCount),
1497
+ std::span<const SoftExclusionPair>(exclusionsBuffer.data(), clampedExclusions),
1498
+ params,
1499
+ dt,
1500
+ std::span<SoftSoftContact>(contactsBuffer.data(), std::min(maxContacts, kSoftMaxContacts)),
1501
+ contactCount,
1502
+ std::span<SoftSensorEvent>(sensorBuffer.data(), std::min(maxSensorEvents, kSoftMaxSensorEvents)),
1503
+ sensorCount);
1504
+
1505
+ for (std::uint32_t i = 0; i < nodeCount; ++i) {
1506
+ packSoftNode(nodeData, i, nodesBuffer[i]);
1507
+ }
1508
+
1509
+ if (outContactData != nullptr && outContactCount != nullptr) {
1510
+ *outContactCount = contactCount;
1511
+ for (std::uint32_t c = 0; c < contactCount; ++c) {
1512
+ packSoftSoftContact(outContactData, c, contactsBuffer[c]);
1513
+ }
1514
+ }
1515
+
1516
+ if (outSensorEventData != nullptr && outSensorEventCount != nullptr) {
1517
+ *outSensorEventCount = sensorCount;
1518
+ for (std::uint32_t s = 0; s < sensorCount; ++s) {
1519
+ packSoftSensorEvent(outSensorEventData, s, sensorBuffer[s]);
1520
+ }
1521
+ }
1522
+
1523
+ return static_cast<std::int32_t>(status);
1524
+ }
1525
+
1526
+ std::int32_t rundot_kinetix_ext_soft_step(
1527
+ double* nodeData,
1528
+ std::uint32_t nodeCount,
1529
+ const double* faceData,
1530
+ std::uint32_t faceCount,
1531
+ const double* bodyDefData,
1532
+ std::uint32_t bodyDefCount,
1533
+ const std::uint32_t* exclusionData,
1534
+ std::uint32_t exclusionCount,
1535
+ double* rigidBodyData,
1536
+ std::uint32_t rigidBodyCount,
1537
+ const double* paramsData,
1538
+ double dt,
1539
+ double* outRigidContacts,
1540
+ std::uint32_t maxRigidContacts,
1541
+ std::uint32_t* outRigidContactCount,
1542
+ double* outSoftContacts,
1543
+ std::uint32_t maxSoftContacts,
1544
+ std::uint32_t* outSoftContactCount,
1545
+ double* outSensorEvents,
1546
+ std::uint32_t maxSensorEvents,
1547
+ std::uint32_t* outSensorEventCount) {
1548
+ if (nodeData == nullptr || nodeCount == 0 || paramsData == nullptr) {
1549
+ return static_cast<std::int32_t>(SoftCollisionStatus::InvalidInput);
1550
+ }
1551
+
1552
+ std::array<SoftNode, kSoftMaxNodes> nodesBuffer;
1553
+ for (std::uint32_t i = 0; i < nodeCount; ++i) {
1554
+ nodesBuffer[i] = unpackSoftNode(nodeData, i);
1555
+ }
1556
+
1557
+ std::array<SoftFace, kSoftMaxFaces> facesBuffer;
1558
+ if (faceData != nullptr) {
1559
+ for (std::uint32_t f = 0; f < faceCount; ++f) {
1560
+ facesBuffer[f] = unpackSoftFace(faceData, f);
1561
+ }
1562
+ }
1563
+
1564
+ std::array<SoftBodyDef, kSoftMaxBodies> bodyDefsBuffer;
1565
+ if (bodyDefData != nullptr) {
1566
+ for (std::uint32_t b = 0; b < bodyDefCount; ++b) {
1567
+ bodyDefsBuffer[b] = unpackSoftBodyDef(bodyDefData, b);
1568
+ }
1569
+ }
1570
+
1571
+ std::array<SoftExclusionPair, kSoftMaxExclusions> exclusionsBuffer;
1572
+ const std::uint32_t clampedExclusions = std::min(exclusionCount, kSoftMaxExclusions);
1573
+ if (exclusionData != nullptr) {
1574
+ for (std::uint32_t k = 0; k < clampedExclusions; ++k) {
1575
+ exclusionsBuffer[k] = SoftExclusionPair{exclusionData[k * 2 + 0], exclusionData[k * 2 + 1]};
1576
+ }
1577
+ }
1578
+
1579
+ std::array<Body, 128> rigidBodiesBuffer;
1580
+ const std::uint32_t clampedRigidCount = std::min(rigidBodyCount, 128u);
1581
+ if (rigidBodyData != nullptr) {
1582
+ for (std::uint32_t r = 0; r < clampedRigidCount; ++r) {
1583
+ rigidBodiesBuffer[r] = unpackRigidBody(rigidBodyData, r);
1584
+ }
1585
+ }
1586
+
1587
+ const SoftCollisionParams params = unpackSoftCollisionParams(paramsData);
1588
+ std::array<SoftRigidContact, kSoftMaxContacts> rigidContactsBuffer;
1589
+ std::array<SoftSoftContact, kSoftMaxContacts> softContactsBuffer;
1590
+ std::array<SoftSensorEvent, kSoftMaxSensorEvents> sensorEventsBuffer;
1591
+
1592
+ std::uint32_t rigidCount = 0;
1593
+ std::uint32_t softCount = 0;
1594
+ std::uint32_t sensorCount = 0;
1595
+
1596
+ const SoftCollisionStatus status = stepSoftCollision(
1597
+ std::span<SoftNode>(nodesBuffer.data(), nodeCount),
1598
+ std::span<const SoftFace>(facesBuffer.data(), faceCount),
1599
+ std::span<const SoftBodyDef>(bodyDefsBuffer.data(), bodyDefCount),
1600
+ std::span<const SoftExclusionPair>(exclusionsBuffer.data(), clampedExclusions),
1601
+ std::span<Body>(rigidBodiesBuffer.data(), clampedRigidCount),
1602
+ params,
1603
+ dt,
1604
+ std::span<SoftRigidContact>(rigidContactsBuffer.data(), std::min(maxRigidContacts, kSoftMaxContacts)),
1605
+ rigidCount,
1606
+ std::span<SoftSoftContact>(softContactsBuffer.data(), std::min(maxSoftContacts, kSoftMaxContacts)),
1607
+ softCount,
1608
+ std::span<SoftSensorEvent>(sensorEventsBuffer.data(), std::min(maxSensorEvents, kSoftMaxSensorEvents)),
1609
+ sensorCount);
1610
+
1611
+ for (std::uint32_t i = 0; i < nodeCount; ++i) {
1612
+ packSoftNode(nodeData, i, nodesBuffer[i]);
1613
+ }
1614
+
1615
+ if (rigidBodyData != nullptr) {
1616
+ for (std::uint32_t r = 0; r < clampedRigidCount; ++r) {
1617
+ packRigidBody(rigidBodyData, r, rigidBodiesBuffer[r]);
1618
+ }
1619
+ }
1620
+
1621
+ if (outRigidContacts != nullptr && outRigidContactCount != nullptr) {
1622
+ *outRigidContactCount = rigidCount;
1623
+ for (std::uint32_t c = 0; c < rigidCount; ++c) {
1624
+ packSoftRigidContact(outRigidContacts, c, rigidContactsBuffer[c]);
1625
+ }
1626
+ }
1627
+
1628
+ if (outSoftContacts != nullptr && outSoftContactCount != nullptr) {
1629
+ *outSoftContactCount = softCount;
1630
+ for (std::uint32_t c = 0; c < softCount; ++c) {
1631
+ packSoftSoftContact(outSoftContacts, c, softContactsBuffer[c]);
1632
+ }
1633
+ }
1634
+
1635
+ if (outSensorEvents != nullptr && outSensorEventCount != nullptr) {
1636
+ *outSensorEventCount = sensorCount;
1637
+ for (std::uint32_t s = 0; s < sensorCount; ++s) {
1638
+ packSoftSensorEvent(outSensorEvents, s, sensorEventsBuffer[s]);
1639
+ }
1640
+ }
1641
+
1642
+ return static_cast<std::int32_t>(status);
1643
+ }
1644
+
1645
+ std::int32_t rundot_kinetix_ext_soft_raycast(
1646
+ const double* nodeData,
1647
+ std::uint32_t nodeCount,
1648
+ const double* faceData,
1649
+ std::uint32_t faceCount,
1650
+ const double* bodyDefData,
1651
+ std::uint32_t bodyDefCount,
1652
+ const double* rayData,
1653
+ double* outHitData) {
1654
+ if (nodeData == nullptr || rayData == nullptr || outHitData == nullptr) {
1655
+ return static_cast<std::int32_t>(SoftCollisionStatus::InvalidInput);
1656
+ }
1657
+
1658
+ std::array<SoftNode, kSoftMaxNodes> nodesBuffer;
1659
+ const std::uint32_t clampedNodes = std::min(nodeCount, kSoftMaxNodes);
1660
+ for (std::uint32_t i = 0; i < clampedNodes; ++i) {
1661
+ nodesBuffer[i] = unpackSoftNode(nodeData, i);
1662
+ }
1663
+
1664
+ std::array<SoftFace, kSoftMaxFaces> facesBuffer;
1665
+ const std::uint32_t clampedFaces = std::min(faceCount, kSoftMaxFaces);
1666
+ if (faceData != nullptr) {
1667
+ for (std::uint32_t f = 0; f < clampedFaces; ++f) {
1668
+ facesBuffer[f] = unpackSoftFace(faceData, f);
1669
+ }
1670
+ }
1671
+
1672
+ std::array<SoftBodyDef, kSoftMaxBodies> bodyDefsBuffer;
1673
+ const std::uint32_t clampedDefs = std::min(bodyDefCount, kSoftMaxBodies);
1674
+ if (bodyDefData != nullptr) {
1675
+ for (std::uint32_t b = 0; b < clampedDefs; ++b) {
1676
+ bodyDefsBuffer[b] = unpackSoftBodyDef(bodyDefData, b);
1677
+ }
1678
+ }
1679
+
1680
+ SoftRay ray;
1681
+ ray.origin = {rayData[0], rayData[1], rayData[2]};
1682
+ ray.direction = {rayData[3], rayData[4], rayData[5]};
1683
+ ray.maxDistance = rayData[6];
1684
+ ray.radius = 0.0;
1685
+ ray.mask = static_cast<std::uint32_t>(rayData[7]);
1686
+
1687
+ const SoftRayHit hit = querySoftRaycast(
1688
+ std::span<const SoftNode>(nodesBuffer.data(), clampedNodes),
1689
+ std::span<const SoftFace>(facesBuffer.data(), clampedFaces),
1690
+ std::span<const SoftBodyDef>(bodyDefsBuffer.data(), clampedDefs),
1691
+ ray);
1692
+
1693
+ outHitData[0] = hit.hit ? 1.0 : 0.0;
1694
+ outHitData[1] = hit.distance;
1695
+ outHitData[2] = hit.point.x;
1696
+ outHitData[3] = hit.point.y;
1697
+ outHitData[4] = hit.point.z;
1698
+ outHitData[5] = hit.normal.x;
1699
+ outHitData[6] = hit.normal.y;
1700
+ outHitData[7] = hit.normal.z;
1701
+
1702
+ return static_cast<std::int32_t>(SoftCollisionStatus::Success);
1703
+ }
1704
+
1705
+ std::int32_t rundot_kinetix_ext_soft_overlap_sphere(
1706
+ const double* nodeData,
1707
+ std::uint32_t nodeCount,
1708
+ const double* bodyDefData,
1709
+ std::uint32_t bodyDefCount,
1710
+ const double* queryData,
1711
+ double* outHitData,
1712
+ std::uint32_t maxHits,
1713
+ std::uint32_t* outHitCount) {
1714
+ if (nodeData == nullptr || queryData == nullptr || outHitData == nullptr || outHitCount == nullptr) {
1715
+ return static_cast<std::int32_t>(SoftCollisionStatus::InvalidInput);
1716
+ }
1717
+
1718
+ std::array<SoftNode, kSoftMaxNodes> nodesBuffer;
1719
+ const std::uint32_t clampedNodes = std::min(nodeCount, kSoftMaxNodes);
1720
+ for (std::uint32_t i = 0; i < clampedNodes; ++i) {
1721
+ nodesBuffer[i] = unpackSoftNode(nodeData, i);
1722
+ }
1723
+
1724
+ std::array<SoftBodyDef, kSoftMaxBodies> bodyDefsBuffer;
1725
+ const std::uint32_t clampedDefs = std::min(bodyDefCount, kSoftMaxBodies);
1726
+ if (bodyDefData != nullptr) {
1727
+ for (std::uint32_t b = 0; b < clampedDefs; ++b) {
1728
+ bodyDefsBuffer[b] = unpackSoftBodyDef(bodyDefData, b);
1729
+ }
1730
+ }
1731
+
1732
+ SoftOverlapSphereQuery query;
1733
+ query.center = {queryData[0], queryData[1], queryData[2]};
1734
+ query.radius = queryData[3];
1735
+ query.mask = static_cast<std::uint32_t>(queryData[4]);
1736
+ query.maxHits = std::min(maxHits, static_cast<std::uint32_t>(kSoftMaxQueryHits));
1737
+
1738
+ std::array<SoftOverlapHit, kSoftMaxQueryHits> hitsBuffer;
1739
+ const std::uint32_t hitCount = querySoftOverlapSphere(
1740
+ std::span<const SoftNode>(nodesBuffer.data(), clampedNodes),
1741
+ std::span<const SoftBodyDef>(bodyDefsBuffer.data(), clampedDefs),
1742
+ query,
1743
+ std::span<SoftOverlapHit>(hitsBuffer.data(), query.maxHits));
1744
+
1745
+ *outHitCount = hitCount;
1746
+ for (std::uint32_t h = 0; h < hitCount; ++h) {
1747
+ const std::uint32_t offset = h * kSoftOverlapHitStride;
1748
+ outHitData[offset + 0] = static_cast<double>(hitsBuffer[h].softBodyId);
1749
+ outHitData[offset + 1] = static_cast<double>(hitsBuffer[h].nodeIndex);
1750
+ outHitData[offset + 2] = hitsBuffer[h].point.x;
1751
+ outHitData[offset + 3] = hitsBuffer[h].point.y;
1752
+ outHitData[offset + 4] = hitsBuffer[h].point.z;
1753
+ outHitData[offset + 5] = hitsBuffer[h].distance;
1754
+ }
1755
+
1756
+ return static_cast<std::int32_t>(SoftCollisionStatus::Success);
1757
+ }
1758
+
1759
+ std::int32_t rundot_kinetix_ext_soft_overlap_aabb(
1760
+ const double* nodeData,
1761
+ std::uint32_t nodeCount,
1762
+ const double* bodyDefData,
1763
+ std::uint32_t bodyDefCount,
1764
+ const double* minBoundsData,
1765
+ const double* maxBoundsData,
1766
+ std::uint32_t mask,
1767
+ std::uint32_t* outNodeIndices,
1768
+ std::uint32_t maxIndices,
1769
+ std::uint32_t* outIndexCount) {
1770
+ if (nodeData == nullptr || minBoundsData == nullptr || maxBoundsData == nullptr ||
1771
+ outNodeIndices == nullptr || outIndexCount == nullptr) {
1772
+ return static_cast<std::int32_t>(SoftCollisionStatus::InvalidInput);
1773
+ }
1774
+
1775
+ std::array<SoftNode, kSoftMaxNodes> nodesBuffer;
1776
+ const std::uint32_t clampedNodes = std::min(nodeCount, kSoftMaxNodes);
1777
+ for (std::uint32_t i = 0; i < clampedNodes; ++i) {
1778
+ nodesBuffer[i] = unpackSoftNode(nodeData, i);
1779
+ }
1780
+
1781
+ std::array<SoftBodyDef, kSoftMaxBodies> bodyDefsBuffer;
1782
+ const std::uint32_t clampedDefs = std::min(bodyDefCount, kSoftMaxBodies);
1783
+ if (bodyDefData != nullptr) {
1784
+ for (std::uint32_t b = 0; b < clampedDefs; ++b) {
1785
+ bodyDefsBuffer[b] = unpackSoftBodyDef(bodyDefData, b);
1786
+ }
1787
+ }
1788
+
1789
+ const SoftVec3 minBounds{minBoundsData[0], minBoundsData[1], minBoundsData[2]};
1790
+ const SoftVec3 maxBounds{maxBoundsData[0], maxBoundsData[1], maxBoundsData[2]};
1791
+
1792
+ const std::uint32_t count = querySoftOverlapAABB(
1793
+ std::span<const SoftNode>(nodesBuffer.data(), clampedNodes),
1794
+ std::span<const SoftBodyDef>(bodyDefsBuffer.data(), clampedDefs),
1795
+ minBounds,
1796
+ maxBounds,
1797
+ mask,
1798
+ std::span<std::uint32_t>(outNodeIndices, maxIndices));
1799
+
1800
+ *outIndexCount = count;
1801
+ return static_cast<std::int32_t>(SoftCollisionStatus::Success);
1802
+ }
1803
+
1804
+ std::int32_t rundot_kinetix_ext_soft_closest_point(
1805
+ const double* nodeData,
1806
+ std::uint32_t nodeCount,
1807
+ const double* faceData,
1808
+ std::uint32_t faceCount,
1809
+ const double* bodyDefData,
1810
+ std::uint32_t bodyDefCount,
1811
+ const double* queryData,
1812
+ double* outResultData) {
1813
+ if (nodeData == nullptr || queryData == nullptr || outResultData == nullptr) {
1814
+ return static_cast<std::int32_t>(SoftCollisionStatus::InvalidInput);
1815
+ }
1816
+
1817
+ std::array<SoftNode, kSoftMaxNodes> nodesBuffer;
1818
+ const std::uint32_t clampedNodes = std::min(nodeCount, kSoftMaxNodes);
1819
+ for (std::uint32_t i = 0; i < clampedNodes; ++i) {
1820
+ nodesBuffer[i] = unpackSoftNode(nodeData, i);
1821
+ }
1822
+
1823
+ std::array<SoftFace, kSoftMaxFaces> facesBuffer;
1824
+ const std::uint32_t clampedFaces = std::min(faceCount, kSoftMaxFaces);
1825
+ if (faceData != nullptr) {
1826
+ for (std::uint32_t f = 0; f < clampedFaces; ++f) {
1827
+ facesBuffer[f] = unpackSoftFace(faceData, f);
1828
+ }
1829
+ }
1830
+
1831
+ std::array<SoftBodyDef, kSoftMaxBodies> bodyDefsBuffer;
1832
+ const std::uint32_t clampedDefs = std::min(bodyDefCount, kSoftMaxBodies);
1833
+ if (bodyDefData != nullptr) {
1834
+ for (std::uint32_t b = 0; b < clampedDefs; ++b) {
1835
+ bodyDefsBuffer[b] = unpackSoftBodyDef(bodyDefData, b);
1836
+ }
1837
+ }
1838
+
1839
+ SoftClosestPointQuery query;
1840
+ query.point = {queryData[0], queryData[1], queryData[2]};
1841
+ query.maxDistance = queryData[3];
1842
+ query.mask = static_cast<std::uint32_t>(queryData[4]);
1843
+
1844
+ const SoftClosestPointResult res = querySoftClosestPoint(
1845
+ std::span<const SoftNode>(nodesBuffer.data(), clampedNodes),
1846
+ std::span<const SoftFace>(facesBuffer.data(), clampedFaces),
1847
+ std::span<const SoftBodyDef>(bodyDefsBuffer.data(), clampedDefs),
1848
+ query);
1849
+
1850
+ outResultData[0] = res.found ? 1.0 : 0.0;
1851
+ outResultData[1] = res.distance;
1852
+ outResultData[2] = res.closestPoint.x;
1853
+ outResultData[3] = res.closestPoint.y;
1854
+ outResultData[4] = res.closestPoint.z;
1855
+ outResultData[5] = res.normal.x;
1856
+ outResultData[6] = res.normal.y;
1857
+ outResultData[7] = res.normal.z;
1858
+
1859
+ return static_cast<std::int32_t>(SoftCollisionStatus::Success);
1860
+ }
1861
+
1862
+ }