@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,1229 @@
1
+ #include "rounded_convex.hpp"
2
+
3
+ #include <algorithm>
4
+ #include <cmath>
5
+ #include <cstdint>
6
+ #include <limits>
7
+ #include <span>
8
+
9
+ namespace rundot::kinetix::physics::rounded_convex {
10
+
11
+ namespace {
12
+
13
+ constexpr double kPi = 3.14159265358979323846;
14
+ constexpr double kEpsilon = 1e-12;
15
+ constexpr double kZeroTolerance = 1e-18;
16
+
17
+ inline Vec3 tripleCross(const Vec3& left, const Vec3& middle, const Vec3& right) {
18
+ return crossVec3(crossVec3(left, middle), right);
19
+ }
20
+
21
+ struct GjkSimplex {
22
+ std::array<Vec3, 4> points{};
23
+ std::uint32_t count = 0;
24
+ };
25
+
26
+ inline void setGjkSimplex(GjkSimplex& simplex, const Vec3& first, const Vec3& second, const Vec3& third) {
27
+ simplex.points[0] = first;
28
+ simplex.points[1] = second;
29
+ simplex.points[2] = third;
30
+ simplex.count = 3;
31
+ }
32
+
33
+ bool updateGjkSimplex(GjkSimplex& simplex, Vec3& direction) {
34
+ const Vec3 a = simplex.points[simplex.count - 1];
35
+ const Vec3 ao = scaleVec3(a, -1.0);
36
+ if (simplex.count == 2) {
37
+ const Vec3 b = simplex.points[0];
38
+ const Vec3 ab = subVec3(b, a);
39
+ if (dotVec3(ab, ao) > 0.0) {
40
+ direction = tripleCross(ab, ao, ab);
41
+ if (dotVec3(direction, direction) <= 1e-24) return true; // Origin lies on segment ab
42
+ } else {
43
+ simplex.points[0] = a;
44
+ simplex.count = 1;
45
+ direction = ao;
46
+ }
47
+ return false;
48
+ }
49
+ if (simplex.count == 3) {
50
+ const Vec3 b = simplex.points[1];
51
+ const Vec3 c = simplex.points[0];
52
+ const Vec3 ab = subVec3(b, a);
53
+ const Vec3 ac = subVec3(c, a);
54
+ const Vec3 abc = crossVec3(ab, ac);
55
+ const Vec3 abPerpendicular = crossVec3(abc, ab);
56
+ if (dotVec3(abPerpendicular, ao) > 0.0) {
57
+ simplex.points[0] = b;
58
+ simplex.points[1] = a;
59
+ simplex.count = 2;
60
+ direction = tripleCross(ab, ao, ab);
61
+ if (dotVec3(direction, direction) <= 1e-24) return true;
62
+ return false;
63
+ }
64
+ const Vec3 acPerpendicular = crossVec3(ac, abc);
65
+ if (dotVec3(acPerpendicular, ao) > 0.0) {
66
+ simplex.points[0] = c;
67
+ simplex.points[1] = a;
68
+ simplex.count = 2;
69
+ direction = tripleCross(ac, ao, ac);
70
+ if (dotVec3(direction, direction) <= 1e-24) return true;
71
+ return false;
72
+ }
73
+ direction = dotVec3(abc, ao) > 0.0 ? abc : scaleVec3(abc, -1.0);
74
+ if (dotVec3(direction, direction) <= 1e-24) return true;
75
+ if (dotVec3(direction, ao) <= 0.0) {
76
+ const Vec3 old = simplex.points[0];
77
+ simplex.points[0] = simplex.points[1];
78
+ simplex.points[1] = old;
79
+ direction = scaleVec3(direction, -1.0);
80
+ }
81
+ return false;
82
+ }
83
+ const Vec3 b = simplex.points[2];
84
+ const Vec3 c = simplex.points[1];
85
+ const Vec3 d = simplex.points[0];
86
+ const auto outsideFace = [&](const Vec3& first, const Vec3& second, const Vec3& third, const Vec3& opposite) {
87
+ Vec3 normal = crossVec3(subVec3(second, first), subVec3(third, first));
88
+ if (dotVec3(normal, subVec3(opposite, first)) > 0.0) normal = scaleVec3(normal, -1.0);
89
+ if (dotVec3(normal, scaleVec3(first, -1.0)) <= 0.0) return false;
90
+ setGjkSimplex(simplex, third, second, first);
91
+ direction = normal;
92
+ return true;
93
+ };
94
+ if (outsideFace(a, b, c, d) || outsideFace(a, c, d, b) || outsideFace(a, d, b, c)) {
95
+ return false;
96
+ }
97
+ return true;
98
+ }
99
+
100
+ template <typename SupportFnA, typename SupportFnB>
101
+ bool gjkIntersection(const SupportFnA& supportA, const SupportFnB& supportB, const Vec3& initialDir) {
102
+ Vec3 direction = initialDir;
103
+ if (dotVec3(direction, direction) <= 1e-24) direction = {1.0, 0.0, 0.0};
104
+
105
+ GjkSimplex simplex;
106
+ simplex.points[0] = subVec3(supportA(direction), supportB(scaleVec3(direction, -1.0)));
107
+ if (dotVec3(simplex.points[0], simplex.points[0]) <= 1e-24) return true;
108
+ simplex.count = 1;
109
+ direction = scaleVec3(simplex.points[0], -1.0);
110
+
111
+ for (std::uint32_t iteration = 0; iteration < 32; iteration += 1) {
112
+ const Vec3 support = subVec3(supportA(direction), supportB(scaleVec3(direction, -1.0)));
113
+ if (dotVec3(support, direction) < 0.0) return false;
114
+ simplex.points[simplex.count] = support;
115
+ simplex.count += 1;
116
+ if (updateGjkSimplex(simplex, direction)) return true;
117
+ if (dotVec3(direction, direction) <= 1e-24) return true;
118
+ }
119
+ return false;
120
+ }
121
+
122
+ } // namespace
123
+
124
+ // Pure deterministic math operations
125
+ double solverSqrt(double value) {
126
+ if (value <= 0.0) return 0.0;
127
+ return std::sqrt(value);
128
+ }
129
+
130
+ Vec3 addVec3(const Vec3& a, const Vec3& b) {
131
+ return {a.x + b.x, a.y + b.y, a.z + b.z};
132
+ }
133
+
134
+ Vec3 subVec3(const Vec3& a, const Vec3& b) {
135
+ return {a.x - b.x, a.y - b.y, a.z - b.z};
136
+ }
137
+
138
+ Vec3 scaleVec3(const Vec3& v, double s) {
139
+ return {v.x * s, v.y * s, v.z * s};
140
+ }
141
+
142
+ double dotVec3(const Vec3& a, const Vec3& b) {
143
+ return a.x * b.x + a.y * b.y + a.z * b.z;
144
+ }
145
+
146
+ Vec3 crossVec3(const Vec3& a, const Vec3& b) {
147
+ return {
148
+ a.y * b.z - a.z * b.y,
149
+ a.z * b.x - a.x * b.z,
150
+ a.x * b.y - a.y * b.x,
151
+ };
152
+ }
153
+
154
+ double lengthSquaredVec3(const Vec3& v) {
155
+ return dotVec3(v, v);
156
+ }
157
+
158
+ double lengthVec3(const Vec3& v) {
159
+ return solverSqrt(lengthSquaredVec3(v));
160
+ }
161
+
162
+ Vec3 normalizeVec3(const Vec3& v) {
163
+ const double len = lengthVec3(v);
164
+ if (len <= kZeroTolerance) return {0.0, 0.0, 0.0};
165
+ return scaleVec3(v, 1.0 / len);
166
+ }
167
+
168
+ Vec3 rotateByQuat(const Quat& q, const Vec3& v) {
169
+ const Vec3 u{q.x, q.y, q.z};
170
+ const Vec3 uv = crossVec3(u, v);
171
+ const Vec3 uuv = crossVec3(u, uv);
172
+ return addVec3(v, addVec3(scaleVec3(uv, 2.0 * q.w), scaleVec3(uuv, 2.0)));
173
+ }
174
+
175
+ Vec3 invRotateByQuat(const Quat& q, const Vec3& v) {
176
+ const Quat invQ{-q.x, -q.y, -q.z, q.w};
177
+ return rotateByQuat(invQ, v);
178
+ }
179
+
180
+ Quat normalizeQuat(const Quat& q) {
181
+ const double lenSq = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
182
+ if (lenSq <= 0.0) return {0.0, 0.0, 0.0, 1.0};
183
+ const double invLen = 1.0 / solverSqrt(lenSq);
184
+ return {q.x * invLen, q.y * invLen, q.z * invLen, q.w * invLen};
185
+ }
186
+
187
+ Quat multiplyQuat(const Quat& a, const Quat& b) {
188
+ return {
189
+ a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
190
+ a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
191
+ a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
192
+ a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
193
+ };
194
+ }
195
+
196
+ // Support Functions
197
+ SupportPoint supportPointMultiSphere(const MultiSphereShape& shape, const Vec3& direction) {
198
+ if (shape.spheres.empty()) {
199
+ return {shape.center, -1};
200
+ }
201
+
202
+ const Vec3 localDir = invRotateByQuat(shape.orientation, direction);
203
+ const double dirLen = lengthVec3(localDir);
204
+ const Vec3 unitDir = dirLen > kZeroTolerance ? scaleVec3(localDir, 1.0 / dirLen) : Vec3{1.0, 0.0, 0.0};
205
+
206
+ double bestProjection = -1e300;
207
+ std::int32_t bestIndex = 0;
208
+
209
+ for (std::size_t i = 0; i < shape.spheres.size(); ++i) {
210
+ const auto& sphere = shape.spheres[i];
211
+ const double proj = dotVec3(sphere.offset, localDir) + sphere.radius * dirLen;
212
+ if (proj > bestProjection) {
213
+ bestProjection = proj;
214
+ bestIndex = static_cast<std::int32_t>(i);
215
+ }
216
+ }
217
+
218
+ const auto& bestSphere = shape.spheres[static_cast<std::size_t>(bestIndex)];
219
+ const Vec3 localPt = addVec3(bestSphere.offset, scaleVec3(unitDir, bestSphere.radius));
220
+ const Vec3 worldPt = addVec3(shape.center, rotateByQuat(shape.orientation, localPt));
221
+ return {worldPt, bestIndex};
222
+ }
223
+
224
+ SupportPoint supportPointRoundedHull(const RoundedConvexHullShape& shape, const Vec3& direction) {
225
+ if (shape.vertices.empty()) {
226
+ return {shape.center, -1};
227
+ }
228
+
229
+ const Vec3 localDir = invRotateByQuat(shape.orientation, direction);
230
+ const double dirLen = lengthVec3(localDir);
231
+ const Vec3 unitDir = dirLen > kZeroTolerance ? scaleVec3(localDir, 1.0 / dirLen) : Vec3{1.0, 0.0, 0.0};
232
+
233
+ double bestProjection = -1e300;
234
+ std::int32_t bestIndex = 0;
235
+
236
+ for (std::size_t i = 0; i < shape.vertices.size(); ++i) {
237
+ const double proj = dotVec3(shape.vertices[i], localDir);
238
+ if (proj > bestProjection) {
239
+ bestProjection = proj;
240
+ bestIndex = static_cast<std::int32_t>(i);
241
+ }
242
+ }
243
+
244
+ Vec3 localPt = shape.vertices[static_cast<std::size_t>(bestIndex)];
245
+ if (shape.convexRadius > 0.0) {
246
+ localPt = addVec3(localPt, scaleVec3(unitDir, shape.convexRadius));
247
+ }
248
+ const Vec3 worldPt = addVec3(shape.center, rotateByQuat(shape.orientation, localPt));
249
+ return {worldPt, bestIndex};
250
+ }
251
+
252
+ SupportPoint supportPointRoundedBox(const RoundedBoxShape& shape, const Vec3& direction) {
253
+ const Vec3 localDir = invRotateByQuat(shape.orientation, direction);
254
+ const double dirLen = lengthVec3(localDir);
255
+ const Vec3 unitDir = dirLen > kZeroTolerance ? scaleVec3(localDir, 1.0 / dirLen) : Vec3{1.0, 0.0, 0.0};
256
+
257
+ Vec3 localCorner{
258
+ localDir.x >= 0.0 ? shape.halfExtents.x : -shape.halfExtents.x,
259
+ localDir.y >= 0.0 ? shape.halfExtents.y : -shape.halfExtents.y,
260
+ localDir.z >= 0.0 ? shape.halfExtents.z : -shape.halfExtents.z,
261
+ };
262
+
263
+ if (shape.convexRadius > 0.0) {
264
+ localCorner = addVec3(localCorner, scaleVec3(unitDir, shape.convexRadius));
265
+ }
266
+ const Vec3 worldPt = addVec3(shape.center, rotateByQuat(shape.orientation, localCorner));
267
+ return {worldPt, -1};
268
+ }
269
+
270
+ Vec3 coreSupportPointRoundedHull(const RoundedConvexHullShape& shape, const Vec3& direction) {
271
+ if (shape.vertices.empty()) return shape.center;
272
+ const Vec3 localDir = invRotateByQuat(shape.orientation, direction);
273
+ double bestProjection = -1e300;
274
+ std::size_t bestIndex = 0;
275
+ for (std::size_t i = 0; i < shape.vertices.size(); ++i) {
276
+ const double proj = dotVec3(shape.vertices[i], localDir);
277
+ if (proj > bestProjection) {
278
+ bestProjection = proj;
279
+ bestIndex = i;
280
+ }
281
+ }
282
+ return addVec3(shape.center, rotateByQuat(shape.orientation, shape.vertices[bestIndex]));
283
+ }
284
+
285
+ Vec3 coreSupportPointRoundedBox(const RoundedBoxShape& shape, const Vec3& direction) {
286
+ const Vec3 localDir = invRotateByQuat(shape.orientation, direction);
287
+ const Vec3 localCorner{
288
+ localDir.x >= 0.0 ? shape.halfExtents.x : -shape.halfExtents.x,
289
+ localDir.y >= 0.0 ? shape.halfExtents.y : -shape.halfExtents.y,
290
+ localDir.z >= 0.0 ? shape.halfExtents.z : -shape.halfExtents.z,
291
+ };
292
+ return addVec3(shape.center, rotateByQuat(shape.orientation, localCorner));
293
+ }
294
+
295
+ // Bounding Boxes
296
+ AABB computeMultiSphereAABB(const MultiSphereShape& shape) {
297
+ if (shape.spheres.empty()) {
298
+ return {shape.center, shape.center};
299
+ }
300
+
301
+ Vec3 minPt{1e300, 1e300, 1e300};
302
+ Vec3 maxPt{-1e300, -1e300, -1e300};
303
+
304
+ for (const auto& sphere : shape.spheres) {
305
+ const Vec3 worldOffset = rotateByQuat(shape.orientation, sphere.offset);
306
+ const Vec3 worldCenter = addVec3(shape.center, worldOffset);
307
+ const double r = sphere.radius;
308
+
309
+ minPt.x = std::min(minPt.x, worldCenter.x - r);
310
+ minPt.y = std::min(minPt.y, worldCenter.y - r);
311
+ minPt.z = std::min(minPt.z, worldCenter.z - r);
312
+
313
+ maxPt.x = std::max(maxPt.x, worldCenter.x + r);
314
+ maxPt.y = std::max(maxPt.y, worldCenter.y + r);
315
+ maxPt.z = std::max(maxPt.z, worldCenter.z + r);
316
+ }
317
+
318
+ return {minPt, maxPt};
319
+ }
320
+
321
+ AABB computeRoundedHullAABB(const RoundedConvexHullShape& shape) {
322
+ if (shape.vertices.empty()) {
323
+ const double r = shape.convexRadius;
324
+ return {
325
+ {shape.center.x - r, shape.center.y - r, shape.center.z - r},
326
+ {shape.center.x + r, shape.center.y + r, shape.center.z + r},
327
+ };
328
+ }
329
+
330
+ Vec3 minPt{1e300, 1e300, 1e300};
331
+ Vec3 maxPt{-1e300, -1e300, -1e300};
332
+
333
+ for (const auto& vertex : shape.vertices) {
334
+ const Vec3 worldVertex = addVec3(shape.center, rotateByQuat(shape.orientation, vertex));
335
+ minPt.x = std::min(minPt.x, worldVertex.x);
336
+ minPt.y = std::min(minPt.y, worldVertex.y);
337
+ minPt.z = std::min(minPt.z, worldVertex.z);
338
+
339
+ maxPt.x = std::max(maxPt.x, worldVertex.x);
340
+ maxPt.y = std::max(maxPt.y, worldVertex.y);
341
+ maxPt.z = std::max(maxPt.z, worldVertex.z);
342
+ }
343
+
344
+ const double r = shape.convexRadius;
345
+ return {
346
+ {minPt.x - r, minPt.y - r, minPt.z - r},
347
+ {maxPt.x + r, maxPt.y + r, maxPt.z + r},
348
+ };
349
+ }
350
+
351
+ AABB computeRoundedBoxAABB(const RoundedBoxShape& shape) {
352
+ const double hx = shape.halfExtents.x;
353
+ const double hy = shape.halfExtents.y;
354
+ const double hz = shape.halfExtents.z;
355
+
356
+ const std::array<Vec3, 8> localCorners{{
357
+ {-hx, -hy, -hz}, {hx, -hy, -hz}, {-hx, hy, -hz}, {hx, hy, -hz},
358
+ {-hx, -hy, hz}, {hx, -hy, hz}, {-hx, hy, hz}, {hx, hy, hz},
359
+ }};
360
+
361
+ Vec3 minPt{1e300, 1e300, 1e300};
362
+ Vec3 maxPt{-1e300, -1e300, -1e300};
363
+
364
+ for (const auto& corner : localCorners) {
365
+ const Vec3 worldCorner = addVec3(shape.center, rotateByQuat(shape.orientation, corner));
366
+ minPt.x = std::min(minPt.x, worldCorner.x);
367
+ minPt.y = std::min(minPt.y, worldCorner.y);
368
+ minPt.z = std::min(minPt.z, worldCorner.z);
369
+
370
+ maxPt.x = std::max(maxPt.x, worldCorner.x);
371
+ maxPt.y = std::max(maxPt.y, worldCorner.y);
372
+ maxPt.z = std::max(maxPt.z, worldCorner.z);
373
+ }
374
+
375
+ const double r = shape.convexRadius;
376
+ return {
377
+ {minPt.x - r, minPt.y - r, minPt.z - r},
378
+ {maxPt.x + r, maxPt.y + r, maxPt.z + r},
379
+ };
380
+ }
381
+
382
+ // Mass Properties
383
+ MassProperties computeMultiSphereMassProperties(std::span<const SphereDef> spheres, double density) {
384
+ if (spheres.empty()) return {};
385
+
386
+ double totalVolume = 0.0;
387
+ Vec3 weightedCenter{0.0, 0.0, 0.0};
388
+
389
+ for (const auto& sphere : spheres) {
390
+ const double r = std::max(0.0, sphere.radius);
391
+ const double vol = (4.0 / 3.0) * kPi * r * r * r;
392
+ totalVolume += vol;
393
+ weightedCenter = addVec3(weightedCenter, scaleVec3(sphere.offset, vol));
394
+ }
395
+
396
+ if (totalVolume <= kZeroTolerance) return {};
397
+
398
+ const Vec3 com = scaleVec3(weightedCenter, 1.0 / totalVolume);
399
+ const double mass = totalVolume * density;
400
+
401
+ Mat3 inertia{};
402
+
403
+ for (const auto& sphere : spheres) {
404
+ const double r = std::max(0.0, sphere.radius);
405
+ const double vol = (4.0 / 3.0) * kPi * r * r * r;
406
+ const double m = vol * density;
407
+ const double iSelf = 0.4 * m * r * r;
408
+ const Vec3 d = subVec3(sphere.offset, com);
409
+
410
+ inertia.xx += iSelf + m * (d.y * d.y + d.z * d.z);
411
+ inertia.yy += iSelf + m * (d.x * d.x + d.z * d.z);
412
+ inertia.zz += iSelf + m * (d.x * d.x + d.y * d.y);
413
+ inertia.xy -= m * d.x * d.y;
414
+ inertia.xz -= m * d.x * d.z;
415
+ inertia.yz -= m * d.y * d.z;
416
+ }
417
+
418
+ inertia.yx = inertia.xy;
419
+ inertia.zx = inertia.xz;
420
+ inertia.zy = inertia.yz;
421
+
422
+ return {totalVolume, mass, com, inertia};
423
+ }
424
+
425
+ MassProperties computeRoundedBoxMassProperties(const Vec3& halfExtents, double convexRadius, double density) {
426
+ const double hx = std::max(0.0, halfExtents.x);
427
+ const double hy = std::max(0.0, halfExtents.y);
428
+ const double hz = std::max(0.0, halfExtents.z);
429
+ const double r = std::max(0.0, convexRadius);
430
+
431
+ const double a = 2.0 * hx;
432
+ const double b = 2.0 * hy;
433
+ const double c = 2.0 * hz;
434
+
435
+ // Steiner volume formula for cuboid inflated by sphere of radius r:
436
+ const double baseVol = a * b * c;
437
+ const double slabVol = 2.0 * r * (a * b + b * c + c * a);
438
+ const double edgeVol = kPi * r * r * (a + b + c);
439
+ const double cornerVol = (4.0 / 3.0) * kPi * r * r * r;
440
+ const double totalVolume = baseVol + slabVol + edgeVol + cornerVol;
441
+
442
+ const double mass = totalVolume * density;
443
+ const Vec3 com{0.0, 0.0, 0.0};
444
+
445
+ // Effective dimensions for inertia moment estimation
446
+ const double hxEff = hx + 0.5 * r;
447
+ const double hyEff = hy + 0.5 * r;
448
+ const double hzEff = hz + 0.5 * r;
449
+
450
+ Mat3 inertia{};
451
+ inertia.xx = (mass / 3.0) * (hyEff * hyEff + hzEff * hzEff);
452
+ inertia.yy = (mass / 3.0) * (hxEff * hxEff + hzEff * hzEff);
453
+ inertia.zz = (mass / 3.0) * (hxEff * hxEff + hyEff * hyEff);
454
+
455
+ return {totalVolume, mass, com, inertia};
456
+ }
457
+
458
+ MassProperties computeRoundedHullMassProperties(std::span<const Vec3> vertices, double convexRadius, double density) {
459
+ if (vertices.size() < 4) {
460
+ Vec3 extent{0.5, 0.5, 0.5};
461
+ if (!vertices.empty()) {
462
+ double maxD = 0.0;
463
+ for (const auto& v : vertices) maxD = std::max(maxD, lengthVec3(v));
464
+ if (maxD > 0.0) extent = {maxD, maxD, maxD};
465
+ }
466
+ return computeRoundedBoxMassProperties(extent, convexRadius, density);
467
+ }
468
+
469
+ Vec3 centroid{0.0, 0.0, 0.0};
470
+ for (const auto& v : vertices) centroid = addVec3(centroid, v);
471
+ centroid = scaleVec3(centroid, 1.0 / static_cast<double>(vertices.size()));
472
+
473
+ // Estimate volume and inertia via tetrahedron integration from centroid
474
+ double baseVolume = 0.0;
475
+ Mat3 inertiaBase{};
476
+
477
+ for (std::size_t i = 1; i + 1 < vertices.size(); ++i) {
478
+ const Vec3 v0 = subVec3(vertices[0], centroid);
479
+ const Vec3 v1 = subVec3(vertices[i], centroid);
480
+ const Vec3 v2 = subVec3(vertices[i + 1], centroid);
481
+
482
+ const double det = dotVec3(v0, crossVec3(v1, v2));
483
+ const double tetVol = std::abs(det) / 6.0;
484
+ baseVolume += tetVol;
485
+
486
+ // Approximate tetrahedron inertia
487
+ const double factor = tetVol / 10.0;
488
+ inertiaBase.xx += factor * (v0.y * v0.y + v1.y * v1.y + v2.y * v2.y + v0.z * v0.z + v1.z * v1.z + v2.z * v2.z);
489
+ inertiaBase.yy += factor * (v0.x * v0.x + v1.x * v1.x + v2.x * v2.x + v0.z * v0.z + v1.z * v1.z + v2.z * v2.z);
490
+ inertiaBase.zz += factor * (v0.x * v0.x + v1.x * v1.x + v2.x * v2.x + v0.y * v0.y + v1.y * v1.y + v2.y * v2.y);
491
+ }
492
+
493
+ if (baseVolume <= kZeroTolerance) {
494
+ return computeRoundedBoxMassProperties({0.5, 0.5, 0.5}, convexRadius, density);
495
+ }
496
+
497
+ const double r = std::max(0.0, convexRadius);
498
+ const double rEff = solverSqrt(inertiaBase.xx / baseVolume);
499
+ const double surfaceEst = 4.0 * kPi * rEff * rEff;
500
+ const double totalVolume = baseVolume + surfaceEst * r + 4.0 * kPi * r * r * rEff + (4.0 / 3.0) * kPi * r * r * r;
501
+ const double mass = totalVolume * density;
502
+
503
+ const double expansionRatio = rEff > kZeroTolerance ? (rEff + r) / rEff : 1.0;
504
+ const double scaleInertia = (mass / baseVolume) * expansionRatio * expansionRatio;
505
+
506
+ Mat3 inertia{};
507
+ inertia.xx = std::max(1e-6, inertiaBase.xx * scaleInertia);
508
+ inertia.yy = std::max(1e-6, inertiaBase.yy * scaleInertia);
509
+ inertia.zz = std::max(1e-6, inertiaBase.zz * scaleInertia);
510
+
511
+ return {totalVolume, mass, centroid, inertia};
512
+ }
513
+
514
+ // Raycasts
515
+ RayHit raycastMultiSphere(const MultiSphereShape& shape, const Ray& ray) {
516
+ if (shape.spheres.empty() || ray.maxDistance <= 0.0) return {};
517
+
518
+ const Vec3 localRayOrig = invRotateByQuat(shape.orientation, subVec3(ray.origin, shape.center));
519
+ const Vec3 localRayDir = invRotateByQuat(shape.orientation, ray.direction);
520
+ const double dirLenSq = dotVec3(localRayDir, localRayDir);
521
+ if (dirLenSq <= kZeroTolerance) return {};
522
+
523
+ double closestT = ray.maxDistance;
524
+ std::int32_t bestSphereIndex = -1;
525
+ Vec3 bestLocalHitNormal{};
526
+
527
+ for (std::size_t i = 0; i < shape.spheres.size(); ++i) {
528
+ const auto& sphere = shape.spheres[i];
529
+ const Vec3 m = subVec3(localRayOrig, sphere.offset);
530
+ const double b = dotVec3(m, localRayDir);
531
+ const double c = dotVec3(m, m) - sphere.radius * sphere.radius;
532
+
533
+ if (c > 0.0 && b > 0.0) continue; // Ray origin outside and pointing away
534
+
535
+ const double discr = b * b - dirLenSq * c;
536
+ if (discr < 0.0) continue;
537
+
538
+ double t = (-b - solverSqrt(discr)) / dirLenSq;
539
+ if (t < 0.0) t = 0.0; // Ray started inside
540
+
541
+ if (t <= closestT) {
542
+ closestT = t;
543
+ bestSphereIndex = static_cast<std::int32_t>(i);
544
+ const Vec3 localHitPt = addVec3(localRayOrig, scaleVec3(localRayDir, t));
545
+ bestLocalHitNormal = normalizeVec3(subVec3(localHitPt, sphere.offset));
546
+ }
547
+ }
548
+
549
+ if (bestSphereIndex < 0) return {};
550
+
551
+ const Vec3 worldHitPt = addVec3(ray.origin, scaleVec3(ray.direction, closestT));
552
+ const Vec3 worldHitNorm = rotateByQuat(shape.orientation, bestLocalHitNormal);
553
+
554
+ return {true, closestT, worldHitPt, worldHitNorm, bestSphereIndex};
555
+ }
556
+
557
+ RayHit raycastRoundedBox(const RoundedBoxShape& shape, const Ray& ray) {
558
+ if (ray.maxDistance <= 0.0) return {};
559
+
560
+ const Vec3 localRayOrig = invRotateByQuat(shape.orientation, subVec3(ray.origin, shape.center));
561
+ const Vec3 localRayDir = invRotateByQuat(shape.orientation, ray.direction);
562
+
563
+ const double hx = shape.halfExtents.x + shape.convexRadius;
564
+ const double hy = shape.halfExtents.y + shape.convexRadius;
565
+ const double hz = shape.halfExtents.z + shape.convexRadius;
566
+
567
+ // Standard slab test on inflated bounds
568
+ double tMin = 0.0;
569
+ double tMax = ray.maxDistance;
570
+ Vec3 hitNormal{-1.0, 0.0, 0.0};
571
+
572
+ for (int i = 0; i < 3; ++i) {
573
+ const double orig = (i == 0) ? localRayOrig.x : (i == 1) ? localRayOrig.y : localRayOrig.z;
574
+ const double dir = (i == 0) ? localRayDir.x : (i == 1) ? localRayDir.y : localRayDir.z;
575
+ const double halfE = (i == 0) ? hx : (i == 1) ? hy : hz;
576
+
577
+ if (std::abs(dir) < 1e-15) {
578
+ if (std::abs(orig) > halfE) return {};
579
+ } else {
580
+ double t1 = (-halfE - orig) / dir;
581
+ double t2 = (halfE - orig) / dir;
582
+ Vec3 n1 = (i == 0) ? Vec3{-1, 0, 0} : (i == 1) ? Vec3{0, -1, 0} : Vec3{0, 0, -1};
583
+ Vec3 n2 = (i == 0) ? Vec3{1, 0, 0} : (i == 1) ? Vec3{0, 1, 0} : Vec3{0, 0, 1};
584
+
585
+ if (t1 > t2) {
586
+ std::swap(t1, t2);
587
+ std::swap(n1, n2);
588
+ }
589
+ if (t1 > tMin) {
590
+ tMin = t1;
591
+ hitNormal = n1;
592
+ }
593
+ if (t2 < tMax) tMax = t2;
594
+ if (tMin > tMax) return {};
595
+ }
596
+ }
597
+
598
+ const Vec3 worldHitPt = addVec3(ray.origin, scaleVec3(ray.direction, tMin));
599
+ const Vec3 worldHitNorm = rotateByQuat(shape.orientation, hitNormal);
600
+ return {true, tMin, worldHitPt, worldHitNorm, -1};
601
+ }
602
+
603
+ RayHit raycastRoundedHull(const RoundedConvexHullShape& shape, const Ray& ray) {
604
+ if (shape.vertices.empty() || ray.maxDistance <= 0.0) return {};
605
+
606
+ // Conservative step raycast using support function
607
+ double t = 0.0;
608
+ for (std::uint32_t step = 0; step < 32 && t <= ray.maxDistance; ++step) {
609
+ const Vec3 currentPoint = addVec3(ray.origin, scaleVec3(ray.direction, t));
610
+ const ClosestPointsResult ptDist = pointToRoundedHullDistance(currentPoint, shape, InteriorPolicy::SignedPenetration);
611
+
612
+ if (ptDist.distance <= 1e-5) {
613
+ return {true, t, currentPoint, ptDist.normal, -1};
614
+ }
615
+
616
+ const double advance = std::max(1e-4, ptDist.distance);
617
+ t += advance;
618
+ }
619
+
620
+ return {};
621
+ }
622
+
623
+ // Point to Shape Distance
624
+ ClosestPointsResult pointToMultiSphereDistance(
625
+ const Vec3& point,
626
+ const MultiSphereShape& shape,
627
+ InteriorPolicy policy) {
628
+ if (shape.spheres.empty()) {
629
+ return {RoundedConvexStatus::InvalidInput};
630
+ }
631
+
632
+ const Vec3 localP = invRotateByQuat(shape.orientation, subVec3(point, shape.center));
633
+
634
+ double minSignedDist = 1e300;
635
+ Vec3 bestSurfacePt{};
636
+ Vec3 bestNormal{0.0, 1.0, 0.0};
637
+
638
+ for (const auto& sphere : shape.spheres) {
639
+ const Vec3 delta = subVec3(localP, sphere.offset);
640
+ const double d = lengthVec3(delta);
641
+ const Vec3 unit = d > kZeroTolerance ? scaleVec3(delta, 1.0 / d) : Vec3{0.0, 1.0, 0.0};
642
+ const double signedDist = d - sphere.radius;
643
+
644
+ if (signedDist < minSignedDist) {
645
+ minSignedDist = signedDist;
646
+ bestSurfacePt = addVec3(sphere.offset, scaleVec3(unit, sphere.radius));
647
+ bestNormal = unit;
648
+ }
649
+ }
650
+
651
+ const Vec3 worldWitnessB = addVec3(shape.center, rotateByQuat(shape.orientation, bestSurfacePt));
652
+ const Vec3 worldNormal = rotateByQuat(shape.orientation, bestNormal);
653
+
654
+ if (minSignedDist > kEpsilon) {
655
+ return {RoundedConvexStatus::Disjoint, minSignedDist, point, worldWitnessB, worldNormal, false};
656
+ } else if (minSignedDist >= -kEpsilon) {
657
+ return {RoundedConvexStatus::Touching, 0.0, worldWitnessB, worldWitnessB, worldNormal, false};
658
+ } else {
659
+ const double dist = (policy == InteriorPolicy::ZeroOnInterior) ? 0.0 : minSignedDist;
660
+ return {RoundedConvexStatus::Penetrating, dist, point, worldWitnessB, worldNormal, true};
661
+ }
662
+ }
663
+
664
+ ClosestPointsResult pointToRoundedBoxDistance(
665
+ const Vec3& point,
666
+ const RoundedBoxShape& shape,
667
+ InteriorPolicy policy) {
668
+ const Vec3 localP = invRotateByQuat(shape.orientation, subVec3(point, shape.center));
669
+
670
+ const Vec3 clamped{
671
+ std::max(-shape.halfExtents.x, std::min(shape.halfExtents.x, localP.x)),
672
+ std::max(-shape.halfExtents.y, std::min(shape.halfExtents.y, localP.y)),
673
+ std::max(-shape.halfExtents.z, std::min(shape.halfExtents.z, localP.z)),
674
+ };
675
+
676
+ const Vec3 delta = subVec3(localP, clamped);
677
+ const double distFromBox = lengthVec3(delta);
678
+
679
+ Vec3 localNormal = distFromBox > kZeroTolerance ? scaleVec3(delta, 1.0 / distFromBox) : Vec3{0.0, 1.0, 0.0};
680
+ const Vec3 localWitnessB = addVec3(clamped, scaleVec3(localNormal, shape.convexRadius));
681
+ const Vec3 worldWitnessB = addVec3(shape.center, rotateByQuat(shape.orientation, localWitnessB));
682
+ const Vec3 worldNormal = rotateByQuat(shape.orientation, localNormal);
683
+
684
+ const double signedDist = distFromBox - shape.convexRadius;
685
+
686
+ if (signedDist > kEpsilon) {
687
+ return {RoundedConvexStatus::Disjoint, signedDist, point, worldWitnessB, worldNormal, false};
688
+ } else if (signedDist >= -kEpsilon) {
689
+ return {RoundedConvexStatus::Touching, 0.0, worldWitnessB, worldWitnessB, worldNormal, false};
690
+ } else {
691
+ const double dist = (policy == InteriorPolicy::ZeroOnInterior) ? 0.0 : signedDist;
692
+ return {RoundedConvexStatus::Penetrating, dist, point, worldWitnessB, worldNormal, true};
693
+ }
694
+ }
695
+
696
+ ClosestPointsResult pointToRoundedHullDistance(
697
+ const Vec3& point,
698
+ const RoundedConvexHullShape& shape,
699
+ InteriorPolicy policy) {
700
+ if (shape.vertices.empty()) {
701
+ return {RoundedConvexStatus::InvalidInput};
702
+ }
703
+
704
+ // Find closest core vertex or face
705
+ const Vec3 localP = invRotateByQuat(shape.orientation, subVec3(point, shape.center));
706
+ double closestDistSq = 1e300;
707
+ Vec3 closestVertex = shape.vertices[0];
708
+
709
+ for (const auto& v : shape.vertices) {
710
+ const Vec3 diff = subVec3(localP, v);
711
+ const double dSq = dotVec3(diff, diff);
712
+ if (dSq < closestDistSq) {
713
+ closestDistSq = dSq;
714
+ closestVertex = v;
715
+ }
716
+ }
717
+
718
+ const double distToCore = solverSqrt(closestDistSq);
719
+ const Vec3 dir = distToCore > kZeroTolerance ? scaleVec3(subVec3(localP, closestVertex), 1.0 / distToCore) : Vec3{0.0, 1.0, 0.0};
720
+
721
+ const Vec3 localWitnessB = addVec3(closestVertex, scaleVec3(dir, shape.convexRadius));
722
+ const Vec3 worldWitnessB = addVec3(shape.center, rotateByQuat(shape.orientation, localWitnessB));
723
+ const Vec3 worldNormal = rotateByQuat(shape.orientation, dir);
724
+
725
+ const double signedDist = distToCore - shape.convexRadius;
726
+
727
+ if (signedDist > kEpsilon) {
728
+ return {RoundedConvexStatus::Disjoint, signedDist, point, worldWitnessB, worldNormal, false};
729
+ } else if (signedDist >= -kEpsilon) {
730
+ return {RoundedConvexStatus::Touching, 0.0, worldWitnessB, worldWitnessB, worldNormal, false};
731
+ } else {
732
+ const double dist = (policy == InteriorPolicy::ZeroOnInterior) ? 0.0 : signedDist;
733
+ return {RoundedConvexStatus::Penetrating, dist, point, worldWitnessB, worldNormal, true};
734
+ }
735
+ }
736
+
737
+ // Narrowphase Collisions & Intersections
738
+ bool testGjkOverlap(const MultiSphereShape& a, const MultiSphereShape& b) {
739
+ const Vec3 initialDir = subVec3(b.center, a.center);
740
+ return gjkIntersection(
741
+ [&](const Vec3& dir) { return supportPointMultiSphere(a, dir).point; },
742
+ [&](const Vec3& dir) { return supportPointMultiSphere(b, dir).point; },
743
+ initialDir);
744
+ }
745
+
746
+ bool testGjkOverlap(const RoundedConvexHullShape& a, const RoundedConvexHullShape& b) {
747
+ const Vec3 initialDir = subVec3(b.center, a.center);
748
+ return gjkIntersection(
749
+ [&](const Vec3& dir) { return supportPointRoundedHull(a, dir).point; },
750
+ [&](const Vec3& dir) { return supportPointRoundedHull(b, dir).point; },
751
+ initialDir);
752
+ }
753
+
754
+ bool testGjkOverlap(const RoundedConvexHullShape& a, const MultiSphereShape& b) {
755
+ const Vec3 initialDir = subVec3(b.center, a.center);
756
+ return gjkIntersection(
757
+ [&](const Vec3& dir) { return supportPointRoundedHull(a, dir).point; },
758
+ [&](const Vec3& dir) { return supportPointMultiSphere(b, dir).point; },
759
+ initialDir);
760
+ }
761
+
762
+ template <typename SupportFnA, typename SupportFnB>
763
+ CollisionResult satSupportCollision(
764
+ const SupportFnA& supportA,
765
+ const SupportFnB& supportB,
766
+ const Vec3& centerA,
767
+ const Vec3& centerB,
768
+ std::span<const Vec3> candidateNormals) {
769
+ const Vec3 centerDiff = subVec3(centerB, centerA);
770
+
771
+ std::array<Vec3, 32> axes{};
772
+ std::uint32_t axisCount = 0;
773
+
774
+ auto addAxis = [&](const Vec3& axis) {
775
+ const double lenSq = dotVec3(axis, axis);
776
+ if (lenSq <= 1e-12 || axisCount >= axes.size()) return;
777
+ Vec3 unit = scaleVec3(axis, 1.0 / solverSqrt(lenSq));
778
+ if (dotVec3(unit, centerDiff) < 0.0) unit = scaleVec3(unit, -1.0);
779
+ for (std::uint32_t i = 0; i < axisCount; ++i) {
780
+ if (dotVec3(axes[i], unit) > 0.999) return;
781
+ }
782
+ axes[axisCount++] = unit;
783
+ };
784
+
785
+ addAxis(centerDiff);
786
+ for (const auto& n : candidateNormals) addAxis(n);
787
+ if (axisCount == 0) addAxis({1.0, 0.0, 0.0});
788
+
789
+ double minPenetration = std::numeric_limits<double>::infinity();
790
+ Vec3 bestNormal = axes[0];
791
+ Vec3 bestPointA = centerA;
792
+ Vec3 bestPointB = centerB;
793
+ std::int32_t bestFeatureA = -1;
794
+ std::int32_t bestFeatureB = -1;
795
+
796
+ for (std::uint32_t i = 0; i < axisCount; ++i) {
797
+ const Vec3& N = axes[i];
798
+ const auto pA = supportA(N);
799
+ const auto pB = supportB(scaleVec3(N, -1.0));
800
+
801
+ const double separation = dotVec3(subVec3(pB.point, pA.point), N);
802
+ if (separation > 1e-6) {
803
+ return {}; // Separating axis found
804
+ }
805
+
806
+ const double penetration = -separation;
807
+ if (penetration < minPenetration) {
808
+ minPenetration = penetration;
809
+ bestNormal = N;
810
+ bestPointA = pA.point;
811
+ bestPointB = pB.point;
812
+ bestFeatureA = pA.featureIndex;
813
+ bestFeatureB = pB.featureIndex;
814
+ }
815
+ }
816
+
817
+ const Vec3 contactPt = scaleVec3(addVec3(bestPointA, bestPointB), 0.5);
818
+ return {
819
+ true,
820
+ bestNormal,
821
+ std::max(0.0, minPenetration),
822
+ contactPt,
823
+ bestPointA,
824
+ bestPointB,
825
+ bestFeatureA,
826
+ bestFeatureB,
827
+ };
828
+ }
829
+
830
+ CollisionResult collideMultiSphereWithMultiSphere(
831
+ const MultiSphereShape& a,
832
+ const MultiSphereShape& b) {
833
+ std::array<Vec3, 16> candidateNormals{};
834
+ std::uint32_t normalCount = 0;
835
+
836
+ for (std::size_t i = 0; i < std::min<std::size_t>(a.spheres.size(), 4); ++i) {
837
+ for (std::size_t j = 0; j < std::min<std::size_t>(b.spheres.size(), 4); ++j) {
838
+ if (normalCount >= candidateNormals.size()) break;
839
+ const Vec3 wA = addVec3(a.center, rotateByQuat(a.orientation, a.spheres[i].offset));
840
+ const Vec3 wB = addVec3(b.center, rotateByQuat(b.orientation, b.spheres[j].offset));
841
+ candidateNormals[normalCount++] = subVec3(wB, wA);
842
+ }
843
+ }
844
+
845
+ return satSupportCollision(
846
+ [&](const Vec3& dir) { return supportPointMultiSphere(a, dir); },
847
+ [&](const Vec3& dir) { return supportPointMultiSphere(b, dir); },
848
+ a.center,
849
+ b.center,
850
+ std::span<const Vec3>(candidateNormals.data(), normalCount));
851
+ }
852
+
853
+ CollisionResult collideRoundedHullWithRoundedHull(
854
+ const RoundedConvexHullShape& a,
855
+ const RoundedConvexHullShape& b) {
856
+ std::array<Vec3, 12> candidateNormals{};
857
+ std::uint32_t normalCount = 0;
858
+
859
+ candidateNormals[normalCount++] = rotateByQuat(a.orientation, {1, 0, 0});
860
+ candidateNormals[normalCount++] = rotateByQuat(a.orientation, {0, 1, 0});
861
+ candidateNormals[normalCount++] = rotateByQuat(a.orientation, {0, 0, 1});
862
+ candidateNormals[normalCount++] = rotateByQuat(b.orientation, {1, 0, 0});
863
+ candidateNormals[normalCount++] = rotateByQuat(b.orientation, {0, 1, 0});
864
+ candidateNormals[normalCount++] = rotateByQuat(b.orientation, {0, 0, 1});
865
+
866
+ return satSupportCollision(
867
+ [&](const Vec3& dir) { return supportPointRoundedHull(a, dir); },
868
+ [&](const Vec3& dir) { return supportPointRoundedHull(b, dir); },
869
+ a.center,
870
+ b.center,
871
+ std::span<const Vec3>(candidateNormals.data(), normalCount));
872
+ }
873
+
874
+ CollisionResult collideRoundedHullWithMultiSphere(
875
+ const RoundedConvexHullShape& a,
876
+ const MultiSphereShape& b) {
877
+ std::array<Vec3, 8> candidateNormals{};
878
+ std::uint32_t normalCount = 0;
879
+
880
+ for (std::size_t i = 0; i < std::min<std::size_t>(b.spheres.size(), 4); ++i) {
881
+ const Vec3 wSphere = addVec3(b.center, rotateByQuat(b.orientation, b.spheres[i].offset));
882
+ candidateNormals[normalCount++] = subVec3(wSphere, a.center);
883
+ }
884
+
885
+ return satSupportCollision(
886
+ [&](const Vec3& dir) { return supportPointRoundedHull(a, dir); },
887
+ [&](const Vec3& dir) { return supportPointMultiSphere(b, dir); },
888
+ a.center,
889
+ b.center,
890
+ std::span<const Vec3>(candidateNormals.data(), normalCount));
891
+ }
892
+
893
+ CollisionResult collideRoundedBoxWithRoundedBox(
894
+ const RoundedBoxShape& a,
895
+ const RoundedBoxShape& b) {
896
+ std::array<Vec3, 15> candidateNormals{};
897
+ std::uint32_t normalCount = 0;
898
+
899
+ const Vec3 ax = rotateByQuat(a.orientation, {1, 0, 0});
900
+ const Vec3 ay = rotateByQuat(a.orientation, {0, 1, 0});
901
+ const Vec3 az = rotateByQuat(a.orientation, {0, 0, 1});
902
+ const Vec3 bx = rotateByQuat(b.orientation, {1, 0, 0});
903
+ const Vec3 by = rotateByQuat(b.orientation, {0, 1, 0});
904
+ const Vec3 bz = rotateByQuat(b.orientation, {0, 0, 1});
905
+
906
+ candidateNormals[normalCount++] = ax;
907
+ candidateNormals[normalCount++] = ay;
908
+ candidateNormals[normalCount++] = az;
909
+ candidateNormals[normalCount++] = bx;
910
+ candidateNormals[normalCount++] = by;
911
+ candidateNormals[normalCount++] = bz;
912
+
913
+ candidateNormals[normalCount++] = crossVec3(ax, bx);
914
+ candidateNormals[normalCount++] = crossVec3(ax, by);
915
+ candidateNormals[normalCount++] = crossVec3(ax, bz);
916
+ candidateNormals[normalCount++] = crossVec3(ay, bx);
917
+ candidateNormals[normalCount++] = crossVec3(ay, by);
918
+ candidateNormals[normalCount++] = crossVec3(ay, bz);
919
+ candidateNormals[normalCount++] = crossVec3(az, bx);
920
+ candidateNormals[normalCount++] = crossVec3(az, by);
921
+ candidateNormals[normalCount++] = crossVec3(az, bz);
922
+
923
+ return satSupportCollision(
924
+ [&](const Vec3& dir) { return supportPointRoundedBox(a, dir); },
925
+ [&](const Vec3& dir) { return supportPointRoundedBox(b, dir); },
926
+ a.center,
927
+ b.center,
928
+ std::span<const Vec3>(candidateNormals.data(), normalCount));
929
+ }
930
+
931
+ } // namespace rundot::kinetix::physics::rounded_convex
932
+
933
+ // C-ABI exports
934
+ extern "C" {
935
+
936
+ using namespace rundot::kinetix::physics::rounded_convex;
937
+
938
+ std::uint32_t rundot_kinetix_physics_rounded_convex_abi_version() {
939
+ return kRoundedConvexAbiVersion;
940
+ }
941
+
942
+ std::int32_t rundot_kinetix_physics_multi_sphere_support(
943
+ const double* spheresData,
944
+ std::uint32_t sphereCount,
945
+ const double* poseData,
946
+ const double* directionData,
947
+ double* outSupportPoint) {
948
+ if (!spheresData || sphereCount == 0 || !poseData || !directionData || !outSupportPoint) {
949
+ return -1;
950
+ }
951
+
952
+ std::array<SphereDef, kMultiSphereMaxSpheres> spheresStorage{};
953
+ const std::uint32_t count = std::min(sphereCount, kMultiSphereMaxSpheres);
954
+ for (std::uint32_t i = 0; i < count; ++i) {
955
+ spheresStorage[i].offset = {spheresData[i * 4 + 0], spheresData[i * 4 + 1], spheresData[i * 4 + 2]};
956
+ spheresStorage[i].radius = spheresData[i * 4 + 3];
957
+ }
958
+
959
+ MultiSphereShape shape{
960
+ .spheres = std::span<const SphereDef>(spheresStorage.data(), count),
961
+ .center = {poseData[0], poseData[1], poseData[2]},
962
+ .orientation = {poseData[3], poseData[4], poseData[5], poseData[6]},
963
+ };
964
+
965
+ const Vec3 dir{directionData[0], directionData[1], directionData[2]};
966
+ const SupportPoint pt = supportPointMultiSphere(shape, dir);
967
+
968
+ outSupportPoint[0] = pt.point.x;
969
+ outSupportPoint[1] = pt.point.y;
970
+ outSupportPoint[2] = pt.point.z;
971
+ outSupportPoint[3] = static_cast<double>(pt.featureIndex);
972
+ return 0;
973
+ }
974
+
975
+ std::int32_t rundot_kinetix_physics_rounded_convex_support(
976
+ const double* verticesData,
977
+ std::uint32_t vertexCount,
978
+ double convexRadius,
979
+ const double* poseData,
980
+ const double* directionData,
981
+ double* outSupportPoint) {
982
+ if (!verticesData || vertexCount == 0 || !poseData || !directionData || !outSupportPoint) {
983
+ return -1;
984
+ }
985
+
986
+ std::array<Vec3, kRoundedHullMaxVertices> verticesStorage{};
987
+ const std::uint32_t count = std::min(vertexCount, kRoundedHullMaxVertices);
988
+ for (std::uint32_t i = 0; i < count; ++i) {
989
+ verticesStorage[i] = {verticesData[i * 3 + 0], verticesData[i * 3 + 1], verticesData[i * 3 + 2]};
990
+ }
991
+
992
+ RoundedConvexHullShape shape{
993
+ .vertices = std::span<const Vec3>(verticesStorage.data(), count),
994
+ .convexRadius = std::max(0.0, convexRadius),
995
+ .center = {poseData[0], poseData[1], poseData[2]},
996
+ .orientation = {poseData[3], poseData[4], poseData[5], poseData[6]},
997
+ };
998
+
999
+ const Vec3 dir{directionData[0], directionData[1], directionData[2]};
1000
+ const SupportPoint pt = supportPointRoundedHull(shape, dir);
1001
+
1002
+ outSupportPoint[0] = pt.point.x;
1003
+ outSupportPoint[1] = pt.point.y;
1004
+ outSupportPoint[2] = pt.point.z;
1005
+ outSupportPoint[3] = static_cast<double>(pt.featureIndex);
1006
+ return 0;
1007
+ }
1008
+
1009
+ std::int32_t rundot_kinetix_physics_rounded_convex_collision(
1010
+ const double* verticesA,
1011
+ std::uint32_t vertexCountA,
1012
+ double convexRadiusA,
1013
+ const double* poseA,
1014
+ const double* verticesB,
1015
+ std::uint32_t vertexCountB,
1016
+ double convexRadiusB,
1017
+ const double* poseB,
1018
+ double* outCollision) {
1019
+ if (!verticesA || vertexCountA == 0 || !poseA || !verticesB || vertexCountB == 0 || !poseB || !outCollision) {
1020
+ return -1;
1021
+ }
1022
+
1023
+ std::array<Vec3, kRoundedHullMaxVertices> storageA{};
1024
+ const std::uint32_t countA = std::min(vertexCountA, kRoundedHullMaxVertices);
1025
+ for (std::uint32_t i = 0; i < countA; ++i) {
1026
+ storageA[i] = {verticesA[i * 3 + 0], verticesA[i * 3 + 1], verticesA[i * 3 + 2]};
1027
+ }
1028
+
1029
+ std::array<Vec3, kRoundedHullMaxVertices> storageB{};
1030
+ const std::uint32_t countB = std::min(vertexCountB, kRoundedHullMaxVertices);
1031
+ for (std::uint32_t i = 0; i < countB; ++i) {
1032
+ storageB[i] = {verticesB[i * 3 + 0], verticesB[i * 3 + 1], verticesB[i * 3 + 2]};
1033
+ }
1034
+
1035
+ RoundedConvexHullShape shapeA{
1036
+ .vertices = std::span<const Vec3>(storageA.data(), countA),
1037
+ .convexRadius = std::max(0.0, convexRadiusA),
1038
+ .center = {poseA[0], poseA[1], poseA[2]},
1039
+ .orientation = {poseA[3], poseA[4], poseA[5], poseA[6]},
1040
+ };
1041
+
1042
+ RoundedConvexHullShape shapeB{
1043
+ .vertices = std::span<const Vec3>(storageB.data(), countB),
1044
+ .convexRadius = std::max(0.0, convexRadiusB),
1045
+ .center = {poseB[0], poseB[1], poseB[2]},
1046
+ .orientation = {poseB[3], poseB[4], poseB[5], poseB[6]},
1047
+ };
1048
+
1049
+ const CollisionResult res = collideRoundedHullWithRoundedHull(shapeA, shapeB);
1050
+
1051
+ outCollision[0] = res.hasCollision ? 1.0 : 0.0;
1052
+ outCollision[1] = res.normal.x;
1053
+ outCollision[2] = res.normal.y;
1054
+ outCollision[3] = res.normal.z;
1055
+ outCollision[4] = res.penetration;
1056
+ outCollision[5] = res.contactPoint.x;
1057
+ outCollision[6] = res.contactPoint.y;
1058
+ outCollision[7] = res.contactPoint.z;
1059
+ outCollision[8] = res.witnessA.x;
1060
+ outCollision[9] = res.witnessA.y;
1061
+ outCollision[10] = res.witnessA.z;
1062
+ outCollision[11] = res.witnessB.x;
1063
+ outCollision[12] = res.witnessB.y;
1064
+ outCollision[13] = res.witnessB.z;
1065
+ return 0;
1066
+ }
1067
+
1068
+ std::int32_t rundot_kinetix_physics_multi_sphere_collision(
1069
+ const double* spheresA,
1070
+ std::uint32_t sphereCountA,
1071
+ const double* poseA,
1072
+ const double* spheresB,
1073
+ std::uint32_t sphereCountB,
1074
+ const double* poseB,
1075
+ double* outCollision) {
1076
+ if (!spheresA || sphereCountA == 0 || !poseA || !spheresB || sphereCountB == 0 || !poseB || !outCollision) {
1077
+ return -1;
1078
+ }
1079
+
1080
+ std::array<SphereDef, kMultiSphereMaxSpheres> storageA{};
1081
+ const std::uint32_t countA = std::min(sphereCountA, kMultiSphereMaxSpheres);
1082
+ for (std::uint32_t i = 0; i < countA; ++i) {
1083
+ storageA[i].offset = {spheresA[i * 4 + 0], spheresA[i * 4 + 1], spheresA[i * 4 + 2]};
1084
+ storageA[i].radius = spheresA[i * 4 + 3];
1085
+ }
1086
+
1087
+ std::array<SphereDef, kMultiSphereMaxSpheres> storageB{};
1088
+ const std::uint32_t countB = std::min(sphereCountB, kMultiSphereMaxSpheres);
1089
+ for (std::uint32_t i = 0; i < countB; ++i) {
1090
+ storageB[i].offset = {spheresB[i * 4 + 0], spheresB[i * 4 + 1], spheresB[i * 4 + 2]};
1091
+ storageB[i].radius = spheresB[i * 4 + 3];
1092
+ }
1093
+
1094
+ MultiSphereShape shapeA{
1095
+ .spheres = std::span<const SphereDef>(storageA.data(), countA),
1096
+ .center = {poseA[0], poseA[1], poseA[2]},
1097
+ .orientation = {poseA[3], poseA[4], poseA[5], poseA[6]},
1098
+ };
1099
+
1100
+ MultiSphereShape shapeB{
1101
+ .spheres = std::span<const SphereDef>(storageB.data(), countB),
1102
+ .center = {poseB[0], poseB[1], poseB[2]},
1103
+ .orientation = {poseB[3], poseB[4], poseB[5], poseB[6]},
1104
+ };
1105
+
1106
+ const CollisionResult res = collideMultiSphereWithMultiSphere(shapeA, shapeB);
1107
+
1108
+ outCollision[0] = res.hasCollision ? 1.0 : 0.0;
1109
+ outCollision[1] = res.normal.x;
1110
+ outCollision[2] = res.normal.y;
1111
+ outCollision[3] = res.normal.z;
1112
+ outCollision[4] = res.penetration;
1113
+ outCollision[5] = res.contactPoint.x;
1114
+ outCollision[6] = res.contactPoint.y;
1115
+ outCollision[7] = res.contactPoint.z;
1116
+ outCollision[8] = res.witnessA.x;
1117
+ outCollision[9] = res.witnessA.y;
1118
+ outCollision[10] = res.witnessA.z;
1119
+ outCollision[11] = res.witnessB.x;
1120
+ outCollision[12] = res.witnessB.y;
1121
+ outCollision[13] = res.witnessB.z;
1122
+ return 0;
1123
+ }
1124
+
1125
+ std::int32_t rundot_kinetix_physics_multi_sphere_mass_properties(
1126
+ const double* spheresData,
1127
+ std::uint32_t sphereCount,
1128
+ double density,
1129
+ double* outMassProperties) {
1130
+ if (!spheresData || sphereCount == 0 || !outMassProperties) return -1;
1131
+
1132
+ std::array<SphereDef, kMultiSphereMaxSpheres> storage{};
1133
+ const std::uint32_t count = std::min(sphereCount, kMultiSphereMaxSpheres);
1134
+ for (std::uint32_t i = 0; i < count; ++i) {
1135
+ storage[i].offset = {spheresData[i * 4 + 0], spheresData[i * 4 + 1], spheresData[i * 4 + 2]};
1136
+ storage[i].radius = spheresData[i * 4 + 3];
1137
+ }
1138
+
1139
+ const MassProperties props = computeMultiSphereMassProperties(
1140
+ std::span<const SphereDef>(storage.data(), count), density);
1141
+
1142
+ outMassProperties[0] = props.volume;
1143
+ outMassProperties[1] = props.mass;
1144
+ outMassProperties[2] = props.centerOfMass.x;
1145
+ outMassProperties[3] = props.centerOfMass.y;
1146
+ outMassProperties[4] = props.centerOfMass.z;
1147
+ outMassProperties[5] = props.inertia.xx;
1148
+ outMassProperties[6] = props.inertia.yy;
1149
+ outMassProperties[7] = props.inertia.zz;
1150
+ outMassProperties[8] = props.inertia.xy;
1151
+ outMassProperties[9] = props.inertia.xz;
1152
+ outMassProperties[10] = props.inertia.yz;
1153
+ return 0;
1154
+ }
1155
+
1156
+ std::int32_t rundot_kinetix_physics_rounded_convex_mass_properties(
1157
+ const double* verticesData,
1158
+ std::uint32_t vertexCount,
1159
+ double convexRadius,
1160
+ double density,
1161
+ double* outMassProperties) {
1162
+ if (!verticesData || vertexCount == 0 || !outMassProperties) return -1;
1163
+
1164
+ std::array<Vec3, kRoundedHullMaxVertices> storage{};
1165
+ const std::uint32_t count = std::min(vertexCount, kRoundedHullMaxVertices);
1166
+ for (std::uint32_t i = 0; i < count; ++i) {
1167
+ storage[i] = {verticesData[i * 3 + 0], verticesData[i * 3 + 1], verticesData[i * 3 + 2]};
1168
+ }
1169
+
1170
+ const MassProperties props = computeRoundedHullMassProperties(
1171
+ std::span<const Vec3>(storage.data(), count), convexRadius, density);
1172
+
1173
+ outMassProperties[0] = props.volume;
1174
+ outMassProperties[1] = props.mass;
1175
+ outMassProperties[2] = props.centerOfMass.x;
1176
+ outMassProperties[3] = props.centerOfMass.y;
1177
+ outMassProperties[4] = props.centerOfMass.z;
1178
+ outMassProperties[5] = props.inertia.xx;
1179
+ outMassProperties[6] = props.inertia.yy;
1180
+ outMassProperties[7] = props.inertia.zz;
1181
+ outMassProperties[8] = props.inertia.xy;
1182
+ outMassProperties[9] = props.inertia.xz;
1183
+ outMassProperties[10] = props.inertia.yz;
1184
+ return 0;
1185
+ }
1186
+
1187
+ std::int32_t rundot_kinetix_physics_rounded_convex_raycast(
1188
+ const double* verticesData,
1189
+ std::uint32_t vertexCount,
1190
+ double convexRadius,
1191
+ const double* poseData,
1192
+ const double* rayData,
1193
+ double* outHit) {
1194
+ if (!verticesData || vertexCount == 0 || !poseData || !rayData || !outHit) return -1;
1195
+
1196
+ std::array<Vec3, kRoundedHullMaxVertices> storage{};
1197
+ const std::uint32_t count = std::min(vertexCount, kRoundedHullMaxVertices);
1198
+ for (std::uint32_t i = 0; i < count; ++i) {
1199
+ storage[i] = {verticesData[i * 3 + 0], verticesData[i * 3 + 1], verticesData[i * 3 + 2]};
1200
+ }
1201
+
1202
+ RoundedConvexHullShape shape{
1203
+ .vertices = std::span<const Vec3>(storage.data(), count),
1204
+ .convexRadius = std::max(0.0, convexRadius),
1205
+ .center = {poseData[0], poseData[1], poseData[2]},
1206
+ .orientation = {poseData[3], poseData[4], poseData[5], poseData[6]},
1207
+ };
1208
+
1209
+ const Ray ray{
1210
+ .origin = {rayData[0], rayData[1], rayData[2]},
1211
+ .direction = {rayData[3], rayData[4], rayData[5]},
1212
+ .maxDistance = rayData[6],
1213
+ };
1214
+
1215
+ const RayHit hit = raycastRoundedHull(shape, ray);
1216
+
1217
+ outHit[0] = hit.hit ? 1.0 : 0.0;
1218
+ outHit[1] = hit.distance;
1219
+ outHit[2] = hit.point.x;
1220
+ outHit[3] = hit.point.y;
1221
+ outHit[4] = hit.point.z;
1222
+ outHit[5] = hit.normal.x;
1223
+ outHit[6] = hit.normal.y;
1224
+ outHit[7] = hit.normal.z;
1225
+ outHit[8] = static_cast<double>(hit.featureIndex);
1226
+ return 0;
1227
+ }
1228
+
1229
+ } // extern "C"