@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,1100 @@
|
|
|
1
|
+
#include "powered_ragdoll.hpp"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <array>
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <cstdint>
|
|
7
|
+
#include <span>
|
|
8
|
+
|
|
9
|
+
namespace rundot::kinetix::physics {
|
|
10
|
+
|
|
11
|
+
namespace {
|
|
12
|
+
|
|
13
|
+
inline double poweredClamp(double value, double minVal, double maxVal) {
|
|
14
|
+
if (value < minVal) return minVal;
|
|
15
|
+
if (value > maxVal) return maxVal;
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
inline double poweredSqrt(double value) {
|
|
20
|
+
if (value <= 0.0) return 0.0;
|
|
21
|
+
return std::sqrt(value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
constexpr double kMagicNumber = 0x50575244; // "PWRD"
|
|
25
|
+
|
|
26
|
+
inline Body unpackBodyRecord(const double* bodyData, std::uint32_t b) {
|
|
27
|
+
const std::uint32_t offset = b * kBodyStride;
|
|
28
|
+
Body body;
|
|
29
|
+
body.x = bodyData[offset + 0];
|
|
30
|
+
body.y = bodyData[offset + 1];
|
|
31
|
+
body.z = bodyData[offset + 2];
|
|
32
|
+
body.vx = bodyData[offset + 3];
|
|
33
|
+
body.vy = bodyData[offset + 4];
|
|
34
|
+
body.vz = bodyData[offset + 5];
|
|
35
|
+
|
|
36
|
+
const std::uint32_t motionType = static_cast<std::uint32_t>(bodyData[offset + 9]);
|
|
37
|
+
const std::uint32_t kind = motionType & kMotionKindMask;
|
|
38
|
+
body.dynamic = (kind == 1);
|
|
39
|
+
body.kinematic = (kind == 2);
|
|
40
|
+
body.trigger = (motionType & kMotionTrigger) != 0;
|
|
41
|
+
body.sensor = (motionType & kMotionSensor) != 0;
|
|
42
|
+
body.sensorLike = body.trigger || body.sensor;
|
|
43
|
+
body.sleeping = body.dynamic && (motionType & kMotionSleeping) != 0;
|
|
44
|
+
body.angularState = (motionType & kMotionAngularState) != 0;
|
|
45
|
+
|
|
46
|
+
body.shape = static_cast<ShapeKind>(static_cast<std::uint32_t>(bodyData[offset + 10]));
|
|
47
|
+
body.gravityScale = bodyData[offset + 13];
|
|
48
|
+
body.linearDamping = bodyData[offset + 14];
|
|
49
|
+
body.mass = bodyData[offset + 18] > 0.0 ? bodyData[offset + 18] : 1.0;
|
|
50
|
+
|
|
51
|
+
body.angularVelX = bodyData[offset + 19];
|
|
52
|
+
body.angularVelY = bodyData[offset + 20];
|
|
53
|
+
body.angularVelZ = bodyData[offset + 21];
|
|
54
|
+
|
|
55
|
+
const double qx = bodyData[offset + 22];
|
|
56
|
+
const double qy = bodyData[offset + 23];
|
|
57
|
+
const double qz = bodyData[offset + 24];
|
|
58
|
+
const double qw = bodyData[offset + 25];
|
|
59
|
+
if (qx * qx + qy * qy + qz * qz + qw * qw > 1e-12) {
|
|
60
|
+
body.orientationX = qx;
|
|
61
|
+
body.orientationY = qy;
|
|
62
|
+
body.orientationZ = qz;
|
|
63
|
+
body.orientationW = qw;
|
|
64
|
+
} else if (bodyData[offset + 10] * bodyData[offset + 10] + bodyData[offset + 11] * bodyData[offset + 11] +
|
|
65
|
+
bodyData[offset + 12] * bodyData[offset + 12] + bodyData[offset + 13] * bodyData[offset + 13] > 1e-12) {
|
|
66
|
+
body.orientationX = bodyData[offset + 10];
|
|
67
|
+
body.orientationY = bodyData[offset + 11];
|
|
68
|
+
body.orientationZ = bodyData[offset + 12];
|
|
69
|
+
body.orientationW = bodyData[offset + 13];
|
|
70
|
+
body.angularVelX = bodyData[offset + 15];
|
|
71
|
+
body.angularVelY = bodyData[offset + 16];
|
|
72
|
+
body.angularVelZ = bodyData[offset + 17];
|
|
73
|
+
} else {
|
|
74
|
+
body.orientationW = 1.0;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
body.angularDamping = bodyData[offset + 26];
|
|
78
|
+
body.friction = bodyData[offset + 27];
|
|
79
|
+
body.restitution = bodyData[offset + 28];
|
|
80
|
+
body.motionLocks = static_cast<std::uint32_t>(bodyData[offset + 29]);
|
|
81
|
+
body.bullet = (bodyData[offset + 30] != 0.0);
|
|
82
|
+
body.inertiaXX = bodyData[offset + 31] > 0.0 ? bodyData[offset + 31] : 1.0;
|
|
83
|
+
body.inertiaYY = bodyData[offset + 32] > 0.0 ? bodyData[offset + 32] : 1.0;
|
|
84
|
+
body.inertiaZZ = bodyData[offset + 33] > 0.0 ? bodyData[offset + 33] : 1.0;
|
|
85
|
+
body.inertiaXY = bodyData[offset + 34];
|
|
86
|
+
body.inertiaXZ = bodyData[offset + 35];
|
|
87
|
+
body.inertiaYZ = bodyData[offset + 36];
|
|
88
|
+
body.centerOfMassX = bodyData[offset + 37];
|
|
89
|
+
body.centerOfMassY = bodyData[offset + 38];
|
|
90
|
+
body.centerOfMassZ = bodyData[offset + 39];
|
|
91
|
+
body.sleepThreshold = bodyData[offset + 40];
|
|
92
|
+
body.sleepEnabled = (bodyData[offset + 41] != 0.0);
|
|
93
|
+
body.sleepTime = bodyData[offset + 42];
|
|
94
|
+
body.rollingResistance = bodyData[offset + 43];
|
|
95
|
+
return body;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
inline void packBodyRecord(double* bodyData, std::uint32_t b, const Body& body) {
|
|
99
|
+
const std::uint32_t offset = b * kBodyStride;
|
|
100
|
+
bodyData[offset + 3] = body.vx;
|
|
101
|
+
bodyData[offset + 4] = body.vy;
|
|
102
|
+
bodyData[offset + 5] = body.vz;
|
|
103
|
+
bodyData[offset + 19] = body.angularVelX;
|
|
104
|
+
bodyData[offset + 20] = body.angularVelY;
|
|
105
|
+
bodyData[offset + 21] = body.angularVelZ;
|
|
106
|
+
bodyData[offset + 15] = body.angularVelX;
|
|
107
|
+
bodyData[offset + 16] = body.angularVelY;
|
|
108
|
+
bodyData[offset + 17] = body.angularVelZ;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
} // namespace
|
|
112
|
+
|
|
113
|
+
PoweredQuat poweredQuatSlerp(const PoweredQuat& q1, const PoweredQuat& q2, double t) {
|
|
114
|
+
const double clampedT = poweredClamp(t, 0.0, 1.0);
|
|
115
|
+
double dot = q1.x * q2.x + q1.y * q2.y + q1.z * q2.z + q1.w * q2.w;
|
|
116
|
+
PoweredQuat target = q2;
|
|
117
|
+
if (dot < 0.0) {
|
|
118
|
+
dot = -dot;
|
|
119
|
+
target = {-q2.x, -q2.y, -q2.z, -q2.w};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (dot > 0.9995) {
|
|
123
|
+
const PoweredQuat res{
|
|
124
|
+
q1.x + clampedT * (target.x - q1.x),
|
|
125
|
+
q1.y + clampedT * (target.y - q1.y),
|
|
126
|
+
q1.z + clampedT * (target.z - q1.z),
|
|
127
|
+
q1.w + clampedT * (target.w - q1.w)
|
|
128
|
+
};
|
|
129
|
+
return quatNormalize(res);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const double theta0 = std::acos(poweredClamp(dot, -1.0, 1.0));
|
|
133
|
+
const double theta = theta0 * clampedT;
|
|
134
|
+
const double sinTheta0 = std::sin(theta0);
|
|
135
|
+
const double sinTheta = std::sin(theta);
|
|
136
|
+
const double s0 = std::cos(theta) - dot * sinTheta / sinTheta0;
|
|
137
|
+
const double s1 = sinTheta / sinTheta0;
|
|
138
|
+
|
|
139
|
+
const PoweredQuat res{
|
|
140
|
+
s0 * q1.x + s1 * target.x,
|
|
141
|
+
s0 * q1.y + s1 * target.y,
|
|
142
|
+
s0 * q1.z + s1 * target.z,
|
|
143
|
+
s0 * q1.w + s1 * target.w
|
|
144
|
+
};
|
|
145
|
+
return quatNormalize(res);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
void poweredQuatToAxisAngle(const PoweredQuat& q, PoweredVec3& outAxis, double& outAngle) {
|
|
149
|
+
PoweredQuat normQ = quatNormalize(q);
|
|
150
|
+
if (normQ.w < 0.0) {
|
|
151
|
+
normQ = {-normQ.x, -normQ.y, -normQ.z, -normQ.w};
|
|
152
|
+
}
|
|
153
|
+
const double sinHalfAngleSq = normQ.x * normQ.x + normQ.y * normQ.y + normQ.z * normQ.z;
|
|
154
|
+
if (sinHalfAngleSq <= 1e-15) {
|
|
155
|
+
outAxis = {1.0, 0.0, 0.0};
|
|
156
|
+
outAngle = 0.0;
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
const double sinHalfAngle = poweredSqrt(sinHalfAngleSq);
|
|
160
|
+
outAngle = 2.0 * std::atan2(sinHalfAngle, normQ.w);
|
|
161
|
+
const double invSin = 1.0 / sinHalfAngle;
|
|
162
|
+
outAxis = {normQ.x * invSin, normQ.y * invSin, normQ.z * invSin};
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
double evaluateBlendCurve(double progress, BlendCurve curve) {
|
|
166
|
+
const double s = poweredClamp(progress, 0.0, 1.0);
|
|
167
|
+
switch (curve) {
|
|
168
|
+
case BlendCurve::Linear:
|
|
169
|
+
return s;
|
|
170
|
+
case BlendCurve::SmoothStep:
|
|
171
|
+
return s * s * (3.0 - 2.0 * s);
|
|
172
|
+
case BlendCurve::SmootherStep:
|
|
173
|
+
return s * s * s * (s * (s * 6.0 - 15.0) + 10.0);
|
|
174
|
+
case BlendCurve::CubicEaseIn:
|
|
175
|
+
return s * s * s;
|
|
176
|
+
case BlendCurve::CubicEaseOut: {
|
|
177
|
+
const double inv = 1.0 - s;
|
|
178
|
+
return 1.0 - inv * inv * inv;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return s;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
PoweredRagdollStatus computeJointServoTorque(
|
|
185
|
+
const PoweredJointDriveConfig& config,
|
|
186
|
+
const Body& parentBody,
|
|
187
|
+
const Body& childBody,
|
|
188
|
+
const PoweredQuat& targetLocalRotation,
|
|
189
|
+
const PoweredVec3& targetAngularVelocity,
|
|
190
|
+
double effectiveBlendWeight,
|
|
191
|
+
PoweredJointState& outState,
|
|
192
|
+
PoweredVec3& outWorldTorque) {
|
|
193
|
+
outWorldTorque = {0.0, 0.0, 0.0};
|
|
194
|
+
outState.jointIndex = config.jointIndex;
|
|
195
|
+
outState.targetLocalRotation = targetLocalRotation;
|
|
196
|
+
outState.targetAngularVelocity = targetAngularVelocity;
|
|
197
|
+
outState.appliedTorque = {0.0, 0.0, 0.0};
|
|
198
|
+
outState.currentErrorAngle = 0.0;
|
|
199
|
+
outState.saturated = false;
|
|
200
|
+
|
|
201
|
+
if (!config.enabled || effectiveBlendWeight <= 0.0) {
|
|
202
|
+
return PoweredRagdollStatus::Success;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const PoweredQuat qP = quatNormalize({
|
|
206
|
+
parentBody.orientationX, parentBody.orientationY, parentBody.orientationZ, parentBody.orientationW
|
|
207
|
+
});
|
|
208
|
+
const PoweredQuat qC = quatNormalize({
|
|
209
|
+
childBody.orientationX, childBody.orientationY, childBody.orientationZ, childBody.orientationW
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
// Current relative orientation: q_rel = q_p^{-1} * q_c
|
|
213
|
+
const PoweredQuat qRel = quatNormalize(quatMultiply(quatConjugate(qP), qC));
|
|
214
|
+
|
|
215
|
+
// Orientation error: q_err = q_target * q_rel^{-1}
|
|
216
|
+
PoweredQuat qErr = quatNormalize(quatMultiply(targetLocalRotation, quatConjugate(qRel)));
|
|
217
|
+
if (qErr.w < 0.0) {
|
|
218
|
+
qErr = {-qErr.x, -qErr.y, -qErr.z, -qErr.w};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
PoweredVec3 errAxis{1.0, 0.0, 0.0};
|
|
222
|
+
double errAngle = 0.0;
|
|
223
|
+
poweredQuatToAxisAngle(qErr, errAxis, errAngle);
|
|
224
|
+
outState.currentErrorAngle = errAngle;
|
|
225
|
+
|
|
226
|
+
// Angular displacement error in parent frame, then transformed into world frame
|
|
227
|
+
const PoweredVec3 deltaThetaLocal = scaleVec3(errAxis, errAngle);
|
|
228
|
+
const PoweredVec3 deltaThetaWorld = rotateByQuat(qP, deltaThetaLocal);
|
|
229
|
+
|
|
230
|
+
// Relative angular velocity in world space: w_rel = w_child - w_parent
|
|
231
|
+
const PoweredVec3 wP{parentBody.angularVelX, parentBody.angularVelY, parentBody.angularVelZ};
|
|
232
|
+
const PoweredVec3 wC{childBody.angularVelX, childBody.angularVelY, childBody.angularVelZ};
|
|
233
|
+
const PoweredVec3 relAngVel = subVec3(wC, wP);
|
|
234
|
+
|
|
235
|
+
// Target relative angular velocity in world space
|
|
236
|
+
const PoweredVec3 targetAngVelWorld = rotateByQuat(qP, targetAngularVelocity);
|
|
237
|
+
const PoweredVec3 angVelErr = subVec3(targetAngVelWorld, relAngVel);
|
|
238
|
+
|
|
239
|
+
// Blend weight and drive strength scaling
|
|
240
|
+
const double effWeight = poweredClamp(effectiveBlendWeight * config.driveStrength, 0.0, 1.0);
|
|
241
|
+
const double effKp = config.kp * effWeight;
|
|
242
|
+
const double effKd = config.kd * effWeight;
|
|
243
|
+
|
|
244
|
+
const PoweredVec3 rawTorque = addVec3(
|
|
245
|
+
scaleVec3(deltaThetaWorld, effKp),
|
|
246
|
+
scaleVec3(angVelErr, effKd)
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
// Torque Capping
|
|
250
|
+
PoweredVec3 cappedTorque = rawTorque;
|
|
251
|
+
bool saturated = false;
|
|
252
|
+
const double maxTorqueScaled = config.maxTorque * effWeight;
|
|
253
|
+
|
|
254
|
+
if (config.torqueCapMode == TorqueCapMode::TotalMagnitude) {
|
|
255
|
+
const double rawMag = lengthVec3(rawTorque);
|
|
256
|
+
if (maxTorqueScaled > 0.0 && rawMag > maxTorqueScaled) {
|
|
257
|
+
cappedTorque = scaleVec3(rawTorque, maxTorqueScaled / rawMag);
|
|
258
|
+
saturated = true;
|
|
259
|
+
}
|
|
260
|
+
} else {
|
|
261
|
+
// Per-axis capping in child bone local frame
|
|
262
|
+
const PoweredVec3 torqueInChild = invRotateByQuat(qC, rawTorque);
|
|
263
|
+
const PoweredVec3 maxAxis = scaleVec3(config.maxTorqueAxis, effWeight);
|
|
264
|
+
const double cx = poweredClamp(torqueInChild.x, -maxAxis.x, maxAxis.x);
|
|
265
|
+
const double cy = poweredClamp(torqueInChild.y, -maxAxis.y, maxAxis.y);
|
|
266
|
+
const double cz = poweredClamp(torqueInChild.z, -maxAxis.z, maxAxis.z);
|
|
267
|
+
if (cx != torqueInChild.x || cy != torqueInChild.y || cz != torqueInChild.z) {
|
|
268
|
+
saturated = true;
|
|
269
|
+
}
|
|
270
|
+
cappedTorque = rotateByQuat(qC, {cx, cy, cz});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
outWorldTorque = cappedTorque;
|
|
274
|
+
outState.appliedTorque = cappedTorque;
|
|
275
|
+
outState.saturated = saturated;
|
|
276
|
+
outState.internalWork += lengthVec3(cappedTorque) * errAngle;
|
|
277
|
+
|
|
278
|
+
return PoweredRagdollStatus::Success;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
void applyTorqueToBody(Body& body, const PoweredVec3& torque, double dt) {
|
|
282
|
+
if (body.kinematic || !body.dynamic || body.mass <= 0.0 || dt <= 0.0) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
const PoweredQuat q = quatNormalize({
|
|
286
|
+
body.orientationX, body.orientationY, body.orientationZ, body.orientationW
|
|
287
|
+
});
|
|
288
|
+
const PoweredVec3 torqueLocal = invRotateByQuat(q, torque);
|
|
289
|
+
|
|
290
|
+
const double invIx = body.inertiaXX > 1e-6 ? 1.0 / body.inertiaXX : 1.0;
|
|
291
|
+
const double invIy = body.inertiaYY > 1e-6 ? 1.0 / body.inertiaYY : 1.0;
|
|
292
|
+
const double invIz = body.inertiaZZ > 1e-6 ? 1.0 / body.inertiaZZ : 1.0;
|
|
293
|
+
|
|
294
|
+
const PoweredVec3 angAccLocal{
|
|
295
|
+
torqueLocal.x * invIx,
|
|
296
|
+
torqueLocal.y * invIy,
|
|
297
|
+
torqueLocal.z * invIz
|
|
298
|
+
};
|
|
299
|
+
const PoweredVec3 angAccWorld = rotateByQuat(q, angAccLocal);
|
|
300
|
+
|
|
301
|
+
body.angularVelX += angAccWorld.x * dt;
|
|
302
|
+
body.angularVelY += angAccWorld.y * dt;
|
|
303
|
+
body.angularVelZ += angAccWorld.z * dt;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
void applyHitImpulseToBody(Body& body, const PoweredVec3& impulse, const PoweredVec3& hitPoint) {
|
|
307
|
+
if (body.kinematic || !body.dynamic || body.mass <= 0.0) {
|
|
308
|
+
return;
|
|
309
|
+
}
|
|
310
|
+
// Linear impulse
|
|
311
|
+
const double invMass = 1.0 / body.mass;
|
|
312
|
+
body.vx += impulse.x * invMass;
|
|
313
|
+
body.vy += impulse.y * invMass;
|
|
314
|
+
body.vz += impulse.z * invMass;
|
|
315
|
+
|
|
316
|
+
// Angular impulse: tau_impulse = r x J
|
|
317
|
+
const PoweredVec3 bodyCenter{
|
|
318
|
+
body.x + body.centerOfMassX,
|
|
319
|
+
body.y + body.centerOfMassY,
|
|
320
|
+
body.z + body.centerOfMassZ
|
|
321
|
+
};
|
|
322
|
+
const PoweredVec3 r = subVec3(hitPoint, bodyCenter);
|
|
323
|
+
const PoweredVec3 angImpulse = crossVec3(r, impulse);
|
|
324
|
+
|
|
325
|
+
const PoweredQuat q = quatNormalize({
|
|
326
|
+
body.orientationX, body.orientationY, body.orientationZ, body.orientationW
|
|
327
|
+
});
|
|
328
|
+
const PoweredVec3 angImpLocal = invRotateByQuat(q, angImpulse);
|
|
329
|
+
|
|
330
|
+
const double invIx = body.inertiaXX > 1e-6 ? 1.0 / body.inertiaXX : 1.0;
|
|
331
|
+
const double invIy = body.inertiaYY > 1e-6 ? 1.0 / body.inertiaYY : 1.0;
|
|
332
|
+
const double invIz = body.inertiaZZ > 1e-6 ? 1.0 / body.inertiaZZ : 1.0;
|
|
333
|
+
|
|
334
|
+
const PoweredVec3 deltaWLocal{
|
|
335
|
+
angImpLocal.x * invIx,
|
|
336
|
+
angImpLocal.y * invIy,
|
|
337
|
+
angImpLocal.z * invIz
|
|
338
|
+
};
|
|
339
|
+
const PoweredVec3 deltaWWorld = rotateByQuat(q, deltaWLocal);
|
|
340
|
+
|
|
341
|
+
body.angularVelX += deltaWWorld.x;
|
|
342
|
+
body.angularVelY += deltaWWorld.y;
|
|
343
|
+
body.angularVelZ += deltaWWorld.z;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
PoweredRagdollStatus applyHitImpulseToSkeleton(
|
|
347
|
+
const RagdollSkeletonDef& skeleton,
|
|
348
|
+
std::span<Body> bodies,
|
|
349
|
+
const HitImpulse& hit,
|
|
350
|
+
const PoweredRagdollConfig& config,
|
|
351
|
+
PoweredRagdollState& state) {
|
|
352
|
+
if (hit.boneIndex >= bodies.size() || (skeleton.boneCount > 0 && hit.boneIndex >= skeleton.boneCount)) {
|
|
353
|
+
return PoweredRagdollStatus::InvalidInput;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// 1. Direct impulse to target bone
|
|
357
|
+
applyHitImpulseToBody(bodies[hit.boneIndex], hit.impulse, hit.point);
|
|
358
|
+
|
|
359
|
+
// 2. Momentum propagation along skeletal hierarchy
|
|
360
|
+
if (hit.propagate && hit.propagationFactor > 0.0 && skeleton.boneCount > 0) {
|
|
361
|
+
const double factor = poweredClamp(hit.propagationFactor, 0.0, 1.0);
|
|
362
|
+
const PoweredVec3 propagatedImpulse = scaleVec3(hit.impulse, factor);
|
|
363
|
+
|
|
364
|
+
// Propagate to parent bone
|
|
365
|
+
const std::int32_t p = skeleton.bones[hit.boneIndex].parentIndex;
|
|
366
|
+
if (p >= 0 && static_cast<std::size_t>(p) < bodies.size()) {
|
|
367
|
+
const Body& parentBody = bodies[static_cast<std::size_t>(p)];
|
|
368
|
+
const PoweredTransform parentPose{
|
|
369
|
+
{parentBody.x, parentBody.y, parentBody.z},
|
|
370
|
+
{parentBody.orientationX, parentBody.orientationY, parentBody.orientationZ, parentBody.orientationW}
|
|
371
|
+
};
|
|
372
|
+
const PoweredVec3 parentAnchor = transformPoint(parentPose, skeleton.bones[hit.boneIndex].joint.anchorA);
|
|
373
|
+
applyHitImpulseToBody(bodies[static_cast<std::size_t>(p)], propagatedImpulse, parentAnchor);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
// Propagate to direct child bones
|
|
377
|
+
for (std::uint32_t c = 0; c < skeleton.boneCount; ++c) {
|
|
378
|
+
if (skeleton.bones[c].parentIndex == static_cast<std::int32_t>(hit.boneIndex) && c < bodies.size()) {
|
|
379
|
+
const Body& childBody = bodies[c];
|
|
380
|
+
const PoweredTransform childPose{
|
|
381
|
+
{childBody.x, childBody.y, childBody.z},
|
|
382
|
+
{childBody.orientationX, childBody.orientationY, childBody.orientationZ, childBody.orientationW}
|
|
383
|
+
};
|
|
384
|
+
const PoweredVec3 childAnchor = transformPoint(childPose, skeleton.bones[c].joint.anchorB);
|
|
385
|
+
applyHitImpulseToBody(bodies[c], propagatedImpulse, childAnchor);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// 3. Update telemetry and fall threshold
|
|
391
|
+
const double impMag = lengthVec3(hit.impulse);
|
|
392
|
+
state.lastHitImpulseMagnitude = impMag;
|
|
393
|
+
state.lastHitBoneIndex = hit.boneIndex;
|
|
394
|
+
|
|
395
|
+
if (impMag >= config.recovery.fallImpulseThreshold && state.mode == PoweredRagdollMode::Powered) {
|
|
396
|
+
state.mode = PoweredRagdollMode::Falling;
|
|
397
|
+
state.blendWeight = 0.0;
|
|
398
|
+
state.settleTimer = 0.0;
|
|
399
|
+
state.fallTriggered = true;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return PoweredRagdollStatus::Success;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
PoweredRagdollStatus updateRagdollStabilityAndSettle(
|
|
406
|
+
const PoweredRagdollConfig& config,
|
|
407
|
+
std::span<const Body> bodies,
|
|
408
|
+
double dt,
|
|
409
|
+
PoweredRagdollState& state) {
|
|
410
|
+
if (bodies.empty() || config.boneCount == 0) {
|
|
411
|
+
return PoweredRagdollStatus::InvalidInput;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// State: Powered -> evaluate tilt instability
|
|
415
|
+
if (state.mode == PoweredRagdollMode::Powered) {
|
|
416
|
+
const std::uint32_t pelvisIdx = config.pelvisBoneIndex < bodies.size() ? config.pelvisBoneIndex : 0;
|
|
417
|
+
const Body& pelvis = bodies[pelvisIdx];
|
|
418
|
+
const PoweredQuat qRoot = quatNormalize({
|
|
419
|
+
pelvis.orientationX, pelvis.orientationY, pelvis.orientationZ, pelvis.orientationW
|
|
420
|
+
});
|
|
421
|
+
const PoweredVec3 upDir = normalizeVec3(config.upVector);
|
|
422
|
+
const PoweredVec3 bodyUp = rotateByQuat(qRoot, upDir);
|
|
423
|
+
const double cosTheta = poweredClamp(dotVec3(bodyUp, upDir), -1.0, 1.0);
|
|
424
|
+
const double tiltAngle = std::acos(cosTheta);
|
|
425
|
+
|
|
426
|
+
if (tiltAngle > config.recovery.fallInstabilityAngle) {
|
|
427
|
+
state.mode = PoweredRagdollMode::Falling;
|
|
428
|
+
state.blendWeight = 0.0;
|
|
429
|
+
state.settleTimer = 0.0;
|
|
430
|
+
state.fallTriggered = true;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// State: Falling -> evaluate kinetic energy and settle threshold
|
|
435
|
+
if (state.mode == PoweredRagdollMode::Falling) {
|
|
436
|
+
double maxLinearSpeed = 0.0;
|
|
437
|
+
double maxAngularSpeed = 0.0;
|
|
438
|
+
double totalEnergy = 0.0;
|
|
439
|
+
|
|
440
|
+
const std::size_t activeCount = std::min(static_cast<std::size_t>(config.boneCount), bodies.size());
|
|
441
|
+
for (std::size_t i = 0; i < activeCount; ++i) {
|
|
442
|
+
const Body& b = bodies[i];
|
|
443
|
+
const double vSq = b.vx * b.vx + b.vy * b.vy + b.vz * b.vz;
|
|
444
|
+
const double wSq = b.angularVelX * b.angularVelX + b.angularVelY * b.angularVelY + b.angularVelZ * b.angularVelZ;
|
|
445
|
+
const double linSpeed = poweredSqrt(vSq);
|
|
446
|
+
const double angSpeed = poweredSqrt(wSq);
|
|
447
|
+
|
|
448
|
+
if (linSpeed > maxLinearSpeed) maxLinearSpeed = linSpeed;
|
|
449
|
+
if (angSpeed > maxAngularSpeed) maxAngularSpeed = angSpeed;
|
|
450
|
+
|
|
451
|
+
const double rotEnergy = 0.5 * (
|
|
452
|
+
b.inertiaXX * b.angularVelX * b.angularVelX +
|
|
453
|
+
b.inertiaYY * b.angularVelY * b.angularVelY +
|
|
454
|
+
b.inertiaZZ * b.angularVelZ * b.angularVelZ
|
|
455
|
+
);
|
|
456
|
+
totalEnergy += 0.5 * b.mass * vSq + rotEnergy;
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
state.totalKineticEnergy = totalEnergy;
|
|
460
|
+
|
|
461
|
+
if (maxLinearSpeed <= config.recovery.settleLinearVelocity &&
|
|
462
|
+
maxAngularSpeed <= config.recovery.settleAngularVelocity) {
|
|
463
|
+
state.settleTimer += dt;
|
|
464
|
+
if (state.settleTimer >= config.recovery.settleTimeRequired) {
|
|
465
|
+
state.mode = PoweredRagdollMode::Settled;
|
|
466
|
+
state.settleTimer = 0.0;
|
|
467
|
+
state.settledTriggered = true;
|
|
468
|
+
|
|
469
|
+
// Detect prone vs supine from chest orientation
|
|
470
|
+
const std::uint32_t chestIdx = config.chestBoneIndex < bodies.size() ? config.chestBoneIndex : 0;
|
|
471
|
+
const Body& chest = bodies[chestIdx];
|
|
472
|
+
const PoweredQuat qChest = quatNormalize({
|
|
473
|
+
chest.orientationX, chest.orientationY, chest.orientationZ, chest.orientationW
|
|
474
|
+
});
|
|
475
|
+
const PoweredVec3 chestFwd = rotateByQuat(qChest, normalizeVec3(config.forwardVector));
|
|
476
|
+
const double forwardUpDot = dotVec3(chestFwd, normalizeVec3(config.upVector));
|
|
477
|
+
|
|
478
|
+
if (forwardUpDot < -0.25) {
|
|
479
|
+
state.orientation = OrientationStatus::Prone;
|
|
480
|
+
} else if (forwardUpDot > 0.25) {
|
|
481
|
+
state.orientation = OrientationStatus::Supine;
|
|
482
|
+
} else {
|
|
483
|
+
state.orientation = OrientationStatus::Unknown;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
if (config.recovery.autoRecover) {
|
|
487
|
+
state.mode = PoweredRagdollMode::Recovering;
|
|
488
|
+
state.recoveryTime = 0.0;
|
|
489
|
+
state.blendWeight = 0.0;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
} else {
|
|
493
|
+
state.settleTimer = 0.0;
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
return PoweredRagdollStatus::Success;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
PoweredRagdollStatus startPoseRecovery(
|
|
501
|
+
PoweredRagdollState& state,
|
|
502
|
+
const PoweredRagdollConfig& config) {
|
|
503
|
+
state.mode = PoweredRagdollMode::Recovering;
|
|
504
|
+
state.recoveryTime = 0.0;
|
|
505
|
+
state.blendWeight = 0.0;
|
|
506
|
+
state.settleTimer = 0.0;
|
|
507
|
+
state.recoveryCompleted = false;
|
|
508
|
+
(void)config;
|
|
509
|
+
return PoweredRagdollStatus::Success;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
PoweredRagdollStatus updatePoseRecovery(
|
|
513
|
+
const PoweredRagdollConfig& config,
|
|
514
|
+
double dt,
|
|
515
|
+
PoweredRagdollState& state) {
|
|
516
|
+
if (state.mode != PoweredRagdollMode::Recovering) {
|
|
517
|
+
return PoweredRagdollStatus::Success;
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
state.recoveryTime += dt;
|
|
521
|
+
const double duration = config.recovery.recoveryDuration > 1e-4 ? config.recovery.recoveryDuration : 1.0;
|
|
522
|
+
const double progress = poweredClamp(state.recoveryTime / duration, 0.0, 1.0);
|
|
523
|
+
const double alpha = evaluateBlendCurve(progress, config.recovery.blendCurve);
|
|
524
|
+
|
|
525
|
+
state.blendWeight = alpha * config.globalBlendWeight;
|
|
526
|
+
|
|
527
|
+
if (progress >= 1.0) {
|
|
528
|
+
state.mode = PoweredRagdollMode::Powered;
|
|
529
|
+
state.recoveryTime = 0.0;
|
|
530
|
+
state.blendWeight = config.globalBlendWeight;
|
|
531
|
+
state.recoveryCompleted = true;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
return PoweredRagdollStatus::Success;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
PoweredRagdollStatus blendRagdollPoses(
|
|
538
|
+
std::span<const PoweredTransform> physPoses,
|
|
539
|
+
std::span<const PoweredTransform> animPoses,
|
|
540
|
+
double blendWeight,
|
|
541
|
+
std::span<PoweredTransform> outBlendedPoses) {
|
|
542
|
+
const std::size_t count = std::min({physPoses.size(), animPoses.size(), outBlendedPoses.size()});
|
|
543
|
+
if (count == 0) {
|
|
544
|
+
return PoweredRagdollStatus::InvalidInput;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const double w = poweredClamp(blendWeight, 0.0, 1.0);
|
|
548
|
+
const double invW = 1.0 - w;
|
|
549
|
+
|
|
550
|
+
for (std::size_t i = 0; i < count; ++i) {
|
|
551
|
+
const PoweredTransform& p = physPoses[i];
|
|
552
|
+
const PoweredTransform& a = animPoses[i];
|
|
553
|
+
PoweredTransform& out = outBlendedPoses[i];
|
|
554
|
+
|
|
555
|
+
out.position = {
|
|
556
|
+
invW * p.position.x + w * a.position.x,
|
|
557
|
+
invW * p.position.y + w * a.position.y,
|
|
558
|
+
invW * p.position.z + w * a.position.z
|
|
559
|
+
};
|
|
560
|
+
out.orientation = poweredQuatSlerp(p.orientation, a.orientation, w);
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
return PoweredRagdollStatus::Success;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
PoweredRagdollStatus stepPoweredRagdoll(
|
|
567
|
+
const RagdollSkeletonDef& skeleton,
|
|
568
|
+
const PoweredRagdollConfig& config,
|
|
569
|
+
const AnimationTargetPose* targetPose,
|
|
570
|
+
std::span<Body> bodies,
|
|
571
|
+
double dt,
|
|
572
|
+
PoweredRagdollState& state) {
|
|
573
|
+
if (bodies.empty() || config.boneCount == 0 || dt <= 0.0) {
|
|
574
|
+
return PoweredRagdollStatus::InvalidInput;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// 1. Advance recovery timer and blend progression
|
|
578
|
+
updatePoseRecovery(config, dt, state);
|
|
579
|
+
|
|
580
|
+
// 2. Evaluate stability tilt and settle logic
|
|
581
|
+
updateRagdollStabilityAndSettle(config, bodies, dt, state);
|
|
582
|
+
|
|
583
|
+
// 3. Clear transient one-frame flags
|
|
584
|
+
state.fallTriggered = false;
|
|
585
|
+
state.settledTriggered = false;
|
|
586
|
+
state.recoveryCompleted = false;
|
|
587
|
+
|
|
588
|
+
state.jointCount = config.jointCount;
|
|
589
|
+
|
|
590
|
+
// 4. Drive active motors if powered or recovering
|
|
591
|
+
const bool motorsActive = (state.blendWeight > 0.0) &&
|
|
592
|
+
(state.mode == PoweredRagdollMode::Powered || state.mode == PoweredRagdollMode::Recovering);
|
|
593
|
+
|
|
594
|
+
for (std::uint32_t j = 0; j < config.jointCount; ++j) {
|
|
595
|
+
const PoweredJointDriveConfig& jcfg = config.joints[j];
|
|
596
|
+
PoweredJointState& jstate = state.joints[j];
|
|
597
|
+
|
|
598
|
+
if (!jcfg.enabled || jcfg.childBoneIndex >= bodies.size() || jcfg.parentBoneIndex >= bodies.size()) {
|
|
599
|
+
jstate.appliedTorque = {0.0, 0.0, 0.0};
|
|
600
|
+
jstate.saturated = false;
|
|
601
|
+
continue;
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
if (!motorsActive) {
|
|
605
|
+
jstate.appliedTorque = {0.0, 0.0, 0.0};
|
|
606
|
+
jstate.saturated = false;
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
PoweredQuat targetRot = jcfg.targetLocalRotation;
|
|
611
|
+
PoweredVec3 targetAngVel = jcfg.targetAngularVelocity;
|
|
612
|
+
|
|
613
|
+
if (targetPose != nullptr && jcfg.childBoneIndex < targetPose->boneCount) {
|
|
614
|
+
targetRot = targetPose->localPoses[jcfg.childBoneIndex].orientation;
|
|
615
|
+
targetAngVel = targetPose->relativeAngularVelocities[jcfg.childBoneIndex];
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
PoweredVec3 appliedTorque{0.0, 0.0, 0.0};
|
|
619
|
+
computeJointServoTorque(
|
|
620
|
+
jcfg,
|
|
621
|
+
bodies[jcfg.parentBoneIndex],
|
|
622
|
+
bodies[jcfg.childBoneIndex],
|
|
623
|
+
targetRot,
|
|
624
|
+
targetAngVel,
|
|
625
|
+
state.blendWeight,
|
|
626
|
+
jstate,
|
|
627
|
+
appliedTorque);
|
|
628
|
+
|
|
629
|
+
// Apply equal and opposite torque: child receives +tau, parent receives -tau
|
|
630
|
+
applyTorqueToBody(bodies[jcfg.childBoneIndex], appliedTorque, dt);
|
|
631
|
+
applyTorqueToBody(bodies[jcfg.parentBoneIndex], scaleVec3(appliedTorque, -1.0), dt);
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// 5. Upright recovery alignment drive for pelvis/root
|
|
635
|
+
if (state.mode == PoweredRagdollMode::Recovering && config.recovery.uprightAlignmentKp > 0.0) {
|
|
636
|
+
const std::uint32_t pelvisIdx = config.pelvisBoneIndex < bodies.size() ? config.pelvisBoneIndex : 0;
|
|
637
|
+
Body& pelvis = bodies[pelvisIdx];
|
|
638
|
+
const PoweredQuat qPelvis = quatNormalize({
|
|
639
|
+
pelvis.orientationX, pelvis.orientationY, pelvis.orientationZ, pelvis.orientationW
|
|
640
|
+
});
|
|
641
|
+
const PoweredVec3 upDir = normalizeVec3(config.upVector);
|
|
642
|
+
const PoweredVec3 currentUp = rotateByQuat(qPelvis, upDir);
|
|
643
|
+
|
|
644
|
+
const PoweredVec3 tiltAxis = crossVec3(currentUp, upDir);
|
|
645
|
+
const double tiltSin = lengthVec3(tiltAxis);
|
|
646
|
+
if (tiltSin > 1e-4) {
|
|
647
|
+
const PoweredVec3 normTiltAxis = normalizeVec3(tiltAxis);
|
|
648
|
+
const double cosAngle = poweredClamp(dotVec3(currentUp, upDir), -1.0, 1.0);
|
|
649
|
+
const double tiltAngle = std::acos(cosAngle);
|
|
650
|
+
|
|
651
|
+
const PoweredVec3 rootAngVel{pelvis.angularVelX, pelvis.angularVelY, pelvis.angularVelZ};
|
|
652
|
+
const double effKp = config.recovery.uprightAlignmentKp * state.blendWeight;
|
|
653
|
+
const double effKd = config.recovery.uprightAlignmentKd * state.blendWeight;
|
|
654
|
+
|
|
655
|
+
const PoweredVec3 alignTorque = subVec3(
|
|
656
|
+
scaleVec3(normTiltAxis, effKp * tiltAngle),
|
|
657
|
+
scaleVec3(rootAngVel, effKd)
|
|
658
|
+
);
|
|
659
|
+
applyTorqueToBody(pelvis, alignTorque, dt);
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
(void)skeleton;
|
|
664
|
+
return PoweredRagdollStatus::Success;
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
std::uint32_t poweredRagdollSerializedStateSize(std::uint32_t jointCount) {
|
|
668
|
+
const std::uint32_t cappedJoints = std::min(jointCount, kPoweredRagdollMaxJoints);
|
|
669
|
+
return kPoweredRagdollStateHeaderStride + cappedJoints * kPoweredJointStateStride;
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
PoweredRagdollStatus capturePoweredRagdollState(
|
|
673
|
+
const PoweredRagdollState& state,
|
|
674
|
+
std::span<double> outBuffer,
|
|
675
|
+
std::uint32_t& outWritten) {
|
|
676
|
+
const std::uint32_t needed = poweredRagdollSerializedStateSize(state.jointCount);
|
|
677
|
+
if (outBuffer.size() < needed) {
|
|
678
|
+
return PoweredRagdollStatus::BufferOverflow;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
// Header serialization (16 doubles)
|
|
682
|
+
outBuffer[0] = static_cast<double>(state.mode);
|
|
683
|
+
outBuffer[1] = static_cast<double>(state.orientation);
|
|
684
|
+
outBuffer[2] = state.blendWeight;
|
|
685
|
+
outBuffer[3] = state.recoveryTime;
|
|
686
|
+
outBuffer[4] = state.settleTimer;
|
|
687
|
+
outBuffer[5] = state.totalKineticEnergy;
|
|
688
|
+
outBuffer[6] = state.lastHitImpulseMagnitude;
|
|
689
|
+
outBuffer[7] = static_cast<double>(state.lastHitBoneIndex);
|
|
690
|
+
outBuffer[8] = state.fallTriggered ? 1.0 : 0.0;
|
|
691
|
+
outBuffer[9] = state.settledTriggered ? 1.0 : 0.0;
|
|
692
|
+
outBuffer[10] = state.recoveryCompleted ? 1.0 : 0.0;
|
|
693
|
+
outBuffer[11] = static_cast<double>(state.jointCount);
|
|
694
|
+
outBuffer[12] = 0.0; // reserved
|
|
695
|
+
outBuffer[13] = 0.0; // reserved
|
|
696
|
+
outBuffer[14] = 0.0; // reserved
|
|
697
|
+
outBuffer[15] = kMagicNumber;
|
|
698
|
+
|
|
699
|
+
// Joint states (14 doubles each)
|
|
700
|
+
std::uint32_t offset = kPoweredRagdollStateHeaderStride;
|
|
701
|
+
const std::uint32_t cappedJoints = std::min(state.jointCount, kPoweredRagdollMaxJoints);
|
|
702
|
+
for (std::uint32_t j = 0; j < cappedJoints; ++j) {
|
|
703
|
+
const PoweredJointState& js = state.joints[j];
|
|
704
|
+
outBuffer[offset + 0] = static_cast<double>(js.jointIndex);
|
|
705
|
+
outBuffer[offset + 1] = js.targetLocalRotation.x;
|
|
706
|
+
outBuffer[offset + 2] = js.targetLocalRotation.y;
|
|
707
|
+
outBuffer[offset + 3] = js.targetLocalRotation.z;
|
|
708
|
+
outBuffer[offset + 4] = js.targetLocalRotation.w;
|
|
709
|
+
outBuffer[offset + 5] = js.targetAngularVelocity.x;
|
|
710
|
+
outBuffer[offset + 6] = js.targetAngularVelocity.y;
|
|
711
|
+
outBuffer[offset + 7] = js.targetAngularVelocity.z;
|
|
712
|
+
outBuffer[offset + 8] = js.appliedTorque.x;
|
|
713
|
+
outBuffer[offset + 9] = js.appliedTorque.y;
|
|
714
|
+
outBuffer[offset + 10] = js.appliedTorque.z;
|
|
715
|
+
outBuffer[offset + 11] = js.currentErrorAngle;
|
|
716
|
+
outBuffer[offset + 12] = js.saturated ? 1.0 : 0.0;
|
|
717
|
+
outBuffer[offset + 13] = js.internalWork;
|
|
718
|
+
offset += kPoweredJointStateStride;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
outWritten = needed;
|
|
722
|
+
return PoweredRagdollStatus::Success;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
PoweredRagdollStatus restorePoweredRagdollState(
|
|
726
|
+
std::span<const double> inBuffer,
|
|
727
|
+
PoweredRagdollState& outState) {
|
|
728
|
+
if (inBuffer.size() < kPoweredRagdollStateHeaderStride) {
|
|
729
|
+
return PoweredRagdollStatus::InvalidState;
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
if (inBuffer[15] != kMagicNumber) {
|
|
733
|
+
return PoweredRagdollStatus::InvalidState;
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
outState.mode = static_cast<PoweredRagdollMode>(static_cast<std::uint32_t>(inBuffer[0]));
|
|
737
|
+
outState.orientation = static_cast<OrientationStatus>(static_cast<std::uint32_t>(inBuffer[1]));
|
|
738
|
+
outState.blendWeight = inBuffer[2];
|
|
739
|
+
outState.recoveryTime = inBuffer[3];
|
|
740
|
+
outState.settleTimer = inBuffer[4];
|
|
741
|
+
outState.totalKineticEnergy = inBuffer[5];
|
|
742
|
+
outState.lastHitImpulseMagnitude = inBuffer[6];
|
|
743
|
+
outState.lastHitBoneIndex = static_cast<std::uint32_t>(inBuffer[7]);
|
|
744
|
+
outState.fallTriggered = (inBuffer[8] != 0.0);
|
|
745
|
+
outState.settledTriggered = (inBuffer[9] != 0.0);
|
|
746
|
+
outState.recoveryCompleted = (inBuffer[10] != 0.0);
|
|
747
|
+
outState.jointCount = static_cast<std::uint32_t>(inBuffer[11]);
|
|
748
|
+
|
|
749
|
+
const std::uint32_t cappedJoints = std::min(outState.jointCount, kPoweredRagdollMaxJoints);
|
|
750
|
+
const std::uint32_t needed = kPoweredRagdollStateHeaderStride + cappedJoints * kPoweredJointStateStride;
|
|
751
|
+
if (inBuffer.size() < needed) {
|
|
752
|
+
return PoweredRagdollStatus::InvalidState;
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
std::uint32_t offset = kPoweredRagdollStateHeaderStride;
|
|
756
|
+
for (std::uint32_t j = 0; j < cappedJoints; ++j) {
|
|
757
|
+
PoweredJointState& js = outState.joints[j];
|
|
758
|
+
js.jointIndex = static_cast<std::uint32_t>(inBuffer[offset + 0]);
|
|
759
|
+
js.targetLocalRotation = {
|
|
760
|
+
inBuffer[offset + 1], inBuffer[offset + 2], inBuffer[offset + 3], inBuffer[offset + 4]
|
|
761
|
+
};
|
|
762
|
+
js.targetAngularVelocity = {
|
|
763
|
+
inBuffer[offset + 5], inBuffer[offset + 6], inBuffer[offset + 7]
|
|
764
|
+
};
|
|
765
|
+
js.appliedTorque = {
|
|
766
|
+
inBuffer[offset + 8], inBuffer[offset + 9], inBuffer[offset + 10]
|
|
767
|
+
};
|
|
768
|
+
js.currentErrorAngle = inBuffer[offset + 11];
|
|
769
|
+
js.saturated = (inBuffer[offset + 12] != 0.0);
|
|
770
|
+
js.internalWork = inBuffer[offset + 13];
|
|
771
|
+
offset += kPoweredJointStateStride;
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
return PoweredRagdollStatus::Success;
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
} // namespace rundot::kinetix::physics
|
|
778
|
+
|
|
779
|
+
// ===========================================================================
|
|
780
|
+
// C-Compatible ABI Exports
|
|
781
|
+
// ===========================================================================
|
|
782
|
+
|
|
783
|
+
extern "C" {
|
|
784
|
+
|
|
785
|
+
using namespace rundot::kinetix::physics;
|
|
786
|
+
|
|
787
|
+
std::uint32_t rundot_kinetix_ext_powered_ragdoll_abi_version() {
|
|
788
|
+
return kPoweredRagdollAbiVersion;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
std::uint32_t rundot_kinetix_ext_powered_ragdoll_config_stride() {
|
|
792
|
+
return kPoweredRagdollConfigStride;
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
std::uint32_t rundot_kinetix_ext_powered_ragdoll_joint_config_stride() {
|
|
796
|
+
return kPoweredJointConfigStride;
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
std::uint32_t rundot_kinetix_ext_powered_ragdoll_joint_state_stride() {
|
|
800
|
+
return kPoweredJointStateStride;
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
std::uint32_t rundot_kinetix_ext_powered_ragdoll_hit_stride() {
|
|
804
|
+
return kPoweredRagdollHitStride;
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
std::uint32_t rundot_kinetix_ext_powered_ragdoll_transform_stride() {
|
|
808
|
+
return kPoweredRagdollTransformStride;
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
std::uint32_t rundot_kinetix_ext_powered_ragdoll_state_size(std::uint32_t jointCount) {
|
|
812
|
+
return poweredRagdollSerializedStateSize(jointCount);
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
std::int32_t rundot_kinetix_ext_powered_ragdoll_step(
|
|
816
|
+
const double* configData,
|
|
817
|
+
const double* jointConfigsData,
|
|
818
|
+
std::uint32_t jointCount,
|
|
819
|
+
double* stateData,
|
|
820
|
+
const double* targetPosesData,
|
|
821
|
+
std::uint32_t boneCount,
|
|
822
|
+
double* bodyData,
|
|
823
|
+
std::uint32_t bodyCount,
|
|
824
|
+
double dt) {
|
|
825
|
+
if (configData == nullptr || stateData == nullptr || bodyData == nullptr || bodyCount == 0) {
|
|
826
|
+
return static_cast<std::int32_t>(PoweredRagdollStatus::InvalidInput);
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
PoweredRagdollConfig config{};
|
|
830
|
+
config.boneCount = boneCount;
|
|
831
|
+
config.jointCount = std::min(jointCount, kPoweredRagdollMaxJoints);
|
|
832
|
+
config.globalBlendWeight = configData[2];
|
|
833
|
+
config.recovery.fallImpulseThreshold = configData[3];
|
|
834
|
+
config.recovery.fallInstabilityAngle = configData[4];
|
|
835
|
+
config.recovery.settleLinearVelocity = configData[5];
|
|
836
|
+
config.recovery.settleAngularVelocity = configData[6];
|
|
837
|
+
config.recovery.settleTimeRequired = configData[7];
|
|
838
|
+
config.recovery.recoveryDuration = configData[8];
|
|
839
|
+
config.recovery.blendCurve = static_cast<BlendCurve>(static_cast<std::uint32_t>(configData[9]));
|
|
840
|
+
config.recovery.autoRecover = (configData[10] != 0.0);
|
|
841
|
+
config.pelvisBoneIndex = static_cast<std::uint32_t>(configData[11]);
|
|
842
|
+
config.chestBoneIndex = static_cast<std::uint32_t>(configData[12]);
|
|
843
|
+
config.upVector = {configData[13], configData[14], configData[15]};
|
|
844
|
+
|
|
845
|
+
if (jointConfigsData != nullptr) {
|
|
846
|
+
for (std::uint32_t j = 0; j < config.jointCount; ++j) {
|
|
847
|
+
const std::uint32_t offset = j * kPoweredJointConfigStride;
|
|
848
|
+
PoweredJointDriveConfig& jcfg = config.joints[j];
|
|
849
|
+
jcfg.jointIndex = static_cast<std::uint32_t>(jointConfigsData[offset + 0]);
|
|
850
|
+
jcfg.parentBoneIndex = static_cast<std::uint32_t>(jointConfigsData[offset + 1]);
|
|
851
|
+
jcfg.childBoneIndex = static_cast<std::uint32_t>(jointConfigsData[offset + 2]);
|
|
852
|
+
jcfg.kp = jointConfigsData[offset + 3];
|
|
853
|
+
jcfg.kd = jointConfigsData[offset + 4];
|
|
854
|
+
jcfg.maxTorque = jointConfigsData[offset + 5];
|
|
855
|
+
jcfg.torqueCapMode = static_cast<TorqueCapMode>(static_cast<std::uint32_t>(jointConfigsData[offset + 6]));
|
|
856
|
+
jcfg.maxTorqueAxis = {
|
|
857
|
+
jointConfigsData[offset + 7], jointConfigsData[offset + 8], jointConfigsData[offset + 9]
|
|
858
|
+
};
|
|
859
|
+
jcfg.enabled = true;
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
PoweredRagdollState state{};
|
|
864
|
+
const std::uint32_t stateBufferSize = poweredRagdollSerializedStateSize(config.jointCount);
|
|
865
|
+
PoweredRagdollStatus restoreStatus = restorePoweredRagdollState(
|
|
866
|
+
std::span<const double>(stateData, stateBufferSize), state);
|
|
867
|
+
if (restoreStatus != PoweredRagdollStatus::Success) {
|
|
868
|
+
// Initialize fresh state if magic check fails
|
|
869
|
+
state.mode = PoweredRagdollMode::Powered;
|
|
870
|
+
state.blendWeight = config.globalBlendWeight;
|
|
871
|
+
state.jointCount = config.jointCount;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
AnimationTargetPose targetPose{};
|
|
875
|
+
targetPose.boneCount = std::min(boneCount, kPoweredRagdollMaxBones);
|
|
876
|
+
if (targetPosesData != nullptr) {
|
|
877
|
+
for (std::uint32_t b = 0; b < targetPose.boneCount; ++b) {
|
|
878
|
+
const std::uint32_t offset = b * kPoweredRagdollTransformStride;
|
|
879
|
+
targetPose.localPoses[b].position = {
|
|
880
|
+
targetPosesData[offset + 0], targetPosesData[offset + 1], targetPosesData[offset + 2]
|
|
881
|
+
};
|
|
882
|
+
targetPose.localPoses[b].orientation = {
|
|
883
|
+
targetPosesData[offset + 3], targetPosesData[offset + 4], targetPosesData[offset + 5], targetPosesData[offset + 6]
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
// Unpack bodies into local fixed array
|
|
889
|
+
const std::uint32_t activeBodyCount = std::min(bodyCount, kPoweredRagdollMaxBones);
|
|
890
|
+
std::array<Body, kPoweredRagdollMaxBones> localBodies{};
|
|
891
|
+
for (std::uint32_t b = 0; b < activeBodyCount; ++b) {
|
|
892
|
+
localBodies[b] = unpackBodyRecord(bodyData, b);
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
RagdollSkeletonDef skeleton{};
|
|
896
|
+
skeleton.boneCount = activeBodyCount;
|
|
897
|
+
|
|
898
|
+
const PoweredRagdollStatus status = stepPoweredRagdoll(
|
|
899
|
+
skeleton,
|
|
900
|
+
config,
|
|
901
|
+
targetPosesData != nullptr ? &targetPose : nullptr,
|
|
902
|
+
std::span<Body>(localBodies.data(), activeBodyCount),
|
|
903
|
+
dt,
|
|
904
|
+
state);
|
|
905
|
+
|
|
906
|
+
// Write updated bodies back to bodyData
|
|
907
|
+
for (std::uint32_t b = 0; b < activeBodyCount; ++b) {
|
|
908
|
+
packBodyRecord(bodyData, b, localBodies[b]);
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
// Write updated state back to stateData
|
|
912
|
+
std::uint32_t written = 0;
|
|
913
|
+
capturePoweredRagdollState(state, std::span<double>(stateData, stateBufferSize), written);
|
|
914
|
+
|
|
915
|
+
return static_cast<std::int32_t>(status);
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
std::int32_t rundot_kinetix_ext_powered_ragdoll_apply_hit(
|
|
919
|
+
const double* configData,
|
|
920
|
+
const double* skeletonData,
|
|
921
|
+
std::uint32_t boneCount,
|
|
922
|
+
double* stateData,
|
|
923
|
+
const double* hitData,
|
|
924
|
+
double* bodyData,
|
|
925
|
+
std::uint32_t bodyCount) {
|
|
926
|
+
if (configData == nullptr || stateData == nullptr || hitData == nullptr || bodyData == nullptr || bodyCount == 0) {
|
|
927
|
+
return static_cast<std::int32_t>(PoweredRagdollStatus::InvalidInput);
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
PoweredRagdollConfig config{};
|
|
931
|
+
config.boneCount = boneCount;
|
|
932
|
+
config.recovery.fallImpulseThreshold = configData[3];
|
|
933
|
+
|
|
934
|
+
HitImpulse hit{};
|
|
935
|
+
hit.boneIndex = static_cast<std::uint32_t>(hitData[0]);
|
|
936
|
+
hit.point = {hitData[1], hitData[2], hitData[3]};
|
|
937
|
+
hit.impulse = {hitData[4], hitData[5], hitData[6]};
|
|
938
|
+
hit.propagationFactor = hitData[7];
|
|
939
|
+
hit.propagate = (hitData[7] > 0.0);
|
|
940
|
+
|
|
941
|
+
RagdollSkeletonDef skeleton{};
|
|
942
|
+
skeleton.boneCount = std::min(boneCount, kPoweredRagdollMaxBones);
|
|
943
|
+
if (skeletonData != nullptr) {
|
|
944
|
+
for (std::uint32_t b = 0; b < skeleton.boneCount; ++b) {
|
|
945
|
+
const std::uint32_t offset = b * kRagdollBoneRecordStride;
|
|
946
|
+
skeleton.bones[b].parentIndex = static_cast<std::int32_t>(skeletonData[offset + 1]);
|
|
947
|
+
skeleton.bones[b].joint.anchorA = {skeletonData[offset + 14], skeletonData[offset + 15], skeletonData[offset + 16]};
|
|
948
|
+
skeleton.bones[b].joint.anchorB = {skeletonData[offset + 17], skeletonData[offset + 18], skeletonData[offset + 19]};
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
const std::uint32_t activeBodyCount = std::min(bodyCount, kPoweredRagdollMaxBones);
|
|
953
|
+
std::array<Body, kPoweredRagdollMaxBones> localBodies{};
|
|
954
|
+
for (std::uint32_t b = 0; b < activeBodyCount; ++b) {
|
|
955
|
+
localBodies[b] = unpackBodyRecord(bodyData, b);
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
PoweredRagdollState state{};
|
|
959
|
+
const std::uint32_t stateBufferSize = poweredRagdollSerializedStateSize(state.jointCount);
|
|
960
|
+
restorePoweredRagdollState(std::span<const double>(stateData, stateBufferSize), state);
|
|
961
|
+
|
|
962
|
+
const PoweredRagdollStatus status = applyHitImpulseToSkeleton(
|
|
963
|
+
skeleton,
|
|
964
|
+
std::span<Body>(localBodies.data(), activeBodyCount),
|
|
965
|
+
hit,
|
|
966
|
+
config,
|
|
967
|
+
state);
|
|
968
|
+
|
|
969
|
+
for (std::uint32_t b = 0; b < activeBodyCount; ++b) {
|
|
970
|
+
packBodyRecord(bodyData, b, localBodies[b]);
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
std::uint32_t written = 0;
|
|
974
|
+
capturePoweredRagdollState(state, std::span<double>(stateData, stateBufferSize), written);
|
|
975
|
+
|
|
976
|
+
return static_cast<std::int32_t>(status);
|
|
977
|
+
}
|
|
978
|
+
|
|
979
|
+
std::int32_t rundot_kinetix_ext_powered_ragdoll_blend_pose(
|
|
980
|
+
const double* physPosesData,
|
|
981
|
+
const double* animPosesData,
|
|
982
|
+
double blendWeight,
|
|
983
|
+
std::uint32_t boneCount,
|
|
984
|
+
double* outBlendedPosesData) {
|
|
985
|
+
if (physPosesData == nullptr || animPosesData == nullptr || outBlendedPosesData == nullptr || boneCount == 0) {
|
|
986
|
+
return static_cast<std::int32_t>(PoweredRagdollStatus::InvalidInput);
|
|
987
|
+
}
|
|
988
|
+
|
|
989
|
+
const std::uint32_t cappedBones = std::min(boneCount, kPoweredRagdollMaxBones);
|
|
990
|
+
std::array<PoweredTransform, kPoweredRagdollMaxBones> physTransforms{};
|
|
991
|
+
std::array<PoweredTransform, kPoweredRagdollMaxBones> animTransforms{};
|
|
992
|
+
std::array<PoweredTransform, kPoweredRagdollMaxBones> blendedTransforms{};
|
|
993
|
+
|
|
994
|
+
for (std::uint32_t b = 0; b < cappedBones; ++b) {
|
|
995
|
+
const std::uint32_t offset = b * kPoweredRagdollTransformStride;
|
|
996
|
+
physTransforms[b].position = {physPosesData[offset + 0], physPosesData[offset + 1], physPosesData[offset + 2]};
|
|
997
|
+
physTransforms[b].orientation = {physPosesData[offset + 3], physPosesData[offset + 4], physPosesData[offset + 5], physPosesData[offset + 6]};
|
|
998
|
+
|
|
999
|
+
animTransforms[b].position = {animPosesData[offset + 0], animPosesData[offset + 1], animPosesData[offset + 2]};
|
|
1000
|
+
animTransforms[b].orientation = {animPosesData[offset + 3], animPosesData[offset + 4], animPosesData[offset + 5], animPosesData[offset + 6]};
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
const PoweredRagdollStatus status = blendRagdollPoses(
|
|
1004
|
+
std::span<const PoweredTransform>(physTransforms.data(), cappedBones),
|
|
1005
|
+
std::span<const PoweredTransform>(animTransforms.data(), cappedBones),
|
|
1006
|
+
blendWeight,
|
|
1007
|
+
std::span<PoweredTransform>(blendedTransforms.data(), cappedBones));
|
|
1008
|
+
|
|
1009
|
+
for (std::uint32_t b = 0; b < cappedBones; ++b) {
|
|
1010
|
+
const std::uint32_t offset = b * kPoweredRagdollTransformStride;
|
|
1011
|
+
outBlendedPosesData[offset + 0] = blendedTransforms[b].position.x;
|
|
1012
|
+
outBlendedPosesData[offset + 1] = blendedTransforms[b].position.y;
|
|
1013
|
+
outBlendedPosesData[offset + 2] = blendedTransforms[b].position.z;
|
|
1014
|
+
outBlendedPosesData[offset + 3] = blendedTransforms[b].orientation.x;
|
|
1015
|
+
outBlendedPosesData[offset + 4] = blendedTransforms[b].orientation.y;
|
|
1016
|
+
outBlendedPosesData[offset + 5] = blendedTransforms[b].orientation.z;
|
|
1017
|
+
outBlendedPosesData[offset + 6] = blendedTransforms[b].orientation.w;
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
return static_cast<std::int32_t>(status);
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
std::int32_t rundot_kinetix_ext_powered_ragdoll_recover_pose(
|
|
1024
|
+
const double* configData,
|
|
1025
|
+
double* stateData) {
|
|
1026
|
+
if (configData == nullptr || stateData == nullptr) {
|
|
1027
|
+
return static_cast<std::int32_t>(PoweredRagdollStatus::InvalidInput);
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
PoweredRagdollConfig config{};
|
|
1031
|
+
config.recovery.recoveryDuration = configData[8];
|
|
1032
|
+
config.recovery.blendCurve = static_cast<BlendCurve>(static_cast<std::uint32_t>(configData[9]));
|
|
1033
|
+
|
|
1034
|
+
PoweredRagdollState state{};
|
|
1035
|
+
const std::uint32_t stateBufferSize = poweredRagdollSerializedStateSize(state.jointCount);
|
|
1036
|
+
restorePoweredRagdollState(std::span<const double>(stateData, stateBufferSize), state);
|
|
1037
|
+
|
|
1038
|
+
const PoweredRagdollStatus status = startPoseRecovery(state, config);
|
|
1039
|
+
|
|
1040
|
+
std::uint32_t written = 0;
|
|
1041
|
+
capturePoweredRagdollState(state, std::span<double>(stateData, stateBufferSize), written);
|
|
1042
|
+
|
|
1043
|
+
return static_cast<std::int32_t>(status);
|
|
1044
|
+
}
|
|
1045
|
+
|
|
1046
|
+
std::int32_t rundot_kinetix_ext_powered_ragdoll_capture_state(
|
|
1047
|
+
const double* stateData,
|
|
1048
|
+
std::uint32_t jointCount,
|
|
1049
|
+
double* outBuffer,
|
|
1050
|
+
std::uint32_t bufferCapacity,
|
|
1051
|
+
std::uint32_t* outWritten) {
|
|
1052
|
+
if (stateData == nullptr || outBuffer == nullptr || outWritten == nullptr) {
|
|
1053
|
+
return static_cast<std::int32_t>(PoweredRagdollStatus::InvalidInput);
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
const std::uint32_t needed = poweredRagdollSerializedStateSize(jointCount);
|
|
1057
|
+
if (bufferCapacity < needed) {
|
|
1058
|
+
return static_cast<std::int32_t>(PoweredRagdollStatus::BufferOverflow);
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
PoweredRagdollState state{};
|
|
1062
|
+
const PoweredRagdollStatus rStatus = restorePoweredRagdollState(
|
|
1063
|
+
std::span<const double>(stateData, needed), state);
|
|
1064
|
+
if (rStatus != PoweredRagdollStatus::Success) {
|
|
1065
|
+
return static_cast<std::int32_t>(rStatus);
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
const PoweredRagdollStatus cStatus = capturePoweredRagdollState(
|
|
1069
|
+
state, std::span<double>(outBuffer, bufferCapacity), *outWritten);
|
|
1070
|
+
return static_cast<std::int32_t>(cStatus);
|
|
1071
|
+
}
|
|
1072
|
+
|
|
1073
|
+
std::int32_t rundot_kinetix_ext_powered_ragdoll_restore_state(
|
|
1074
|
+
const double* inBuffer,
|
|
1075
|
+
std::uint32_t bufferSize,
|
|
1076
|
+
double* outStateData,
|
|
1077
|
+
std::uint32_t jointCount) {
|
|
1078
|
+
if (inBuffer == nullptr || outStateData == nullptr) {
|
|
1079
|
+
return static_cast<std::int32_t>(PoweredRagdollStatus::InvalidInput);
|
|
1080
|
+
}
|
|
1081
|
+
|
|
1082
|
+
const std::uint32_t needed = poweredRagdollSerializedStateSize(jointCount);
|
|
1083
|
+
if (bufferSize < needed) {
|
|
1084
|
+
return static_cast<std::int32_t>(PoweredRagdollStatus::BufferOverflow);
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
PoweredRagdollState state{};
|
|
1088
|
+
const PoweredRagdollStatus rStatus = restorePoweredRagdollState(
|
|
1089
|
+
std::span<const double>(inBuffer, bufferSize), state);
|
|
1090
|
+
if (rStatus != PoweredRagdollStatus::Success) {
|
|
1091
|
+
return static_cast<std::int32_t>(rStatus);
|
|
1092
|
+
}
|
|
1093
|
+
|
|
1094
|
+
std::uint32_t written = 0;
|
|
1095
|
+
const PoweredRagdollStatus cStatus = capturePoweredRagdollState(
|
|
1096
|
+
state, std::span<double>(outStateData, needed), written);
|
|
1097
|
+
return static_cast<std::int32_t>(cStatus);
|
|
1098
|
+
}
|
|
1099
|
+
|
|
1100
|
+
} // extern "C"
|