@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,781 @@
|
|
|
1
|
+
#include "path_joint.hpp"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <array>
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <cstdint>
|
|
7
|
+
#include <limits>
|
|
8
|
+
#include <span>
|
|
9
|
+
#include <vector>
|
|
10
|
+
|
|
11
|
+
namespace rundot::kinetix::physics {
|
|
12
|
+
namespace {
|
|
13
|
+
|
|
14
|
+
inline double solverSqrt(double value) {
|
|
15
|
+
if (value <= 0.0) return 0.0;
|
|
16
|
+
return std::sqrt(value);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
inline double clampVal(double val, double minVal, double maxVal) {
|
|
20
|
+
return std::max(minVal, std::min(maxVal, val));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
inline PathVec3 addVec(const PathVec3& a, const PathVec3& b) {
|
|
24
|
+
return {a.x + b.x, a.y + b.y, a.z + b.z};
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
inline PathVec3 subVec(const PathVec3& a, const PathVec3& b) {
|
|
28
|
+
return {a.x - b.x, a.y - b.y, a.z - b.z};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
inline PathVec3 scaleVec(const PathVec3& v, double s) {
|
|
32
|
+
return {v.x * s, v.y * s, v.z * s};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
inline double dotVec(const PathVec3& a, const PathVec3& b) {
|
|
36
|
+
return a.x * b.x + a.y * b.y + a.z * b.z;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
inline PathVec3 crossVec(const PathVec3& a, const PathVec3& b) {
|
|
40
|
+
return {
|
|
41
|
+
a.y * b.z - a.z * b.y,
|
|
42
|
+
a.z * b.x - a.x * b.z,
|
|
43
|
+
a.x * b.y - a.y * b.x,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
inline double lengthSqVec(const PathVec3& v) {
|
|
48
|
+
return dotVec(v, v);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
inline double lengthVec(const PathVec3& v) {
|
|
52
|
+
return solverSqrt(lengthSqVec(v));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
inline PathVec3 normalizeVec(const PathVec3& v) {
|
|
56
|
+
const double len = lengthVec(v);
|
|
57
|
+
if (len <= 1e-15) return {0.0, 0.0, 0.0};
|
|
58
|
+
return scaleVec(v, 1.0 / len);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
std::array<double, 9> bodyRotationMatrix(const Body& body) {
|
|
62
|
+
const double x = body.orientationX;
|
|
63
|
+
const double y = body.orientationY;
|
|
64
|
+
const double z = body.orientationZ;
|
|
65
|
+
const double w = body.orientationW;
|
|
66
|
+
const double norm = x * x + y * y + z * z + w * w;
|
|
67
|
+
if (norm <= 1e-24) return {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
|
|
68
|
+
const double scale = 2.0 / norm;
|
|
69
|
+
return {
|
|
70
|
+
1.0 - scale * (y * y + z * z), scale * (x * y - z * w), scale * (x * z + y * w),
|
|
71
|
+
scale * (x * y + z * w), 1.0 - scale * (x * x + z * z), scale * (y * z - x * w),
|
|
72
|
+
scale * (x * z - y * w), scale * (y * z + x * w), 1.0 - scale * (x * x + y * y),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
std::array<double, 9> quatRotationMatrix(const PathQuat& q) {
|
|
77
|
+
const double norm = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
|
|
78
|
+
if (norm <= 1e-24) return {1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
|
|
79
|
+
const double scale = 2.0 / norm;
|
|
80
|
+
return {
|
|
81
|
+
1.0 - scale * (q.y * q.y + q.z * q.z), scale * (q.x * q.y - q.z * q.w), scale * (q.x * q.z + q.y * q.w),
|
|
82
|
+
scale * (q.x * q.y + q.z * q.w), 1.0 - scale * (q.x * q.x + q.z * q.z), scale * (q.y * q.z - q.x * q.w),
|
|
83
|
+
scale * (q.x * q.z - q.y * q.w), scale * (q.y * q.z + q.x * q.w), 1.0 - scale * (q.x * q.x + q.y * q.y),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
PathVec3 rotateMatrix(const std::array<double, 9>& m, const PathVec3& v) {
|
|
88
|
+
return {
|
|
89
|
+
m[0] * v.x + m[1] * v.y + m[2] * v.z,
|
|
90
|
+
m[3] * v.x + m[4] * v.y + m[5] * v.z,
|
|
91
|
+
m[6] * v.x + m[7] * v.y + m[8] * v.z,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
PathVec3 transposeRotateMatrix(const std::array<double, 9>& m, const PathVec3& v) {
|
|
96
|
+
return {
|
|
97
|
+
m[0] * v.x + m[3] * v.y + m[6] * v.z,
|
|
98
|
+
m[1] * v.x + m[4] * v.y + m[7] * v.z,
|
|
99
|
+
m[2] * v.x + m[5] * v.y + m[8] * v.z,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
PathQuat normalizeQuat(const PathQuat& q) {
|
|
104
|
+
const double normSq = q.x * q.x + q.y * q.y + q.z * q.z + q.w * q.w;
|
|
105
|
+
if (normSq <= 1e-24) return {0.0, 0.0, 0.0, 1.0};
|
|
106
|
+
const double invNorm = 1.0 / solverSqrt(normSq);
|
|
107
|
+
return {q.x * invNorm, q.y * invNorm, q.z * invNorm, q.w * invNorm};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
PathQuat multiplyQuat(const PathQuat& q1, const PathQuat& q2) {
|
|
111
|
+
return {
|
|
112
|
+
q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y,
|
|
113
|
+
q1.w * q2.y - q1.x * q2.z + q1.y * q2.w + q1.z * q2.x,
|
|
114
|
+
q1.w * q2.z + q1.x * q2.y - q1.y * q2.x + q1.z * q2.w,
|
|
115
|
+
q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
PathQuat quatFromOrthonormalFrame(const PathVec3& tangent, const PathVec3& normal, const PathVec3& binormal) {
|
|
120
|
+
const double m00 = tangent.x, m01 = normal.x, m02 = binormal.x;
|
|
121
|
+
const double m10 = tangent.y, m11 = normal.y, m12 = binormal.y;
|
|
122
|
+
const double m20 = tangent.z, m21 = normal.z, m22 = binormal.z;
|
|
123
|
+
const double trace = m00 + m11 + m22;
|
|
124
|
+
PathQuat q{};
|
|
125
|
+
if (trace > 0.0) {
|
|
126
|
+
const double s = solverSqrt(trace + 1.0) * 2.0;
|
|
127
|
+
q.w = 0.25 * s;
|
|
128
|
+
q.x = (m21 - m12) / s;
|
|
129
|
+
q.y = (m02 - m20) / s;
|
|
130
|
+
q.z = (m10 - m01) / s;
|
|
131
|
+
} else if ((m00 > m11) && (m00 > m22)) {
|
|
132
|
+
const double s = solverSqrt(1.0 + m00 - m11 - m22) * 2.0;
|
|
133
|
+
q.w = (m21 - m12) / s;
|
|
134
|
+
q.x = 0.25 * s;
|
|
135
|
+
q.y = (m01 + m10) / s;
|
|
136
|
+
q.z = (m02 + m20) / s;
|
|
137
|
+
} else if (m11 > m22) {
|
|
138
|
+
const double s = solverSqrt(1.0 + m11 - m00 - m22) * 2.0;
|
|
139
|
+
q.w = (m02 - m20) / s;
|
|
140
|
+
q.x = (m01 + m10) / s;
|
|
141
|
+
q.y = 0.25 * s;
|
|
142
|
+
q.z = (m12 + m21) / s;
|
|
143
|
+
} else {
|
|
144
|
+
const double s = solverSqrt(1.0 + m22 - m00 - m11) * 2.0;
|
|
145
|
+
q.w = (m10 - m01) / s;
|
|
146
|
+
q.x = (m02 + m20) / s;
|
|
147
|
+
q.y = (m12 + m21) / s;
|
|
148
|
+
q.z = 0.25 * s;
|
|
149
|
+
}
|
|
150
|
+
return normalizeQuat(q);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
} // namespace
|
|
154
|
+
|
|
155
|
+
CookedPath cookPath(std::span<const PathVec3> controlPoints, const PathCookingOptions& options) {
|
|
156
|
+
CookedPath result;
|
|
157
|
+
result.curveType = options.curveType;
|
|
158
|
+
result.endPolicy = options.endPolicy;
|
|
159
|
+
result.isLoop = (options.endPolicy == PathEndPolicy::Loop);
|
|
160
|
+
|
|
161
|
+
if (controlPoints.size() < 2) {
|
|
162
|
+
return result;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const std::uint32_t samplesPerSeg = std::clamp(options.samplesPerSegment, 1u, 64u);
|
|
166
|
+
const std::size_t n = controlPoints.size();
|
|
167
|
+
const std::size_t numSegments = result.isLoop ? n : (n - 1);
|
|
168
|
+
|
|
169
|
+
std::vector<PathSample> rawSamples;
|
|
170
|
+
rawSamples.reserve(numSegments * samplesPerSeg + 1);
|
|
171
|
+
|
|
172
|
+
if (options.curveType == PathCurveType::Polyline) {
|
|
173
|
+
for (std::size_t seg = 0; seg < numSegments; ++seg) {
|
|
174
|
+
const PathVec3& p0 = controlPoints[seg];
|
|
175
|
+
const PathVec3& p1 = controlPoints[(seg + 1) % n];
|
|
176
|
+
const PathVec3 dir = subVec(p1, p0);
|
|
177
|
+
PathVec3 tangent = normalizeVec(dir);
|
|
178
|
+
if (lengthSqVec(tangent) <= 1e-12) {
|
|
179
|
+
tangent = {1.0, 0.0, 0.0};
|
|
180
|
+
}
|
|
181
|
+
const std::size_t subSteps = (seg == numSegments - 1 && !result.isLoop) ? samplesPerSeg : (samplesPerSeg - 1);
|
|
182
|
+
for (std::size_t step = 0; step <= subSteps; ++step) {
|
|
183
|
+
const double u = static_cast<double>(step) / static_cast<double>(samplesPerSeg);
|
|
184
|
+
PathSample smp;
|
|
185
|
+
smp.position = addVec(p0, scaleVec(dir, u));
|
|
186
|
+
smp.tangent = tangent;
|
|
187
|
+
rawSamples.push_back(smp);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
} else {
|
|
191
|
+
// Catmull-Rom Spline
|
|
192
|
+
for (std::size_t seg = 0; seg < numSegments; ++seg) {
|
|
193
|
+
PathVec3 p0, p1, p2, p3;
|
|
194
|
+
if (result.isLoop) {
|
|
195
|
+
p0 = controlPoints[(seg + n - 1) % n];
|
|
196
|
+
p1 = controlPoints[seg];
|
|
197
|
+
p2 = controlPoints[(seg + 1) % n];
|
|
198
|
+
p3 = controlPoints[(seg + 2) % n];
|
|
199
|
+
} else {
|
|
200
|
+
p0 = (seg == 0) ? controlPoints[0] : controlPoints[seg - 1];
|
|
201
|
+
p1 = controlPoints[seg];
|
|
202
|
+
p2 = controlPoints[seg + 1];
|
|
203
|
+
p3 = (seg + 2 < n) ? controlPoints[seg + 2] : controlPoints[n - 1];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const std::size_t subSteps = (seg == numSegments - 1 && !result.isLoop) ? samplesPerSeg : (samplesPerSeg - 1);
|
|
207
|
+
for (std::size_t step = 0; step <= subSteps; ++step) {
|
|
208
|
+
const double u = static_cast<double>(step) / static_cast<double>(samplesPerSeg);
|
|
209
|
+
const double u2 = u * u;
|
|
210
|
+
const double u3 = u2 * u;
|
|
211
|
+
|
|
212
|
+
const PathVec3 term0 = scaleVec(p1, 2.0);
|
|
213
|
+
const PathVec3 term1 = scaleVec(addVec(scaleVec(p0, -1.0), p2), u);
|
|
214
|
+
const PathVec3 term2 = scaleVec(
|
|
215
|
+
addVec(scaleVec(p0, 2.0),
|
|
216
|
+
addVec(scaleVec(p1, -5.0),
|
|
217
|
+
addVec(scaleVec(p2, 4.0), scaleVec(p3, -1.0)))),
|
|
218
|
+
u2);
|
|
219
|
+
const PathVec3 term3 = scaleVec(
|
|
220
|
+
addVec(scaleVec(p0, -1.0),
|
|
221
|
+
addVec(scaleVec(p1, 3.0),
|
|
222
|
+
addVec(scaleVec(p2, -3.0), p3))),
|
|
223
|
+
u3);
|
|
224
|
+
|
|
225
|
+
const PathVec3 pos = scaleVec(addVec(term0, addVec(term1, addVec(term2, term3))), 0.5);
|
|
226
|
+
|
|
227
|
+
const PathVec3 dTerm1 = addVec(scaleVec(p0, -1.0), p2);
|
|
228
|
+
const PathVec3 dTerm2 = scaleVec(
|
|
229
|
+
addVec(scaleVec(p0, 2.0),
|
|
230
|
+
addVec(scaleVec(p1, -5.0),
|
|
231
|
+
addVec(scaleVec(p2, 4.0), scaleVec(p3, -1.0)))),
|
|
232
|
+
2.0 * u);
|
|
233
|
+
const PathVec3 dTerm3 = scaleVec(
|
|
234
|
+
addVec(scaleVec(p0, -1.0),
|
|
235
|
+
addVec(scaleVec(p1, 3.0),
|
|
236
|
+
addVec(scaleVec(p2, -3.0), p3))),
|
|
237
|
+
3.0 * u2);
|
|
238
|
+
|
|
239
|
+
const PathVec3 vel = scaleVec(addVec(dTerm1, addVec(dTerm2, dTerm3)), 0.5);
|
|
240
|
+
|
|
241
|
+
PathVec3 tangent = normalizeVec(vel);
|
|
242
|
+
if (lengthSqVec(tangent) <= 1e-12) {
|
|
243
|
+
tangent = normalizeVec(subVec(p2, p1));
|
|
244
|
+
if (lengthSqVec(tangent) <= 1e-12) {
|
|
245
|
+
tangent = {1.0, 0.0, 0.0};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
PathSample smp;
|
|
250
|
+
smp.position = pos;
|
|
251
|
+
smp.tangent = tangent;
|
|
252
|
+
rawSamples.push_back(smp);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
if (rawSamples.empty()) {
|
|
258
|
+
return result;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Precompute segment lengths and cumulative arc length strictly in index order
|
|
262
|
+
rawSamples[0].arcLength = 0.0;
|
|
263
|
+
for (std::size_t k = 0; k + 1 < rawSamples.size(); ++k) {
|
|
264
|
+
const double segLen = lengthVec(subVec(rawSamples[k + 1].position, rawSamples[k].position));
|
|
265
|
+
rawSamples[k + 1].arcLength = rawSamples[k].arcLength + segLen;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
if (result.isLoop) {
|
|
269
|
+
const double loopClosingLen = lengthVec(subVec(rawSamples[0].position, rawSamples.back().position));
|
|
270
|
+
result.totalLength = rawSamples.back().arcLength + loopClosingLen;
|
|
271
|
+
} else {
|
|
272
|
+
result.totalLength = rawSamples.back().arcLength;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
// Compute rotation-minimising frames (RMF) along the path in index order
|
|
276
|
+
const PathVec3 t0 = rawSamples[0].tangent;
|
|
277
|
+
PathVec3 up = {0.0, 1.0, 0.0};
|
|
278
|
+
if (std::abs(dotVec(t0, up)) > 0.99) {
|
|
279
|
+
up = {1.0, 0.0, 0.0};
|
|
280
|
+
}
|
|
281
|
+
PathVec3 b0 = normalizeVec(crossVec(t0, up));
|
|
282
|
+
PathVec3 n0 = normalizeVec(crossVec(b0, t0));
|
|
283
|
+
rawSamples[0].normal = n0;
|
|
284
|
+
rawSamples[0].binormal = b0;
|
|
285
|
+
|
|
286
|
+
for (std::size_t k = 1; k < rawSamples.size(); ++k) {
|
|
287
|
+
const PathVec3& prevT = rawSamples[k - 1].tangent;
|
|
288
|
+
const PathVec3& prevN = rawSamples[k - 1].normal;
|
|
289
|
+
const PathVec3& currT = rawSamples[k].tangent;
|
|
290
|
+
|
|
291
|
+
const PathVec3 v = crossVec(prevT, currT);
|
|
292
|
+
const double c = dotVec(prevT, currT);
|
|
293
|
+
|
|
294
|
+
PathVec3 currN;
|
|
295
|
+
if (c > -0.999999) {
|
|
296
|
+
const double factor = 1.0 / (1.0 + c);
|
|
297
|
+
const PathVec3 vCrossN = crossVec(v, prevN);
|
|
298
|
+
currN = addVec(prevN, addVec(vCrossN, scaleVec(crossVec(v, vCrossN), factor)));
|
|
299
|
+
} else {
|
|
300
|
+
currN = subVec(prevN, scaleVec(currT, 2.0 * dotVec(prevN, currT)));
|
|
301
|
+
}
|
|
302
|
+
currN = normalizeVec(subVec(currN, scaleVec(currT, dotVec(currN, currT))));
|
|
303
|
+
const PathVec3 currB = normalizeVec(crossVec(currT, currN));
|
|
304
|
+
currN = normalizeVec(crossVec(currB, currT));
|
|
305
|
+
|
|
306
|
+
rawSamples[k].normal = currN;
|
|
307
|
+
rawSamples[k].binormal = currB;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
result.samples = std::move(rawSamples);
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
PathSample sampleCookedPath(const CookedPath& path, double arcLength) {
|
|
315
|
+
if (path.empty()) {
|
|
316
|
+
return PathSample{};
|
|
317
|
+
}
|
|
318
|
+
if (path.samples.size() == 1 || path.totalLength <= 1e-15) {
|
|
319
|
+
PathSample smp = path.samples[0];
|
|
320
|
+
smp.arcLength = 0.0;
|
|
321
|
+
return smp;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
double s = arcLength;
|
|
325
|
+
if (path.isLoop) {
|
|
326
|
+
s = std::fmod(s, path.totalLength);
|
|
327
|
+
if (s < 0.0) s += path.totalLength;
|
|
328
|
+
} else {
|
|
329
|
+
s = clampVal(s, 0.0, path.totalLength);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
auto it = std::upper_bound(path.samples.begin(), path.samples.end(), s,
|
|
333
|
+
[](double val, const PathSample& smp) { return val < smp.arcLength; });
|
|
334
|
+
|
|
335
|
+
const std::size_t idx = (it == path.samples.begin()) ? 0 : std::distance(path.samples.begin(), it) - 1;
|
|
336
|
+
|
|
337
|
+
if (idx >= path.samples.size() - 1) {
|
|
338
|
+
if (path.isLoop) {
|
|
339
|
+
const PathSample& sA = path.samples.back();
|
|
340
|
+
const PathSample& sB = path.samples[0];
|
|
341
|
+
const double segLen = path.totalLength - sA.arcLength;
|
|
342
|
+
const double alpha = (segLen > 1e-14) ? clampVal((s - sA.arcLength) / segLen, 0.0, 1.0) : 0.0;
|
|
343
|
+
|
|
344
|
+
PathSample out;
|
|
345
|
+
out.position = addVec(scaleVec(sA.position, 1.0 - alpha), scaleVec(sB.position, alpha));
|
|
346
|
+
out.tangent = normalizeVec(addVec(scaleVec(sA.tangent, 1.0 - alpha), scaleVec(sB.tangent, alpha)));
|
|
347
|
+
out.normal = normalizeVec(addVec(scaleVec(sA.normal, 1.0 - alpha), scaleVec(sB.normal, alpha)));
|
|
348
|
+
out.binormal = normalizeVec(crossVec(out.tangent, out.normal));
|
|
349
|
+
out.normal = normalizeVec(crossVec(out.binormal, out.tangent));
|
|
350
|
+
out.arcLength = s;
|
|
351
|
+
return out;
|
|
352
|
+
} else {
|
|
353
|
+
PathSample out = path.samples.back();
|
|
354
|
+
out.arcLength = s;
|
|
355
|
+
return out;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
const PathSample& sA = path.samples[idx];
|
|
360
|
+
const PathSample& sB = path.samples[idx + 1];
|
|
361
|
+
const double segLen = sB.arcLength - sA.arcLength;
|
|
362
|
+
const double alpha = (segLen > 1e-14) ? clampVal((s - sA.arcLength) / segLen, 0.0, 1.0) : 0.0;
|
|
363
|
+
|
|
364
|
+
PathSample out;
|
|
365
|
+
out.position = addVec(scaleVec(sA.position, 1.0 - alpha), scaleVec(sB.position, alpha));
|
|
366
|
+
out.tangent = normalizeVec(addVec(scaleVec(sA.tangent, 1.0 - alpha), scaleVec(sB.tangent, alpha)));
|
|
367
|
+
out.normal = normalizeVec(addVec(scaleVec(sA.normal, 1.0 - alpha), scaleVec(sB.normal, alpha)));
|
|
368
|
+
out.binormal = normalizeVec(crossVec(out.tangent, out.normal));
|
|
369
|
+
out.normal = normalizeVec(crossVec(out.binormal, out.tangent));
|
|
370
|
+
out.arcLength = s;
|
|
371
|
+
return out;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
double progressArcLength(const CookedPath& path, double currentArcLength, double delta) {
|
|
375
|
+
if (path.empty() || path.totalLength <= 1e-15) return 0.0;
|
|
376
|
+
double nextS = currentArcLength + delta;
|
|
377
|
+
if (path.isLoop) {
|
|
378
|
+
nextS = std::fmod(nextS, path.totalLength);
|
|
379
|
+
if (nextS < 0.0) nextS += path.totalLength;
|
|
380
|
+
} else {
|
|
381
|
+
nextS = clampVal(nextS, 0.0, path.totalLength);
|
|
382
|
+
}
|
|
383
|
+
return nextS;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
double findClosestArcLength(const CookedPath& path, const PathVec3& point, PathSample* outSample) {
|
|
387
|
+
if (path.empty()) {
|
|
388
|
+
if (outSample != nullptr) *outSample = PathSample{};
|
|
389
|
+
return 0.0;
|
|
390
|
+
}
|
|
391
|
+
if (path.samples.size() == 1 || path.totalLength <= 1e-15) {
|
|
392
|
+
if (outSample != nullptr) *outSample = path.samples[0];
|
|
393
|
+
return 0.0;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
double bestDistSq = std::numeric_limits<double>::infinity();
|
|
397
|
+
double bestS = 0.0;
|
|
398
|
+
|
|
399
|
+
const std::size_t numSegs = path.isLoop ? path.samples.size() : (path.samples.size() - 1);
|
|
400
|
+
for (std::size_t k = 0; k < numSegs; ++k) {
|
|
401
|
+
const PathSample& sA = path.samples[k];
|
|
402
|
+
const PathSample& sB = (k + 1 < path.samples.size()) ? path.samples[k + 1] : path.samples[0];
|
|
403
|
+
const double segArcLen = (k + 1 < path.samples.size()) ? (sB.arcLength - sA.arcLength) : (path.totalLength - sA.arcLength);
|
|
404
|
+
|
|
405
|
+
const PathVec3 segDir = subVec(sB.position, sA.position);
|
|
406
|
+
const double segLenSq = lengthSqVec(segDir);
|
|
407
|
+
|
|
408
|
+
double u = 0.0;
|
|
409
|
+
if (segLenSq > 1e-14) {
|
|
410
|
+
u = clampVal(dotVec(subVec(point, sA.position), segDir) / segLenSq, 0.0, 1.0);
|
|
411
|
+
}
|
|
412
|
+
const PathVec3 closestPt = addVec(sA.position, scaleVec(segDir, u));
|
|
413
|
+
const double distSq = lengthSqVec(subVec(point, closestPt));
|
|
414
|
+
|
|
415
|
+
if (distSq < bestDistSq) {
|
|
416
|
+
bestDistSq = distSq;
|
|
417
|
+
bestS = sA.arcLength + u * segArcLen;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
if (path.isLoop) {
|
|
422
|
+
bestS = std::fmod(bestS, path.totalLength);
|
|
423
|
+
if (bestS < 0.0) bestS += path.totalLength;
|
|
424
|
+
} else {
|
|
425
|
+
bestS = clampVal(bestS, 0.0, path.totalLength);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (outSample != nullptr) {
|
|
429
|
+
*outSample = sampleCookedPath(path, bestS);
|
|
430
|
+
}
|
|
431
|
+
return bestS;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
std::uint32_t buildPathConstraintRows(
|
|
435
|
+
const PathConstraint& constraint,
|
|
436
|
+
const Body& bodyA,
|
|
437
|
+
const Body& bodyB,
|
|
438
|
+
double dt,
|
|
439
|
+
std::span<ConstraintRow6> outputRows,
|
|
440
|
+
std::span<PathConstraintRowMetadata> outputMetadata) {
|
|
441
|
+
if (outputRows.empty() || dt <= 0.0 || constraint.bodyA == constraint.bodyB || constraint.path == nullptr) {
|
|
442
|
+
return 0;
|
|
443
|
+
}
|
|
444
|
+
if (constraint.path->empty() || constraint.path->totalLength <= 1e-15) {
|
|
445
|
+
return 0;
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
const auto rotMatA = bodyRotationMatrix(bodyA);
|
|
449
|
+
const auto rotMatB = bodyRotationMatrix(bodyB);
|
|
450
|
+
|
|
451
|
+
const PathVec3 comA = rotateMatrix(rotMatA, {bodyA.centerOfMassX, bodyA.centerOfMassY, bodyA.centerOfMassZ});
|
|
452
|
+
const PathVec3 cA = {bodyA.x + comA.x, bodyA.y + comA.y, bodyA.z + comA.z};
|
|
453
|
+
|
|
454
|
+
const PathVec3 comB = rotateMatrix(rotMatB, {bodyB.centerOfMassX, bodyB.centerOfMassY, bodyB.centerOfMassZ});
|
|
455
|
+
const PathVec3 cB = {bodyB.x + comB.x, bodyB.y + comB.y, bodyB.z + comB.z};
|
|
456
|
+
|
|
457
|
+
const PathVec3 rotAnchorB = rotateMatrix(rotMatB, constraint.anchorB);
|
|
458
|
+
const PathVec3 wB = {bodyB.x + rotAnchorB.x, bodyB.y + rotAnchorB.y, bodyB.z + rotAnchorB.z};
|
|
459
|
+
const PathVec3 rB = subVec(wB, cB);
|
|
460
|
+
|
|
461
|
+
const PathVec3 rotAnchorA = rotateMatrix(rotMatA, constraint.anchorA);
|
|
462
|
+
const PathVec3 wAnchorA = {bodyA.x + rotAnchorA.x, bodyA.y + rotAnchorA.y, bodyA.z + rotAnchorA.z};
|
|
463
|
+
|
|
464
|
+
const PathVec3 diffToOrigin = subVec(wB, wAnchorA);
|
|
465
|
+
const PathVec3 pLocal = transposeRotateMatrix(rotMatA, diffToOrigin);
|
|
466
|
+
|
|
467
|
+
PathSample localSample{};
|
|
468
|
+
const double s = findClosestArcLength(*constraint.path, pLocal, &localSample);
|
|
469
|
+
|
|
470
|
+
const PathVec3 rotLocalPos = rotateMatrix(rotMatA, localSample.position);
|
|
471
|
+
const PathVec3 wA = addVec(wAnchorA, rotLocalPos);
|
|
472
|
+
const PathVec3 rA = subVec(wA, cA);
|
|
473
|
+
|
|
474
|
+
const PathVec3 tWorld = normalizeVec(rotateMatrix(rotMatA, localSample.tangent));
|
|
475
|
+
const PathVec3 nWorld = normalizeVec(rotateMatrix(rotMatA, localSample.normal));
|
|
476
|
+
const PathVec3 bWorld = normalizeVec(rotateMatrix(rotMatA, localSample.binormal));
|
|
477
|
+
|
|
478
|
+
const PathVec3 d = subVec(wB, wA);
|
|
479
|
+
|
|
480
|
+
std::uint32_t count = 0;
|
|
481
|
+
const auto emitRow = [&](const ConstraintRow6& row, PathRowKind kind) {
|
|
482
|
+
if (count < outputRows.size()) {
|
|
483
|
+
outputRows[count] = row;
|
|
484
|
+
if (count < outputMetadata.size()) {
|
|
485
|
+
outputMetadata[count] = {static_cast<std::uint8_t>(count), kind};
|
|
486
|
+
}
|
|
487
|
+
count++;
|
|
488
|
+
}
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
// 1. Normal Lock Row
|
|
492
|
+
{
|
|
493
|
+
const PathVec3 u = nWorld;
|
|
494
|
+
const PathVec3 angA = crossVec(rA, u);
|
|
495
|
+
const PathVec3 angB = crossVec(rB, u);
|
|
496
|
+
const double pos = dotVec(d, u);
|
|
497
|
+
|
|
498
|
+
ConstraintRow6 row{};
|
|
499
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
500
|
+
row.bodyA = constraint.bodyA;
|
|
501
|
+
row.bodyB = constraint.bodyB;
|
|
502
|
+
row.jacobianA = {-u.x, -u.y, -u.z, -angA.x, -angA.y, -angA.z};
|
|
503
|
+
row.jacobianB = { u.x, u.y, u.z, angB.x, angB.y, angB.z};
|
|
504
|
+
row.scalar = constraintSoftness(pos, dt, 0.0, 1.0, constraint.maxNormalForce);
|
|
505
|
+
if (constraint.maxNormalForce > 0.0) {
|
|
506
|
+
row.scalar.lower = -constraint.maxNormalForce * dt;
|
|
507
|
+
row.scalar.upper = constraint.maxNormalForce * dt;
|
|
508
|
+
} else {
|
|
509
|
+
row.scalar.lower = -std::numeric_limits<double>::infinity();
|
|
510
|
+
row.scalar.upper = std::numeric_limits<double>::infinity();
|
|
511
|
+
}
|
|
512
|
+
emitRow(row, PathRowKind::NormalLock);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// 2. Binormal Lock Row
|
|
516
|
+
{
|
|
517
|
+
const PathVec3 u = bWorld;
|
|
518
|
+
const PathVec3 angA = crossVec(rA, u);
|
|
519
|
+
const PathVec3 angB = crossVec(rB, u);
|
|
520
|
+
const double pos = dotVec(d, u);
|
|
521
|
+
|
|
522
|
+
ConstraintRow6 row{};
|
|
523
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
524
|
+
row.bodyA = constraint.bodyA;
|
|
525
|
+
row.bodyB = constraint.bodyB;
|
|
526
|
+
row.jacobianA = {-u.x, -u.y, -u.z, -angA.x, -angA.y, -angA.z};
|
|
527
|
+
row.jacobianB = { u.x, u.y, u.z, angB.x, angB.y, angB.z};
|
|
528
|
+
row.scalar = constraintSoftness(pos, dt, 0.0, 1.0, constraint.maxNormalForce);
|
|
529
|
+
if (constraint.maxNormalForce > 0.0) {
|
|
530
|
+
row.scalar.lower = -constraint.maxNormalForce * dt;
|
|
531
|
+
row.scalar.upper = constraint.maxNormalForce * dt;
|
|
532
|
+
} else {
|
|
533
|
+
row.scalar.lower = -std::numeric_limits<double>::infinity();
|
|
534
|
+
row.scalar.upper = std::numeric_limits<double>::infinity();
|
|
535
|
+
}
|
|
536
|
+
emitRow(row, PathRowKind::BinormalLock);
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// 3. Tangent DOF: Limits, Motor, Spring
|
|
540
|
+
const PathVec3 u = tWorld;
|
|
541
|
+
const PathVec3 angA = crossVec(rA, u);
|
|
542
|
+
const PathVec3 angB = crossVec(rB, u);
|
|
543
|
+
const double tangentOffset = dotVec(d, u);
|
|
544
|
+
const double effectiveS = s + tangentOffset;
|
|
545
|
+
|
|
546
|
+
if (constraint.limit.enabled) {
|
|
547
|
+
const double lower = constraint.limit.lower;
|
|
548
|
+
const double upper = constraint.limit.upper;
|
|
549
|
+
const double maxF = constraint.limit.maxForce;
|
|
550
|
+
|
|
551
|
+
if (lower == upper) {
|
|
552
|
+
ConstraintRow6 row{};
|
|
553
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
554
|
+
row.bodyA = constraint.bodyA;
|
|
555
|
+
row.bodyB = constraint.bodyB;
|
|
556
|
+
row.jacobianA = {-u.x, -u.y, -u.z, -angA.x, -angA.y, -angA.z};
|
|
557
|
+
row.jacobianB = { u.x, u.y, u.z, angB.x, angB.y, angB.z};
|
|
558
|
+
const double error = effectiveS - lower;
|
|
559
|
+
row.scalar = constraintSoftness(error, dt, 0.0, 1.0, maxF);
|
|
560
|
+
if (maxF > 0.0) {
|
|
561
|
+
row.scalar.lower = -maxF * dt;
|
|
562
|
+
row.scalar.upper = maxF * dt;
|
|
563
|
+
}
|
|
564
|
+
emitRow(row, PathRowKind::TangentLimitEqual);
|
|
565
|
+
} else if (effectiveS < lower) {
|
|
566
|
+
ConstraintRow6 row{};
|
|
567
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
568
|
+
row.bodyA = constraint.bodyA;
|
|
569
|
+
row.bodyB = constraint.bodyB;
|
|
570
|
+
row.jacobianA = {-u.x, -u.y, -u.z, -angA.x, -angA.y, -angA.z};
|
|
571
|
+
row.jacobianB = { u.x, u.y, u.z, angB.x, angB.y, angB.z};
|
|
572
|
+
const double error = effectiveS - lower;
|
|
573
|
+
row.scalar = constraintSoftness(error, dt, 0.0, 1.0, maxF);
|
|
574
|
+
row.scalar.lower = 0.0;
|
|
575
|
+
row.scalar.upper = (maxF > 0.0) ? maxF * dt : std::numeric_limits<double>::infinity();
|
|
576
|
+
emitRow(row, PathRowKind::TangentLimitLower);
|
|
577
|
+
} else if (effectiveS > upper) {
|
|
578
|
+
ConstraintRow6 row{};
|
|
579
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
580
|
+
row.bodyA = constraint.bodyA;
|
|
581
|
+
row.bodyB = constraint.bodyB;
|
|
582
|
+
row.jacobianA = {-u.x, -u.y, -u.z, -angA.x, -angA.y, -angA.z};
|
|
583
|
+
row.jacobianB = { u.x, u.y, u.z, angB.x, angB.y, angB.z};
|
|
584
|
+
const double error = effectiveS - upper;
|
|
585
|
+
row.scalar = constraintSoftness(error, dt, 0.0, 1.0, maxF);
|
|
586
|
+
row.scalar.lower = (maxF > 0.0) ? -maxF * dt : -std::numeric_limits<double>::infinity();
|
|
587
|
+
row.scalar.upper = 0.0;
|
|
588
|
+
emitRow(row, PathRowKind::TangentLimitUpper);
|
|
589
|
+
}
|
|
590
|
+
} else if (!constraint.path->isLoop && constraint.endPolicy == PathEndPolicy::Clamp) {
|
|
591
|
+
if (effectiveS < 0.0) {
|
|
592
|
+
ConstraintRow6 row{};
|
|
593
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
594
|
+
row.bodyA = constraint.bodyA;
|
|
595
|
+
row.bodyB = constraint.bodyB;
|
|
596
|
+
row.jacobianA = {-u.x, -u.y, -u.z, -angA.x, -angA.y, -angA.z};
|
|
597
|
+
row.jacobianB = { u.x, u.y, u.z, angB.x, angB.y, angB.z};
|
|
598
|
+
const double error = effectiveS;
|
|
599
|
+
row.scalar = constraintSoftness(error, dt, 0.0, 1.0, 0.0);
|
|
600
|
+
row.scalar.lower = 0.0;
|
|
601
|
+
row.scalar.upper = std::numeric_limits<double>::infinity();
|
|
602
|
+
emitRow(row, PathRowKind::TangentLimitLower);
|
|
603
|
+
} else if (effectiveS > constraint.path->totalLength) {
|
|
604
|
+
ConstraintRow6 row{};
|
|
605
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
606
|
+
row.bodyA = constraint.bodyA;
|
|
607
|
+
row.bodyB = constraint.bodyB;
|
|
608
|
+
row.jacobianA = {-u.x, -u.y, -u.z, -angA.x, -angA.y, -angA.z};
|
|
609
|
+
row.jacobianB = { u.x, u.y, u.z, angB.x, angB.y, angB.z};
|
|
610
|
+
const double error = effectiveS - constraint.path->totalLength;
|
|
611
|
+
row.scalar = constraintSoftness(error, dt, 0.0, 1.0, 0.0);
|
|
612
|
+
row.scalar.lower = -std::numeric_limits<double>::infinity();
|
|
613
|
+
row.scalar.upper = 0.0;
|
|
614
|
+
emitRow(row, PathRowKind::TangentLimitUpper);
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
if (constraint.motor.enabled) {
|
|
619
|
+
ConstraintRow6 row{};
|
|
620
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
621
|
+
row.bodyA = constraint.bodyA;
|
|
622
|
+
row.bodyB = constraint.bodyB;
|
|
623
|
+
row.jacobianA = {-u.x, -u.y, -u.z, -angA.x, -angA.y, -angA.z};
|
|
624
|
+
row.jacobianB = { u.x, u.y, u.z, angB.x, angB.y, angB.z};
|
|
625
|
+
row.scalar.bias = -constraint.motor.targetVelocity;
|
|
626
|
+
row.scalar.massScale = 1.0;
|
|
627
|
+
row.scalar.impulseScale = 0.0;
|
|
628
|
+
const double maxImpulse = (constraint.motor.maxForce > 0.0) ? (constraint.motor.maxForce * dt) : std::numeric_limits<double>::infinity();
|
|
629
|
+
row.scalar.lower = -maxImpulse;
|
|
630
|
+
row.scalar.upper = maxImpulse;
|
|
631
|
+
emitRow(row, PathRowKind::TangentMotor);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
if (constraint.spring.enabled && constraint.spring.frequencyHz > 0.0) {
|
|
635
|
+
ConstraintRow6 row{};
|
|
636
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
637
|
+
row.bodyA = constraint.bodyA;
|
|
638
|
+
row.bodyB = constraint.bodyB;
|
|
639
|
+
row.jacobianA = {-u.x, -u.y, -u.z, -angA.x, -angA.y, -angA.z};
|
|
640
|
+
row.jacobianB = { u.x, u.y, u.z, angB.x, angB.y, angB.z};
|
|
641
|
+
const double error = effectiveS - constraint.spring.targetPosition;
|
|
642
|
+
row.scalar = constraintSoftness(error, dt, constraint.spring.frequencyHz, constraint.spring.dampingRatio, constraint.spring.maxForce);
|
|
643
|
+
if (constraint.spring.maxForce > 0.0) {
|
|
644
|
+
row.scalar.lower = -constraint.spring.maxForce * dt;
|
|
645
|
+
row.scalar.upper = constraint.spring.maxForce * dt;
|
|
646
|
+
}
|
|
647
|
+
emitRow(row, PathRowKind::TangentSpring);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// 4. Orientation Policy
|
|
651
|
+
if (constraint.orientationPolicy == PathOrientationPolicy::ConstrainAroundTangent) {
|
|
652
|
+
const PathQuat qB{bodyB.orientationX, bodyB.orientationY, bodyB.orientationZ, bodyB.orientationW};
|
|
653
|
+
const PathQuat qBFrame = multiplyQuat(qB, constraint.frameOrientationB);
|
|
654
|
+
const auto mB = quatRotationMatrix(qBFrame);
|
|
655
|
+
const PathVec3 forwardB = {mB[0], mB[3], mB[6]};
|
|
656
|
+
|
|
657
|
+
{
|
|
658
|
+
const PathVec3 rotAxis = nWorld;
|
|
659
|
+
ConstraintRow6 row{};
|
|
660
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
661
|
+
row.bodyA = constraint.bodyA;
|
|
662
|
+
row.bodyB = constraint.bodyB;
|
|
663
|
+
row.jacobianA = {0, 0, 0, -rotAxis.x, -rotAxis.y, -rotAxis.z};
|
|
664
|
+
row.jacobianB = {0, 0, 0, rotAxis.x, rotAxis.y, rotAxis.z};
|
|
665
|
+
const double error = dotVec(forwardB, bWorld);
|
|
666
|
+
row.scalar = constraintSoftness(error, dt, 0.0, 1.0, 0.0);
|
|
667
|
+
emitRow(row, PathRowKind::AngularNormalLock);
|
|
668
|
+
}
|
|
669
|
+
{
|
|
670
|
+
const PathVec3 rotAxis = bWorld;
|
|
671
|
+
ConstraintRow6 row{};
|
|
672
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
673
|
+
row.bodyA = constraint.bodyA;
|
|
674
|
+
row.bodyB = constraint.bodyB;
|
|
675
|
+
row.jacobianA = {0, 0, 0, -rotAxis.x, -rotAxis.y, -rotAxis.z};
|
|
676
|
+
row.jacobianB = {0, 0, 0, rotAxis.x, rotAxis.y, rotAxis.z};
|
|
677
|
+
const double error = -dotVec(forwardB, nWorld);
|
|
678
|
+
row.scalar = constraintSoftness(error, dt, 0.0, 1.0, 0.0);
|
|
679
|
+
emitRow(row, PathRowKind::AngularBinormalLock);
|
|
680
|
+
}
|
|
681
|
+
} else if (constraint.orientationPolicy == PathOrientationPolicy::ConstrainToPath) {
|
|
682
|
+
const PathQuat qPath = quatFromOrthonormalFrame(tWorld, nWorld, bWorld);
|
|
683
|
+
const PathQuat qB{bodyB.orientationX, bodyB.orientationY, bodyB.orientationZ, bodyB.orientationW};
|
|
684
|
+
const PathQuat qBFrame = multiplyQuat(qB, constraint.frameOrientationB);
|
|
685
|
+
|
|
686
|
+
PathQuat qRel = multiplyQuat({-qPath.x, -qPath.y, -qPath.z, qPath.w}, qBFrame);
|
|
687
|
+
if (qRel.w < 0.0) {
|
|
688
|
+
qRel.x = -qRel.x;
|
|
689
|
+
qRel.y = -qRel.y;
|
|
690
|
+
qRel.z = -qRel.z;
|
|
691
|
+
qRel.w = -qRel.w;
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
const double angErrT = 2.0 * std::atan2(qRel.x, qRel.w);
|
|
695
|
+
const double angErrN = 2.0 * std::atan2(qRel.y, qRel.w);
|
|
696
|
+
const double angErrB = 2.0 * std::atan2(qRel.z, qRel.w);
|
|
697
|
+
|
|
698
|
+
{
|
|
699
|
+
const PathVec3 rotAxis = tWorld;
|
|
700
|
+
ConstraintRow6 row{};
|
|
701
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
702
|
+
row.bodyA = constraint.bodyA;
|
|
703
|
+
row.bodyB = constraint.bodyB;
|
|
704
|
+
row.jacobianA = {0, 0, 0, -rotAxis.x, -rotAxis.y, -rotAxis.z};
|
|
705
|
+
row.jacobianB = {0, 0, 0, rotAxis.x, rotAxis.y, rotAxis.z};
|
|
706
|
+
row.scalar = constraintSoftness(angErrT, dt, 0.0, 1.0, 0.0);
|
|
707
|
+
emitRow(row, PathRowKind::AngularTangentLock);
|
|
708
|
+
}
|
|
709
|
+
{
|
|
710
|
+
const PathVec3 rotAxis = nWorld;
|
|
711
|
+
ConstraintRow6 row{};
|
|
712
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
713
|
+
row.bodyA = constraint.bodyA;
|
|
714
|
+
row.bodyB = constraint.bodyB;
|
|
715
|
+
row.jacobianA = {0, 0, 0, -rotAxis.x, -rotAxis.y, -rotAxis.z};
|
|
716
|
+
row.jacobianB = {0, 0, 0, rotAxis.x, rotAxis.y, rotAxis.z};
|
|
717
|
+
row.scalar = constraintSoftness(angErrN, dt, 0.0, 1.0, 0.0);
|
|
718
|
+
emitRow(row, PathRowKind::AngularNormalLock);
|
|
719
|
+
}
|
|
720
|
+
{
|
|
721
|
+
const PathVec3 rotAxis = bWorld;
|
|
722
|
+
ConstraintRow6 row{};
|
|
723
|
+
row.scalar = ConstraintScalar::unconstrained();
|
|
724
|
+
row.bodyA = constraint.bodyA;
|
|
725
|
+
row.bodyB = constraint.bodyB;
|
|
726
|
+
row.jacobianA = {0, 0, 0, -rotAxis.x, -rotAxis.y, -rotAxis.z};
|
|
727
|
+
row.jacobianB = {0, 0, 0, rotAxis.x, rotAxis.y, rotAxis.z};
|
|
728
|
+
row.scalar = constraintSoftness(angErrB, dt, 0.0, 1.0, 0.0);
|
|
729
|
+
emitRow(row, PathRowKind::AngularBinormalLock);
|
|
730
|
+
}
|
|
731
|
+
}
|
|
732
|
+
|
|
733
|
+
return count;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
std::uint32_t solvePathConstraint(
|
|
737
|
+
PathConstraint& constraint,
|
|
738
|
+
std::span<Body> bodies,
|
|
739
|
+
std::span<const ConstraintBodyMobility> mobility,
|
|
740
|
+
double dt,
|
|
741
|
+
std::uint32_t iterations) {
|
|
742
|
+
if (constraint.bodyA >= bodies.size() || constraint.bodyB >= bodies.size()) return 0;
|
|
743
|
+
if (constraint.bodyA >= mobility.size() || constraint.bodyB >= mobility.size()) return 0;
|
|
744
|
+
if (dt <= 0.0 || iterations == 0 || constraint.bodyA == constraint.bodyB || constraint.path == nullptr) return 0;
|
|
745
|
+
|
|
746
|
+
std::array<ConstraintRow6, kMaxPathConstraintRows> rows{};
|
|
747
|
+
const std::uint32_t count = buildPathConstraintRows(
|
|
748
|
+
constraint, bodies[constraint.bodyA], bodies[constraint.bodyB], dt, rows);
|
|
749
|
+
|
|
750
|
+
for (std::uint32_t r = 0; r < count; ++r) {
|
|
751
|
+
prepareConstraintRow(rows[r], mobility);
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
for (std::uint32_t iter = 0; iter < iterations; ++iter) {
|
|
755
|
+
for (std::uint32_t r = 0; r < count; ++r) {
|
|
756
|
+
solveConstraintRow(rows[r], bodies, mobility);
|
|
757
|
+
}
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
const auto rotMatA = bodyRotationMatrix(bodies[constraint.bodyA]);
|
|
761
|
+
const auto rotMatB = bodyRotationMatrix(bodies[constraint.bodyB]);
|
|
762
|
+
const PathVec3 rotAnchorA = rotateMatrix(rotMatA, constraint.anchorA);
|
|
763
|
+
const PathVec3 wAnchorA = {
|
|
764
|
+
bodies[constraint.bodyA].x + rotAnchorA.x,
|
|
765
|
+
bodies[constraint.bodyA].y + rotAnchorA.y,
|
|
766
|
+
bodies[constraint.bodyA].z + rotAnchorA.z
|
|
767
|
+
};
|
|
768
|
+
const PathVec3 rotAnchorB = rotateMatrix(rotMatB, constraint.anchorB);
|
|
769
|
+
const PathVec3 wB = {
|
|
770
|
+
bodies[constraint.bodyB].x + rotAnchorB.x,
|
|
771
|
+
bodies[constraint.bodyB].y + rotAnchorB.y,
|
|
772
|
+
bodies[constraint.bodyB].z + rotAnchorB.z
|
|
773
|
+
};
|
|
774
|
+
const PathVec3 pLocal = transposeRotateMatrix(rotMatA, subVec(wB, wAnchorA));
|
|
775
|
+
|
|
776
|
+
constraint.currentArcLength = findClosestArcLength(*constraint.path, pLocal);
|
|
777
|
+
|
|
778
|
+
return count;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
} // namespace rundot::kinetix::physics
|