@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.
- package/README.md +133 -0
- package/core/deterministic-math.d.ts +75 -1
- package/core/native/instance_ledger.hpp +1095 -0
- package/core/native/legacy_checkpoint_ids.hpp +107 -0
- package/core/native/physics/abi.cpp +39 -211
- package/core/native/physics/abi.hpp +2 -6
- package/core/native/physics/articulation.cpp +1434 -0
- package/core/native/physics/articulation.hpp +279 -0
- package/core/native/physics/buoyancy.cpp +1588 -0
- package/core/native/physics/buoyancy.hpp +254 -0
- package/core/native/physics/capsule_triangle.cpp +168 -0
- package/core/native/physics/capsule_triangle.hpp +40 -0
- package/core/native/physics/capsule_triangle_native.inc +291 -0
- package/core/native/physics/certified_sum.hpp +218 -0
- package/core/native/physics/character.cpp +638 -0
- package/core/native/physics/character.hpp +169 -0
- package/core/native/physics/checkpoint_schema.hpp +379 -0
- package/core/native/physics/cloth.cpp +1401 -0
- package/core/native/physics/cloth.hpp +357 -0
- package/core/native/physics/constraint_rows.cpp +363 -2
- package/core/native/physics/constraint_rows.hpp +72 -1
- package/core/native/physics/deformable_query.cpp +1630 -0
- package/core/native/physics/deformable_query.hpp +578 -0
- package/core/native/physics/distance.cpp +1166 -0
- package/core/native/physics/distance.hpp +151 -0
- package/core/native/physics/exact/exact-applied.cpp +101 -0
- package/core/native/physics/exact/exact-applied.hpp +112 -0
- package/core/native/physics/exact/exact-dyadic.cpp +691 -0
- package/core/native/physics/exact/exact-dyadic.hpp +58 -0
- package/core/native/physics/exact/exact-operation.hpp +51 -0
- package/core/native/physics/exact/word-core.hpp +262 -0
- package/core/native/physics/extended_abi.cpp +1242 -0
- package/core/native/physics/extended_abi.hpp +355 -0
- package/core/native/physics/extended_abi_characters.cpp +261 -0
- package/core/native/physics/extended_abi_characters.hpp +434 -0
- package/core/native/physics/extended_abi_core_features.cpp +713 -0
- package/core/native/physics/extended_abi_core_features.hpp +226 -0
- package/core/native/physics/extended_abi_deformables.cpp +448 -0
- package/core/native/physics/extended_abi_deformables.hpp +867 -0
- package/core/native/physics/extended_abi_vehicles.cpp +411 -0
- package/core/native/physics/extended_abi_vehicles.hpp +222 -0
- package/core/native/physics/fracture.cpp +1981 -0
- package/core/native/physics/fracture.hpp +460 -0
- package/core/native/physics/gear.cpp +563 -0
- package/core/native/physics/gear.hpp +158 -0
- package/core/native/physics/geometry_memory.hpp +476 -0
- package/core/native/physics/geometry_store.cpp +547 -0
- package/core/native/physics/geometry_store.hpp +134 -0
- package/core/native/physics/lag_compensation.cpp +1626 -0
- package/core/native/physics/lag_compensation.hpp +405 -0
- package/core/native/physics/mass_properties.cpp +449 -0
- package/core/native/physics/mass_properties.hpp +23 -0
- package/core/native/physics/material_pose.hpp +988 -0
- package/core/native/physics/particles.cpp +1065 -0
- package/core/native/physics/particles.hpp +261 -0
- package/core/native/physics/path_joint.cpp +781 -0
- package/core/native/physics/path_joint.hpp +154 -0
- package/core/native/physics/physics.cpp +13043 -1065
- package/core/native/physics/physics.hpp +184 -8
- package/core/native/physics/planar_deformable.cpp +2433 -0
- package/core/native/physics/planar_deformable.hpp +889 -0
- package/core/native/physics/platformer_2d.cpp +1353 -0
- package/core/native/physics/platformer_2d.hpp +340 -0
- package/core/native/physics/powered_ragdoll.cpp +1100 -0
- package/core/native/physics/powered_ragdoll.hpp +279 -0
- package/core/native/physics/powertrain.cpp +586 -0
- package/core/native/physics/powertrain.hpp +180 -0
- package/core/native/physics/pulley.cpp +311 -0
- package/core/native/physics/pulley.hpp +121 -0
- package/core/native/physics/ragdoll.cpp +586 -0
- package/core/native/physics/ragdoll.hpp +221 -0
- package/core/native/physics/rope.cpp +692 -0
- package/core/native/physics/rope.hpp +215 -0
- package/core/native/physics/rounded_convex.cpp +1229 -0
- package/core/native/physics/rounded_convex.hpp +287 -0
- package/core/native/physics/sdf_geometry.cpp +1821 -0
- package/core/native/physics/sdf_geometry.hpp +474 -0
- package/core/native/physics/soft_body.cpp +1449 -0
- package/core/native/physics/soft_body.hpp +328 -0
- package/core/native/physics/soft_collision.cpp +1862 -0
- package/core/native/physics/soft_collision.hpp +561 -0
- package/core/native/physics/surface_velocity.cpp +366 -0
- package/core/native/physics/surface_velocity.hpp +243 -0
- package/core/native/physics/topology.cpp +357 -39
- package/core/native/physics/topology.hpp +97 -6
- package/core/native/physics/tracks.cpp +1179 -0
- package/core/native/physics/tracks.hpp +404 -0
- package/core/native/physics/vehicle.cpp +629 -0
- package/core/native/physics/vehicle.hpp +191 -0
- package/core/native/physics/voxel_cooking.cpp +1929 -0
- package/core/native/physics/voxel_cooking.hpp +395 -0
- package/core/native/physics/world.cpp +5290 -854
- package/core/native/physics/world.hpp +796 -24
- package/core/native/physics/world_abi.cpp +3840 -306
- package/core/native/physics/world_abi.hpp +530 -10
- package/core/native/physics/world_digest_visitor.hpp +277 -0
- package/core/native/physics/world_strong_digest.hpp +557 -0
- package/core/native/physics2d/ccd.cpp +1302 -0
- package/core/native/physics2d/ccd.hpp +141 -0
- package/core/native/physics2d/checkpoint_memory.hpp +349 -0
- package/core/native/physics2d/clock_primitives.hpp +138 -0
- package/core/native/physics2d/commands.cpp +174 -28
- package/core/native/physics2d/commands.hpp +3 -1
- package/core/native/physics2d/compound.cpp +581 -0
- package/core/native/physics2d/compound.hpp +248 -0
- package/core/native/physics2d/contour.cpp +325 -0
- package/core/native/physics2d/contour.hpp +143 -0
- package/core/native/physics2d/frame_scope.hpp +124 -0
- package/core/native/physics2d/joint_phases.hpp +93 -0
- package/core/native/physics2d/joints.cpp +587 -150
- package/core/native/physics2d/joints.hpp +5 -0
- package/core/native/physics2d/memory_private.hpp +125 -0
- package/core/native/physics2d/owned_abi.cpp +1354 -879
- package/core/native/physics2d/owned_abi.hpp +190 -52
- package/core/native/physics2d/phase_test_hooks.hpp +17 -0
- package/core/native/physics2d/physics2d.cpp +2073 -120
- package/core/native/physics2d/physics2d.hpp +154 -2
- package/core/native/physics2d/queries.cpp +1055 -79
- package/core/native/physics2d/queries.hpp +71 -5
- package/core/native/physics2d/world.cpp +1313 -353
- package/core/native/physics2d/world.hpp +124 -13
- package/core/native/sha256/sha256_compress_unsigned.hpp +68 -0
- package/core/native/sha256/sha256_fips_constants.hpp +24 -0
- package/core/native/sha256/sha256_length.hpp +56 -0
- package/core/native/sha256/sha256_memo.hpp +48 -0
- package/core/native/sha256/sha256_sink.hpp +234 -0
- package/core/physics2d-owned.d.ts +466 -126
- package/core/runtime.d.ts +192 -19
- package/core/scripts/build-native.mjs +34 -0
- package/core/scripts/build-wasm-physics.mjs +272 -23
- package/core/scripts/extended-physics-profile.mjs +634 -0
- package/core/scripts/generate-physics2d-vectors.mjs +59 -0
- package/core/src/kinetix-deterministic-math.d.ts +2 -0
- package/core/src/kinetix-deterministic-math.mjs +513 -27
- package/core/src/kinetix-owned-runtime-adapter.mjs +788 -0
- package/core/src/kinetix-runtime-adapter-utils.mjs +54 -6
- package/core/src/kinetix-runtime-contract.mjs +55 -9
- package/core/src/kinetix-runtime-identity.mjs +12 -1
- package/core/src/kinetix-session-config.mjs +7 -2
- package/core/src/kinetix-state-report.mjs +36 -0
- package/core/src/physics/abi-constants.mjs +5 -4
- package/core/src/physics/articulation.d.mts +194 -0
- package/core/src/physics/articulation.mjs +1141 -0
- package/core/src/physics/buoyancy.d.mts +143 -0
- package/core/src/physics/buoyancy.mjs +268 -0
- package/core/src/physics/cloth.d.mts +314 -0
- package/core/src/physics/cloth.mjs +1156 -0
- package/core/src/physics/deformable-query.d.mts +217 -0
- package/core/src/physics/deformable-query.mjs +1412 -0
- package/core/src/physics/distance.d.mts +160 -0
- package/core/src/physics/distance.mjs +175 -0
- package/core/src/physics/extended-wasm-instance.d.mts +5 -0
- package/core/src/physics/extended-wasm-instance.mjs +48 -0
- package/core/src/physics/feature-api.d.mts +74 -0
- package/core/src/physics/feature-api.mjs +98 -0
- package/core/src/physics/fracture.d.mts +133 -0
- package/core/src/physics/fracture.mjs +1324 -0
- package/core/src/physics/gear.d.mts +585 -0
- package/core/src/physics/gear.mjs +955 -0
- package/core/src/physics/lag-compensation.d.mts +101 -0
- package/core/src/physics/lag-compensation.mjs +1100 -0
- package/core/src/physics/mass-properties.d.mts +114 -0
- package/core/src/physics/mass-properties.mjs +137 -468
- package/core/src/physics/particles.d.mts +177 -0
- package/core/src/physics/particles.mjs +758 -0
- package/core/src/physics/path-joint.d.mts +171 -0
- package/core/src/physics/path-joint.mjs +1231 -0
- package/core/src/physics/physics-extended-wasm-module.generated.mjs +13 -0
- package/core/src/physics/physics-profile-wasm-module.generated.mjs +48 -0
- package/core/src/physics/physics-wasm-module.generated.mjs +34 -5
- package/core/src/physics/powered-ragdoll.d.mts +188 -0
- package/core/src/physics/powered-ragdoll.mjs +1043 -0
- package/core/src/physics/powertrain.d.mts +260 -0
- package/core/src/physics/powertrain.mjs +621 -0
- package/core/src/physics/pulley.d.mts +98 -0
- package/core/src/physics/pulley.mjs +686 -0
- package/core/src/physics/ragdoll.d.mts +289 -0
- package/core/src/physics/ragdoll.mjs +1323 -0
- package/core/src/physics/rope.d.mts +241 -0
- package/core/src/physics/rope.mjs +319 -0
- package/core/src/physics/rounded-convex.d.mts +328 -0
- package/core/src/physics/rounded-convex.mjs +1404 -0
- package/core/src/physics/sdf-geometry.d.mts +117 -0
- package/core/src/physics/sdf-geometry.mjs +1204 -0
- package/core/src/physics/soft-body.d.mts +144 -0
- package/core/src/physics/soft-body.mjs +1285 -0
- package/core/src/physics/soft-collision.d.mts +651 -0
- package/core/src/physics/soft-collision.mjs +1883 -0
- package/core/src/physics/surface-velocity.d.mts +156 -0
- package/core/src/physics/surface-velocity.mjs +722 -0
- package/core/src/physics/tracks.d.mts +411 -0
- package/core/src/physics/tracks.mjs +1074 -0
- package/core/src/physics/vehicle.d.mts +3 -0
- package/core/src/physics/vehicle.mjs +172 -0
- package/core/src/physics/voxel-cooking.d.mts +219 -0
- package/core/src/physics/voxel-cooking.mjs +1499 -0
- package/core/src/physics/voxel-swept-quad.mjs +110 -0
- package/core/src/physics/wasm-instance.mjs +160 -23
- package/core/src/physics/world.d.mts +62 -0
- package/core/src/physics/world.mjs +1947 -152
- package/core/src/physics2d/body-state.mjs +218 -15
- package/core/src/physics2d/collision.d.mts +52 -0
- package/core/src/physics2d/collision.mjs +234 -0
- package/core/src/physics2d/compound.mjs +454 -0
- package/core/src/physics2d/contour-state.mjs +771 -0
- package/core/src/physics2d/feature-api.d.mts +211 -0
- package/core/src/physics2d/feature-api.mjs +3 -0
- package/core/src/physics2d/fluid-state.mjs +145 -0
- package/core/src/physics2d/joint-state.mjs +1 -1
- package/core/src/physics2d/owned-queries.mjs +3 -1
- package/core/src/physics2d/owned-transport.mjs +268 -122
- package/core/src/physics2d/owned-world.mjs +353 -230
- package/core/src/physics2d/particle-fluid.d.mts +152 -0
- package/core/src/physics2d/particle-fluid.mjs +233 -0
- package/core/src/physics2d/physics2d-wasm-module.generated.mjs +27 -10
- package/core/src/physics2d/query-codec.mjs +61 -13
- package/core/src/physics2d/submerged-geometry.d.mts +51 -0
- package/core/src/physics2d/submerged-geometry.mjs +186 -0
- package/core/src/runtime.mjs +23 -0
- package/core/src/shared/bytes.d.mts +2 -0
- package/core/src/shared/bytes.mjs +33 -0
- package/core/src/shared/sha256.d.mts +3 -0
- package/core/src/shared/sha256.mjs +72 -22
- package/core/units.d.ts +50 -0
- package/core/world.d.ts +253 -5
- package/dist/authority-room.d.ts +9 -0
- package/dist/authority-room.js +45 -16
- package/dist/browser.d.ts +6 -5
- package/dist/browser.js +4 -3
- package/dist/cadence.d.ts +7 -0
- package/dist/cadence.js +19 -0
- package/dist/canonical.d.ts +1 -0
- package/dist/canonical.js +3 -0
- package/dist/checksum-policy.d.ts +10 -0
- package/dist/checksum-policy.js +14 -0
- package/dist/collider-cooking-hull.d.ts +18 -0
- package/dist/collider-cooking-hull.js +248 -0
- package/dist/collider-cooking.d.ts +140 -0
- package/dist/collider-cooking.js +794 -0
- package/dist/compat/v5.d.ts +121 -0
- package/dist/compat/v5.js +479 -0
- package/dist/creator.d.ts +7 -6
- package/dist/creator.js +4 -3
- package/dist/game-byte-section.d.ts +36 -0
- package/dist/game-byte-section.js +350 -0
- package/dist/index.d.ts +13 -6
- package/dist/index.js +9 -4
- package/dist/instant-replay.d.ts +27 -2
- package/dist/instant-replay.js +91 -16
- package/dist/lag-compensation3d.js +11 -10
- package/dist/match-log.d.ts +21 -4
- package/dist/match-log.js +92 -9
- package/dist/math.d.ts +2 -0
- package/dist/math.js +1 -0
- package/dist/modules/character-3d.d.ts +16 -1
- package/dist/modules/character-3d.js +42 -5
- package/dist/modules/combat.js +6 -5
- package/dist/movement3d-revision1.d.ts +2 -0
- package/dist/movement3d-revision1.js +973 -0
- package/dist/movement3d.d.ts +53 -3
- package/dist/movement3d.js +917 -247
- package/dist/networked-client.d.ts +15 -0
- package/dist/networked-client.js +378 -105
- package/dist/node.d.ts +3 -2
- package/dist/node.js +2 -1
- package/dist/physics/2d.d.ts +38 -4
- package/dist/physics/2d.js +57 -36
- package/dist/physics/3d.d.ts +755 -111
- package/dist/physics/3d.js +2355 -158
- package/dist/physics/edit-result-graph.d.ts +36 -0
- package/dist/physics/edit-result-graph.js +141 -0
- package/dist/physics/game-codec3d.d.ts +41 -0
- package/dist/physics/game-codec3d.js +544 -0
- package/dist/physics/installed-checkpoint3d.d.ts +13 -0
- package/dist/physics/installed-checkpoint3d.js +120 -0
- package/dist/physics/installed-runtime3d.d.ts +51 -0
- package/dist/physics/installed-runtime3d.js +372 -0
- package/dist/physics/installed-state-commitment3d.d.ts +2 -0
- package/dist/physics/installed-state-commitment3d.js +24 -0
- package/dist/physics/units.d.ts +38 -0
- package/dist/physics/units.js +56 -0
- package/dist/physics/vehicle-state3d.d.ts +93 -0
- package/dist/physics/vehicle-state3d.js +1191 -0
- package/dist/physics/voxel-body-state.d.ts +61 -0
- package/dist/physics/voxel-body-state.js +410 -0
- package/dist/r3f-render-adapter.d.ts +59 -1
- package/dist/r3f-render-adapter.js +137 -2
- package/dist/replay.d.ts +13 -0
- package/dist/replay.js +95 -70
- package/dist/run-room-transport.js +5 -1
- package/dist/runner.d.ts +17 -2
- package/dist/runner.js +472 -84
- package/dist/runtime-cleanup.d.ts +54 -0
- package/dist/runtime-cleanup.js +266 -0
- package/dist/runtime-lease.d.ts +11 -1
- package/dist/runtime-lease.js +23 -10
- package/dist/runtime-room-metadata.d.ts +2 -8
- package/dist/runtime-room-metadata.js +11 -14
- package/dist/runtime-session.d.ts +8 -0
- package/dist/runtime-session.js +222 -58
- package/dist/schema-codegen.js +3 -0
- package/dist/schema-dsl-binary.d.ts +10 -4
- package/dist/schema-dsl-binary.js +375 -76
- package/dist/scratch-pool.d.ts +32 -0
- package/dist/scratch-pool.js +60 -0
- package/dist/sdk-session.d.ts +1 -0
- package/dist/sdk-session.js +4 -0
- package/dist/secret-config.js +2 -60
- package/dist/secret-crypto.js +70 -36
- package/dist/session-snapshot.d.ts +15 -4
- package/dist/session-snapshot.js +35 -33
- package/dist/session-wire.js +14 -22
- package/dist/sparse-set.d.ts +1 -0
- package/dist/sparse-set.js +22 -0
- package/dist/spatial/spatial-grid-3d.d.ts +41 -0
- package/dist/spatial/spatial-grid-3d.js +115 -0
- package/dist/synctest.d.ts +9 -0
- package/dist/synctest.js +106 -17
- package/dist/testing-network.d.ts +2 -0
- package/dist/testing-network.js +122 -18
- package/dist/testing.d.ts +27 -6
- package/dist/testing.js +297 -56
- package/dist/tools/index.d.ts +3 -2
- package/dist/tools/index.js +3 -2
- package/dist/tools/module-certification.d.ts +6 -0
- package/dist/tools/module-certification.js +26 -2
- package/package.json +18 -5
- package/core/scripts/run-box3d-benchmark.mjs +0 -350
- package/core/src/physics/index.mjs +0 -3169
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
#include "gear.hpp"
|
|
2
|
+
#include "../deterministic_math.hpp"
|
|
3
|
+
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <limits>
|
|
7
|
+
|
|
8
|
+
namespace rundot::kinetix::physics {
|
|
9
|
+
|
|
10
|
+
GearVec3 addGearVec3(const GearVec3& a, const GearVec3& b) {
|
|
11
|
+
return {a.x + b.x, a.y + b.y, a.z + b.z};
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
GearVec3 subGearVec3(const GearVec3& a, const GearVec3& b) {
|
|
15
|
+
return {a.x - b.x, a.y - b.y, a.z - b.z};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
GearVec3 scaleGearVec3(const GearVec3& v, double s) {
|
|
19
|
+
return {v.x * s, v.y * s, v.z * s};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
double dotGearVec3(const GearVec3& a, const GearVec3& b) {
|
|
23
|
+
return a.x * b.x + a.y * b.y + a.z * b.z;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
GearVec3 crossGearVec3(const GearVec3& a, const GearVec3& b) {
|
|
27
|
+
return {
|
|
28
|
+
a.y * b.z - a.z * b.y,
|
|
29
|
+
a.z * b.x - a.x * b.z,
|
|
30
|
+
a.x * b.y - a.y * b.x,
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
double lengthSquaredGearVec3(const GearVec3& v) {
|
|
35
|
+
return dotGearVec3(v, v);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
double lengthGearVec3(const GearVec3& v) {
|
|
39
|
+
const double sq = lengthSquaredGearVec3(v);
|
|
40
|
+
return sq > 0.0 ? std::sqrt(sq) : 0.0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
GearVec3 normalizeGearVec3(const GearVec3& v) {
|
|
44
|
+
const double len = lengthGearVec3(v);
|
|
45
|
+
if (len <= 1e-15) return {1.0, 0.0, 0.0};
|
|
46
|
+
const double inv = 1.0 / len;
|
|
47
|
+
return {v.x * inv, v.y * inv, v.z * inv};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
GearVec3 rotateByQuat(const GearQuat& q, const GearVec3& v) {
|
|
51
|
+
const GearVec3 u{q.x, q.y, q.z};
|
|
52
|
+
const GearVec3 uv = crossGearVec3(u, v);
|
|
53
|
+
const GearVec3 uuv = crossGearVec3(u, uv);
|
|
54
|
+
return addGearVec3(v, addGearVec3(scaleGearVec3(uv, 2.0 * q.w), scaleGearVec3(uuv, 2.0)));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
GearQuat multiplyQuat(const GearQuat& a, const GearQuat& b) {
|
|
58
|
+
return {
|
|
59
|
+
a.w * b.x + a.x * b.w + a.y * b.z - a.z * b.y,
|
|
60
|
+
a.w * b.y - a.x * b.z + a.y * b.w + a.z * b.x,
|
|
61
|
+
a.w * b.z + a.x * b.y - a.y * b.x + a.z * b.w,
|
|
62
|
+
a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z,
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
GearQuat conjugateQuat(const GearQuat& q) {
|
|
67
|
+
return {-q.x, -q.y, -q.z, q.w};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
GearQuat normalizeQuat(const GearQuat& q) {
|
|
71
|
+
const double normSq = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
|
|
72
|
+
if (!(normSq > 1e-24)) return {0.0, 0.0, 0.0, 1.0};
|
|
73
|
+
const double invNorm = 1.0 / std::sqrt(normSq);
|
|
74
|
+
return {q.x * invNorm, q.y * invNorm, q.z * invNorm, q.w * invNorm};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
double wrapJointAngle(double angle) {
|
|
78
|
+
constexpr double pi = 3.1415926535897932384626433832795;
|
|
79
|
+
constexpr double twoPi = 2.0 * pi;
|
|
80
|
+
while (angle > pi) angle -= twoPi;
|
|
81
|
+
while (angle < -pi) angle += twoPi;
|
|
82
|
+
return angle;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
namespace {
|
|
86
|
+
|
|
87
|
+
GearQuat bodyOrientation(const Body& body) {
|
|
88
|
+
return normalizeQuat({body.orientationX, body.orientationY, body.orientationZ, body.orientationW});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
GearVec3 bodyPosition(const Body& body) {
|
|
92
|
+
return {body.x, body.y, body.z};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
GearVec3 bodyCenterOfMassWorld(const Body& body) {
|
|
96
|
+
const GearQuat q = bodyOrientation(body);
|
|
97
|
+
const GearVec3 localCom{body.centerOfMassX, body.centerOfMassY, body.centerOfMassZ};
|
|
98
|
+
return addGearVec3(bodyPosition(body), rotateByQuat(q, localCom));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
GearVec3 bodyAnchorWorld(const Body& body, const GearVec3& localAnchor) {
|
|
102
|
+
const GearQuat q = bodyOrientation(body);
|
|
103
|
+
return addGearVec3(bodyPosition(body), rotateByQuat(q, localAnchor));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
double computeBodyAngle(
|
|
107
|
+
const Body& body,
|
|
108
|
+
const GearQuat& refOrientation,
|
|
109
|
+
const GearVec3& localAxis) {
|
|
110
|
+
const GearQuat qCurrent = bodyOrientation(body);
|
|
111
|
+
GearQuat qRel = multiplyQuat(conjugateQuat(refOrientation), qCurrent);
|
|
112
|
+
if (qRel.w < 0.0) {
|
|
113
|
+
qRel.x = -qRel.x;
|
|
114
|
+
qRel.y = -qRel.y;
|
|
115
|
+
qRel.z = -qRel.z;
|
|
116
|
+
qRel.w = -qRel.w;
|
|
117
|
+
}
|
|
118
|
+
const double proj = dotGearVec3({qRel.x, qRel.y, qRel.z}, localAxis);
|
|
119
|
+
return wrapJointAngle(2.0 * rundot::detmath::det_atan2(proj, qRel.w));
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
} // namespace
|
|
123
|
+
|
|
124
|
+
GearStatus evaluateGearKinematics(
|
|
125
|
+
const GearConstraintConfig& config,
|
|
126
|
+
GearConstraintState& state,
|
|
127
|
+
const Body& bodyA,
|
|
128
|
+
const Body& bodyB) {
|
|
129
|
+
if (config.ratio == 0.0 || !std::isfinite(config.ratio)) {
|
|
130
|
+
return GearStatus::InvalidRatio;
|
|
131
|
+
}
|
|
132
|
+
if (config.bodyA == config.bodyB) {
|
|
133
|
+
return GearStatus::InvalidBodies;
|
|
134
|
+
}
|
|
135
|
+
if (!config.enabled || state.broken) {
|
|
136
|
+
return state.broken ? GearStatus::Broken : GearStatus::Success;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const GearQuat qA = bodyOrientation(bodyA);
|
|
140
|
+
const GearQuat qB = bodyOrientation(bodyB);
|
|
141
|
+
const GearVec3 uA = rotateByQuat(qA, normalizeGearVec3(config.axisA));
|
|
142
|
+
const GearVec3 uB = rotateByQuat(qB, normalizeGearVec3(config.axisB));
|
|
143
|
+
|
|
144
|
+
double coordA = 0.0;
|
|
145
|
+
double speedA = 0.0;
|
|
146
|
+
double coordB = 0.0;
|
|
147
|
+
double speedB = 0.0;
|
|
148
|
+
|
|
149
|
+
if (config.mode == GearCouplingMode::AngularAngular) {
|
|
150
|
+
const double rawAngleA = computeBodyAngle(bodyA, config.referenceOrientationA, normalizeGearVec3(config.axisA));
|
|
151
|
+
const double rawAngleB = computeBodyAngle(bodyB, config.referenceOrientationB, normalizeGearVec3(config.axisB));
|
|
152
|
+
|
|
153
|
+
if (!state.initialized) {
|
|
154
|
+
state.previousWrappedAngleA = rawAngleA;
|
|
155
|
+
state.unwrappedCoordinateA = rawAngleA;
|
|
156
|
+
state.previousWrappedAngleB = rawAngleB;
|
|
157
|
+
state.unwrappedCoordinateB = rawAngleB;
|
|
158
|
+
state.initialized = true;
|
|
159
|
+
} else {
|
|
160
|
+
const double deltaA = wrapJointAngle(rawAngleA - state.previousWrappedAngleA);
|
|
161
|
+
state.unwrappedCoordinateA += deltaA;
|
|
162
|
+
state.previousWrappedAngleA = rawAngleA;
|
|
163
|
+
|
|
164
|
+
const double deltaB = wrapJointAngle(rawAngleB - state.previousWrappedAngleB);
|
|
165
|
+
state.unwrappedCoordinateB += deltaB;
|
|
166
|
+
state.previousWrappedAngleB = rawAngleB;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
coordA = state.unwrappedCoordinateA;
|
|
170
|
+
coordB = state.unwrappedCoordinateB;
|
|
171
|
+
speedA = dotGearVec3({bodyA.angularVelX, bodyA.angularVelY, bodyA.angularVelZ}, uA);
|
|
172
|
+
speedB = dotGearVec3({bodyB.angularVelX, bodyB.angularVelY, bodyB.angularVelZ}, uB);
|
|
173
|
+
} else if (config.mode == GearCouplingMode::AngularLinear) {
|
|
174
|
+
// Body A is pinion (angular), Body B is rack (linear)
|
|
175
|
+
const double rawAngleA = computeBodyAngle(bodyA, config.referenceOrientationA, normalizeGearVec3(config.axisA));
|
|
176
|
+
if (!state.initialized) {
|
|
177
|
+
state.previousWrappedAngleA = rawAngleA;
|
|
178
|
+
state.unwrappedCoordinateA = rawAngleA;
|
|
179
|
+
state.previousWrappedAngleB = 0.0;
|
|
180
|
+
state.unwrappedCoordinateB = 0.0;
|
|
181
|
+
state.initialized = true;
|
|
182
|
+
} else {
|
|
183
|
+
const double deltaA = wrapJointAngle(rawAngleA - state.previousWrappedAngleA);
|
|
184
|
+
state.unwrappedCoordinateA += deltaA;
|
|
185
|
+
state.previousWrappedAngleA = rawAngleA;
|
|
186
|
+
}
|
|
187
|
+
coordA = state.unwrappedCoordinateA;
|
|
188
|
+
speedA = dotGearVec3({bodyA.angularVelX, bodyA.angularVelY, bodyA.angularVelZ}, uA);
|
|
189
|
+
|
|
190
|
+
const GearVec3 wB = bodyAnchorWorld(bodyB, config.anchorB);
|
|
191
|
+
const GearVec3 comB = bodyCenterOfMassWorld(bodyB);
|
|
192
|
+
const GearVec3 rB = subGearVec3(wB, comB);
|
|
193
|
+
const GearVec3 vB_lin{bodyB.vx, bodyB.vy, bodyB.vz};
|
|
194
|
+
const GearVec3 wB_ang{bodyB.angularVelX, bodyB.angularVelY, bodyB.angularVelZ};
|
|
195
|
+
const GearVec3 vAnchorB = addGearVec3(vB_lin, crossGearVec3(wB_ang, rB));
|
|
196
|
+
|
|
197
|
+
coordB = dotGearVec3(subGearVec3(wB, config.referencePositionB), uB);
|
|
198
|
+
state.unwrappedCoordinateB = coordB;
|
|
199
|
+
speedB = dotGearVec3(vAnchorB, uB);
|
|
200
|
+
} else if (config.mode == GearCouplingMode::LinearAngular) {
|
|
201
|
+
// Body A is rack (linear), Body B is pinion (angular)
|
|
202
|
+
const double rawAngleB = computeBodyAngle(bodyB, config.referenceOrientationB, normalizeGearVec3(config.axisB));
|
|
203
|
+
if (!state.initialized) {
|
|
204
|
+
state.previousWrappedAngleA = 0.0;
|
|
205
|
+
state.unwrappedCoordinateA = 0.0;
|
|
206
|
+
state.previousWrappedAngleB = rawAngleB;
|
|
207
|
+
state.unwrappedCoordinateB = rawAngleB;
|
|
208
|
+
state.initialized = true;
|
|
209
|
+
} else {
|
|
210
|
+
const double deltaB = wrapJointAngle(rawAngleB - state.previousWrappedAngleB);
|
|
211
|
+
state.unwrappedCoordinateB += deltaB;
|
|
212
|
+
state.previousWrappedAngleB = rawAngleB;
|
|
213
|
+
}
|
|
214
|
+
coordB = state.unwrappedCoordinateB;
|
|
215
|
+
speedB = dotGearVec3({bodyB.angularVelX, bodyB.angularVelY, bodyB.angularVelZ}, uB);
|
|
216
|
+
|
|
217
|
+
const GearVec3 wA = bodyAnchorWorld(bodyA, config.anchorA);
|
|
218
|
+
const GearVec3 comA = bodyCenterOfMassWorld(bodyA);
|
|
219
|
+
const GearVec3 rA = subGearVec3(wA, comA);
|
|
220
|
+
const GearVec3 vA_lin{bodyA.vx, bodyA.vy, bodyA.vz};
|
|
221
|
+
const GearVec3 wA_ang{bodyA.angularVelX, bodyA.angularVelY, bodyA.angularVelZ};
|
|
222
|
+
const GearVec3 vAnchorA = addGearVec3(vA_lin, crossGearVec3(wA_ang, rA));
|
|
223
|
+
|
|
224
|
+
coordA = dotGearVec3(subGearVec3(wA, config.referencePositionB), uA);
|
|
225
|
+
state.unwrappedCoordinateA = coordA;
|
|
226
|
+
speedA = dotGearVec3(vAnchorA, uA);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
state.coordinate = coordB + config.ratio * coordA;
|
|
230
|
+
state.speed = speedB + config.ratio * speedA;
|
|
231
|
+
|
|
232
|
+
return GearStatus::Success;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
std::uint32_t buildGearConstraintRows(
|
|
236
|
+
const GearConstraintConfig& config,
|
|
237
|
+
GearConstraintState& state,
|
|
238
|
+
const Body& bodyA,
|
|
239
|
+
const Body& bodyB,
|
|
240
|
+
double dt,
|
|
241
|
+
std::span<ConstraintRow6> outputRows) {
|
|
242
|
+
if (outputRows.empty() || dt <= 0.0) return 0;
|
|
243
|
+
|
|
244
|
+
const GearStatus status = evaluateGearKinematics(config, state, bodyA, bodyB);
|
|
245
|
+
if (status != GearStatus::Success) return 0;
|
|
246
|
+
|
|
247
|
+
const double ratioScale = std::max(1.0, std::abs(config.ratio));
|
|
248
|
+
const double coeffA = config.ratio / ratioScale;
|
|
249
|
+
const double coeffB = 1.0 / ratioScale;
|
|
250
|
+
|
|
251
|
+
const GearQuat qA = bodyOrientation(bodyA);
|
|
252
|
+
const GearQuat qB = bodyOrientation(bodyB);
|
|
253
|
+
const GearVec3 uA = rotateByQuat(qA, normalizeGearVec3(config.axisA));
|
|
254
|
+
const GearVec3 uB = rotateByQuat(qB, normalizeGearVec3(config.axisB));
|
|
255
|
+
|
|
256
|
+
ConstraintRow6 row{};
|
|
257
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
258
|
+
row.bodyA = config.bodyA;
|
|
259
|
+
row.bodyB = config.bodyB;
|
|
260
|
+
|
|
261
|
+
if (config.mode == GearCouplingMode::AngularAngular) {
|
|
262
|
+
row.jacobianA = {0.0, 0.0, 0.0, coeffA * uA.x, coeffA * uA.y, coeffA * uA.z};
|
|
263
|
+
row.jacobianB = {0.0, 0.0, 0.0, coeffB * uB.x, coeffB * uB.y, coeffB * uB.z};
|
|
264
|
+
} else if (config.mode == GearCouplingMode::AngularLinear) {
|
|
265
|
+
const GearVec3 wB = bodyAnchorWorld(bodyB, config.anchorB);
|
|
266
|
+
const GearVec3 comB = bodyCenterOfMassWorld(bodyB);
|
|
267
|
+
const GearVec3 rB = subGearVec3(wB, comB);
|
|
268
|
+
const GearVec3 tauB = crossGearVec3(rB, uB);
|
|
269
|
+
|
|
270
|
+
row.jacobianA = {0.0, 0.0, 0.0, coeffA * uA.x, coeffA * uA.y, coeffA * uA.z};
|
|
271
|
+
row.jacobianB = {coeffB * uB.x, coeffB * uB.y, coeffB * uB.z, coeffB * tauB.x, coeffB * tauB.y, coeffB * tauB.z};
|
|
272
|
+
} else if (config.mode == GearCouplingMode::LinearAngular) {
|
|
273
|
+
const GearVec3 wA = bodyAnchorWorld(bodyA, config.anchorA);
|
|
274
|
+
const GearVec3 comA = bodyCenterOfMassWorld(bodyA);
|
|
275
|
+
const GearVec3 rA = subGearVec3(wA, comA);
|
|
276
|
+
const GearVec3 tauA = crossGearVec3(rA, uA);
|
|
277
|
+
|
|
278
|
+
row.jacobianA = {coeffA * uA.x, coeffA * uA.y, coeffA * uA.z, coeffA * tauA.x, coeffA * tauA.y, coeffA * tauA.z};
|
|
279
|
+
row.jacobianB = {0.0, 0.0, 0.0, coeffB * uB.x, coeffB * uB.y, coeffB * uB.z};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
const double C = state.coordinate;
|
|
283
|
+
if (config.limitMode == GearLimitMode::Free) {
|
|
284
|
+
const double error = (C - config.phase) / ratioScale;
|
|
285
|
+
row.scalar = constraintSoftness(error, dt, config.frequencyHz, config.dampingRatio, config.maxForce);
|
|
286
|
+
if (config.maxForce > 0.0) {
|
|
287
|
+
row.scalar.lower = -config.maxForce * dt;
|
|
288
|
+
row.scalar.upper = config.maxForce * dt;
|
|
289
|
+
} else {
|
|
290
|
+
row.scalar.lower = -std::numeric_limits<double>::infinity();
|
|
291
|
+
row.scalar.upper = std::numeric_limits<double>::infinity();
|
|
292
|
+
}
|
|
293
|
+
} else if (config.limitMode == GearLimitMode::Locked) {
|
|
294
|
+
const double error = (C - config.phase) / ratioScale;
|
|
295
|
+
row.scalar = constraintSoftness(error, dt, config.frequencyHz, config.dampingRatio, config.maxForce);
|
|
296
|
+
if (config.maxForce > 0.0) {
|
|
297
|
+
row.scalar.lower = -config.maxForce * dt;
|
|
298
|
+
row.scalar.upper = config.maxForce * dt;
|
|
299
|
+
} else {
|
|
300
|
+
row.scalar.lower = -std::numeric_limits<double>::infinity();
|
|
301
|
+
row.scalar.upper = std::numeric_limits<double>::infinity();
|
|
302
|
+
}
|
|
303
|
+
} else if (config.limitMode == GearLimitMode::Limited) {
|
|
304
|
+
if (config.lowerLimit == config.upperLimit) {
|
|
305
|
+
const double error = (C - config.lowerLimit) / ratioScale;
|
|
306
|
+
row.scalar = constraintSoftness(error, dt, config.frequencyHz, config.dampingRatio, config.maxForce);
|
|
307
|
+
if (config.maxForce > 0.0) {
|
|
308
|
+
row.scalar.lower = -config.maxForce * dt;
|
|
309
|
+
row.scalar.upper = config.maxForce * dt;
|
|
310
|
+
} else {
|
|
311
|
+
row.scalar.lower = -std::numeric_limits<double>::infinity();
|
|
312
|
+
row.scalar.upper = std::numeric_limits<double>::infinity();
|
|
313
|
+
}
|
|
314
|
+
} else if (C < config.lowerLimit) {
|
|
315
|
+
const double error = (C - config.lowerLimit) / ratioScale;
|
|
316
|
+
row.scalar = constraintSoftness(error, dt, config.frequencyHz, config.dampingRatio, config.maxForce);
|
|
317
|
+
row.scalar.lower = 0.0;
|
|
318
|
+
row.scalar.upper = config.maxForce > 0.0 ? config.maxForce * dt : std::numeric_limits<double>::infinity();
|
|
319
|
+
} else if (C > config.upperLimit) {
|
|
320
|
+
const double error = (C - config.upperLimit) / ratioScale;
|
|
321
|
+
row.scalar = constraintSoftness(error, dt, config.frequencyHz, config.dampingRatio, config.maxForce);
|
|
322
|
+
row.scalar.lower = config.maxForce > 0.0 ? -config.maxForce * dt : -std::numeric_limits<double>::infinity();
|
|
323
|
+
row.scalar.upper = 0.0;
|
|
324
|
+
} else {
|
|
325
|
+
// Within limits: no unilateral resistance
|
|
326
|
+
state.accumulatedImpulse = 0.0;
|
|
327
|
+
return 0;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
row.scalar.accumulatedImpulse = state.accumulatedImpulse;
|
|
332
|
+
outputRows[0] = row;
|
|
333
|
+
return 1;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
std::uint32_t solveGearConstraints(
|
|
337
|
+
std::span<const GearConstraintConfig> configs,
|
|
338
|
+
std::span<GearConstraintState> states,
|
|
339
|
+
std::span<Body> bodies,
|
|
340
|
+
std::span<const ConstraintBodyMobility> mobility,
|
|
341
|
+
double dt,
|
|
342
|
+
std::uint32_t iterations) {
|
|
343
|
+
if (configs.empty() || dt <= 0.0 || iterations == 0) return 0;
|
|
344
|
+
|
|
345
|
+
constexpr std::uint32_t kMaxRows = 64;
|
|
346
|
+
std::array<ConstraintRow6, kMaxRows> rows{};
|
|
347
|
+
std::array<std::uint32_t, kMaxRows> configIndices{};
|
|
348
|
+
std::uint32_t rowCount = 0;
|
|
349
|
+
|
|
350
|
+
for (std::uint32_t i = 0; i < configs.size() && rowCount < kMaxRows; ++i) {
|
|
351
|
+
const auto& config = configs[i];
|
|
352
|
+
auto& state = states[i];
|
|
353
|
+
if (config.bodyA >= bodies.size() || config.bodyB >= bodies.size()) continue;
|
|
354
|
+
|
|
355
|
+
ConstraintRow6 row{};
|
|
356
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
357
|
+
const std::uint32_t emitted = buildGearConstraintRows(
|
|
358
|
+
config, state, bodies[config.bodyA], bodies[config.bodyB], dt,
|
|
359
|
+
std::span<ConstraintRow6>(&row, 1));
|
|
360
|
+
if (emitted == 1 && prepareConstraintRow(row, mobility)) {
|
|
361
|
+
if (row.scalar.accumulatedImpulse != 0.0) {
|
|
362
|
+
applyConstraintRowImpulse(row, bodies, mobility, row.scalar.accumulatedImpulse);
|
|
363
|
+
}
|
|
364
|
+
rows[rowCount] = row;
|
|
365
|
+
configIndices[rowCount] = i;
|
|
366
|
+
rowCount++;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
for (std::uint32_t iter = 0; iter < iterations; ++iter) {
|
|
371
|
+
for (std::uint32_t r = 0; r < rowCount; ++r) {
|
|
372
|
+
solveConstraintRow(rows[r], bodies, mobility);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
for (std::uint32_t r = 0; r < rowCount; ++r) {
|
|
377
|
+
const std::uint32_t idx = configIndices[r];
|
|
378
|
+
const auto& config = configs[idx];
|
|
379
|
+
auto& state = states[idx];
|
|
380
|
+
const double impulse = rows[r].scalar.accumulatedImpulse;
|
|
381
|
+
state.accumulatedImpulse = impulse;
|
|
382
|
+
|
|
383
|
+
const double ratioScale = std::max(1.0, std::abs(config.ratio));
|
|
384
|
+
const double absImpulse = std::abs(impulse);
|
|
385
|
+
state.reactionForce = (1.0 / ratioScale) * absImpulse / dt;
|
|
386
|
+
state.reactionTorque = (std::abs(config.ratio) / ratioScale) * absImpulse / dt;
|
|
387
|
+
|
|
388
|
+
if (config.breakForce > 0.0 && state.reactionForce > config.breakForce) {
|
|
389
|
+
state.broken = true;
|
|
390
|
+
state.accumulatedImpulse = 0.0;
|
|
391
|
+
}
|
|
392
|
+
if (config.breakTorque > 0.0 && state.reactionTorque > config.breakTorque) {
|
|
393
|
+
state.broken = true;
|
|
394
|
+
state.accumulatedImpulse = 0.0;
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return rowCount;
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
void editGearRatio(GearConstraintConfig& config, GearConstraintState& state, double newRatio) {
|
|
402
|
+
if (newRatio != 0.0 && std::isfinite(newRatio)) {
|
|
403
|
+
config.ratio = newRatio;
|
|
404
|
+
state.accumulatedImpulse = 0.0;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
void editGearPhase(GearConstraintConfig& config, double newPhase) {
|
|
409
|
+
if (std::isfinite(newPhase)) {
|
|
410
|
+
config.phase = newPhase;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
void editGearLimits(GearConstraintConfig& config, GearLimitMode mode, double lower, double upper) {
|
|
415
|
+
config.limitMode = mode;
|
|
416
|
+
config.lowerLimit = lower;
|
|
417
|
+
config.upperLimit = upper;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
void resetGearWarmState(GearConstraintState& state) {
|
|
421
|
+
state.accumulatedImpulse = 0.0;
|
|
422
|
+
state.reactionForce = 0.0;
|
|
423
|
+
state.reactionTorque = 0.0;
|
|
424
|
+
state.initialized = false;
|
|
425
|
+
state.broken = false;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
} // namespace rundot::kinetix::physics
|
|
429
|
+
|
|
430
|
+
extern "C" {
|
|
431
|
+
|
|
432
|
+
std::uint32_t rundot_kinetix_physics_gear_abi_version() {
|
|
433
|
+
return rundot::kinetix::physics::kGearAbiVersion;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
std::uint32_t rundot_kinetix_physics_gear_config_stride() {
|
|
437
|
+
return rundot::kinetix::physics::kGearConfigStride;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
std::uint32_t rundot_kinetix_physics_gear_state_stride() {
|
|
441
|
+
return rundot::kinetix::physics::kGearStateStride;
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
std::int32_t rundot_kinetix_physics_gear_step(
|
|
445
|
+
const double* configData,
|
|
446
|
+
std::uint32_t configCount,
|
|
447
|
+
double* stateData,
|
|
448
|
+
double* bodyData,
|
|
449
|
+
std::uint32_t bodyCount,
|
|
450
|
+
double dt,
|
|
451
|
+
std::uint32_t iterations) {
|
|
452
|
+
if (configData == nullptr || stateData == nullptr || bodyData == nullptr) return -1;
|
|
453
|
+
if (configCount == 0 || bodyCount == 0 || dt <= 0.0) return 0;
|
|
454
|
+
|
|
455
|
+
using namespace rundot::kinetix::physics;
|
|
456
|
+
|
|
457
|
+
constexpr std::uint32_t kMaxStepConfigs = 64;
|
|
458
|
+
constexpr std::uint32_t kMaxStepBodies = 64;
|
|
459
|
+
if (configCount > kMaxStepConfigs || bodyCount > kMaxStepBodies) return -2;
|
|
460
|
+
|
|
461
|
+
std::array<GearConstraintConfig, kMaxStepConfigs> configs{};
|
|
462
|
+
std::array<GearConstraintState, kMaxStepConfigs> states{};
|
|
463
|
+
std::array<Body, kMaxStepBodies> bodies{};
|
|
464
|
+
std::array<ConstraintBodyMobility, kMaxStepBodies> mobility{};
|
|
465
|
+
|
|
466
|
+
for (std::uint32_t i = 0; i < configCount; ++i) {
|
|
467
|
+
const double* c = configData + i * kGearConfigStride;
|
|
468
|
+
configs[i].bodyA = static_cast<std::uint32_t>(c[0]);
|
|
469
|
+
configs[i].bodyB = static_cast<std::uint32_t>(c[1]);
|
|
470
|
+
configs[i].mode = static_cast<GearCouplingMode>(static_cast<std::uint32_t>(c[2]));
|
|
471
|
+
configs[i].ratio = c[3];
|
|
472
|
+
configs[i].phase = c[4];
|
|
473
|
+
configs[i].axisA = {c[5], c[6], c[7]};
|
|
474
|
+
configs[i].axisB = {c[8], c[9], c[10]};
|
|
475
|
+
configs[i].anchorA = {c[11], c[12], c[13]};
|
|
476
|
+
configs[i].anchorB = {c[14], c[15], c[16]};
|
|
477
|
+
configs[i].limitMode = static_cast<GearLimitMode>(static_cast<std::uint32_t>(c[17]));
|
|
478
|
+
configs[i].lowerLimit = c[18];
|
|
479
|
+
configs[i].upperLimit = c[19];
|
|
480
|
+
configs[i].frequencyHz = c[20];
|
|
481
|
+
configs[i].dampingRatio = c[21];
|
|
482
|
+
configs[i].maxForce = c[22];
|
|
483
|
+
configs[i].breakForce = c[23];
|
|
484
|
+
configs[i].breakTorque = c[24];
|
|
485
|
+
configs[i].enabled = c[25] != 0.0;
|
|
486
|
+
|
|
487
|
+
double* s = stateData + i * kGearStateStride;
|
|
488
|
+
states[i].unwrappedCoordinateA = s[0];
|
|
489
|
+
states[i].previousWrappedAngleA = s[1];
|
|
490
|
+
states[i].unwrappedCoordinateB = s[2];
|
|
491
|
+
states[i].previousWrappedAngleB = s[3];
|
|
492
|
+
states[i].coordinate = s[4];
|
|
493
|
+
states[i].speed = s[5];
|
|
494
|
+
states[i].accumulatedImpulse = s[6];
|
|
495
|
+
states[i].reactionForce = s[7];
|
|
496
|
+
states[i].reactionTorque = s[8];
|
|
497
|
+
states[i].broken = s[9] != 0.0;
|
|
498
|
+
states[i].initialized = s[10] != 0.0;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
for (std::uint32_t b = 0; b < bodyCount; ++b) {
|
|
502
|
+
const double* src = bodyData + b * kBodyStride;
|
|
503
|
+
Body& body = bodies[b];
|
|
504
|
+
body.x = src[0];
|
|
505
|
+
body.y = src[1];
|
|
506
|
+
body.z = src[2];
|
|
507
|
+
body.vx = src[3];
|
|
508
|
+
body.vy = src[4];
|
|
509
|
+
body.vz = src[5];
|
|
510
|
+
body.orientationX = src[10];
|
|
511
|
+
body.orientationY = src[11];
|
|
512
|
+
body.orientationZ = src[12];
|
|
513
|
+
body.orientationW = src[13];
|
|
514
|
+
body.angularVelX = src[15];
|
|
515
|
+
body.angularVelY = src[16];
|
|
516
|
+
body.angularVelZ = src[17];
|
|
517
|
+
body.mass = src[14] > 0.0 ? src[14] : 1.0;
|
|
518
|
+
const std::uint32_t motionType = static_cast<std::uint32_t>(src[9]);
|
|
519
|
+
body.dynamic = (motionType & 3u) == 1u;
|
|
520
|
+
body.kinematic = (motionType & 3u) == 2u;
|
|
521
|
+
body.angularState = true;
|
|
522
|
+
body.inertiaXX = src[31] > 0.0 ? src[31] : 1.0;
|
|
523
|
+
body.inertiaYY = src[32] > 0.0 ? src[32] : 1.0;
|
|
524
|
+
body.inertiaZZ = src[33] > 0.0 ? src[33] : 1.0;
|
|
525
|
+
mobility[b] = constraintBodyMobility(body);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
solveGearConstraints(
|
|
529
|
+
std::span<const GearConstraintConfig>(configs.data(), configCount),
|
|
530
|
+
std::span<GearConstraintState>(states.data(), configCount),
|
|
531
|
+
std::span<Body>(bodies.data(), bodyCount),
|
|
532
|
+
std::span<const ConstraintBodyMobility>(mobility.data(), bodyCount),
|
|
533
|
+
dt, iterations);
|
|
534
|
+
|
|
535
|
+
for (std::uint32_t i = 0; i < configCount; ++i) {
|
|
536
|
+
double* s = stateData + i * kGearStateStride;
|
|
537
|
+
s[0] = states[i].unwrappedCoordinateA;
|
|
538
|
+
s[1] = states[i].previousWrappedAngleA;
|
|
539
|
+
s[2] = states[i].unwrappedCoordinateB;
|
|
540
|
+
s[3] = states[i].previousWrappedAngleB;
|
|
541
|
+
s[4] = states[i].coordinate;
|
|
542
|
+
s[5] = states[i].speed;
|
|
543
|
+
s[6] = states[i].accumulatedImpulse;
|
|
544
|
+
s[7] = states[i].reactionForce;
|
|
545
|
+
s[8] = states[i].reactionTorque;
|
|
546
|
+
s[9] = states[i].broken ? 1.0 : 0.0;
|
|
547
|
+
s[10] = states[i].initialized ? 1.0 : 0.0;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
for (std::uint32_t b = 0; b < bodyCount; ++b) {
|
|
551
|
+
double* dst = bodyData + b * kBodyStride;
|
|
552
|
+
dst[3] = bodies[b].vx;
|
|
553
|
+
dst[4] = bodies[b].vy;
|
|
554
|
+
dst[5] = bodies[b].vz;
|
|
555
|
+
dst[15] = bodies[b].angularVelX;
|
|
556
|
+
dst[16] = bodies[b].angularVelY;
|
|
557
|
+
dst[17] = bodies[b].angularVelZ;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
return 0;
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
} // extern "C"
|