@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,1401 @@
1
+ #include "cloth.hpp"
2
+
3
+ #include <algorithm>
4
+ #include <cmath>
5
+ #include <cstring>
6
+
7
+ namespace rundot::kinetix::physics {
8
+
9
+ namespace {
10
+
11
+ inline double clothSqrt(double value) {
12
+ if (value <= 0.0) return 0.0;
13
+ return std::sqrt(value);
14
+ }
15
+
16
+ inline double clothClamp(double value, double minVal, double maxVal) {
17
+ if (value < minVal) return minVal;
18
+ if (value > maxVal) return maxVal;
19
+ return value;
20
+ }
21
+
22
+ inline double clothAcos(double value) {
23
+ return std::acos(clothClamp(value, -1.0, 1.0));
24
+ }
25
+
26
+ inline double bodyInverseMass(const Body& body) {
27
+ if (!body.dynamic || body.mass <= 0.0) return 0.0;
28
+ return 1.0 / body.mass;
29
+ }
30
+
31
+ } // namespace
32
+
33
+ ClothVec3 addClothVec3(const ClothVec3& a, const ClothVec3& b) {
34
+ return {a.x + b.x, a.y + b.y, a.z + b.z};
35
+ }
36
+
37
+ ClothVec3 subClothVec3(const ClothVec3& a, const ClothVec3& b) {
38
+ return {a.x - b.x, a.y - b.y, a.z - b.z};
39
+ }
40
+
41
+ ClothVec3 scaleClothVec3(const ClothVec3& v, double s) {
42
+ return {v.x * s, v.y * s, v.z * s};
43
+ }
44
+
45
+ double dotClothVec3(const ClothVec3& a, const ClothVec3& b) {
46
+ return a.x * b.x + a.y * b.y + a.z * b.z;
47
+ }
48
+
49
+ ClothVec3 crossClothVec3(const ClothVec3& a, const ClothVec3& b) {
50
+ return {
51
+ a.y * b.z - a.z * b.y,
52
+ a.z * b.x - a.x * b.z,
53
+ a.x * b.y - a.y * b.x,
54
+ };
55
+ }
56
+
57
+ double lengthSquaredClothVec3(const ClothVec3& v) {
58
+ return dotClothVec3(v, v);
59
+ }
60
+
61
+ double lengthClothVec3(const ClothVec3& v) {
62
+ return clothSqrt(lengthSquaredClothVec3(v));
63
+ }
64
+
65
+ ClothVec3 normalizeClothVec3(const ClothVec3& v) {
66
+ const double len = lengthClothVec3(v);
67
+ if (len <= 1e-15) return {0.0, 0.0, 0.0};
68
+ return scaleClothVec3(v, 1.0 / len);
69
+ }
70
+
71
+ ClothVec3 rotateByQuat(const ClothQuat& q, const ClothVec3& v) {
72
+ const ClothVec3 u{q.x, q.y, q.z};
73
+ const ClothVec3 uv = crossClothVec3(u, v);
74
+ const ClothVec3 uuv = crossClothVec3(u, uv);
75
+ return addClothVec3(v, addClothVec3(scaleClothVec3(uv, 2.0 * q.w), scaleClothVec3(uuv, 2.0)));
76
+ }
77
+
78
+ ClothQuat multiplyQuat(const ClothQuat& a, const ClothQuat& b) {
79
+ return {
80
+ a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
81
+ a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
82
+ a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
83
+ a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
84
+ };
85
+ }
86
+
87
+ ClothStatus stepCloth(
88
+ ClothSimulation& cloth,
89
+ std::span<Body> bodies,
90
+ double dt) {
91
+ if (dt <= 0.0 || cloth.nodeCount < 3) return ClothStatus::InvalidNodeCount;
92
+ if (cloth.nodeCount > kClothMaxNodes ||
93
+ cloth.stretchCount > kClothMaxStretchConstraints ||
94
+ cloth.shearCount > kClothMaxShearConstraints ||
95
+ cloth.bendCount > kClothMaxBendConstraints ||
96
+ cloth.faceCount > kClothMaxFaces ||
97
+ cloth.pinCount > kClothMaxPins ||
98
+ cloth.tetherCount > kClothMaxTethers) {
99
+ return ClothStatus::BufferOverflow;
100
+ }
101
+
102
+ const double invDt = 1.0 / dt;
103
+ const double invDtSq = invDt * invDt;
104
+ const ClothVec3 gravity{cloth.params.gravityX, cloth.params.gravityY, cloth.params.gravityZ};
105
+ const double dampingFactor = clothClamp(1.0 - cloth.params.damping, 0.0, 1.0);
106
+
107
+ cloth.maxTension = 0.0;
108
+ cloth.maxShear = 0.0;
109
+ cloth.maxBend = 0.0;
110
+ cloth.totalAerodynamicForce = 0.0;
111
+
112
+ // 1. Aerodynamic Drag & Lift Forces on Faces
113
+ if (cloth.params.enableAerodynamics && cloth.faceCount > 0) {
114
+ const ClothVec3 wind{cloth.params.windX, cloth.params.windY, cloth.params.windZ};
115
+ const double rho = cloth.params.airDensity;
116
+ const double Cd = cloth.params.dragCoefficient;
117
+ const double Cl = cloth.params.liftCoefficient;
118
+
119
+ for (std::uint32_t f = 0; f < cloth.faceCount; ++f) {
120
+ auto& face = cloth.faces[f];
121
+ if (face.nodeA >= cloth.nodeCount || face.nodeB >= cloth.nodeCount || face.nodeC >= cloth.nodeCount) {
122
+ continue;
123
+ }
124
+ auto& nA = cloth.nodes[face.nodeA];
125
+ auto& nB = cloth.nodes[face.nodeB];
126
+ auto& nC = cloth.nodes[face.nodeC];
127
+
128
+ const ClothVec3 vFace = scaleClothVec3(
129
+ addClothVec3(nA.velocity, addClothVec3(nB.velocity, nC.velocity)), 1.0 / 3.0);
130
+ const ClothVec3 vRel = subClothVec3(wind, vFace);
131
+ const double relSpeed = lengthClothVec3(vRel);
132
+
133
+ const ClothVec3 e1 = subClothVec3(nB.position, nA.position);
134
+ const ClothVec3 e2 = subClothVec3(nC.position, nA.position);
135
+ const ClothVec3 normalVec = crossClothVec3(e1, e2);
136
+ const double normalLen = lengthClothVec3(normalVec);
137
+ const double area = 0.5 * normalLen;
138
+ face.currentArea = area;
139
+
140
+ if (normalLen <= 1e-12 || area <= 1e-12) {
141
+ face.normal = {0.0, 1.0, 0.0};
142
+ face.aerodynamicForce = {0.0, 0.0, 0.0};
143
+ continue;
144
+ }
145
+
146
+ const ClothVec3 n = scaleClothVec3(normalVec, 1.0 / normalLen);
147
+ face.normal = n;
148
+
149
+ if (relSpeed > 1e-9) {
150
+ const double vn = dotClothVec3(vRel, n);
151
+ const ClothVec3 vt = subClothVec3(vRel, scaleClothVec3(n, vn));
152
+ const double vtLen = lengthClothVec3(vt);
153
+
154
+ // Thin-surface aerodynamic drag along normal
155
+ const ClothVec3 fDrag = scaleClothVec3(n, 0.5 * rho * area * Cd * std::abs(vn) * vn);
156
+
157
+ // Aerodynamic lift perpendicular to normal in direction of tangential relative velocity
158
+ ClothVec3 fLift{0.0, 0.0, 0.0};
159
+ if (vtLen > 1e-9) {
160
+ fLift = scaleClothVec3(vt, 0.5 * rho * area * Cl * std::abs(vn));
161
+ }
162
+
163
+ const ClothVec3 fAero = addClothVec3(fDrag, fLift);
164
+ face.aerodynamicForce = fAero;
165
+ cloth.totalAerodynamicForce += lengthClothVec3(fAero);
166
+
167
+ const ClothVec3 fThird = scaleClothVec3(fAero, 1.0 / 3.0);
168
+ if (nA.inverseMass > 0.0) {
169
+ nA.velocity = addClothVec3(nA.velocity, scaleClothVec3(fThird, nA.inverseMass * dt));
170
+ }
171
+ if (nB.inverseMass > 0.0) {
172
+ nB.velocity = addClothVec3(nB.velocity, scaleClothVec3(fThird, nB.inverseMass * dt));
173
+ }
174
+ if (nC.inverseMass > 0.0) {
175
+ nC.velocity = addClothVec3(nC.velocity, scaleClothVec3(fThird, nC.inverseMass * dt));
176
+ }
177
+ } else {
178
+ face.aerodynamicForce = {0.0, 0.0, 0.0};
179
+ }
180
+ }
181
+ }
182
+
183
+ // 2. Prediction Step: External forces (gravity), damping, position integration
184
+ for (std::uint32_t i = 0; i < cloth.nodeCount; ++i) {
185
+ auto& node = cloth.nodes[i];
186
+ if (node.inverseMass <= 0.0) {
187
+ node.previousPosition = node.position;
188
+ continue;
189
+ }
190
+ node.velocity = addClothVec3(node.velocity, scaleClothVec3(gravity, dt));
191
+ node.velocity = scaleClothVec3(node.velocity, dampingFactor);
192
+ node.previousPosition = node.position;
193
+ node.position = addClothVec3(node.position, scaleClothVec3(node.velocity, dt));
194
+ }
195
+
196
+ // Multiplier accumulators for exact force & telemetry metrics
197
+ std::array<double, kClothMaxStretchConstraints> accumulatedStretchLambda{};
198
+ std::array<double, kClothMaxShearConstraints> accumulatedShearLambda{};
199
+ std::array<double, kClothMaxBendConstraints> accumulatedBendLambda{};
200
+ std::array<double, kClothMaxPins> accumulatedPinImpulse{};
201
+
202
+ // 3. Position-Based Constraint Solver Iterations
203
+ const std::uint32_t iterations = cloth.params.solverIterations > 0 ? cloth.params.solverIterations : 10;
204
+ for (std::uint32_t iter = 0; iter < iterations; ++iter) {
205
+ // A. Pins & Driven Attachments
206
+ for (std::uint32_t p = 0; p < cloth.pinCount; ++p) {
207
+ auto& pin = cloth.pins[p];
208
+ if (!pin.active || pin.nodeIndex >= cloth.nodeCount) continue;
209
+ auto& node = cloth.nodes[pin.nodeIndex];
210
+
211
+ if (pin.bodyIndex >= 0 && static_cast<std::size_t>(pin.bodyIndex) < bodies.size()) {
212
+ auto& body = bodies[pin.bodyIndex];
213
+ const ClothQuat bodyQ{body.orientationX, body.orientationY, body.orientationZ, body.orientationW};
214
+ const ClothVec3 worldOffset = rotateByQuat(bodyQ, pin.localOffset);
215
+ const ClothVec3 targetPos{body.x + worldOffset.x, body.y + worldOffset.y, body.z + worldOffset.z};
216
+ pin.worldPosition = targetPos;
217
+
218
+ const double bInvMass = bodyInverseMass(body);
219
+ if (bInvMass <= 0.0) {
220
+ const ClothVec3 diff = subClothVec3(node.position, targetPos);
221
+ const ClothVec3 corr = scaleClothVec3(diff, pin.pinStiffness);
222
+ node.position = subClothVec3(node.position, corr);
223
+ accumulatedPinImpulse[p] += lengthClothVec3(corr);
224
+ } else {
225
+ // Dynamic two-way coupling
226
+ const ClothVec3 diff = subClothVec3(node.position, targetPos);
227
+ const double wTot = node.inverseMass + bInvMass;
228
+ if (wTot > 1e-12) {
229
+ const ClothVec3 corr = scaleClothVec3(diff, pin.pinStiffness / wTot);
230
+ node.position = subClothVec3(node.position, scaleClothVec3(corr, node.inverseMass));
231
+ body.x += corr.x * bInvMass;
232
+ body.y += corr.y * bInvMass;
233
+ body.z += corr.z * bInvMass;
234
+ accumulatedPinImpulse[p] += lengthClothVec3(corr);
235
+ }
236
+ }
237
+ } else {
238
+ // Fixed world anchor
239
+ pin.worldPosition = pin.localOffset;
240
+ const ClothVec3 diff = subClothVec3(node.position, pin.localOffset);
241
+ const ClothVec3 corr = scaleClothVec3(diff, pin.pinStiffness);
242
+ node.position = subClothVec3(node.position, corr);
243
+ accumulatedPinImpulse[p] += lengthClothVec3(corr);
244
+ }
245
+ }
246
+
247
+ // B. Tether Limits (Long-Range Attachments)
248
+ if (cloth.params.enableTethers) {
249
+ for (std::uint32_t t = 0; t < cloth.tetherCount; ++t) {
250
+ auto& tether = cloth.tethers[t];
251
+ if (!tether.active || tether.nodeIndex >= cloth.nodeCount || tether.anchorNodeIndex >= cloth.nodeCount) {
252
+ continue;
253
+ }
254
+ auto& node = cloth.nodes[tether.nodeIndex];
255
+ auto& anchor = cloth.nodes[tether.anchorNodeIndex];
256
+
257
+ const ClothVec3 delta = subClothVec3(node.position, anchor.position);
258
+ const double dist = lengthClothVec3(delta);
259
+ tether.currentDistance = dist;
260
+
261
+ if (dist > tether.maxDistance && dist > 1e-12) {
262
+ const double excess = dist - tether.maxDistance;
263
+ const double wTot = node.inverseMass + anchor.inverseMass;
264
+ if (wTot > 1e-12) {
265
+ const ClothVec3 dir = scaleClothVec3(delta, 1.0 / dist);
266
+ const ClothVec3 corr = scaleClothVec3(dir, (excess / wTot) * clothClamp(tether.stiffness, 0.0, 1.0));
267
+ node.position = subClothVec3(node.position, scaleClothVec3(corr, node.inverseMass));
268
+ anchor.position = addClothVec3(anchor.position, scaleClothVec3(corr, anchor.inverseMass));
269
+ tether.tension = excess * invDtSq;
270
+ }
271
+ } else {
272
+ tether.tension = 0.0;
273
+ }
274
+ }
275
+ }
276
+
277
+ // C. Stretch Constraints (Structural Edges)
278
+ for (std::uint32_t s = 0; s < cloth.stretchCount; ++s) {
279
+ auto& seg = cloth.stretchConstraints[s];
280
+ if (seg.broken || seg.nodeA >= cloth.nodeCount || seg.nodeB >= cloth.nodeCount) {
281
+ continue;
282
+ }
283
+ auto& nA = cloth.nodes[seg.nodeA];
284
+ auto& nB = cloth.nodes[seg.nodeB];
285
+
286
+ const ClothVec3 delta = subClothVec3(nB.position, nA.position);
287
+ const double currentLen = lengthClothVec3(delta);
288
+ seg.currentLength = currentLen;
289
+ if (currentLen <= 1e-12) continue;
290
+
291
+ const double diff = currentLen - seg.restLength;
292
+ const double wTot = nA.inverseMass + nB.inverseMass;
293
+ if (wTot <= 1e-12) continue;
294
+
295
+ const double stiffness = clothClamp(seg.stiffness, 0.0, 1.0);
296
+ const double dLambda = (diff / wTot) * stiffness;
297
+ accumulatedStretchLambda[s] += dLambda;
298
+
299
+ const ClothVec3 corr = scaleClothVec3(delta, dLambda / currentLen);
300
+ nA.position = addClothVec3(nA.position, scaleClothVec3(corr, nA.inverseMass));
301
+ nB.position = subClothVec3(nB.position, scaleClothVec3(corr, nB.inverseMass));
302
+ }
303
+
304
+ // D. Shear Constraints (Diagonal Edges)
305
+ for (std::uint32_t sh = 0; sh < cloth.shearCount; ++sh) {
306
+ auto& shear = cloth.shearConstraints[sh];
307
+ if (shear.broken || shear.nodeA >= cloth.nodeCount || shear.nodeB >= cloth.nodeCount) {
308
+ continue;
309
+ }
310
+ auto& nA = cloth.nodes[shear.nodeA];
311
+ auto& nB = cloth.nodes[shear.nodeB];
312
+
313
+ const ClothVec3 delta = subClothVec3(nB.position, nA.position);
314
+ const double currentLen = lengthClothVec3(delta);
315
+ shear.currentLength = currentLen;
316
+ if (currentLen <= 1e-12) continue;
317
+
318
+ const double diff = currentLen - shear.restLength;
319
+ const double wTot = nA.inverseMass + nB.inverseMass;
320
+ if (wTot <= 1e-12) continue;
321
+
322
+ const double stiffness = clothClamp(shear.stiffness, 0.0, 1.0);
323
+ const double dLambda = (diff / wTot) * stiffness;
324
+ accumulatedShearLambda[sh] += dLambda;
325
+
326
+ const ClothVec3 corr = scaleClothVec3(delta, dLambda / currentLen);
327
+ nA.position = addClothVec3(nA.position, scaleClothVec3(corr, nA.inverseMass));
328
+ nB.position = subClothVec3(nB.position, scaleClothVec3(corr, nB.inverseMass));
329
+ }
330
+
331
+ // E. Bend Constraints (Isometric / Dihedral Bending)
332
+ for (std::uint32_t b = 0; b < cloth.bendCount; ++b) {
333
+ auto& bend = cloth.bendConstraints[b];
334
+ if (bend.broken || bend.nodeA >= cloth.nodeCount || bend.nodeB >= cloth.nodeCount) {
335
+ continue;
336
+ }
337
+ auto& nA = cloth.nodes[bend.nodeA];
338
+ auto& nB = cloth.nodes[bend.nodeB];
339
+
340
+ if (bend.nodeC < cloth.nodeCount && bend.nodeD < cloth.nodeCount) {
341
+ // 4-Node Dihedral Bending across shared edge (nodeC, nodeD)
342
+ auto& nC = cloth.nodes[bend.nodeC];
343
+ auto& nD = cloth.nodes[bend.nodeD];
344
+
345
+ const ClothVec3 edge = subClothVec3(nD.position, nC.position);
346
+ const double eLen = lengthClothVec3(edge);
347
+ if (eLen <= 1e-12) continue;
348
+
349
+ const ClothVec3 n1 = crossClothVec3(edge, subClothVec3(nA.position, nC.position));
350
+ const ClothVec3 n2 = crossClothVec3(subClothVec3(nB.position, nC.position), edge);
351
+ const double l1 = lengthClothVec3(n1);
352
+ const double l2 = lengthClothVec3(n2);
353
+ if (l1 <= 1e-12 || l2 <= 1e-12) continue;
354
+
355
+ const ClothVec3 u1 = scaleClothVec3(n1, 1.0 / l1);
356
+ const ClothVec3 u2 = scaleClothVec3(n2, 1.0 / l2);
357
+ const double cosTheta = clothClamp(dotClothVec3(u1, u2), -1.0, 1.0);
358
+ const double theta = clothAcos(cosTheta);
359
+ bend.currentAngle = theta;
360
+
361
+ const double diff = theta - bend.restAngle;
362
+ const double wTot = nA.inverseMass + nB.inverseMass + 0.25 * (nC.inverseMass + nD.inverseMass);
363
+ if (wTot <= 1e-12) continue;
364
+
365
+ const double stiffness = clothClamp(bend.bendStiffness, 0.0, 1.0);
366
+ const double dLambda = (diff / wTot) * stiffness;
367
+ accumulatedBendLambda[b] += dLambda;
368
+
369
+ nA.position = subClothVec3(nA.position, scaleClothVec3(u1, dLambda * nA.inverseMass));
370
+ nB.position = subClothVec3(nB.position, scaleClothVec3(u2, dLambda * nB.inverseMass));
371
+ nC.position = addClothVec3(nC.position, scaleClothVec3(u1, dLambda * 0.25 * nC.inverseMass));
372
+ nD.position = addClothVec3(nD.position, scaleClothVec3(u2, dLambda * 0.25 * nD.inverseMass));
373
+ } else if (bend.nodeC < cloth.nodeCount) {
374
+ // 3-Node Angle Bending: nodeC between nodeA and nodeB
375
+ auto& nC = cloth.nodes[bend.nodeC];
376
+ const ClothVec3 mid = scaleClothVec3(addClothVec3(nA.position, nB.position), 0.5);
377
+ const ClothVec3 h = subClothVec3(nC.position, mid);
378
+ const double hLen = lengthClothVec3(h);
379
+ bend.currentAngle = hLen;
380
+
381
+ const double diff = hLen - bend.restAngle;
382
+ const double wTot = 0.25 * nA.inverseMass + 0.25 * nB.inverseMass + nC.inverseMass;
383
+ if (wTot > 1e-12 && hLen > 1e-12) {
384
+ const double stiffness = clothClamp(bend.bendStiffness, 0.0, 1.0);
385
+ const double dLambda = (diff / wTot) * stiffness;
386
+ accumulatedBendLambda[b] += dLambda;
387
+
388
+ const ClothVec3 dir = scaleClothVec3(h, 1.0 / hLen);
389
+ const ClothVec3 corr = scaleClothVec3(dir, dLambda);
390
+ nC.position = subClothVec3(nC.position, scaleClothVec3(corr, nC.inverseMass));
391
+ nA.position = addClothVec3(nA.position, scaleClothVec3(corr, 0.25 * nA.inverseMass));
392
+ nB.position = addClothVec3(nB.position, scaleClothVec3(corr, 0.25 * nB.inverseMass));
393
+ }
394
+ }
395
+ }
396
+
397
+ // F. Ground Collision & Rigid Body Obstacles
398
+ if (cloth.params.enableGround) {
399
+ const double floorY = cloth.params.groundY + cloth.params.collisionRadius;
400
+ for (std::uint32_t i = 0; i < cloth.nodeCount; ++i) {
401
+ auto& node = cloth.nodes[i];
402
+ if (node.inverseMass <= 0.0) continue;
403
+ if (node.position.y < floorY) {
404
+ node.position.y = floorY;
405
+ const double dx = node.position.x - node.previousPosition.x;
406
+ const double dz = node.position.z - node.previousPosition.z;
407
+ const double frictScale = clothClamp(1.0 - cloth.params.groundFriction, 0.0, 1.0);
408
+ node.position.x = node.previousPosition.x + dx * frictScale;
409
+ node.position.z = node.previousPosition.z + dz * frictScale;
410
+ }
411
+ }
412
+ }
413
+
414
+ // Rigid body obstacle collision
415
+ for (const auto& body : bodies) {
416
+ if (body.shape == ShapeKind::Sphere) {
417
+ const ClothVec3 bc{body.x, body.y, body.z};
418
+ const double r = body.shape0 + cloth.params.collisionRadius;
419
+ const double rSq = r * r;
420
+ for (std::uint32_t i = 0; i < cloth.nodeCount; ++i) {
421
+ auto& node = cloth.nodes[i];
422
+ if (node.inverseMass <= 0.0) continue;
423
+ const ClothVec3 delta = subClothVec3(node.position, bc);
424
+ const double d2 = lengthSquaredClothVec3(delta);
425
+ if (d2 < rSq && d2 > 1e-12) {
426
+ const double dist = clothSqrt(d2);
427
+ const ClothVec3 n = scaleClothVec3(delta, 1.0 / dist);
428
+ node.position = addClothVec3(bc, scaleClothVec3(n, r));
429
+ }
430
+ }
431
+ }
432
+ }
433
+ }
434
+
435
+ // 4. Velocity Update from Resolved Positions
436
+ for (std::uint32_t i = 0; i < cloth.nodeCount; ++i) {
437
+ auto& node = cloth.nodes[i];
438
+ if (node.inverseMass <= 0.0) {
439
+ node.velocity = {0.0, 0.0, 0.0};
440
+ } else {
441
+ node.velocity = scaleClothVec3(subClothVec3(node.position, node.previousPosition), invDt);
442
+ }
443
+ node.normal = {0.0, 0.0, 0.0};
444
+ }
445
+
446
+ // 5. Update Vertex Normals from Adjacent Faces
447
+ for (std::uint32_t f = 0; f < cloth.faceCount; ++f) {
448
+ const auto& face = cloth.faces[f];
449
+ if (face.nodeA >= cloth.nodeCount || face.nodeB >= cloth.nodeCount || face.nodeC >= cloth.nodeCount) {
450
+ continue;
451
+ }
452
+ const ClothVec3 e1 = subClothVec3(cloth.nodes[face.nodeB].position, cloth.nodes[face.nodeA].position);
453
+ const ClothVec3 e2 = subClothVec3(cloth.nodes[face.nodeC].position, cloth.nodes[face.nodeA].position);
454
+ const ClothVec3 n = crossClothVec3(e1, e2);
455
+ cloth.nodes[face.nodeA].normal = addClothVec3(cloth.nodes[face.nodeA].normal, n);
456
+ cloth.nodes[face.nodeB].normal = addClothVec3(cloth.nodes[face.nodeB].normal, n);
457
+ cloth.nodes[face.nodeC].normal = addClothVec3(cloth.nodes[face.nodeC].normal, n);
458
+ }
459
+ for (std::uint32_t i = 0; i < cloth.nodeCount; ++i) {
460
+ const double nLen = lengthClothVec3(cloth.nodes[i].normal);
461
+ if (nLen > 1e-9) {
462
+ cloth.nodes[i].normal = scaleClothVec3(cloth.nodes[i].normal, 1.0 / nLen);
463
+ } else {
464
+ cloth.nodes[i].normal = {0.0, 1.0, 0.0};
465
+ }
466
+ }
467
+
468
+ // 6. Update Metrics & Check Break Events
469
+ for (std::uint32_t s = 0; s < cloth.stretchCount; ++s) {
470
+ auto& seg = cloth.stretchConstraints[s];
471
+ if (seg.broken) continue;
472
+ seg.tension = std::abs(accumulatedStretchLambda[s]) * invDtSq;
473
+ if (seg.tension > cloth.maxTension) {
474
+ cloth.maxTension = seg.tension;
475
+ }
476
+ if (seg.tension > cloth.params.breakThreshold) {
477
+ seg.broken = true;
478
+ if (cloth.breakEventCount < kClothMaxBreakEvents) {
479
+ cloth.breakEvents[cloth.breakEventCount++] = {
480
+ .clothId = cloth.id,
481
+ .elementIndex = s,
482
+ .reason = ClothBreakReason::TensionExceeded,
483
+ .measuredValue = seg.tension,
484
+ };
485
+ }
486
+ }
487
+ }
488
+
489
+ for (std::uint32_t sh = 0; sh < cloth.shearCount; ++sh) {
490
+ auto& shear = cloth.shearConstraints[sh];
491
+ if (shear.broken) continue;
492
+ shear.shearForce = std::abs(accumulatedShearLambda[sh]) * invDtSq;
493
+ if (shear.shearForce > cloth.maxShear) {
494
+ cloth.maxShear = shear.shearForce;
495
+ }
496
+ if (shear.shearForce > cloth.params.breakThreshold) {
497
+ shear.broken = true;
498
+ if (cloth.breakEventCount < kClothMaxBreakEvents) {
499
+ cloth.breakEvents[cloth.breakEventCount++] = {
500
+ .clothId = cloth.id,
501
+ .elementIndex = sh,
502
+ .reason = ClothBreakReason::ShearExceeded,
503
+ .measuredValue = shear.shearForce,
504
+ };
505
+ }
506
+ }
507
+ }
508
+
509
+ for (std::uint32_t b = 0; b < cloth.bendCount; ++b) {
510
+ auto& bend = cloth.bendConstraints[b];
511
+ if (bend.broken) continue;
512
+ bend.bendingMoment = std::abs(accumulatedBendLambda[b]) * invDtSq;
513
+ if (bend.bendingMoment > cloth.maxBend) {
514
+ cloth.maxBend = bend.bendingMoment;
515
+ }
516
+ if (bend.bendingMoment > cloth.params.breakThreshold) {
517
+ bend.broken = true;
518
+ if (cloth.breakEventCount < kClothMaxBreakEvents) {
519
+ cloth.breakEvents[cloth.breakEventCount++] = {
520
+ .clothId = cloth.id,
521
+ .elementIndex = b,
522
+ .reason = ClothBreakReason::BendExceeded,
523
+ .measuredValue = bend.bendingMoment,
524
+ };
525
+ }
526
+ }
527
+ }
528
+
529
+ // 7. Update Pin Reaction Forces
530
+ for (std::uint32_t p = 0; p < cloth.pinCount; ++p) {
531
+ const auto& pin = cloth.pins[p];
532
+ if (!pin.active || pin.nodeIndex >= cloth.nodeCount) continue;
533
+ if (cloth.nodes[pin.nodeIndex].inverseMass <= 0.0) {
534
+ // Hard pinned vertex: sum tension of attached stretch constraints
535
+ double attachedTension = 0.0;
536
+ for (std::uint32_t s = 0; s < cloth.stretchCount; ++s) {
537
+ const auto& seg = cloth.stretchConstraints[s];
538
+ if (seg.nodeA == pin.nodeIndex || seg.nodeB == pin.nodeIndex) {
539
+ attachedTension += seg.tension;
540
+ }
541
+ }
542
+ cloth.pins[p].reactionForce = attachedTension;
543
+ } else {
544
+ cloth.pins[p].reactionForce = accumulatedPinImpulse[p] * invDtSq;
545
+ }
546
+ }
547
+
548
+ return ClothStatus::Success;
549
+ }
550
+
551
+ ClothStatus initClothGrid(
552
+ ClothSimulation& cloth,
553
+ double width,
554
+ double height,
555
+ std::uint32_t cols,
556
+ std::uint32_t rows,
557
+ ClothGridOrientation orientation,
558
+ double totalMass,
559
+ double stretchStiffness,
560
+ double shearStiffness,
561
+ double bendStiffness,
562
+ bool pinTopCorners,
563
+ bool pinTopEdge) {
564
+ if (cols < 2 || rows < 2) return ClothStatus::InvalidNodeCount;
565
+ const std::uint32_t totalNodes = cols * rows;
566
+ if (totalNodes > kClothMaxNodes) return ClothStatus::BufferOverflow;
567
+
568
+ cloth.nodeCount = totalNodes;
569
+ const double dx = width / static_cast<double>(cols - 1);
570
+ const double dy = height / static_cast<double>(rows - 1);
571
+ const double nodeMass = (totalMass > 0.0) ? (totalMass / static_cast<double>(totalNodes)) : 1.0;
572
+ const double nodeInvMass = 1.0 / nodeMass;
573
+
574
+ cloth.pinCount = 0;
575
+
576
+ // Initialize Nodes
577
+ for (std::uint32_t r = 0; r < rows; ++r) {
578
+ for (std::uint32_t c = 0; c < cols; ++c) {
579
+ const std::uint32_t idx = r * cols + c;
580
+ ClothVec3 pos{0.0, 0.0, 0.0};
581
+ if (orientation == ClothGridOrientation::XY) {
582
+ pos = {
583
+ static_cast<double>(c) * dx - width * 0.5,
584
+ height * 0.5 - static_cast<double>(r) * dy,
585
+ 0.0
586
+ };
587
+ } else if (orientation == ClothGridOrientation::XZ) {
588
+ pos = {
589
+ static_cast<double>(c) * dx - width * 0.5,
590
+ 0.0,
591
+ static_cast<double>(r) * dy - height * 0.5
592
+ };
593
+ } else {
594
+ pos = {
595
+ 0.0,
596
+ height * 0.5 - static_cast<double>(r) * dy,
597
+ static_cast<double>(c) * dx - width * 0.5
598
+ };
599
+ }
600
+
601
+ cloth.nodes[idx].position = pos;
602
+ cloth.nodes[idx].previousPosition = pos;
603
+ cloth.nodes[idx].velocity = {0.0, 0.0, 0.0};
604
+ cloth.nodes[idx].normal = {0.0, 1.0, 0.0};
605
+
606
+ const bool isCorner = (r == 0 && (c == 0 || c == cols - 1));
607
+ const bool isTopEdge = (r == 0);
608
+
609
+ if ((pinTopCorners && isCorner) || (pinTopEdge && isTopEdge)) {
610
+ cloth.nodes[idx].inverseMass = 0.0;
611
+ addClothPin(cloth, idx, -1, pos, 1.0);
612
+ } else {
613
+ cloth.nodes[idx].inverseMass = nodeInvMass;
614
+ }
615
+ }
616
+ }
617
+
618
+ // Initialize Stretch Constraints (Horizontal & Vertical Edges)
619
+ cloth.stretchCount = 0;
620
+ for (std::uint32_t r = 0; r < rows; ++r) {
621
+ for (std::uint32_t c = 0; c < cols; ++c) {
622
+ const std::uint32_t idx = r * cols + c;
623
+ if (c + 1 < cols && cloth.stretchCount < kClothMaxStretchConstraints) {
624
+ cloth.stretchConstraints[cloth.stretchCount++] = {
625
+ .nodeA = idx,
626
+ .nodeB = idx + 1,
627
+ .restLength = dx,
628
+ .stiffness = stretchStiffness,
629
+ .damping = 0.01,
630
+ .currentLength = dx,
631
+ .tension = 0.0,
632
+ .broken = false,
633
+ };
634
+ }
635
+ if (r + 1 < rows && cloth.stretchCount < kClothMaxStretchConstraints) {
636
+ cloth.stretchConstraints[cloth.stretchCount++] = {
637
+ .nodeA = idx,
638
+ .nodeB = idx + cols,
639
+ .restLength = dy,
640
+ .stiffness = stretchStiffness,
641
+ .damping = 0.01,
642
+ .currentLength = dy,
643
+ .tension = 0.0,
644
+ .broken = false,
645
+ };
646
+ }
647
+ }
648
+ }
649
+
650
+ // Initialize Shear Constraints (Quad Diagonals)
651
+ cloth.shearCount = 0;
652
+ const double diagLen = clothSqrt(dx * dx + dy * dy);
653
+ for (std::uint32_t r = 0; r + 1 < rows; ++r) {
654
+ for (std::uint32_t c = 0; c + 1 < cols; ++c) {
655
+ const std::uint32_t v00 = r * cols + c;
656
+ const std::uint32_t v10 = v00 + 1;
657
+ const std::uint32_t v01 = v00 + cols;
658
+ const std::uint32_t v11 = v01 + 1;
659
+
660
+ if (cloth.shearCount < kClothMaxShearConstraints) {
661
+ cloth.shearConstraints[cloth.shearCount++] = {
662
+ .nodeA = v00,
663
+ .nodeB = v11,
664
+ .restLength = diagLen,
665
+ .stiffness = shearStiffness,
666
+ .damping = 0.01,
667
+ .currentLength = diagLen,
668
+ .shearForce = 0.0,
669
+ .broken = false,
670
+ };
671
+ }
672
+ if (cloth.shearCount < kClothMaxShearConstraints) {
673
+ cloth.shearConstraints[cloth.shearCount++] = {
674
+ .nodeA = v10,
675
+ .nodeB = v01,
676
+ .restLength = diagLen,
677
+ .stiffness = shearStiffness,
678
+ .damping = 0.01,
679
+ .currentLength = diagLen,
680
+ .shearForce = 0.0,
681
+ .broken = false,
682
+ };
683
+ }
684
+ }
685
+ }
686
+
687
+ // Initialize Bend Constraints (Across adjacent cells & 4-node dihedral)
688
+ cloth.bendCount = 0;
689
+ for (std::uint32_t r = 0; r < rows; ++r) {
690
+ for (std::uint32_t c = 0; c < cols; ++c) {
691
+ const std::uint32_t idx = r * cols + c;
692
+ if (c + 2 < cols && cloth.bendCount < kClothMaxBendConstraints) {
693
+ cloth.bendConstraints[cloth.bendCount++] = {
694
+ .nodeA = idx,
695
+ .nodeB = idx + 2,
696
+ .nodeC = idx + 1,
697
+ .nodeD = static_cast<std::uint32_t>(-1),
698
+ .restAngle = 0.0,
699
+ .bendStiffness = bendStiffness,
700
+ .currentAngle = 0.0,
701
+ .bendingMoment = 0.0,
702
+ .broken = false,
703
+ };
704
+ }
705
+ if (r + 2 < rows && cloth.bendCount < kClothMaxBendConstraints) {
706
+ cloth.bendConstraints[cloth.bendCount++] = {
707
+ .nodeA = idx,
708
+ .nodeB = idx + 2 * cols,
709
+ .nodeC = idx + cols,
710
+ .nodeD = static_cast<std::uint32_t>(-1),
711
+ .restAngle = 0.0,
712
+ .bendStiffness = bendStiffness,
713
+ .currentAngle = 0.0,
714
+ .bendingMoment = 0.0,
715
+ .broken = false,
716
+ };
717
+ }
718
+ }
719
+ }
720
+
721
+ // Initialize Triangular Faces
722
+ cloth.faceCount = 0;
723
+ const double faceArea = 0.5 * dx * dy;
724
+ for (std::uint32_t r = 0; r + 1 < rows; ++r) {
725
+ for (std::uint32_t c = 0; c + 1 < cols; ++c) {
726
+ const std::uint32_t v00 = r * cols + c;
727
+ const std::uint32_t v10 = v00 + 1;
728
+ const std::uint32_t v01 = v00 + cols;
729
+ const std::uint32_t v11 = v01 + 1;
730
+
731
+ if (cloth.faceCount < kClothMaxFaces) {
732
+ cloth.faces[cloth.faceCount++] = {
733
+ .nodeA = v00,
734
+ .nodeB = v01,
735
+ .nodeC = v11,
736
+ .restArea = faceArea,
737
+ .currentArea = faceArea,
738
+ .normal = {0.0, 1.0, 0.0},
739
+ .aerodynamicForce = {0.0, 0.0, 0.0},
740
+ };
741
+ }
742
+ if (cloth.faceCount < kClothMaxFaces) {
743
+ cloth.faces[cloth.faceCount++] = {
744
+ .nodeA = v00,
745
+ .nodeB = v11,
746
+ .nodeC = v10,
747
+ .restArea = faceArea,
748
+ .currentArea = faceArea,
749
+ .normal = {0.0, 1.0, 0.0},
750
+ .aerodynamicForce = {0.0, 0.0, 0.0},
751
+ };
752
+ }
753
+ }
754
+ }
755
+
756
+ // Initialize Tethers (Long-Range Attachments to anchor nodes)
757
+ cloth.tetherCount = 0;
758
+ if (pinTopCorners || pinTopEdge) {
759
+ const std::uint32_t anchorLeft = 0;
760
+ const std::uint32_t anchorRight = cols - 1;
761
+ for (std::uint32_t r = 1; r < rows; ++r) {
762
+ for (std::uint32_t c = 0; c < cols; ++c) {
763
+ const std::uint32_t idx = r * cols + c;
764
+ const std::uint32_t anchor = (c < cols / 2) ? anchorLeft : anchorRight;
765
+ const double restDist = lengthClothVec3(
766
+ subClothVec3(cloth.nodes[idx].position, cloth.nodes[anchor].position));
767
+ addClothTether(cloth, idx, anchor, restDist * cloth.params.tetherLimitRatio, 1.0);
768
+ }
769
+ }
770
+ }
771
+
772
+ return ClothStatus::Success;
773
+ }
774
+
775
+ ClothStatus addClothPin(
776
+ ClothSimulation& cloth,
777
+ std::uint32_t nodeIndex,
778
+ std::int32_t bodyIndex,
779
+ const ClothVec3& localOffset,
780
+ double pinStiffness) {
781
+ if (nodeIndex >= cloth.nodeCount || cloth.pinCount >= kClothMaxPins) {
782
+ return ClothStatus::BufferOverflow;
783
+ }
784
+ cloth.pins[cloth.pinCount++] = {
785
+ .nodeIndex = nodeIndex,
786
+ .bodyIndex = bodyIndex,
787
+ .localOffset = localOffset,
788
+ .worldPosition = localOffset,
789
+ .pinStiffness = clothClamp(pinStiffness, 0.0, 1.0),
790
+ .reactionForce = 0.0,
791
+ .active = true,
792
+ };
793
+ return ClothStatus::Success;
794
+ }
795
+
796
+ ClothStatus addClothTether(
797
+ ClothSimulation& cloth,
798
+ std::uint32_t nodeIndex,
799
+ std::uint32_t anchorNodeIndex,
800
+ double maxDistance,
801
+ double stiffness) {
802
+ if (nodeIndex >= cloth.nodeCount || anchorNodeIndex >= cloth.nodeCount || cloth.tetherCount >= kClothMaxTethers) {
803
+ return ClothStatus::BufferOverflow;
804
+ }
805
+ cloth.tethers[cloth.tetherCount++] = {
806
+ .nodeIndex = nodeIndex,
807
+ .anchorNodeIndex = anchorNodeIndex,
808
+ .maxDistance = maxDistance,
809
+ .stiffness = clothClamp(stiffness, 0.0, 1.0),
810
+ .currentDistance = maxDistance,
811
+ .tension = 0.0,
812
+ .active = true,
813
+ };
814
+ return ClothStatus::Success;
815
+ }
816
+
817
+ namespace {
818
+ static ClothSimulation sRawCloth;
819
+ static std::array<Body, 64> sRawClothBodies;
820
+ }
821
+
822
+ ClothStatus stepClothRaw(
823
+ double* nodeData,
824
+ std::uint32_t nodeCount,
825
+ double* stretchData,
826
+ std::uint32_t stretchCount,
827
+ double* shearData,
828
+ std::uint32_t shearCount,
829
+ double* bendData,
830
+ std::uint32_t bendCount,
831
+ double* faceData,
832
+ std::uint32_t faceCount,
833
+ double* pinData,
834
+ std::uint32_t pinCount,
835
+ double* tetherData,
836
+ std::uint32_t tetherCount,
837
+ double* bodyData,
838
+ std::uint32_t bodyCount,
839
+ const double* paramsData,
840
+ double* breakEventsData,
841
+ std::uint32_t* breakEventCount,
842
+ double dt) {
843
+ if (nodeData == nullptr || nodeCount < 3) {
844
+ return ClothStatus::InvalidInput;
845
+ }
846
+ if (nodeCount > kClothMaxNodes || stretchCount > kClothMaxStretchConstraints ||
847
+ shearCount > kClothMaxShearConstraints || bendCount > kClothMaxBendConstraints ||
848
+ faceCount > kClothMaxFaces || pinCount > kClothMaxPins || tetherCount > kClothMaxTethers) {
849
+ return ClothStatus::BufferOverflow;
850
+ }
851
+
852
+ ClothSimulation& sim = sRawCloth;
853
+ sim.nodeCount = nodeCount;
854
+ for (std::uint32_t i = 0; i < nodeCount; ++i) {
855
+ const std::uint32_t o = i * kClothNodeStride;
856
+ sim.nodes[i].position = {nodeData[o], nodeData[o + 1], nodeData[o + 2]};
857
+ sim.nodes[i].previousPosition = {nodeData[o + 3], nodeData[o + 4], nodeData[o + 5]};
858
+ sim.nodes[i].velocity = {nodeData[o + 6], nodeData[o + 7], nodeData[o + 8]};
859
+ sim.nodes[i].normal = {nodeData[o + 9], nodeData[o + 10], nodeData[o + 11]};
860
+ sim.nodes[i].inverseMass = nodeData[o + 12];
861
+ }
862
+
863
+ sim.stretchCount = (stretchData != nullptr) ? stretchCount : 0;
864
+ for (std::uint32_t i = 0; i < sim.stretchCount; ++i) {
865
+ const std::uint32_t o = i * kClothStretchStride;
866
+ sim.stretchConstraints[i].nodeA = static_cast<std::uint32_t>(stretchData[o]);
867
+ sim.stretchConstraints[i].nodeB = static_cast<std::uint32_t>(stretchData[o + 1]);
868
+ sim.stretchConstraints[i].restLength = stretchData[o + 2];
869
+ sim.stretchConstraints[i].stiffness = stretchData[o + 3];
870
+ sim.stretchConstraints[i].damping = stretchData[o + 4];
871
+ sim.stretchConstraints[i].currentLength = stretchData[o + 5];
872
+ sim.stretchConstraints[i].tension = stretchData[o + 6];
873
+ sim.stretchConstraints[i].broken = stretchData[o + 7] > 0.5;
874
+ }
875
+
876
+ sim.shearCount = (shearData != nullptr) ? shearCount : 0;
877
+ for (std::uint32_t i = 0; i < sim.shearCount; ++i) {
878
+ const std::uint32_t o = i * kClothShearStride;
879
+ sim.shearConstraints[i].nodeA = static_cast<std::uint32_t>(shearData[o]);
880
+ sim.shearConstraints[i].nodeB = static_cast<std::uint32_t>(shearData[o + 1]);
881
+ sim.shearConstraints[i].restLength = shearData[o + 2];
882
+ sim.shearConstraints[i].stiffness = shearData[o + 3];
883
+ sim.shearConstraints[i].damping = shearData[o + 4];
884
+ sim.shearConstraints[i].currentLength = shearData[o + 5];
885
+ sim.shearConstraints[i].shearForce = shearData[o + 6];
886
+ sim.shearConstraints[i].broken = shearData[o + 7] > 0.5;
887
+ }
888
+
889
+ sim.bendCount = (bendData != nullptr) ? bendCount : 0;
890
+ for (std::uint32_t i = 0; i < sim.bendCount; ++i) {
891
+ const std::uint32_t o = i * kClothBendStride;
892
+ sim.bendConstraints[i].nodeA = static_cast<std::uint32_t>(bendData[o]);
893
+ sim.bendConstraints[i].nodeB = static_cast<std::uint32_t>(bendData[o + 1]);
894
+ sim.bendConstraints[i].nodeC = static_cast<std::uint32_t>(bendData[o + 2]);
895
+ sim.bendConstraints[i].nodeD = static_cast<std::uint32_t>(bendData[o + 3]);
896
+ sim.bendConstraints[i].restAngle = bendData[o + 4];
897
+ sim.bendConstraints[i].bendStiffness = bendData[o + 5];
898
+ sim.bendConstraints[i].currentAngle = bendData[o + 6];
899
+ sim.bendConstraints[i].bendingMoment = bendData[o + 7];
900
+ sim.bendConstraints[i].broken = bendData[o + 8] > 0.5;
901
+ }
902
+
903
+ sim.faceCount = (faceData != nullptr) ? faceCount : 0;
904
+ for (std::uint32_t i = 0; i < sim.faceCount; ++i) {
905
+ const std::uint32_t o = i * kClothFaceStride;
906
+ sim.faces[i].nodeA = static_cast<std::uint32_t>(faceData[o]);
907
+ sim.faces[i].nodeB = static_cast<std::uint32_t>(faceData[o + 1]);
908
+ sim.faces[i].nodeC = static_cast<std::uint32_t>(faceData[o + 2]);
909
+ sim.faces[i].restArea = faceData[o + 3];
910
+ sim.faces[i].currentArea = faceData[o + 4];
911
+ sim.faces[i].normal = {faceData[o + 5], faceData[o + 6], faceData[o + 7]};
912
+ sim.faces[i].aerodynamicForce = {faceData[o + 8], 0.0, 0.0};
913
+ }
914
+
915
+ sim.pinCount = (pinData != nullptr) ? pinCount : 0;
916
+ for (std::uint32_t i = 0; i < sim.pinCount; ++i) {
917
+ const std::uint32_t o = i * kClothPinStride;
918
+ sim.pins[i].nodeIndex = static_cast<std::uint32_t>(pinData[o]);
919
+ sim.pins[i].bodyIndex = static_cast<std::int32_t>(pinData[o + 1]);
920
+ sim.pins[i].localOffset = {pinData[o + 2], pinData[o + 3], pinData[o + 4]};
921
+ sim.pins[i].worldPosition = {pinData[o + 5], pinData[o + 6], pinData[o + 7]};
922
+ sim.pins[i].pinStiffness = pinData[o + 8];
923
+ sim.pins[i].reactionForce = pinData[o + 9];
924
+ sim.pins[i].active = pinData[o + 10] > 0.5;
925
+ }
926
+
927
+ sim.tetherCount = (tetherData != nullptr) ? tetherCount : 0;
928
+ for (std::uint32_t i = 0; i < sim.tetherCount; ++i) {
929
+ const std::uint32_t o = i * kClothTetherStride;
930
+ sim.tethers[i].nodeIndex = static_cast<std::uint32_t>(tetherData[o]);
931
+ sim.tethers[i].anchorNodeIndex = static_cast<std::uint32_t>(tetherData[o + 1]);
932
+ sim.tethers[i].maxDistance = tetherData[o + 2];
933
+ sim.tethers[i].stiffness = tetherData[o + 3];
934
+ sim.tethers[i].currentDistance = tetherData[o + 4];
935
+ sim.tethers[i].tension = tetherData[o + 5];
936
+ sim.tethers[i].active = tetherData[o + 6] > 0.5;
937
+ }
938
+
939
+ if (paramsData != nullptr) {
940
+ sim.params.gravityX = paramsData[0];
941
+ sim.params.gravityY = paramsData[1];
942
+ sim.params.gravityZ = paramsData[2];
943
+ sim.params.damping = paramsData[3];
944
+ sim.params.airDensity = paramsData[4];
945
+ sim.params.windX = paramsData[5];
946
+ sim.params.windY = paramsData[6];
947
+ sim.params.windZ = paramsData[7];
948
+ sim.params.dragCoefficient = paramsData[8];
949
+ sim.params.liftCoefficient = paramsData[9];
950
+ sim.params.solverIterations = static_cast<std::uint32_t>(paramsData[10]);
951
+ sim.params.collisionRadius = paramsData[11];
952
+ sim.params.enableGround = paramsData[12] > 0.5;
953
+ sim.params.groundY = paramsData[13];
954
+ sim.params.groundFriction = paramsData[14];
955
+ sim.params.groundRestitution = paramsData[15];
956
+ sim.params.breakThreshold = paramsData[16];
957
+ sim.params.tetherLimitRatio = paramsData[17];
958
+ sim.params.enableTethers = paramsData[18] > 0.5;
959
+ sim.params.enableAerodynamics = paramsData[19] > 0.5;
960
+ }
961
+
962
+ const std::uint32_t clampedBodyCount = std::min(bodyCount, static_cast<std::uint32_t>(sRawClothBodies.size()));
963
+ if (bodyData != nullptr && clampedBodyCount > 0) {
964
+ for (std::uint32_t i = 0; i < clampedBodyCount; ++i) {
965
+ const std::uint32_t o = i * kBodyStride;
966
+ sRawClothBodies[i].x = bodyData[o + 0];
967
+ sRawClothBodies[i].y = bodyData[o + 1];
968
+ sRawClothBodies[i].z = bodyData[o + 2];
969
+ sRawClothBodies[i].vx = bodyData[o + 3];
970
+ sRawClothBodies[i].vy = bodyData[o + 4];
971
+ sRawClothBodies[i].vz = bodyData[o + 5];
972
+ const std::uint32_t motionType = static_cast<std::uint32_t>(bodyData[o + 9]);
973
+ sRawClothBodies[i].dynamic = (motionType & kMotionKindMask) == 1;
974
+ sRawClothBodies[i].shape = static_cast<ShapeKind>(static_cast<std::uint32_t>(bodyData[o + 10]));
975
+ sRawClothBodies[i].mass = bodyData[o + 18];
976
+ sRawClothBodies[i].orientationX = bodyData[o + 22];
977
+ sRawClothBodies[i].orientationY = bodyData[o + 23];
978
+ sRawClothBodies[i].orientationZ = bodyData[o + 24];
979
+ sRawClothBodies[i].orientationW = bodyData[o + 25];
980
+ sRawClothBodies[i].shape0 = bodyData[o + 30];
981
+ sRawClothBodies[i].shape1 = bodyData[o + 31];
982
+ sRawClothBodies[i].shape2 = bodyData[o + 32];
983
+ }
984
+ }
985
+
986
+ std::span<Body> bodiesSpan(sRawClothBodies.data(), clampedBodyCount);
987
+ const ClothStatus status = stepCloth(sim, bodiesSpan, dt);
988
+ if (status != ClothStatus::Success) return status;
989
+
990
+ // Marshal State Back to Buffers
991
+ for (std::uint32_t i = 0; i < nodeCount; ++i) {
992
+ const std::uint32_t o = i * kClothNodeStride;
993
+ nodeData[o] = sim.nodes[i].position.x;
994
+ nodeData[o + 1] = sim.nodes[i].position.y;
995
+ nodeData[o + 2] = sim.nodes[i].position.z;
996
+ nodeData[o + 3] = sim.nodes[i].previousPosition.x;
997
+ nodeData[o + 4] = sim.nodes[i].previousPosition.y;
998
+ nodeData[o + 5] = sim.nodes[i].previousPosition.z;
999
+ nodeData[o + 6] = sim.nodes[i].velocity.x;
1000
+ nodeData[o + 7] = sim.nodes[i].velocity.y;
1001
+ nodeData[o + 8] = sim.nodes[i].velocity.z;
1002
+ nodeData[o + 9] = sim.nodes[i].normal.x;
1003
+ nodeData[o + 10] = sim.nodes[i].normal.y;
1004
+ nodeData[o + 11] = sim.nodes[i].normal.z;
1005
+ nodeData[o + 12] = sim.nodes[i].inverseMass;
1006
+ }
1007
+
1008
+ for (std::uint32_t i = 0; i < sim.stretchCount; ++i) {
1009
+ const std::uint32_t o = i * kClothStretchStride;
1010
+ stretchData[o + 5] = sim.stretchConstraints[i].currentLength;
1011
+ stretchData[o + 6] = sim.stretchConstraints[i].tension;
1012
+ stretchData[o + 7] = sim.stretchConstraints[i].broken ? 1.0 : 0.0;
1013
+ }
1014
+
1015
+ if (shearData != nullptr && sim.shearCount > 0) {
1016
+ for (std::uint32_t i = 0; i < sim.shearCount; ++i) {
1017
+ const std::uint32_t o = i * kClothShearStride;
1018
+ shearData[o + 5] = sim.shearConstraints[i].currentLength;
1019
+ shearData[o + 6] = sim.shearConstraints[i].shearForce;
1020
+ shearData[o + 7] = sim.shearConstraints[i].broken ? 1.0 : 0.0;
1021
+ }
1022
+ }
1023
+
1024
+ if (bendData != nullptr && sim.bendCount > 0) {
1025
+ for (std::uint32_t i = 0; i < sim.bendCount; ++i) {
1026
+ const std::uint32_t o = i * kClothBendStride;
1027
+ bendData[o + 6] = sim.bendConstraints[i].currentAngle;
1028
+ bendData[o + 7] = sim.bendConstraints[i].bendingMoment;
1029
+ bendData[o + 8] = sim.bendConstraints[i].broken ? 1.0 : 0.0;
1030
+ }
1031
+ }
1032
+
1033
+ if (faceData != nullptr && sim.faceCount > 0) {
1034
+ for (std::uint32_t i = 0; i < sim.faceCount; ++i) {
1035
+ const std::uint32_t o = i * kClothFaceStride;
1036
+ faceData[o + 4] = sim.faces[i].currentArea;
1037
+ faceData[o + 5] = sim.faces[i].normal.x;
1038
+ faceData[o + 6] = sim.faces[i].normal.y;
1039
+ faceData[o + 7] = sim.faces[i].normal.z;
1040
+ faceData[o + 8] = lengthClothVec3(sim.faces[i].aerodynamicForce);
1041
+ }
1042
+ }
1043
+
1044
+ if (pinData != nullptr && sim.pinCount > 0) {
1045
+ for (std::uint32_t i = 0; i < sim.pinCount; ++i) {
1046
+ const std::uint32_t o = i * kClothPinStride;
1047
+ pinData[o + 5] = sim.pins[i].worldPosition.x;
1048
+ pinData[o + 6] = sim.pins[i].worldPosition.y;
1049
+ pinData[o + 7] = sim.pins[i].worldPosition.z;
1050
+ pinData[o + 9] = sim.pins[i].reactionForce;
1051
+ }
1052
+ }
1053
+
1054
+ if (tetherData != nullptr && sim.tetherCount > 0) {
1055
+ for (std::uint32_t i = 0; i < sim.tetherCount; ++i) {
1056
+ const std::uint32_t o = i * kClothTetherStride;
1057
+ tetherData[o + 4] = sim.tethers[i].currentDistance;
1058
+ tetherData[o + 5] = sim.tethers[i].tension;
1059
+ }
1060
+ }
1061
+
1062
+ if (bodyData != nullptr && clampedBodyCount > 0) {
1063
+ for (std::uint32_t i = 0; i < clampedBodyCount; ++i) {
1064
+ const std::uint32_t o = i * kBodyStride;
1065
+ bodyData[o + 0] = sRawClothBodies[i].x;
1066
+ bodyData[o + 1] = sRawClothBodies[i].y;
1067
+ bodyData[o + 2] = sRawClothBodies[i].z;
1068
+ }
1069
+ }
1070
+
1071
+ if (breakEventsData != nullptr && breakEventCount != nullptr) {
1072
+ const std::uint32_t maxEv = *breakEventCount;
1073
+ const std::uint32_t count = std::min(maxEv, sim.breakEventCount);
1074
+ *breakEventCount = count;
1075
+ for (std::uint32_t i = 0; i < count; ++i) {
1076
+ const std::uint32_t o = i * kClothBreakEventStride;
1077
+ breakEventsData[o] = static_cast<double>(sim.breakEvents[i].clothId);
1078
+ breakEventsData[o + 1] = static_cast<double>(sim.breakEvents[i].elementIndex);
1079
+ breakEventsData[o + 2] = static_cast<double>(sim.breakEvents[i].reason);
1080
+ breakEventsData[o + 3] = sim.breakEvents[i].measuredValue;
1081
+ }
1082
+ }
1083
+
1084
+ return ClothStatus::Success;
1085
+ }
1086
+
1087
+ } // namespace rundot::kinetix::physics
1088
+
1089
+ // C-ABI Exports
1090
+ extern "C" {
1091
+
1092
+ std::uint32_t rundot_kinetix_physics_cloth_abi_version() {
1093
+ return rundot::kinetix::physics::kClothAbiVersion;
1094
+ }
1095
+
1096
+ std::uint32_t rundot_kinetix_physics_cloth_node_stride() {
1097
+ return rundot::kinetix::physics::kClothNodeStride;
1098
+ }
1099
+
1100
+ std::uint32_t rundot_kinetix_physics_cloth_stretch_stride() {
1101
+ return rundot::kinetix::physics::kClothStretchStride;
1102
+ }
1103
+
1104
+ std::uint32_t rundot_kinetix_physics_cloth_shear_stride() {
1105
+ return rundot::kinetix::physics::kClothShearStride;
1106
+ }
1107
+
1108
+ std::uint32_t rundot_kinetix_physics_cloth_bend_stride() {
1109
+ return rundot::kinetix::physics::kClothBendStride;
1110
+ }
1111
+
1112
+ std::uint32_t rundot_kinetix_physics_cloth_face_stride() {
1113
+ return rundot::kinetix::physics::kClothFaceStride;
1114
+ }
1115
+
1116
+ std::uint32_t rundot_kinetix_physics_cloth_pin_stride() {
1117
+ return rundot::kinetix::physics::kClothPinStride;
1118
+ }
1119
+
1120
+ std::uint32_t rundot_kinetix_physics_cloth_tether_stride() {
1121
+ return rundot::kinetix::physics::kClothTetherStride;
1122
+ }
1123
+
1124
+ std::uint32_t rundot_kinetix_physics_cloth_params_stride() {
1125
+ return rundot::kinetix::physics::kClothParamsStride;
1126
+ }
1127
+
1128
+ std::uint32_t rundot_kinetix_physics_cloth_break_event_stride() {
1129
+ return rundot::kinetix::physics::kClothBreakEventStride;
1130
+ }
1131
+
1132
+ std::uint32_t rundot_kinetix_physics_cloth_max_nodes() {
1133
+ return rundot::kinetix::physics::kClothMaxNodes;
1134
+ }
1135
+
1136
+ std::uint32_t rundot_kinetix_physics_cloth_max_faces() {
1137
+ return rundot::kinetix::physics::kClothMaxFaces;
1138
+ }
1139
+
1140
+ std::uint32_t rundot_kinetix_physics_cloth_max_pins() {
1141
+ return rundot::kinetix::physics::kClothMaxPins;
1142
+ }
1143
+
1144
+ std::int32_t rundot_kinetix_physics_cloth_step(
1145
+ double* nodeData,
1146
+ std::uint32_t nodeCount,
1147
+ double* stretchData,
1148
+ std::uint32_t stretchCount,
1149
+ double* shearData,
1150
+ std::uint32_t shearCount,
1151
+ double* bendData,
1152
+ std::uint32_t bendCount,
1153
+ double* faceData,
1154
+ std::uint32_t faceCount,
1155
+ double* pinData,
1156
+ std::uint32_t pinCount,
1157
+ double* tetherData,
1158
+ std::uint32_t tetherCount,
1159
+ double* bodyData,
1160
+ std::uint32_t bodyCount,
1161
+ const double* paramsData,
1162
+ double* breakEventsData,
1163
+ std::uint32_t* breakEventCount,
1164
+ double dt) {
1165
+ return static_cast<std::int32_t>(rundot::kinetix::physics::stepClothRaw(
1166
+ nodeData, nodeCount,
1167
+ stretchData, stretchCount,
1168
+ shearData, shearCount,
1169
+ bendData, bendCount,
1170
+ faceData, faceCount,
1171
+ pinData, pinCount,
1172
+ tetherData, tetherCount,
1173
+ bodyData, bodyCount,
1174
+ paramsData,
1175
+ breakEventsData, breakEventCount,
1176
+ dt));
1177
+ }
1178
+
1179
+ // Extended ABI Aliases
1180
+ std::uint32_t rundot_kinetix_ext_cloth_node_stride() {
1181
+ return rundot::kinetix::physics::kClothNodeStride;
1182
+ }
1183
+
1184
+ std::uint32_t rundot_kinetix_ext_cloth_stretch_stride() {
1185
+ return rundot::kinetix::physics::kClothStretchStride;
1186
+ }
1187
+
1188
+ std::uint32_t rundot_kinetix_ext_cloth_shear_stride() {
1189
+ return rundot::kinetix::physics::kClothShearStride;
1190
+ }
1191
+
1192
+ std::uint32_t rundot_kinetix_ext_cloth_bend_stride() {
1193
+ return rundot::kinetix::physics::kClothBendStride;
1194
+ }
1195
+
1196
+ std::uint32_t rundot_kinetix_ext_cloth_face_stride() {
1197
+ return rundot::kinetix::physics::kClothFaceStride;
1198
+ }
1199
+
1200
+ std::uint32_t rundot_kinetix_ext_cloth_pin_stride() {
1201
+ return rundot::kinetix::physics::kClothPinStride;
1202
+ }
1203
+
1204
+ std::uint32_t rundot_kinetix_ext_cloth_tether_stride() {
1205
+ return rundot::kinetix::physics::kClothTetherStride;
1206
+ }
1207
+
1208
+ std::uint32_t rundot_kinetix_ext_cloth_params_stride() {
1209
+ return rundot::kinetix::physics::kClothParamsStride;
1210
+ }
1211
+
1212
+ std::uint32_t rundot_kinetix_ext_cloth_max_nodes() {
1213
+ return rundot::kinetix::physics::kClothMaxNodes;
1214
+ }
1215
+
1216
+ std::uint32_t rundot_kinetix_ext_cloth_max_faces() {
1217
+ return rundot::kinetix::physics::kClothMaxFaces;
1218
+ }
1219
+
1220
+ std::uint32_t rundot_kinetix_ext_cloth_max_pins() {
1221
+ return rundot::kinetix::physics::kClothMaxPins;
1222
+ }
1223
+
1224
+ std::int32_t rundot_kinetix_ext_cloth_step(
1225
+ double* nodeData,
1226
+ std::uint32_t nodeCount,
1227
+ double* stretchData,
1228
+ std::uint32_t stretchCount,
1229
+ double* shearData,
1230
+ std::uint32_t shearCount,
1231
+ double* bendData,
1232
+ std::uint32_t bendCount,
1233
+ double* faceData,
1234
+ std::uint32_t faceCount,
1235
+ double* pinData,
1236
+ std::uint32_t pinCount,
1237
+ double* tetherData,
1238
+ std::uint32_t tetherCount,
1239
+ double* bodyData,
1240
+ std::uint32_t bodyCount,
1241
+ const double* paramsData,
1242
+ double* breakEventsData,
1243
+ std::uint32_t* breakEventCount,
1244
+ double dt) {
1245
+ return rundot_kinetix_physics_cloth_step(
1246
+ nodeData, nodeCount,
1247
+ stretchData, stretchCount,
1248
+ shearData, shearCount,
1249
+ bendData, bendCount,
1250
+ faceData, faceCount,
1251
+ pinData, pinCount,
1252
+ tetherData, tetherCount,
1253
+ bodyData, bodyCount,
1254
+ paramsData,
1255
+ breakEventsData, breakEventCount,
1256
+ dt);
1257
+ }
1258
+
1259
+ namespace {
1260
+ constexpr std::uint32_t kProbeValues = 64;
1261
+ double gProbeClothBuffer[kProbeValues]{};
1262
+ static rundot::kinetix::physics::ClothSimulation sProbeCloth;
1263
+ }
1264
+
1265
+ std::uint32_t rundot_cloth_probe_size() {
1266
+ return kProbeValues;
1267
+ }
1268
+
1269
+ const double* rundot_cloth_probe(std::uint32_t scenario) {
1270
+ using namespace rundot::kinetix::physics;
1271
+ std::memset(gProbeClothBuffer, 0, sizeof(gProbeClothBuffer));
1272
+
1273
+ ClothSimulation& cloth = sProbeCloth;
1274
+ cloth = ClothSimulation{};
1275
+ cloth.params.groundY = -5.0; // Place floor well below hanging cloth
1276
+
1277
+ if (scenario == 0) {
1278
+ // Scenario 0: Hanging cloth under gravity (pinned top corners)
1279
+ initClothGrid(
1280
+ cloth,
1281
+ 2.0, 2.0,
1282
+ 4, 4,
1283
+ ClothGridOrientation::XY,
1284
+ 1.0, // total mass
1285
+ 0.95, // stretch stiffness
1286
+ 0.8, // shear stiffness
1287
+ 0.5, // bend stiffness
1288
+ true, // pin top corners
1289
+ false // pin top edge
1290
+ );
1291
+
1292
+ for (int tick = 0; tick < 60; ++tick) {
1293
+ stepCloth(cloth, {}, 1.0 / 60.0);
1294
+ }
1295
+
1296
+ gProbeClothBuffer[0] = static_cast<double>(cloth.nodeCount);
1297
+ gProbeClothBuffer[1] = static_cast<double>(cloth.stretchCount);
1298
+ gProbeClothBuffer[2] = static_cast<double>(cloth.shearCount);
1299
+ gProbeClothBuffer[3] = static_cast<double>(cloth.bendCount);
1300
+ gProbeClothBuffer[4] = static_cast<double>(cloth.faceCount);
1301
+ gProbeClothBuffer[5] = static_cast<double>(cloth.pinCount);
1302
+
1303
+ // Bottom-center node (index 14) position
1304
+ gProbeClothBuffer[6] = cloth.nodes[14].position.x;
1305
+ gProbeClothBuffer[7] = cloth.nodes[14].position.y;
1306
+ gProbeClothBuffer[8] = cloth.nodes[14].position.z;
1307
+
1308
+ // Center node (index 5) position
1309
+ gProbeClothBuffer[9] = cloth.nodes[5].position.x;
1310
+ gProbeClothBuffer[10] = cloth.nodes[5].position.y;
1311
+ gProbeClothBuffer[11] = cloth.nodes[5].position.z;
1312
+
1313
+ // Forces and reactions
1314
+ gProbeClothBuffer[12] = cloth.maxTension;
1315
+ gProbeClothBuffer[13] = cloth.maxShear;
1316
+ gProbeClothBuffer[14] = cloth.maxBend;
1317
+ gProbeClothBuffer[15] = cloth.pins[0].reactionForce;
1318
+ gProbeClothBuffer[16] = cloth.tethers[0].currentDistance;
1319
+ } else if (scenario == 1) {
1320
+ // Scenario 1: Hanging cloth with aerodynamic wind force
1321
+ initClothGrid(
1322
+ cloth,
1323
+ 2.0, 2.0,
1324
+ 4, 4,
1325
+ ClothGridOrientation::XY,
1326
+ 1.0,
1327
+ 0.95,
1328
+ 0.8,
1329
+ 0.5,
1330
+ true,
1331
+ false
1332
+ );
1333
+
1334
+ cloth.params.enableAerodynamics = true;
1335
+ cloth.params.windZ = 12.0; // Strong wind blowing in +Z
1336
+
1337
+ for (int tick = 0; tick < 60; ++tick) {
1338
+ stepCloth(cloth, {}, 1.0 / 60.0);
1339
+ }
1340
+
1341
+ gProbeClothBuffer[0] = static_cast<double>(cloth.nodeCount);
1342
+ // Under wind in +Z, bottom center node should deflect significantly in +Z
1343
+ gProbeClothBuffer[1] = cloth.nodes[14].position.x;
1344
+ gProbeClothBuffer[2] = cloth.nodes[14].position.y;
1345
+ gProbeClothBuffer[3] = cloth.nodes[14].position.z; // Deflected in +Z
1346
+ gProbeClothBuffer[4] = cloth.totalAerodynamicForce;
1347
+ gProbeClothBuffer[5] = cloth.maxTension;
1348
+ } else {
1349
+ // Scenario 2: Driven attachment (top edge pinned to a moving rigid body)
1350
+ initClothGrid(
1351
+ cloth,
1352
+ 2.0, 2.0,
1353
+ 4, 4,
1354
+ ClothGridOrientation::XY,
1355
+ 1.0,
1356
+ 0.95,
1357
+ 0.8,
1358
+ 0.5,
1359
+ false,
1360
+ true // pin top edge
1361
+ );
1362
+
1363
+ // Attach pins to body 0
1364
+ Body testBody{};
1365
+ testBody.dynamic = false;
1366
+ testBody.kinematic = true;
1367
+ testBody.x = 0.0;
1368
+ testBody.y = 1.0;
1369
+ testBody.z = 0.0;
1370
+ testBody.vx = 1.0; // Moving in +X
1371
+ testBody.orientationW = 1.0;
1372
+
1373
+ for (std::uint32_t p = 0; p < cloth.pinCount; ++p) {
1374
+ cloth.pins[p].bodyIndex = 0;
1375
+ // Local offset relative to body origin
1376
+ cloth.pins[p].localOffset = {
1377
+ cloth.nodes[cloth.pins[p].nodeIndex].position.x - testBody.x,
1378
+ cloth.nodes[cloth.pins[p].nodeIndex].position.y - testBody.y,
1379
+ cloth.nodes[cloth.pins[p].nodeIndex].position.z - testBody.z
1380
+ };
1381
+ }
1382
+
1383
+ std::array<Body, 1> bodiesArr = {testBody};
1384
+ for (int tick = 0; tick < 60; ++tick) {
1385
+ bodiesArr[0].x += bodiesArr[0].vx * (1.0 / 60.0);
1386
+ stepCloth(cloth, std::span<Body>(bodiesArr.data(), 1), 1.0 / 60.0);
1387
+ }
1388
+
1389
+ gProbeClothBuffer[0] = static_cast<double>(cloth.nodeCount);
1390
+ // Pin 0 should have followed the moving body in +X
1391
+ gProbeClothBuffer[1] = cloth.nodes[0].position.x;
1392
+ gProbeClothBuffer[2] = cloth.nodes[0].position.y;
1393
+ gProbeClothBuffer[3] = cloth.nodes[0].position.z;
1394
+ gProbeClothBuffer[4] = bodiesArr[0].x;
1395
+ gProbeClothBuffer[5] = cloth.pins[0].reactionForce;
1396
+ }
1397
+
1398
+ return gProbeClothBuffer;
1399
+ }
1400
+
1401
+ }