@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,1179 @@
1
+ #include "tracks.hpp"
2
+
3
+ #include <algorithm>
4
+ #include <array>
5
+ #include <cmath>
6
+ #include <cstdint>
7
+ #include <limits>
8
+ #include <span>
9
+
10
+ namespace rundot::kinetix::physics::tracks {
11
+
12
+ namespace {
13
+
14
+ constexpr double kGravity = 9.80665;
15
+
16
+ inline double jsRound(double value) {
17
+ const double ceiling = std::ceil(value);
18
+ return (ceiling - 0.5 <= value) ? ceiling : (ceiling - 1.0);
19
+ }
20
+
21
+ inline double solverSqrt(double value) {
22
+ if (value <= 0.0) return 0.0;
23
+ return std::sqrt(value);
24
+ }
25
+
26
+ } // namespace
27
+
28
+ double quantizeValue(double value) {
29
+ const double quantized = jsRound(value * 1e6) / 1e6;
30
+ return (quantized == 0.0) ? 0.0 : quantized;
31
+ }
32
+
33
+ double clampValue(double v, double minVal, double maxVal) {
34
+ return std::max(minVal, std::min(maxVal, v));
35
+ }
36
+
37
+ Vector3 addVec3(const Vector3& a, const Vector3& b) {
38
+ return {a.x + b.x, a.y + b.y, a.z + b.z};
39
+ }
40
+
41
+ Vector3 subVec3(const Vector3& a, const Vector3& b) {
42
+ return {a.x - b.x, a.y - b.y, a.z - b.z};
43
+ }
44
+
45
+ Vector3 scaleVec3(const Vector3& v, double s) {
46
+ return {v.x * s, v.y * s, v.z * s};
47
+ }
48
+
49
+ double dotVec3(const Vector3& a, const Vector3& b) {
50
+ return a.x * b.x + a.y * b.y + a.z * b.z;
51
+ }
52
+
53
+ Vector3 crossVec3(const Vector3& a, const Vector3& b) {
54
+ return {
55
+ a.y * b.z - a.z * b.y,
56
+ a.z * b.x - a.x * b.z,
57
+ a.x * b.y - a.y * b.x,
58
+ };
59
+ }
60
+
61
+ double lengthSquaredVec3(const Vector3& v) {
62
+ return dotVec3(v, v);
63
+ }
64
+
65
+ double lengthVec3(const Vector3& v) {
66
+ return solverSqrt(lengthSquaredVec3(v));
67
+ }
68
+
69
+ Vector3 normalizeVec3(const Vector3& v) {
70
+ const double len = lengthVec3(v);
71
+ if (len <= 1e-15) return {0.0, 0.0, 0.0};
72
+ return scaleVec3(v, 1.0 / len);
73
+ }
74
+
75
+ Vector3 rotateByQuat(const Quaternion& q, const Vector3& v) {
76
+ const Vector3 u{q.x, q.y, q.z};
77
+ const Vector3 uv = crossVec3(u, v);
78
+ const Vector3 uuv = crossVec3(u, uv);
79
+ return addVec3(v, addVec3(scaleVec3(uv, 2.0 * q.w), scaleVec3(uuv, 2.0)));
80
+ }
81
+
82
+ Vector3 invRotateByQuat(const Quaternion& q, const Vector3& v) {
83
+ const Quaternion conj{-q.x, -q.y, -q.z, q.w};
84
+ return rotateByQuat(conj, v);
85
+ }
86
+
87
+ // ---------------------------------------------------------------------------
88
+ // Track Contact and Friction Implementation
89
+ // ---------------------------------------------------------------------------
90
+
91
+ TrackContactState evaluateTrackContactPoint(
92
+ const TrackWheelConfig& wheel,
93
+ const Vector3& trackOffset,
94
+ const Vector3& bodyPos,
95
+ const Quaternion& bodyOri,
96
+ const Vector3& bodyLinVel,
97
+ const Vector3& bodyAngVel,
98
+ double trackLinearSpeed,
99
+ double muLon,
100
+ double muLat,
101
+ double groundY,
102
+ double dt) {
103
+ TrackContactState contact{};
104
+
105
+ // Compute road wheel suspension mount in world coordinates
106
+ const Vector3 localPos = addVec3(trackOffset, wheel.localAnchor);
107
+ const Vector3 mountWorld = addVec3(bodyPos, rotateByQuat(bodyOri, localPos));
108
+ const Vector3 suspDirWorld = rotateByQuat(bodyOri, wheel.suspensionDir);
109
+
110
+ // Velocity of the mount point
111
+ const Vector3 armWorld = subVec3(mountWorld, bodyPos);
112
+ const Vector3 mountVel = addVec3(bodyLinVel, crossVec3(bodyAngVel, armWorld));
113
+
114
+ // Forward and lateral headings of the vehicle
115
+ const Vector3 fwdHeading = rotateByQuat(bodyOri, Vector3{1.0, 0.0, 0.0});
116
+ const Vector3 latHeading = rotateByQuat(bodyOri, Vector3{0.0, 0.0, 1.0});
117
+
118
+ // Ray / swept ground intersection along suspension direction
119
+ // For ground plane at y = groundY:
120
+ // mountWorld.y + t * suspDirWorld.y = groundY + wheel.wheelRadius
121
+ if (std::abs(suspDirWorld.y) > 1e-6) {
122
+ const double targetY = groundY + wheel.wheelRadius;
123
+ const double t = (targetY - mountWorld.y) / suspDirWorld.y;
124
+ const double totalRest = wheel.suspensionRestLength;
125
+
126
+ if (t >= 0.0 && t <= totalRest + wheel.suspensionMaxTravel) {
127
+ const double compression = clampValue(totalRest - t, 0.0, wheel.suspensionMaxTravel);
128
+ if (compression > 1e-5) {
129
+ contact.grounded = true;
130
+ contact.compression = compression;
131
+ contact.contactPoint = addVec3(mountWorld, scaleVec3(suspDirWorld, t));
132
+ contact.contactNormal = Vector3{0.0, 1.0, 0.0};
133
+
134
+ // Compression rate
135
+ const double compressionVel = -dotVec3(mountVel, suspDirWorld);
136
+ const double springForce = wheel.stiffness * compression;
137
+ const double damperForce = wheel.damping * compressionVel;
138
+ contact.normalForce = std::max(0.0, springForce + damperForce);
139
+
140
+ // Ground velocity at contact point
141
+ const Vector3 contactArm = subVec3(contact.contactPoint, bodyPos);
142
+ const Vector3 contactVel = addVec3(bodyLinVel, crossVec3(bodyAngVel, contactArm));
143
+
144
+ const double contactVx = dotVec3(contactVel, fwdHeading);
145
+ const double contactVz = dotVec3(contactVel, latHeading);
146
+
147
+ // Track friction evaluation
148
+ double fLon = 0.0;
149
+ double fLat = 0.0;
150
+ double scrubM = 0.0;
151
+ computeTrackFriction(
152
+ trackLinearSpeed,
153
+ contactVx,
154
+ contactVz,
155
+ contact.normalForce,
156
+ muLon,
157
+ muLat,
158
+ wheel.localAnchor.x,
159
+ fLon,
160
+ fLat,
161
+ scrubM);
162
+
163
+ contact.longitudinalForce = fLon;
164
+ contact.lateralForce = fLat;
165
+ contact.longitudinalSlip = (trackLinearSpeed - contactVx) / std::max(1.0, std::abs(contactVx));
166
+ contact.lateralSlip = -contactVz / std::max(1.0, std::abs(contactVx));
167
+ }
168
+ }
169
+ }
170
+
171
+ (void)dt;
172
+ return contact;
173
+ }
174
+
175
+ void computeTrackFriction(
176
+ double trackLinearSpeed,
177
+ double contactVx,
178
+ double contactVz,
179
+ double normalForce,
180
+ double muLon,
181
+ double muLat,
182
+ double contactXOffset,
183
+ double& outLongitudinalForce,
184
+ double& outLateralForce,
185
+ double& outScrubTorque) {
186
+ if (normalForce <= 1e-4) {
187
+ outLongitudinalForce = 0.0;
188
+ outLateralForce = 0.0;
189
+ outScrubTorque = 0.0;
190
+ return;
191
+ }
192
+
193
+ // Longitudinal slip: difference between track belt linear speed and ground forward speed
194
+ const double vSpeedMag = std::max(0.5, std::max(std::abs(trackLinearSpeed), std::abs(contactVx)));
195
+ const double lonSlip = (trackLinearSpeed - contactVx) / vSpeedMag;
196
+
197
+ // Lateral scrub velocity
198
+ const double vLatMag = std::max(0.5, std::abs(contactVz));
199
+ const double latSlip = -contactVz / vLatMag;
200
+
201
+ // Smooth Coulomb saturation using rational form s / (1 + |s|) for bit determinism
202
+ const double lonSat = lonSlip / (1.0 + std::abs(lonSlip));
203
+ const double latSat = latSlip / (1.0 + std::abs(latSlip));
204
+
205
+ double fLon = normalForce * muLon * lonSat * 1.5;
206
+ double fLat = normalForce * muLat * latSat * 1.5;
207
+
208
+ // Elliptical friction limit: (fLon / (muLon * N))^2 + (fLat / (muLat * N))^2 <= 1.0
209
+ const double maxLon = normalForce * muLon;
210
+ const double maxLat = normalForce * muLat;
211
+
212
+ if (maxLon > 1e-6 && maxLat > 1e-6) {
213
+ const double ratioLon = fLon / maxLon;
214
+ const double ratioLat = fLat / maxLat;
215
+ const double ellipseDistSq = ratioLon * ratioLon + ratioLat * ratioLat;
216
+ if (ellipseDistSq > 1.0) {
217
+ const double scale = 1.0 / solverSqrt(ellipseDistSq);
218
+ fLon *= scale;
219
+ fLat *= scale;
220
+ }
221
+ }
222
+
223
+ outLongitudinalForce = quantizeValue(fLon);
224
+ outLateralForce = quantizeValue(fLat);
225
+
226
+ // Lateral scrub resistance moment opposing yaw rotation:
227
+ // Friction force at offset x creates torque: tau = x * F_lat
228
+ outScrubTorque = quantizeValue(contactXOffset * fLat);
229
+ }
230
+
231
+ void evaluateDifferentialSteering(
232
+ const TrackedVehicleConfig& config,
233
+ const TrackedVehicleInput& input,
234
+ double currentForwardSpeed,
235
+ double& outTargetLeftSpeed,
236
+ double& outTargetRightSpeed,
237
+ double& outDifferentialTorque) {
238
+ // Command speed based on forward/reverse input
239
+ const double maxSpd = config.maxSpeed;
240
+ const double cmdSpeed = input.forward * maxSpd;
241
+
242
+ // Skid steer turning command
243
+ // Left and right ratios modify track velocities
244
+ double lRatio = input.leftRatio;
245
+ double rRatio = input.rightRatio;
246
+
247
+ if (std::abs(input.steer) > 1e-4) {
248
+ // Skid steer turning:
249
+ // Right turn (steer > 0): outer (left) track speeds up, inner (right) track slows down
250
+ // Left turn (steer < 0): inner (left) track slows down, outer (right) track speeds up
251
+ lRatio = clampValue(lRatio + input.steer, -1.0, 1.0);
252
+ rRatio = clampValue(rRatio - input.steer, -1.0, 1.0);
253
+ }
254
+
255
+ // Account for individual track braking
256
+ const double effLeftBrake = clampValue(input.brake + input.leftBrake, 0.0, 1.0);
257
+ const double effRightBrake = clampValue(input.brake + input.rightBrake, 0.0, 1.0);
258
+
259
+ outTargetLeftSpeed = quantizeValue(cmdSpeed * lRatio * (1.0 - effLeftBrake));
260
+ outTargetRightSpeed = quantizeValue(cmdSpeed * rRatio * (1.0 - effRightBrake));
261
+
262
+ // Ideal differential torque across track gauge B
263
+ const double speedDiff = outTargetRightSpeed - outTargetLeftSpeed;
264
+ const double gauge = std::max(0.5, config.trackGauge);
265
+ // Differential drive torque: tau = (F_r - F_l) * (B / 2)
266
+ const double estimatedThrustDiff = speedDiff * (config.mass / 2.0);
267
+ outDifferentialTorque = quantizeValue(estimatedThrustDiff * (gauge * 0.5));
268
+
269
+ (void)currentForwardSpeed;
270
+ }
271
+
272
+ TrackedVehicleOutput stepTrackedVehicle(
273
+ const TrackedVehicleConfig& config,
274
+ TrackedVehicleState& state,
275
+ const TrackedVehicleInput& input,
276
+ double groundElevation,
277
+ double dt) {
278
+ TrackedVehicleOutput output{};
279
+ if (dt <= 0.0) return output;
280
+
281
+ const Vector3 fwdHeading = rotateByQuat(state.orientation, Vector3{1.0, 0.0, 0.0});
282
+ const Vector3 upHeading = rotateByQuat(state.orientation, Vector3{0.0, 1.0, 0.0});
283
+ const Vector3 latHeading = rotateByQuat(state.orientation, Vector3{0.0, 0.0, 1.0});
284
+
285
+ // Evaluate differential steering target speeds
286
+ double targetLeftSpeed = 0.0;
287
+ double targetRightSpeed = 0.0;
288
+ double diffTorque = 0.0;
289
+ const double currentFwdSpeed = dotVec3(state.linearVel, fwdHeading);
290
+
291
+ evaluateDifferentialSteering(
292
+ config,
293
+ input,
294
+ currentFwdSpeed,
295
+ targetLeftSpeed,
296
+ targetRightSpeed,
297
+ diffTorque);
298
+
299
+ // Track drive and braking torques
300
+ const double maxDriveT = config.tracks[0].maxDriveTorque;
301
+ const double maxBrakeT = config.tracks[0].maxBrakeTorque;
302
+
303
+ const double leftBrakeFrac = clampValue(input.brake + input.leftBrake, 0.0, 1.0);
304
+ const double rightBrakeFrac = clampValue(input.brake + input.rightBrake, 0.0, 1.0);
305
+
306
+ output.leftTrack.driveTorque = quantizeValue(input.forward * maxDriveT * input.leftRatio * (1.0 - leftBrakeFrac));
307
+ output.rightTrack.driveTorque = quantizeValue(input.forward * maxDriveT * input.rightRatio * (1.0 - rightBrakeFrac));
308
+ output.leftTrack.brakeTorque = quantizeValue(leftBrakeFrac * maxBrakeT);
309
+ output.rightTrack.brakeTorque = quantizeValue(rightBrakeFrac * maxBrakeT);
310
+
311
+ // Integrate sprocket angular velocities and track linear speeds
312
+ const double rSprocket = std::max(0.1, config.sprocketRadius);
313
+ const double iSprocket = std::max(0.5, config.tracks[0].sprocketInertia);
314
+
315
+ // Left track speed update
316
+ const double leftSpeedError = targetLeftSpeed - state.leftTrackLinearSpeed;
317
+ const double leftAccel = (output.leftTrack.driveTorque / rSprocket + leftSpeedError * 1500.0) / (iSprocket * 10.0);
318
+ state.leftTrackLinearSpeed = quantizeValue(state.leftTrackLinearSpeed + leftAccel * dt);
319
+ state.leftSprocketAngularVel = state.leftTrackLinearSpeed / rSprocket;
320
+ output.leftTrack.linearSpeed = state.leftTrackLinearSpeed;
321
+
322
+ // Right track speed update
323
+ const double rightSpeedError = targetRightSpeed - state.rightTrackLinearSpeed;
324
+ const double rightAccel = (output.rightTrack.driveTorque / rSprocket + rightSpeedError * 1500.0) / (iSprocket * 10.0);
325
+ state.rightTrackLinearSpeed = quantizeValue(state.rightTrackLinearSpeed + rightAccel * dt);
326
+ state.rightSprocketAngularVel = state.rightTrackLinearSpeed / rSprocket;
327
+ output.rightTrack.linearSpeed = state.rightTrackLinearSpeed;
328
+
329
+ // Step contacts and friction for both tracks in fixed index order
330
+ Vector3 totalForce{0.0, 0.0, 0.0};
331
+ Vector3 totalTorque{0.0, 0.0, 0.0};
332
+ std::uint32_t groundedCount = 0;
333
+
334
+ // Ensure tracks have lateral offsets based on track gauge if not explicitly set
335
+ TrackedVehicleConfig vehicleConfig = config;
336
+ const double trackGauge = std::max(0.5, vehicleConfig.trackGauge);
337
+ if (std::abs(vehicleConfig.tracks[0].offset.z) < 1e-6 && std::abs(vehicleConfig.tracks[1].offset.z) < 1e-6) {
338
+ vehicleConfig.tracks[0].offset = {0.0, 0.0, -trackGauge * 0.5};
339
+ vehicleConfig.tracks[1].offset = {0.0, 0.0, trackGauge * 0.5};
340
+ }
341
+
342
+ for (std::uint32_t t = 0; t < kMaxTracks; ++t) {
343
+ TrackConfig& trkCfg = vehicleConfig.tracks[t];
344
+ const std::uint32_t nWheels = std::min(trkCfg.wheelCount, kMaxRoadWheelsPerTrack);
345
+ const double segLen = trkCfg.trackLength / std::max(1.0, static_cast<double>(nWheels - 1));
346
+ const double startX = -trkCfg.trackLength * 0.5;
347
+ for (std::uint32_t w = 0; w < nWheels; ++w) {
348
+ if (trkCfg.wheels[w].stiffness <= 0.0) {
349
+ trkCfg.wheels[w].stiffness = 45000.0;
350
+ trkCfg.wheels[w].damping = 3500.0;
351
+ trkCfg.wheels[w].suspensionRestLength = 0.5;
352
+ trkCfg.wheels[w].suspensionMaxTravel = 0.3;
353
+ trkCfg.wheels[w].wheelRadius = 0.25;
354
+ trkCfg.wheels[w].suspensionDir = {0.0, -1.0, 0.0};
355
+ }
356
+ if (w > 0 && trkCfg.wheels[w].localAnchor.x == 0.0) {
357
+ trkCfg.wheels[w].localAnchor = {startX + w * segLen, -0.3, 0.0};
358
+ }
359
+ }
360
+ }
361
+
362
+ for (std::uint32_t t = 0; t < kMaxTracks; ++t) {
363
+ TrackOutput& trkOut = (t == 0) ? output.leftTrack : output.rightTrack;
364
+ const TrackConfig& trkCfg = vehicleConfig.tracks[t];
365
+ const double trkSpeed = (t == 0) ? state.leftTrackLinearSpeed : state.rightTrackLinearSpeed;
366
+
367
+ double totalTraction = 0.0;
368
+ double totalScrub = 0.0;
369
+ double totalScrubTorque = 0.0;
370
+ std::uint32_t trkGrounded = 0;
371
+
372
+ const std::uint32_t nWheels = std::min(trkCfg.wheelCount, kMaxRoadWheelsPerTrack);
373
+ for (std::uint32_t w = 0; w < nWheels; ++w) {
374
+ const auto contact = evaluateTrackContactPoint(
375
+ trkCfg.wheels[w],
376
+ trkCfg.offset,
377
+ state.position,
378
+ state.orientation,
379
+ state.linearVel,
380
+ state.angularVel,
381
+ trkSpeed,
382
+ trkCfg.muLongitudinal,
383
+ trkCfg.muLateral,
384
+ groundElevation,
385
+ dt);
386
+
387
+ trkOut.wheelContacts[w] = contact;
388
+ if (contact.grounded) {
389
+ trkGrounded++;
390
+ totalTraction += contact.longitudinalForce;
391
+ totalScrub += contact.lateralForce;
392
+ totalScrubTorque += contact.lateralForce * trkCfg.wheels[w].localAnchor.x;
393
+
394
+ // Add 3D forces in world space
395
+ const Vector3 contactForce = addVec3(
396
+ scaleVec3(upHeading, contact.normalForce),
397
+ addVec3(
398
+ scaleVec3(fwdHeading, contact.longitudinalForce),
399
+ scaleVec3(latHeading, contact.lateralForce)));
400
+
401
+ totalForce = addVec3(totalForce, contactForce);
402
+
403
+ // Torque about chassis CoM: tau = r x F
404
+ const Vector3 rContact = subVec3(contact.contactPoint, state.position);
405
+ const Vector3 contactTorque = crossVec3(rContact, contactForce);
406
+ totalTorque = addVec3(totalTorque, contactTorque);
407
+ }
408
+ }
409
+
410
+ trkOut.groundedWheelCount = trkGrounded;
411
+ trkOut.tractionForce = quantizeValue(totalTraction);
412
+ trkOut.lateralScrubForce = quantizeValue(totalScrub);
413
+ trkOut.scrubResistanceTorque = quantizeValue(totalScrubTorque);
414
+ groundedCount += trkGrounded;
415
+ }
416
+
417
+ output.netForce = totalForce;
418
+ output.netTorque = totalTorque;
419
+ output.totalGroundedWheels = groundedCount;
420
+
421
+ // Kinematic turn radius and yaw rate
422
+ const double deltaSpeed = state.leftTrackLinearSpeed - state.rightTrackLinearSpeed;
423
+ if (std::abs(deltaSpeed) > 1e-4) {
424
+ output.turnRadius = quantizeValue((trackGauge * 0.5) * ((state.leftTrackLinearSpeed + state.rightTrackLinearSpeed) / deltaSpeed));
425
+ output.yawRate = quantizeValue(deltaSpeed / trackGauge);
426
+ } else {
427
+ output.turnRadius = 1e6;
428
+ output.yawRate = 0.0;
429
+ }
430
+ return output;
431
+ }
432
+
433
+ // ---------------------------------------------------------------------------
434
+ // Motorcycle Balance and Two-Wheel Steering Implementation
435
+ // ---------------------------------------------------------------------------
436
+
437
+ double computeMotorcycleLeanAngle(const Quaternion& orientation) {
438
+ // Extract local up and lateral unit vectors transformed to world
439
+ const Vector3 latWorld = rotateByQuat(orientation, Vector3{0.0, 0.0, 1.0});
440
+ const Vector3 upWorld = rotateByQuat(orientation, Vector3{0.0, 1.0, 0.0});
441
+
442
+ // Roll angle phi relative to vertical:
443
+ // Positive lean = leaning to the right (+lat)
444
+ const double roll = std::atan2(-latWorld.y, upWorld.y);
445
+ return quantizeValue(roll);
446
+ }
447
+
448
+ double computeTargetLeanAngle(
449
+ double forwardSpeed,
450
+ double yawRate,
451
+ double steerAngle,
452
+ double wheelbase,
453
+ double maxLeanAngle) {
454
+ // Target lean angle based on inverted pendulum steady-state cornering:
455
+ // Centripetal acceleration a_c = v * psi_dot
456
+ // Kinematic yaw rate from steer angle delta: psi_dot_kin = (v / L) * tan(delta)
457
+ const double wb = std::max(0.5, wheelbase);
458
+ const double kinYawRate = (std::abs(forwardSpeed) > 0.1)
459
+ ? (forwardSpeed / wb) * std::tan(clampValue(steerAngle, -0.7, 0.7))
460
+ : 0.0;
461
+
462
+ // Blend actual yaw rate and kinematic yaw rate for responsive balance
463
+ const double blendedYawRate = (std::abs(forwardSpeed) > 1.0)
464
+ ? (0.7 * yawRate + 0.3 * kinYawRate)
465
+ : kinYawRate;
466
+
467
+ const double centripetalAccel = forwardSpeed * blendedYawRate;
468
+ // tan(phi) = a_c / g
469
+ const double targetRoll = std::atan(centripetalAccel / kGravity);
470
+
471
+ return quantizeValue(clampValue(targetRoll, -maxLeanAngle, maxLeanAngle));
472
+ }
473
+
474
+ double computeBoundedBalanceTorque(
475
+ const MotorcycleConfig& config,
476
+ double currentLean,
477
+ double currentLeanRate,
478
+ double targetLean,
479
+ double& inOutIntegral,
480
+ double dt) {
481
+ if (!config.enableLeanController) {
482
+ return 0.0;
483
+ }
484
+
485
+ // Roll angle error
486
+ const double leanError = currentLean - targetLean;
487
+
488
+ // Update integral with exponential decay anti-windup
489
+ const double decay = clampValue(config.leanSpringIntegrationCoefficientDecay, 0.0, 1.0);
490
+ const double decayFactor = std::pow(decay, std::max(0.001, dt));
491
+ inOutIntegral = quantizeValue(inOutIntegral * decayFactor + leanError * dt);
492
+
493
+ // PD + I controller
494
+ const double pTerm = config.leanSpringConstant * leanError;
495
+ const double dTerm = config.leanSpringDamping * currentLeanRate;
496
+ const double iTerm = config.leanSpringIntegrationCoefficient * inOutIntegral;
497
+
498
+ const double rawTorque = -(pTerm + dTerm + iTerm);
499
+
500
+ // STRICT BOUND ON CONTROLLER ROLL TORQUE
501
+ const double maxTorque = std::max(10.0, config.maxBalanceTorque);
502
+ const double boundedTorque = clampValue(rawTorque, -maxTorque, maxTorque);
503
+
504
+ return quantizeValue(boundedTorque);
505
+ }
506
+
507
+ double computeGyroscopicTorque(
508
+ const MotorcycleConfig& config,
509
+ double frontOmega,
510
+ double rearOmega,
511
+ double steerAngularVel,
512
+ double rollRate) {
513
+ // Gyroscopic precession torque: tau = L x omega
514
+ // Front wheel angular momentum: L_f = I_f * omega_f
515
+ // Rear wheel angular momentum: L_r = I_r * omega_r
516
+ const double lFront = config.frontInertia * frontOmega;
517
+ const double lRear = config.rearInertia * rearOmega;
518
+ const double totalL = lFront + lRear;
519
+
520
+ // Steering rate produces roll precession torque
521
+ const double rakeCos = std::cos(config.rakeAngle);
522
+ const double rollFromSteer = -lFront * steerAngularVel * rakeCos * config.gyroscopicFactor;
523
+
524
+ // Roll rate produces self-centering steering torque
525
+ const double steerFromRoll = totalL * rollRate * rakeCos * 0.5 * config.gyroscopicFactor;
526
+
527
+ return quantizeValue(rollFromSteer + steerFromRoll);
528
+ }
529
+
530
+ double computeCounterSteerTorque(
531
+ const MotorcycleConfig& config,
532
+ double forwardSpeed,
533
+ double steerCommand,
534
+ double currentSteerAngle,
535
+ double currentLean,
536
+ double targetLean) {
537
+ // To lean right (targetLean > currentLean), the rider momentarily steers LEFT (negative delta)
538
+ // This generates an outward lateral tire force at ground level, creating a roll moment toward the turn.
539
+ const double leanDelta = targetLean - currentLean;
540
+ const double spdFactor = clampValue(std::abs(forwardSpeed) / 10.0, 0.1, 3.0);
541
+
542
+ // Countersteering impulse torque on the fork
543
+ const double counterSteerTorque = -config.counterSteerFactor * leanDelta * (spdFactor * 120.0);
544
+
545
+ // Steady-state steering alignment torque
546
+ const double steerError = steerCommand * config.maxSteerAngle - currentSteerAngle;
547
+ const double alignTorque = steerError * 180.0;
548
+
549
+ const double totalSteerTorque = counterSteerTorque + alignTorque;
550
+
551
+ // STRICT BOUND ON CONTROLLER STEER FORCES
552
+ const double maxTorque = std::max(5.0, config.maxSteerTorque);
553
+ return quantizeValue(clampValue(totalSteerTorque, -maxTorque, maxTorque));
554
+ }
555
+
556
+ MotorcycleOutput stepMotorcycle(
557
+ const MotorcycleConfig& config,
558
+ MotorcycleState& state,
559
+ const MotorcycleInput& input,
560
+ double groundElevation,
561
+ double dt) {
562
+ MotorcycleOutput output{};
563
+ if (dt <= 0.0) return output;
564
+
565
+ const Vector3 fwdHeading = rotateByQuat(state.orientation, Vector3{1.0, 0.0, 0.0});
566
+ const Vector3 upHeading = rotateByQuat(state.orientation, Vector3{0.0, 1.0, 0.0});
567
+ const Vector3 latHeading = rotateByQuat(state.orientation, Vector3{0.0, 0.0, 1.0});
568
+
569
+ const double forwardSpeed = dotVec3(state.linearVel, fwdHeading);
570
+ const double yawRate = dotVec3(state.angularVel, Vector3{0.0, 1.0, 0.0});
571
+
572
+ // 1. Current roll/lean angle and roll rate
573
+ state.currentLean = computeMotorcycleLeanAngle(state.orientation);
574
+ state.currentLeanRate = dotVec3(state.angularVel, fwdHeading);
575
+
576
+ // Smooth lean tracking
577
+ const double smoothAlpha = clampValue(config.leanSmoothingFactor, 0.01, 1.0);
578
+ state.smoothedLean = quantizeValue(state.smoothedLean + smoothAlpha * (state.currentLean - state.smoothedLean));
579
+
580
+ // 2. Target lean angle computation
581
+ if (std::abs(input.targetLeanOverride) > 1e-4) {
582
+ state.targetLean = clampValue(input.targetLeanOverride, -config.maxLeanAngle, config.maxLeanAngle);
583
+ } else {
584
+ state.targetLean = computeTargetLeanAngle(
585
+ forwardSpeed,
586
+ yawRate,
587
+ state.steerAngle,
588
+ config.wheelbase,
589
+ config.maxLeanAngle);
590
+ }
591
+ output.targetLean = state.targetLean;
592
+
593
+ // 3. Bounded active balance roll torque
594
+ output.balanceTorque = computeBoundedBalanceTorque(
595
+ config,
596
+ state.currentLean,
597
+ state.currentLeanRate,
598
+ state.targetLean,
599
+ state.leanErrorIntegral,
600
+ dt);
601
+
602
+ // 4. Countersteering and fork steer torque
603
+ output.counterSteerTorque = computeCounterSteerTorque(
604
+ config,
605
+ forwardSpeed,
606
+ input.steer,
607
+ state.steerAngle,
608
+ state.currentLean,
609
+ state.targetLean);
610
+ output.steerTorque = output.counterSteerTorque;
611
+
612
+ // 5. Wheel spin angular velocities
613
+ const double rFront = std::max(0.1, config.frontRadius);
614
+ const double rRear = std::max(0.1, config.rearRadius);
615
+
616
+ state.frontWheelOmega = forwardSpeed / rFront;
617
+ state.rearWheelOmega = forwardSpeed / rRear;
618
+
619
+ // 6. Gyroscopic precession moment
620
+ output.gyroscopicTorque = computeGyroscopicTorque(
621
+ config,
622
+ state.frontWheelOmega,
623
+ state.rearWheelOmega,
624
+ state.steerAngularVel,
625
+ state.currentLeanRate);
626
+
627
+ // 7. Steer angle integration with lean steering limit
628
+ double steerLimit = config.maxSteerAngle;
629
+ if (config.enableLeanSteeringLimit) {
630
+ // Reduce steering limit at speed and high lean angle to prevent lowsides
631
+ const double leanCos = std::max(0.1, std::cos(state.currentLean));
632
+ const double spdAtten = 1.0 / (1.0 + 0.05 * forwardSpeed * forwardSpeed);
633
+ steerLimit = clampValue(config.maxSteerAngle * leanCos * spdAtten, 0.15, config.maxSteerAngle);
634
+ }
635
+
636
+ const double steerAccel = output.steerTorque / std::max(0.2, config.frontInertia);
637
+ state.steerAngularVel = clampValue(state.steerAngularVel + steerAccel * dt, -5.0, 5.0);
638
+ state.steerAngle = clampValue(state.steerAngle + state.steerAngularVel * dt, -steerLimit, steerLimit);
639
+ output.updatedSteerAngle = quantizeValue(state.steerAngle);
640
+
641
+ // 8. Ground contact and normal force distribution
642
+ const double halfWb = config.wheelbase * 0.5;
643
+ const Vector3 frontContactMount = addVec3(state.position, rotateByQuat(state.orientation, Vector3{halfWb, -config.comHeight, 0.0}));
644
+ const Vector3 rearContactMount = addVec3(state.position, rotateByQuat(state.orientation, Vector3{-halfWb, -config.comHeight, 0.0}));
645
+
646
+ state.frontGrounded = (frontContactMount.y <= groundElevation + rFront + 0.05);
647
+ state.rearGrounded = (rearContactMount.y <= groundElevation + rRear + 0.05);
648
+
649
+ const double totalWeight = config.mass * kGravity;
650
+ // Weight transfer under acceleration / braking
651
+ const double accelPitch = (input.throttle * config.maxDriveTorque / rRear - (input.frontBrake + input.rearBrake) * config.maxBrakeTorque / rFront) / config.mass;
652
+ const double weightShift = (config.mass * accelPitch * config.comHeight) / config.wheelbase;
653
+
654
+ state.frontNormalForce = state.frontGrounded ? std::max(50.0, totalWeight * 0.5 - weightShift) : 0.0;
655
+ state.rearNormalForce = state.rearGrounded ? std::max(50.0, totalWeight * 0.5 + weightShift) : 0.0;
656
+
657
+ // 9. Tire friction forces (Longitudinal, Cornering, Camber Thrust)
658
+ // Longitudinal traction and braking
659
+ const double driveForce = state.rearGrounded ? (input.throttle * config.maxDriveTorque / rRear) : 0.0;
660
+ const double frontBrakeForce = state.frontGrounded ? (input.frontBrake * config.maxBrakeTorque / rFront) : 0.0;
661
+ const double rearBrakeForce = state.rearGrounded ? (input.rearBrake * config.maxBrakeTorque * 0.5 / rRear) : 0.0;
662
+
663
+ output.rearLongitudinalForce = quantizeValue(clampValue(driveForce - rearBrakeForce, -0.85 * state.rearNormalForce, 0.85 * state.rearNormalForce));
664
+ output.frontLongitudinalForce = quantizeValue(clampValue(-frontBrakeForce, -0.85 * state.frontNormalForce, 0.85 * state.frontNormalForce));
665
+
666
+ // Slip angles
667
+ const double frontSlipAngle = std::atan2(dotVec3(state.linearVel, latHeading), std::max(0.5, std::abs(forwardSpeed))) - state.steerAngle;
668
+ const double rearSlipAngle = std::atan2(dotVec3(state.linearVel, latHeading), std::max(0.5, std::abs(forwardSpeed)));
669
+
670
+ // Cornering forces: F_corner = -C_alpha * alpha
671
+ const double frontCornering = -config.frontCorneringStiffness * frontSlipAngle;
672
+ const double rearCornering = -config.rearCorneringStiffness * rearSlipAngle;
673
+
674
+ // Camber thrust: rounded tire profile generates lateral thrust into the lean
675
+ output.frontCamberForce = quantizeValue(config.frontCamberStiffness * state.currentLean);
676
+ output.rearCamberForce = quantizeValue(config.rearCamberStiffness * state.currentLean);
677
+
678
+ // Total lateral forces clamped by friction ellipse
679
+ output.frontLateralForce = quantizeValue(clampValue(frontCornering + output.frontCamberForce, -0.9 * state.frontNormalForce, 0.9 * state.frontNormalForce));
680
+ output.rearLateralForce = quantizeValue(clampValue(rearCornering + output.rearCamberForce, -0.9 * state.rearNormalForce, 0.9 * state.rearNormalForce));
681
+
682
+ // 10. Combine 3D forces and torques
683
+ const double netLongitudinal = output.frontLongitudinalForce + output.rearLongitudinalForce;
684
+ const double netLateral = output.frontLateralForce + output.rearLateralForce;
685
+ const double netNormal = (state.frontNormalForce + state.rearNormalForce) - totalWeight;
686
+
687
+ output.netForce = addVec3(
688
+ scaleVec3(fwdHeading, netLongitudinal),
689
+ addVec3(
690
+ scaleVec3(latHeading, netLateral),
691
+ scaleVec3(upHeading, netNormal)));
692
+
693
+ // Torques:
694
+ // Roll torque: stabilizing balance torque + gyroscopic torque + camber roll moment
695
+ const double totalRollTorque = output.balanceTorque + output.gyroscopicTorque;
696
+ // Yaw torque: tire lateral forces across wheelbase
697
+ const double totalYawTorque = (output.frontLateralForce * halfWb) - (output.rearLateralForce * halfWb);
698
+
699
+ output.netTorque = addVec3(
700
+ scaleVec3(fwdHeading, totalRollTorque),
701
+ scaleVec3(upHeading, totalYawTorque));
702
+
703
+ // Stability assessment
704
+ output.leanStable = (std::abs(state.currentLean - state.targetLean) < 0.25) &&
705
+ (std::abs(state.currentLeanRate) < 1.5);
706
+
707
+ return output;
708
+ }
709
+
710
+ } // namespace rundot::kinetix::physics::tracks
711
+
712
+ // ---------------------------------------------------------------------------
713
+ // Plain C ABI Exports
714
+ // ---------------------------------------------------------------------------
715
+
716
+ extern "C" {
717
+
718
+ std::uint32_t rundot_kinetix_tracks_abi_version() {
719
+ return rundot::kinetix::physics::tracks::kTracksAbiVersion;
720
+ }
721
+
722
+ std::uint32_t rundot_kinetix_tracks_wheel_stride() {
723
+ return rundot::kinetix::physics::tracks::kTrackWheelStride;
724
+ }
725
+
726
+ std::uint32_t rundot_kinetix_tracks_track_config_stride() {
727
+ return rundot::kinetix::physics::tracks::kTrackConfigStride;
728
+ }
729
+
730
+ std::uint32_t rundot_kinetix_tracks_vehicle_config_stride() {
731
+ return rundot::kinetix::physics::tracks::kTrackedVehicleConfigStride;
732
+ }
733
+
734
+ std::uint32_t rundot_kinetix_tracks_input_stride() {
735
+ return rundot::kinetix::physics::tracks::kTrackedVehicleInputStride;
736
+ }
737
+
738
+ std::uint32_t rundot_kinetix_tracks_state_stride() {
739
+ return rundot::kinetix::physics::tracks::kTrackedVehicleStateStride;
740
+ }
741
+
742
+ std::uint32_t rundot_kinetix_tracks_output_stride() {
743
+ return rundot::kinetix::physics::tracks::kTrackedVehicleOutputStride;
744
+ }
745
+
746
+ std::uint32_t rundot_kinetix_tracks_motorcycle_config_stride() {
747
+ return rundot::kinetix::physics::tracks::kMotorcycleConfigStride;
748
+ }
749
+
750
+ std::uint32_t rundot_kinetix_tracks_motorcycle_input_stride() {
751
+ return rundot::kinetix::physics::tracks::kMotorcycleInputStride;
752
+ }
753
+
754
+ std::uint32_t rundot_kinetix_tracks_motorcycle_state_stride() {
755
+ return rundot::kinetix::physics::tracks::kMotorcycleStateStride;
756
+ }
757
+
758
+ std::uint32_t rundot_kinetix_tracks_motorcycle_output_stride() {
759
+ return rundot::kinetix::physics::tracks::kMotorcycleOutputStride;
760
+ }
761
+
762
+ std::int32_t rundot_kinetix_tracks_step_vehicle(
763
+ const double* configData,
764
+ const double* stateData,
765
+ const double* inputData,
766
+ double groundElevation,
767
+ double dt,
768
+ double* outputData,
769
+ double* nextStateData) {
770
+ if (configData == nullptr || stateData == nullptr || inputData == nullptr ||
771
+ outputData == nullptr || nextStateData == nullptr) {
772
+ return -1;
773
+ }
774
+
775
+ rundot::kinetix::physics::tracks::TrackedVehicleConfig config{};
776
+ config.mass = configData[0];
777
+ config.length = configData[1];
778
+ config.width = configData[2];
779
+ config.height = configData[3];
780
+ config.inertia = {configData[4], configData[5], configData[6]};
781
+ config.trackGauge = configData[7];
782
+ config.sprocketRadius = configData[8];
783
+ config.maxSpeed = configData[9];
784
+ config.roadWheelCount = static_cast<std::uint32_t>(configData[10]);
785
+
786
+ // Decode left track config
787
+ const double* leftTrackData = configData + 11;
788
+ config.tracks[0].side = 0;
789
+ config.tracks[0].offset = {leftTrackData[0], leftTrackData[1], leftTrackData[2]};
790
+ config.tracks[0].trackLength = leftTrackData[3];
791
+ config.tracks[0].trackWidth = leftTrackData[4];
792
+ config.tracks[0].wheelCount = static_cast<std::uint32_t>(leftTrackData[5]);
793
+ config.tracks[0].muLongitudinal = leftTrackData[6];
794
+ config.tracks[0].muLateral = leftTrackData[7];
795
+ config.tracks[0].trackTension = leftTrackData[8];
796
+ config.tracks[0].sprocketRadius = leftTrackData[9];
797
+ config.tracks[0].sprocketInertia = leftTrackData[10];
798
+ config.tracks[0].maxDriveTorque = leftTrackData[11];
799
+ config.tracks[0].maxBrakeTorque = leftTrackData[12];
800
+
801
+ // Decode right track config
802
+ const double* rightTrackData = leftTrackData + rundot::kinetix::physics::tracks::kTrackConfigStride;
803
+ config.tracks[1].side = 1;
804
+ config.tracks[1].offset = {rightTrackData[0], rightTrackData[1], rightTrackData[2]};
805
+ config.tracks[1].trackLength = rightTrackData[3];
806
+ config.tracks[1].trackWidth = rightTrackData[4];
807
+ config.tracks[1].wheelCount = static_cast<std::uint32_t>(rightTrackData[5]);
808
+ config.tracks[1].muLongitudinal = rightTrackData[6];
809
+ config.tracks[1].muLateral = rightTrackData[7];
810
+ config.tracks[1].trackTension = rightTrackData[8];
811
+ config.tracks[1].sprocketRadius = rightTrackData[9];
812
+ config.tracks[1].sprocketInertia = rightTrackData[10];
813
+ config.tracks[1].maxDriveTorque = rightTrackData[11];
814
+ config.tracks[1].maxBrakeTorque = rightTrackData[12];
815
+
816
+ // Configure default road wheel anchors if needed
817
+ for (std::uint32_t t = 0; t < 2; ++t) {
818
+ const std::uint32_t wCount = std::min(config.tracks[t].wheelCount, rundot::kinetix::physics::tracks::kMaxRoadWheelsPerTrack);
819
+ const double segLen = config.tracks[t].trackLength / std::max(1.0, static_cast<double>(wCount - 1));
820
+ const double startX = -config.tracks[t].trackLength * 0.5;
821
+ for (std::uint32_t w = 0; w < wCount; ++w) {
822
+ config.tracks[t].wheels[w].localAnchor = {startX + w * segLen, -0.4, 0.0};
823
+ config.tracks[t].wheels[w].suspensionDir = {0.0, -1.0, 0.0};
824
+ config.tracks[t].wheels[w].suspensionRestLength = 0.4;
825
+ config.tracks[t].wheels[w].suspensionMaxTravel = 0.25;
826
+ config.tracks[t].wheels[w].stiffness = 45000.0;
827
+ config.tracks[t].wheels[w].damping = 3500.0;
828
+ config.tracks[t].wheels[w].wheelRadius = 0.25;
829
+ }
830
+ }
831
+
832
+ // Decode State
833
+ rundot::kinetix::physics::tracks::TrackedVehicleState state{};
834
+ state.position = {stateData[0], stateData[1], stateData[2]};
835
+ state.orientation = {stateData[3], stateData[4], stateData[5], stateData[6]};
836
+ state.linearVel = {stateData[7], stateData[8], stateData[9]};
837
+ state.angularVel = {stateData[10], stateData[11], stateData[12]};
838
+ state.leftTrackLinearSpeed = stateData[13];
839
+ state.rightTrackLinearSpeed = stateData[14];
840
+ state.leftSprocketAngularVel = stateData[15];
841
+ state.rightSprocketAngularVel = stateData[16];
842
+ state.leftTrackSlip = stateData[17];
843
+ state.rightTrackSlip = stateData[18];
844
+
845
+ // Decode Input
846
+ rundot::kinetix::physics::tracks::TrackedVehicleInput input{};
847
+ input.forward = inputData[0];
848
+ input.steer = inputData[1];
849
+ input.leftRatio = inputData[2];
850
+ input.rightRatio = inputData[3];
851
+ input.brake = inputData[4];
852
+ input.leftBrake = inputData[5];
853
+ input.rightBrake = inputData[6];
854
+
855
+ // Step
856
+ const auto output = rundot::kinetix::physics::tracks::stepTrackedVehicle(
857
+ config, state, input, groundElevation, dt);
858
+
859
+ // Encode Output
860
+ outputData[0] = output.netForce.x;
861
+ outputData[1] = output.netForce.y;
862
+ outputData[2] = output.netForce.z;
863
+ outputData[3] = output.netTorque.x;
864
+ outputData[4] = output.netTorque.y;
865
+ outputData[5] = output.netTorque.z;
866
+ outputData[6] = output.leftTrack.tractionForce;
867
+ outputData[7] = output.rightTrack.tractionForce;
868
+ outputData[8] = output.leftTrack.lateralScrubForce;
869
+ outputData[9] = output.rightTrack.lateralScrubForce;
870
+ outputData[10] = output.leftTrack.scrubResistanceTorque + output.rightTrack.scrubResistanceTorque;
871
+ outputData[11] = output.turnRadius;
872
+ outputData[12] = output.yawRate;
873
+ outputData[13] = output.leftTrack.linearSpeed;
874
+ outputData[14] = output.rightTrack.linearSpeed;
875
+ outputData[15] = static_cast<double>(output.totalGroundedWheels);
876
+
877
+ // Encode next state
878
+ nextStateData[0] = state.position.x;
879
+ nextStateData[1] = state.position.y;
880
+ nextStateData[2] = state.position.z;
881
+ nextStateData[3] = state.orientation.x;
882
+ nextStateData[4] = state.orientation.y;
883
+ nextStateData[5] = state.orientation.z;
884
+ nextStateData[6] = state.orientation.w;
885
+ nextStateData[7] = state.linearVel.x;
886
+ nextStateData[8] = state.linearVel.y;
887
+ nextStateData[9] = state.linearVel.z;
888
+ nextStateData[10] = state.angularVel.x;
889
+ nextStateData[11] = state.angularVel.y;
890
+ nextStateData[12] = state.angularVel.z;
891
+ nextStateData[13] = state.leftTrackLinearSpeed;
892
+ nextStateData[14] = state.rightTrackLinearSpeed;
893
+ nextStateData[15] = state.leftSprocketAngularVel;
894
+ nextStateData[16] = state.rightSprocketAngularVel;
895
+ nextStateData[17] = state.leftTrackSlip;
896
+ nextStateData[18] = state.rightTrackSlip;
897
+
898
+ return 0;
899
+ }
900
+
901
+ std::int32_t rundot_kinetix_tracks_evaluate_friction(
902
+ double trackSpeed,
903
+ double contactVx,
904
+ double contactVz,
905
+ double normalForce,
906
+ double muLon,
907
+ double muLat,
908
+ double contactXOffset,
909
+ double* outForces) {
910
+ if (outForces == nullptr) return -1;
911
+ double fLon = 0.0;
912
+ double fLat = 0.0;
913
+ double scrubT = 0.0;
914
+
915
+ rundot::kinetix::physics::tracks::computeTrackFriction(
916
+ trackSpeed,
917
+ contactVx,
918
+ contactVz,
919
+ normalForce,
920
+ muLon,
921
+ muLat,
922
+ contactXOffset,
923
+ fLon,
924
+ fLat,
925
+ scrubT);
926
+
927
+ outForces[0] = fLon;
928
+ outForces[1] = fLat;
929
+ outForces[2] = scrubT;
930
+ return 0;
931
+ }
932
+
933
+ std::int32_t rundot_kinetix_tracks_differential_steering(
934
+ double forwardInput,
935
+ double leftRatio,
936
+ double rightRatio,
937
+ double trackGauge,
938
+ double maxSpeed,
939
+ double currentForwardSpeed,
940
+ double* outSpeedsAndTorque) {
941
+ if (outSpeedsAndTorque == nullptr) return -1;
942
+
943
+ rundot::kinetix::physics::tracks::TrackedVehicleConfig config{};
944
+ config.trackGauge = trackGauge;
945
+ config.maxSpeed = maxSpeed;
946
+ config.mass = 12000.0;
947
+
948
+ rundot::kinetix::physics::tracks::TrackedVehicleInput input{};
949
+ input.forward = forwardInput;
950
+ input.leftRatio = leftRatio;
951
+ input.rightRatio = rightRatio;
952
+
953
+ double targetLeft = 0.0;
954
+ double targetRight = 0.0;
955
+ double diffTorque = 0.0;
956
+
957
+ rundot::kinetix::physics::tracks::evaluateDifferentialSteering(
958
+ config,
959
+ input,
960
+ currentForwardSpeed,
961
+ targetLeft,
962
+ targetRight,
963
+ diffTorque);
964
+
965
+ outSpeedsAndTorque[0] = targetLeft;
966
+ outSpeedsAndTorque[1] = targetRight;
967
+ outSpeedsAndTorque[2] = diffTorque;
968
+ return 0;
969
+ }
970
+
971
+ std::int32_t rundot_kinetix_motorcycle_step(
972
+ const double* configData,
973
+ const double* stateData,
974
+ const double* inputData,
975
+ double groundElevation,
976
+ double dt,
977
+ double* outputData,
978
+ double* nextStateData) {
979
+ if (configData == nullptr || stateData == nullptr || inputData == nullptr ||
980
+ outputData == nullptr || nextStateData == nullptr) {
981
+ return -1;
982
+ }
983
+
984
+ rundot::kinetix::physics::tracks::MotorcycleConfig config{};
985
+ config.mass = configData[0];
986
+ config.inertia = {configData[1], configData[2], configData[3]};
987
+ config.wheelbase = configData[4];
988
+ config.rakeAngle = configData[5];
989
+ config.trail = configData[6];
990
+ config.comHeight = configData[7];
991
+ config.frontRadius = configData[8];
992
+ config.rearRadius = configData[9];
993
+ config.frontInertia = configData[10];
994
+ config.rearInertia = configData[11];
995
+ config.frontCorneringStiffness = configData[12];
996
+ config.rearCorneringStiffness = configData[13];
997
+ config.frontCamberStiffness = configData[14];
998
+ config.rearCamberStiffness = configData[15];
999
+ config.enableLeanController = (configData[16] > 0.5);
1000
+ config.enableLeanSteeringLimit = (configData[17] > 0.5);
1001
+ config.leanSpringConstant = configData[18];
1002
+ config.leanSpringDamping = configData[19];
1003
+ config.leanSpringIntegrationCoefficient = configData[20];
1004
+ config.leanSpringIntegrationCoefficientDecay = configData[21];
1005
+ config.leanSmoothingFactor = configData[22];
1006
+ config.maxLeanAngle = configData[23];
1007
+ config.maxBalanceTorque = configData[24];
1008
+ config.maxSteerTorque = configData[25];
1009
+ config.maxSteerAngle = configData[26];
1010
+ config.counterSteerFactor = configData[27];
1011
+ config.gyroscopicFactor = configData[28];
1012
+ config.maxDriveTorque = configData[29];
1013
+ config.maxBrakeTorque = configData[30];
1014
+
1015
+ rundot::kinetix::physics::tracks::MotorcycleState state{};
1016
+ state.position = {stateData[0], stateData[1], stateData[2]};
1017
+ state.orientation = {stateData[3], stateData[4], stateData[5], stateData[6]};
1018
+ state.linearVel = {stateData[7], stateData[8], stateData[9]};
1019
+ state.angularVel = {stateData[10], stateData[11], stateData[12]};
1020
+ state.steerAngle = stateData[13];
1021
+ state.steerAngularVel = stateData[14];
1022
+ state.frontWheelOmega = stateData[15];
1023
+ state.rearWheelOmega = stateData[16];
1024
+ state.currentLean = stateData[17];
1025
+ state.currentLeanRate = stateData[18];
1026
+ state.targetLean = stateData[19];
1027
+ state.leanErrorIntegral = stateData[20];
1028
+ state.smoothedLean = stateData[21];
1029
+ state.frontNormalForce = stateData[22];
1030
+ state.rearNormalForce = stateData[23];
1031
+ state.frontGrounded = (stateData[24] > 0.5);
1032
+ state.rearGrounded = (stateData[25] > 0.5);
1033
+
1034
+ rundot::kinetix::physics::tracks::MotorcycleInput input{};
1035
+ input.throttle = inputData[0];
1036
+ input.steer = inputData[1];
1037
+ input.frontBrake = inputData[2];
1038
+ input.rearBrake = inputData[3];
1039
+ input.targetLeanOverride = inputData[4];
1040
+
1041
+ const auto output = rundot::kinetix::physics::tracks::stepMotorcycle(
1042
+ config, state, input, groundElevation, dt);
1043
+
1044
+ // Encode Output
1045
+ outputData[0] = output.balanceTorque;
1046
+ outputData[1] = output.steerTorque;
1047
+ outputData[2] = output.updatedSteerAngle;
1048
+ outputData[3] = output.gyroscopicTorque;
1049
+ outputData[4] = output.counterSteerTorque;
1050
+ outputData[5] = output.frontLongitudinalForce;
1051
+ outputData[6] = output.frontLateralForce;
1052
+ outputData[7] = output.frontCamberForce;
1053
+ outputData[8] = output.rearLongitudinalForce;
1054
+ outputData[9] = output.rearLateralForce;
1055
+ outputData[10] = output.rearCamberForce;
1056
+ outputData[11] = output.netForce.x;
1057
+ outputData[12] = output.netForce.y;
1058
+ outputData[13] = output.netForce.z;
1059
+ outputData[14] = output.netTorque.x;
1060
+ outputData[15] = output.netTorque.y;
1061
+ outputData[16] = output.netTorque.z;
1062
+ outputData[17] = output.targetLean;
1063
+ outputData[18] = output.leanStable ? 1.0 : 0.0;
1064
+
1065
+ // Encode next state
1066
+ nextStateData[0] = state.position.x;
1067
+ nextStateData[1] = state.position.y;
1068
+ nextStateData[2] = state.position.z;
1069
+ nextStateData[3] = state.orientation.x;
1070
+ nextStateData[4] = state.orientation.y;
1071
+ nextStateData[5] = state.orientation.z;
1072
+ nextStateData[6] = state.orientation.w;
1073
+ nextStateData[7] = state.linearVel.x;
1074
+ nextStateData[8] = state.linearVel.y;
1075
+ nextStateData[9] = state.linearVel.z;
1076
+ nextStateData[10] = state.angularVel.x;
1077
+ nextStateData[11] = state.angularVel.y;
1078
+ nextStateData[12] = state.angularVel.z;
1079
+ nextStateData[13] = state.steerAngle;
1080
+ nextStateData[14] = state.steerAngularVel;
1081
+ nextStateData[15] = state.frontWheelOmega;
1082
+ nextStateData[16] = state.rearWheelOmega;
1083
+ nextStateData[17] = state.currentLean;
1084
+ nextStateData[18] = state.currentLeanRate;
1085
+ nextStateData[19] = state.targetLean;
1086
+ nextStateData[20] = state.leanErrorIntegral;
1087
+ nextStateData[21] = state.smoothedLean;
1088
+ nextStateData[22] = state.frontNormalForce;
1089
+ nextStateData[23] = state.rearNormalForce;
1090
+ nextStateData[24] = state.frontGrounded ? 1.0 : 0.0;
1091
+ nextStateData[25] = state.rearGrounded ? 1.0 : 0.0;
1092
+
1093
+ return 0;
1094
+ }
1095
+
1096
+ std::int32_t rundot_kinetix_motorcycle_lean_stability(
1097
+ const double* configData,
1098
+ double currentLean,
1099
+ double currentLeanRate,
1100
+ double targetLean,
1101
+ double leanIntegral,
1102
+ double dt,
1103
+ double* outTorque,
1104
+ double* outNewIntegral) {
1105
+ if (configData == nullptr || outTorque == nullptr || outNewIntegral == nullptr) {
1106
+ return -1;
1107
+ }
1108
+
1109
+ rundot::kinetix::physics::tracks::MotorcycleConfig config{};
1110
+ config.enableLeanController = (configData[16] > 0.5);
1111
+ config.leanSpringConstant = configData[18];
1112
+ config.leanSpringDamping = configData[19];
1113
+ config.leanSpringIntegrationCoefficient = configData[20];
1114
+ config.leanSpringIntegrationCoefficientDecay = configData[21];
1115
+ config.maxBalanceTorque = configData[24];
1116
+
1117
+ double integral = leanIntegral;
1118
+ const double torque = rundot::kinetix::physics::tracks::computeBoundedBalanceTorque(
1119
+ config,
1120
+ currentLean,
1121
+ currentLeanRate,
1122
+ targetLean,
1123
+ integral,
1124
+ dt);
1125
+
1126
+ *outTorque = torque;
1127
+ *outNewIntegral = integral;
1128
+ return 0;
1129
+ }
1130
+
1131
+ std::int32_t rundot_kinetix_motorcycle_countersteer(
1132
+ double forwardSpeed,
1133
+ double steerCommand,
1134
+ double currentSteerAngle,
1135
+ double currentLean,
1136
+ double targetLean,
1137
+ double wheelbase,
1138
+ double rakeAngle,
1139
+ double counterSteerFactor,
1140
+ double maxSteerTorque,
1141
+ double* outCounterSteerTorque,
1142
+ double* outTargetSteerAngle) {
1143
+ if (outCounterSteerTorque == nullptr || outTargetSteerAngle == nullptr) {
1144
+ return -1;
1145
+ }
1146
+
1147
+ rundot::kinetix::physics::tracks::MotorcycleConfig config{};
1148
+ config.wheelbase = wheelbase;
1149
+ config.rakeAngle = rakeAngle;
1150
+ config.counterSteerFactor = counterSteerFactor;
1151
+ config.maxSteerTorque = maxSteerTorque;
1152
+ config.maxSteerAngle = 0.65;
1153
+
1154
+ const double torque = rundot::kinetix::physics::tracks::computeCounterSteerTorque(
1155
+ config,
1156
+ forwardSpeed,
1157
+ steerCommand,
1158
+ currentSteerAngle,
1159
+ currentLean,
1160
+ targetLean);
1161
+
1162
+ *outCounterSteerTorque = torque;
1163
+ // Compute kinematic steady-state steer angle corresponding to target lean
1164
+ // tan(phi) = (v^2 / (g * R)) = (v^2 / (g * (L / tan(delta))))
1165
+ // => tan(delta) = (g * L * tan(phi)) / v^2
1166
+ double targetDelta = 0.0;
1167
+ const double spdSq = forwardSpeed * forwardSpeed;
1168
+ if (spdSq > 1.0) {
1169
+ const double tanDelta = (9.80665 * wheelbase * std::tan(targetLean)) / spdSq;
1170
+ targetDelta = std::atan(tanDelta);
1171
+ } else {
1172
+ targetDelta = steerCommand * config.maxSteerAngle;
1173
+ }
1174
+
1175
+ *outTargetSteerAngle = rundot::kinetix::physics::tracks::quantizeValue(targetDelta);
1176
+ return 0;
1177
+ }
1178
+
1179
+ } // extern "C"