@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,1449 @@
|
|
|
1
|
+
#include "soft_body.hpp"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <cmath>
|
|
5
|
+
#include <cstring>
|
|
6
|
+
|
|
7
|
+
namespace rundot::kinetix::physics {
|
|
8
|
+
|
|
9
|
+
namespace {
|
|
10
|
+
|
|
11
|
+
inline double softBodySqrt(double value) {
|
|
12
|
+
if (value <= 0.0) return 0.0;
|
|
13
|
+
return std::sqrt(value);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
inline double softBodyClamp(double value, double minVal, double maxVal) {
|
|
17
|
+
if (value < minVal) return minVal;
|
|
18
|
+
if (value > maxVal) return maxVal;
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Static buffer for unpacking bodies in raw ABI calls
|
|
23
|
+
constexpr std::uint32_t kMaxRawBodies = 64;
|
|
24
|
+
static std::array<Body, kMaxRawBodies> sRawBodies{};
|
|
25
|
+
|
|
26
|
+
} // namespace
|
|
27
|
+
|
|
28
|
+
SoftBodyVec3 addSoftBodyVec3(const SoftBodyVec3& a, const SoftBodyVec3& b) {
|
|
29
|
+
return {a.x + b.x, a.y + b.y, a.z + b.z};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
SoftBodyVec3 subSoftBodyVec3(const SoftBodyVec3& a, const SoftBodyVec3& b) {
|
|
33
|
+
return {a.x - b.x, a.y - b.y, a.z - b.z};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
SoftBodyVec3 scaleSoftBodyVec3(const SoftBodyVec3& v, double s) {
|
|
37
|
+
return {v.x * s, v.y * s, v.z * s};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
double dotSoftBodyVec3(const SoftBodyVec3& a, const SoftBodyVec3& b) {
|
|
41
|
+
return a.x * b.x + a.y * b.y + a.z * b.z;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
SoftBodyVec3 crossSoftBodyVec3(const SoftBodyVec3& a, const SoftBodyVec3& b) {
|
|
45
|
+
return {
|
|
46
|
+
a.y * b.z - a.z * b.y,
|
|
47
|
+
a.z * b.x - a.x * b.z,
|
|
48
|
+
a.x * b.y - a.y * b.x,
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
double lengthSquaredSoftBodyVec3(const SoftBodyVec3& v) {
|
|
53
|
+
return dotSoftBodyVec3(v, v);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
double lengthSoftBodyVec3(const SoftBodyVec3& v) {
|
|
57
|
+
return softBodySqrt(lengthSquaredSoftBodyVec3(v));
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
SoftBodyVec3 normalizeSoftBodyVec3(const SoftBodyVec3& v) {
|
|
61
|
+
const double len = lengthSoftBodyVec3(v);
|
|
62
|
+
if (len <= 1e-15) return {0.0, 0.0, 0.0};
|
|
63
|
+
return scaleSoftBodyVec3(v, 1.0 / len);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
SoftBodyVec3 rotateByQuat(const SoftBodyQuat& q, const SoftBodyVec3& v) {
|
|
67
|
+
const SoftBodyVec3 u{q.x, q.y, q.z};
|
|
68
|
+
const SoftBodyVec3 uv = crossSoftBodyVec3(u, v);
|
|
69
|
+
const SoftBodyVec3 uuv = crossSoftBodyVec3(u, uv);
|
|
70
|
+
return addSoftBodyVec3(v, addSoftBodyVec3(scaleSoftBodyVec3(uv, 2.0 * q.w), scaleSoftBodyVec3(uuv, 2.0)));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
SoftBodyVec3 mat3MultiplyVec3(const SoftBodyMat3& m, const SoftBodyVec3& v) {
|
|
74
|
+
return {
|
|
75
|
+
m.m[0][0] * v.x + m.m[0][1] * v.y + m.m[0][2] * v.z,
|
|
76
|
+
m.m[1][0] * v.x + m.m[1][1] * v.y + m.m[1][2] * v.z,
|
|
77
|
+
m.m[2][0] * v.x + m.m[2][1] * v.y + m.m[2][2] * v.z,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
SoftBodyMat3 mat3Multiply(const SoftBodyMat3& a, const SoftBodyMat3& b) {
|
|
82
|
+
SoftBodyMat3 result{};
|
|
83
|
+
for (int r = 0; r < 3; ++r) {
|
|
84
|
+
for (int c = 0; c < 3; ++c) {
|
|
85
|
+
result.m[r][c] = a.m[r][0] * b.m[0][c]
|
|
86
|
+
+ a.m[r][1] * b.m[1][c]
|
|
87
|
+
+ a.m[r][2] * b.m[2][c];
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return result;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
SoftBodyMat3 mat3Transpose(const SoftBodyMat3& m) {
|
|
94
|
+
SoftBodyMat3 result{};
|
|
95
|
+
for (int r = 0; r < 3; ++r) {
|
|
96
|
+
for (int c = 0; c < 3; ++c) {
|
|
97
|
+
result.m[r][c] = m.m[c][r];
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
double mat3Determinant(const SoftBodyMat3& m) {
|
|
104
|
+
return m.m[0][0] * (m.m[1][1] * m.m[2][2] - m.m[1][2] * m.m[2][1])
|
|
105
|
+
- m.m[0][1] * (m.m[1][0] * m.m[2][2] - m.m[1][2] * m.m[2][0])
|
|
106
|
+
+ m.m[0][2] * (m.m[1][0] * m.m[2][1] - m.m[1][1] * m.m[2][0]);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
bool mat3Inverse(const SoftBodyMat3& m, SoftBodyMat3& outInv) {
|
|
110
|
+
const double det = mat3Determinant(m);
|
|
111
|
+
if (std::abs(det) < 1e-15) {
|
|
112
|
+
outInv = SoftBodyMat3::identity();
|
|
113
|
+
return false;
|
|
114
|
+
}
|
|
115
|
+
const double invDet = 1.0 / det;
|
|
116
|
+
|
|
117
|
+
outInv.m[0][0] = (m.m[1][1] * m.m[2][2] - m.m[1][2] * m.m[2][1]) * invDet;
|
|
118
|
+
outInv.m[0][1] = -(m.m[0][1] * m.m[2][2] - m.m[0][2] * m.m[2][1]) * invDet;
|
|
119
|
+
outInv.m[0][2] = (m.m[0][1] * m.m[1][2] - m.m[0][2] * m.m[1][1]) * invDet;
|
|
120
|
+
|
|
121
|
+
outInv.m[1][0] = -(m.m[1][0] * m.m[2][2] - m.m[1][2] * m.m[2][0]) * invDet;
|
|
122
|
+
outInv.m[1][1] = (m.m[0][0] * m.m[2][2] - m.m[0][2] * m.m[2][0]) * invDet;
|
|
123
|
+
outInv.m[1][2] = -(m.m[0][0] * m.m[1][2] - m.m[0][2] * m.m[1][0]) * invDet;
|
|
124
|
+
|
|
125
|
+
outInv.m[2][0] = (m.m[1][0] * m.m[2][1] - m.m[1][1] * m.m[2][0]) * invDet;
|
|
126
|
+
outInv.m[2][1] = -(m.m[0][0] * m.m[2][1] - m.m[0][1] * m.m[2][0]) * invDet;
|
|
127
|
+
outInv.m[2][2] = (m.m[0][0] * m.m[1][1] - m.m[0][1] * m.m[1][0]) * invDet;
|
|
128
|
+
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
SoftBodyMat3 extractRotationPolar(const SoftBodyMat3& a) {
|
|
133
|
+
SoftBodyMat3 R = a;
|
|
134
|
+
const double det = mat3Determinant(R);
|
|
135
|
+
if (std::abs(det) < 1e-15) {
|
|
136
|
+
return SoftBodyMat3::identity();
|
|
137
|
+
}
|
|
138
|
+
if (det < 0.0) {
|
|
139
|
+
R.m[0][2] = -R.m[0][2];
|
|
140
|
+
R.m[1][2] = -R.m[1][2];
|
|
141
|
+
R.m[2][2] = -R.m[2][2];
|
|
142
|
+
}
|
|
143
|
+
for (int iter = 0; iter < 10; ++iter) {
|
|
144
|
+
SoftBodyMat3 Rinv{};
|
|
145
|
+
if (!mat3Inverse(R, Rinv)) {
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
SoftBodyMat3 RinvT = mat3Transpose(Rinv);
|
|
149
|
+
SoftBodyMat3 Rnext{};
|
|
150
|
+
double maxDiff = 0.0;
|
|
151
|
+
for (int r = 0; r < 3; ++r) {
|
|
152
|
+
for (int c = 0; c < 3; ++c) {
|
|
153
|
+
Rnext.m[r][c] = 0.5 * (R.m[r][c] + RinvT.m[r][c]);
|
|
154
|
+
const double diff = std::abs(Rnext.m[r][c] - R.m[r][c]);
|
|
155
|
+
if (diff > maxDiff) maxDiff = diff;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
R = Rnext;
|
|
159
|
+
if (maxDiff < 1e-9) break;
|
|
160
|
+
}
|
|
161
|
+
return R;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
double computeTetVolume(
|
|
165
|
+
const SoftBodyVec3& p0,
|
|
166
|
+
const SoftBodyVec3& p1,
|
|
167
|
+
const SoftBodyVec3& p2,
|
|
168
|
+
const SoftBodyVec3& p3) {
|
|
169
|
+
const SoftBodyVec3 d1 = subSoftBodyVec3(p1, p0);
|
|
170
|
+
const SoftBodyVec3 d2 = subSoftBodyVec3(p2, p0);
|
|
171
|
+
const SoftBodyVec3 d3 = subSoftBodyVec3(p3, p0);
|
|
172
|
+
return dotSoftBodyVec3(d1, crossSoftBodyVec3(d2, d3)) / 6.0;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
void computeTetGradients(
|
|
176
|
+
const SoftBodyVec3& p0,
|
|
177
|
+
const SoftBodyVec3& p1,
|
|
178
|
+
const SoftBodyVec3& p2,
|
|
179
|
+
const SoftBodyVec3& p3,
|
|
180
|
+
SoftBodyVec3& grad0,
|
|
181
|
+
SoftBodyVec3& grad1,
|
|
182
|
+
SoftBodyVec3& grad2,
|
|
183
|
+
SoftBodyVec3& grad3) {
|
|
184
|
+
const SoftBodyVec3 d1 = subSoftBodyVec3(p1, p0);
|
|
185
|
+
const SoftBodyVec3 d2 = subSoftBodyVec3(p2, p0);
|
|
186
|
+
const SoftBodyVec3 d3 = subSoftBodyVec3(p3, p0);
|
|
187
|
+
|
|
188
|
+
grad1 = scaleSoftBodyVec3(crossSoftBodyVec3(d2, d3), 1.0 / 6.0);
|
|
189
|
+
grad2 = scaleSoftBodyVec3(crossSoftBodyVec3(d3, d1), 1.0 / 6.0);
|
|
190
|
+
grad3 = scaleSoftBodyVec3(crossSoftBodyVec3(d1, d2), 1.0 / 6.0);
|
|
191
|
+
grad0 = subSoftBodyVec3(subSoftBodyVec3(scaleSoftBodyVec3(grad1, -1.0), grad2), grad3);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
double computeTotalEnclosedVolume(const SoftBodySimulation& sim) {
|
|
195
|
+
if (sim.faceCount > 0) {
|
|
196
|
+
// Gauss divergence theorem on closed boundary surface: V = 1/6 * sum x_A . (x_B x x_C)
|
|
197
|
+
double sum = 0.0;
|
|
198
|
+
for (std::uint32_t f = 0; f < sim.faceCount; ++f) {
|
|
199
|
+
const auto& face = sim.faces[f];
|
|
200
|
+
const SoftBodyVec3& a = sim.nodes[face.nodeA].position;
|
|
201
|
+
const SoftBodyVec3& b = sim.nodes[face.nodeB].position;
|
|
202
|
+
const SoftBodyVec3& c = sim.nodes[face.nodeC].position;
|
|
203
|
+
sum += dotSoftBodyVec3(a, crossSoftBodyVec3(b, c));
|
|
204
|
+
}
|
|
205
|
+
return sum / 6.0;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Fallback: sum of tetrahedral volumes
|
|
209
|
+
double total = 0.0;
|
|
210
|
+
for (std::uint32_t t = 0; t < sim.tetCount; ++t) {
|
|
211
|
+
const auto& tet = sim.tetrahedra[t];
|
|
212
|
+
total += computeTetVolume(
|
|
213
|
+
sim.nodes[tet.node0].position,
|
|
214
|
+
sim.nodes[tet.node1].position,
|
|
215
|
+
sim.nodes[tet.node2].position,
|
|
216
|
+
sim.nodes[tet.node3].position);
|
|
217
|
+
}
|
|
218
|
+
return total;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
void computeFaceGeometry(
|
|
222
|
+
const SoftBodyVec3& pA,
|
|
223
|
+
const SoftBodyVec3& pB,
|
|
224
|
+
const SoftBodyVec3& pC,
|
|
225
|
+
double& outArea,
|
|
226
|
+
SoftBodyVec3& outNormal) {
|
|
227
|
+
const SoftBodyVec3 ab = subSoftBodyVec3(pB, pA);
|
|
228
|
+
const SoftBodyVec3 ac = subSoftBodyVec3(pC, pA);
|
|
229
|
+
const SoftBodyVec3 crossProd = crossSoftBodyVec3(ab, ac);
|
|
230
|
+
const double len = lengthSoftBodyVec3(crossProd);
|
|
231
|
+
outArea = 0.5 * len;
|
|
232
|
+
if (len > 1e-15) {
|
|
233
|
+
outNormal = scaleSoftBodyVec3(crossProd, 1.0 / len);
|
|
234
|
+
} else {
|
|
235
|
+
outNormal = {0.0, 1.0, 0.0};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
SoftBodyStatus initSoftBody(SoftBodySimulation& sim) {
|
|
240
|
+
if (sim.nodeCount < 4) return SoftBodyStatus::InvalidNodeCount;
|
|
241
|
+
if (sim.nodeCount > kSoftBodyMaxNodes || sim.tetCount > kSoftBodyMaxTetrahedra) {
|
|
242
|
+
return SoftBodyStatus::BufferOverflow;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// 1. Record rest positions and calculate initial center of mass
|
|
246
|
+
SoftBodyVec3 cm{0.0, 0.0, 0.0};
|
|
247
|
+
double totalMass = 0.0;
|
|
248
|
+
for (std::uint32_t i = 0; i < sim.nodeCount; ++i) {
|
|
249
|
+
sim.restPositions[i] = sim.nodes[i].position;
|
|
250
|
+
sim.nodes[i].previousPosition = sim.nodes[i].position;
|
|
251
|
+
const double mass = (sim.nodes[i].inverseMass > 0.0) ? (1.0 / sim.nodes[i].inverseMass) : 1.0;
|
|
252
|
+
cm = addSoftBodyVec3(cm, scaleSoftBodyVec3(sim.nodes[i].position, mass));
|
|
253
|
+
totalMass += mass;
|
|
254
|
+
}
|
|
255
|
+
if (totalMass > 1e-15) {
|
|
256
|
+
sim.restCenterOfMass = scaleSoftBodyVec3(cm, 1.0 / totalMass);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// 2. Initialize tetrahedra rest volumes and check initial orientation
|
|
260
|
+
sim.totalRestVolume = 0.0;
|
|
261
|
+
for (std::uint32_t t = 0; t < sim.tetCount; ++t) {
|
|
262
|
+
auto& tet = sim.tetrahedra[t];
|
|
263
|
+
double v = computeTetVolume(
|
|
264
|
+
sim.nodes[tet.node0].position,
|
|
265
|
+
sim.nodes[tet.node1].position,
|
|
266
|
+
sim.nodes[tet.node2].position,
|
|
267
|
+
sim.nodes[tet.node3].position);
|
|
268
|
+
|
|
269
|
+
if (v < 0.0) {
|
|
270
|
+
// Correct node winding so initial volume is positive
|
|
271
|
+
std::swap(tet.node1, tet.node2);
|
|
272
|
+
v = -v;
|
|
273
|
+
}
|
|
274
|
+
tet.restVolume = v;
|
|
275
|
+
tet.currentVolume = v;
|
|
276
|
+
tet.plasticVolumeStrain = 0.0;
|
|
277
|
+
tet.inverted = false;
|
|
278
|
+
sim.totalRestVolume += v;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// 3. Initialize boundary faces
|
|
282
|
+
for (std::uint32_t f = 0; f < sim.faceCount; ++f) {
|
|
283
|
+
auto& face = sim.faces[f];
|
|
284
|
+
computeFaceGeometry(
|
|
285
|
+
sim.nodes[face.nodeA].position,
|
|
286
|
+
sim.nodes[face.nodeB].position,
|
|
287
|
+
sim.nodes[face.nodeC].position,
|
|
288
|
+
face.area,
|
|
289
|
+
face.normal);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// 4. Initialize edges
|
|
293
|
+
for (std::uint32_t e = 0; e < sim.edgeCount; ++e) {
|
|
294
|
+
auto& edge = sim.edges[e];
|
|
295
|
+
const double len = lengthSoftBodyVec3(
|
|
296
|
+
subSoftBodyVec3(sim.nodes[edge.nodeA].position, sim.nodes[edge.nodeB].position));
|
|
297
|
+
edge.restLength = len;
|
|
298
|
+
edge.currentLength = len;
|
|
299
|
+
edge.plasticStrain = 0.0;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
sim.currentTotalVolume = sim.totalRestVolume;
|
|
303
|
+
sim.invertedCount = 0;
|
|
304
|
+
|
|
305
|
+
return SoftBodyStatus::Success;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
void handleInvertedTetrahedra(SoftBodySimulation& sim) {
|
|
309
|
+
sim.invertedCount = 0;
|
|
310
|
+
const double threshold = sim.params.inversionThreshold;
|
|
311
|
+
const double kRestoration = softBodyClamp(sim.params.inversionRestorationStiffness, 0.01, 1.0);
|
|
312
|
+
|
|
313
|
+
for (std::uint32_t t = 0; t < sim.tetCount; ++t) {
|
|
314
|
+
auto& tet = sim.tetrahedra[t];
|
|
315
|
+
auto& n0 = sim.nodes[tet.node0];
|
|
316
|
+
auto& n1 = sim.nodes[tet.node1];
|
|
317
|
+
auto& n2 = sim.nodes[tet.node2];
|
|
318
|
+
auto& n3 = sim.nodes[tet.node3];
|
|
319
|
+
|
|
320
|
+
tet.currentVolume = computeTetVolume(n0.position, n1.position, n2.position, n3.position);
|
|
321
|
+
|
|
322
|
+
const double minAllowed = (tet.restVolume > 0.0)
|
|
323
|
+
? std::max(threshold, 0.02 * tet.restVolume)
|
|
324
|
+
: threshold;
|
|
325
|
+
|
|
326
|
+
if (tet.currentVolume < minAllowed) {
|
|
327
|
+
tet.inverted = true;
|
|
328
|
+
sim.invertedCount++;
|
|
329
|
+
|
|
330
|
+
// Opposite face (0, 1, 2) relative to node 3
|
|
331
|
+
const SoftBodyVec3 p0 = n0.position;
|
|
332
|
+
const SoftBodyVec3 p1 = n1.position;
|
|
333
|
+
const SoftBodyVec3 p2 = n2.position;
|
|
334
|
+
const SoftBodyVec3 p3 = n3.position;
|
|
335
|
+
|
|
336
|
+
SoftBodyVec3 faceNormal = crossSoftBodyVec3(subSoftBodyVec3(p1, p0), subSoftBodyVec3(p2, p0));
|
|
337
|
+
const double faceAreaTimesTwo = lengthSoftBodyVec3(faceNormal);
|
|
338
|
+
|
|
339
|
+
if (faceAreaTimesTwo > 1e-12) {
|
|
340
|
+
const SoftBodyVec3 unitNormal = scaleSoftBodyVec3(faceNormal, 1.0 / faceAreaTimesTwo);
|
|
341
|
+
const double targetVol = (tet.restVolume > 0.0) ? (0.25 * tet.restVolume) : 0.01;
|
|
342
|
+
const double hCurrent = dotSoftBodyVec3(subSoftBodyVec3(p3, p0), unitNormal);
|
|
343
|
+
const double hTarget = 6.0 * targetVol / faceAreaTimesTwo;
|
|
344
|
+
const double deficit = hTarget - hCurrent;
|
|
345
|
+
|
|
346
|
+
if (deficit > 0.0) {
|
|
347
|
+
const double wOpp = n3.inverseMass;
|
|
348
|
+
const double wFace = (n0.inverseMass + n1.inverseMass + n2.inverseMass) / 3.0;
|
|
349
|
+
const double totalW = wOpp + wFace;
|
|
350
|
+
if (totalW > 1e-15) {
|
|
351
|
+
const double pushOpp = (wOpp / totalW) * deficit * kRestoration;
|
|
352
|
+
const double pushFace = (wFace / totalW) * deficit * kRestoration / 3.0;
|
|
353
|
+
|
|
354
|
+
n3.position = addSoftBodyVec3(n3.position, scaleSoftBodyVec3(unitNormal, pushOpp));
|
|
355
|
+
if (n0.inverseMass > 0.0) n0.position = subSoftBodyVec3(n0.position, scaleSoftBodyVec3(unitNormal, pushFace));
|
|
356
|
+
if (n1.inverseMass > 0.0) n1.position = subSoftBodyVec3(n1.position, scaleSoftBodyVec3(unitNormal, pushFace));
|
|
357
|
+
if (n2.inverseMass > 0.0) n2.position = subSoftBodyVec3(n2.position, scaleSoftBodyVec3(unitNormal, pushFace));
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
} else {
|
|
361
|
+
// Degenerate face: restore relative to element centroid
|
|
362
|
+
const SoftBodyVec3 curCentroid = scaleSoftBodyVec3(
|
|
363
|
+
addSoftBodyVec3(addSoftBodyVec3(p0, p1), addSoftBodyVec3(p2, p3)), 0.25);
|
|
364
|
+
const SoftBodyVec3 r0 = sim.restPositions[tet.node0];
|
|
365
|
+
const SoftBodyVec3 r1 = sim.restPositions[tet.node1];
|
|
366
|
+
const SoftBodyVec3 r2 = sim.restPositions[tet.node2];
|
|
367
|
+
const SoftBodyVec3 r3 = sim.restPositions[tet.node3];
|
|
368
|
+
const SoftBodyVec3 restCentroid = scaleSoftBodyVec3(
|
|
369
|
+
addSoftBodyVec3(addSoftBodyVec3(r0, r1), addSoftBodyVec3(r2, r3)), 0.25);
|
|
370
|
+
|
|
371
|
+
const SoftBodyVec3 disp0 = subSoftBodyVec3(r0, restCentroid);
|
|
372
|
+
const SoftBodyVec3 disp1 = subSoftBodyVec3(r1, restCentroid);
|
|
373
|
+
const SoftBodyVec3 disp2 = subSoftBodyVec3(r2, restCentroid);
|
|
374
|
+
const SoftBodyVec3 disp3 = subSoftBodyVec3(r3, restCentroid);
|
|
375
|
+
|
|
376
|
+
if (n0.inverseMass > 0.0) n0.position = addSoftBodyVec3(curCentroid, scaleSoftBodyVec3(disp0, 0.5 * kRestoration));
|
|
377
|
+
if (n1.inverseMass > 0.0) n1.position = addSoftBodyVec3(curCentroid, scaleSoftBodyVec3(disp1, 0.5 * kRestoration));
|
|
378
|
+
if (n2.inverseMass > 0.0) n2.position = addSoftBodyVec3(curCentroid, scaleSoftBodyVec3(disp2, 0.5 * kRestoration));
|
|
379
|
+
if (n3.inverseMass > 0.0) n3.position = addSoftBodyVec3(curCentroid, scaleSoftBodyVec3(disp3, 0.5 * kRestoration));
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
tet.currentVolume = computeTetVolume(n0.position, n1.position, n2.position, n3.position);
|
|
383
|
+
if (tet.currentVolume >= minAllowed) {
|
|
384
|
+
tet.inverted = false;
|
|
385
|
+
}
|
|
386
|
+
} else {
|
|
387
|
+
tet.inverted = false;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
void solveVolumePreservation(SoftBodySimulation& sim, double dt) {
|
|
393
|
+
if (dt <= 0.0 || sim.tetCount == 0) return;
|
|
394
|
+
const double dtSq = dt * dt;
|
|
395
|
+
const double bulkStiffness = sim.params.volumePreservationStiffness;
|
|
396
|
+
if (bulkStiffness <= 0.0) return;
|
|
397
|
+
const double compliance = 1.0 / (bulkStiffness * dtSq);
|
|
398
|
+
|
|
399
|
+
for (std::uint32_t t = 0; t < sim.tetCount; ++t) {
|
|
400
|
+
auto& tet = sim.tetrahedra[t];
|
|
401
|
+
if (tet.inverted) continue;
|
|
402
|
+
|
|
403
|
+
auto& n0 = sim.nodes[tet.node0];
|
|
404
|
+
auto& n1 = sim.nodes[tet.node1];
|
|
405
|
+
auto& n2 = sim.nodes[tet.node2];
|
|
406
|
+
auto& n3 = sim.nodes[tet.node3];
|
|
407
|
+
|
|
408
|
+
const double currentVol = computeTetVolume(n0.position, n1.position, n2.position, n3.position);
|
|
409
|
+
tet.currentVolume = currentVol;
|
|
410
|
+
|
|
411
|
+
const double targetVol = tet.restVolume + tet.plasticVolumeStrain;
|
|
412
|
+
const double C = currentVol - targetVol;
|
|
413
|
+
|
|
414
|
+
SoftBodyVec3 g0{}, g1{}, g2{}, g3{};
|
|
415
|
+
computeTetGradients(n0.position, n1.position, n2.position, n3.position, g0, g1, g2, g3);
|
|
416
|
+
|
|
417
|
+
const double w0 = n0.inverseMass;
|
|
418
|
+
const double w1 = n1.inverseMass;
|
|
419
|
+
const double w2 = n2.inverseMass;
|
|
420
|
+
const double w3 = n3.inverseMass;
|
|
421
|
+
|
|
422
|
+
const double denom = w0 * lengthSquaredSoftBodyVec3(g0)
|
|
423
|
+
+ w1 * lengthSquaredSoftBodyVec3(g1)
|
|
424
|
+
+ w2 * lengthSquaredSoftBodyVec3(g2)
|
|
425
|
+
+ w3 * lengthSquaredSoftBodyVec3(g3)
|
|
426
|
+
+ compliance;
|
|
427
|
+
|
|
428
|
+
if (denom > 1e-15) {
|
|
429
|
+
const double deltaLambda = -C / denom;
|
|
430
|
+
if (w0 > 0.0) n0.position = addSoftBodyVec3(n0.position, scaleSoftBodyVec3(g0, w0 * deltaLambda));
|
|
431
|
+
if (w1 > 0.0) n1.position = addSoftBodyVec3(n1.position, scaleSoftBodyVec3(g1, w1 * deltaLambda));
|
|
432
|
+
if (w2 > 0.0) n2.position = addSoftBodyVec3(n2.position, scaleSoftBodyVec3(g2, w2 * deltaLambda));
|
|
433
|
+
if (w3 > 0.0) n3.position = addSoftBodyVec3(n3.position, scaleSoftBodyVec3(g3, w3 * deltaLambda));
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
void solvePressure(SoftBodySimulation& sim, double dt) {
|
|
439
|
+
if (dt <= 0.0 || sim.faceCount == 0) return;
|
|
440
|
+
const double basePressure = sim.params.pressure;
|
|
441
|
+
const double pressureStiffness = sim.params.pressureStiffness;
|
|
442
|
+
|
|
443
|
+
const double currentVolume = computeTotalEnclosedVolume(sim);
|
|
444
|
+
sim.currentTotalVolume = currentVolume;
|
|
445
|
+
const double restVolume = (sim.totalRestVolume > 0.0) ? sim.totalRestVolume : 1.0;
|
|
446
|
+
|
|
447
|
+
// Gas overpressure law: P = P_base + k * (V_rest - V_cur) / V_rest
|
|
448
|
+
const double volumeCompressionRatio = softBodyClamp((restVolume - currentVolume) / restVolume, -0.9, 10.0);
|
|
449
|
+
const double effectivePressure = basePressure + pressureStiffness * volumeCompressionRatio;
|
|
450
|
+
|
|
451
|
+
if (std::abs(effectivePressure) < 1e-9) return;
|
|
452
|
+
|
|
453
|
+
const double dtSq = dt * dt;
|
|
454
|
+
|
|
455
|
+
for (std::uint32_t f = 0; f < sim.faceCount; ++f) {
|
|
456
|
+
auto& face = sim.faces[f];
|
|
457
|
+
auto& nA = sim.nodes[face.nodeA];
|
|
458
|
+
auto& nB = sim.nodes[face.nodeB];
|
|
459
|
+
auto& nC = sim.nodes[face.nodeC];
|
|
460
|
+
|
|
461
|
+
computeFaceGeometry(nA.position, nB.position, nC.position, face.area, face.normal);
|
|
462
|
+
|
|
463
|
+
const double facePressure = effectivePressure * face.pressureMultiplier;
|
|
464
|
+
const SoftBodyVec3 force = scaleSoftBodyVec3(face.normal, facePressure * face.area);
|
|
465
|
+
const SoftBodyVec3 forcePerVertex = scaleSoftBodyVec3(force, 1.0 / 3.0);
|
|
466
|
+
|
|
467
|
+
if (nA.inverseMass > 0.0) {
|
|
468
|
+
nA.position = addSoftBodyVec3(nA.position, scaleSoftBodyVec3(forcePerVertex, nA.inverseMass * dtSq));
|
|
469
|
+
}
|
|
470
|
+
if (nB.inverseMass > 0.0) {
|
|
471
|
+
nB.position = addSoftBodyVec3(nB.position, scaleSoftBodyVec3(forcePerVertex, nB.inverseMass * dtSq));
|
|
472
|
+
}
|
|
473
|
+
if (nC.inverseMass > 0.0) {
|
|
474
|
+
nC.position = addSoftBodyVec3(nC.position, scaleSoftBodyVec3(forcePerVertex, nC.inverseMass * dtSq));
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
void solveElasticEdges(SoftBodySimulation& sim, double dt) {
|
|
480
|
+
if (dt <= 0.0 || sim.edgeCount == 0) return;
|
|
481
|
+
const double dtSq = dt * dt;
|
|
482
|
+
|
|
483
|
+
for (std::uint32_t e = 0; e < sim.edgeCount; ++e) {
|
|
484
|
+
auto& edge = sim.edges[e];
|
|
485
|
+
auto& nA = sim.nodes[edge.nodeA];
|
|
486
|
+
auto& nB = sim.nodes[edge.nodeB];
|
|
487
|
+
|
|
488
|
+
const SoftBodyVec3 diff = subSoftBodyVec3(nA.position, nB.position);
|
|
489
|
+
const double currentLen = lengthSoftBodyVec3(diff);
|
|
490
|
+
edge.currentLength = currentLen;
|
|
491
|
+
|
|
492
|
+
if (currentLen <= 1e-12) continue;
|
|
493
|
+
|
|
494
|
+
const double C = currentLen - edge.restLength;
|
|
495
|
+
const double compliance = (edge.stretchStiffness > 0.0)
|
|
496
|
+
? (1.0 / (edge.stretchStiffness * dtSq))
|
|
497
|
+
: 0.0;
|
|
498
|
+
|
|
499
|
+
const double wA = nA.inverseMass;
|
|
500
|
+
const double wB = nB.inverseMass;
|
|
501
|
+
const double denom = wA + wB + compliance;
|
|
502
|
+
|
|
503
|
+
if (denom > 1e-15) {
|
|
504
|
+
const double deltaLambda = -C / denom;
|
|
505
|
+
const SoftBodyVec3 dir = scaleSoftBodyVec3(diff, 1.0 / currentLen);
|
|
506
|
+
if (wA > 0.0) nA.position = addSoftBodyVec3(nA.position, scaleSoftBodyVec3(dir, wA * deltaLambda));
|
|
507
|
+
if (wB > 0.0) nB.position = subSoftBodyVec3(nB.position, scaleSoftBodyVec3(dir, wB * deltaLambda));
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
void solveShapeMatching(SoftBodySimulation& sim) {
|
|
513
|
+
const double kShape = softBodyClamp(sim.params.shapeMatchingStiffness, 0.0, 1.0);
|
|
514
|
+
if (kShape <= 0.0 || sim.nodeCount < 4) return;
|
|
515
|
+
|
|
516
|
+
SoftBodyVec3 curCm{0.0, 0.0, 0.0};
|
|
517
|
+
double totalMass = 0.0;
|
|
518
|
+
for (std::uint32_t i = 0; i < sim.nodeCount; ++i) {
|
|
519
|
+
const double mass = (sim.nodes[i].inverseMass > 0.0) ? (1.0 / sim.nodes[i].inverseMass) : 1.0;
|
|
520
|
+
curCm = addSoftBodyVec3(curCm, scaleSoftBodyVec3(sim.nodes[i].position, mass));
|
|
521
|
+
totalMass += mass;
|
|
522
|
+
}
|
|
523
|
+
if (totalMass > 1e-15) {
|
|
524
|
+
curCm = scaleSoftBodyVec3(curCm, 1.0 / totalMass);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
SoftBodyMat3 A = SoftBodyMat3::zero();
|
|
528
|
+
for (std::uint32_t i = 0; i < sim.nodeCount; ++i) {
|
|
529
|
+
const double mass = (sim.nodes[i].inverseMass > 0.0) ? (1.0 / sim.nodes[i].inverseMass) : 1.0;
|
|
530
|
+
const SoftBodyVec3 p = subSoftBodyVec3(sim.nodes[i].position, curCm);
|
|
531
|
+
const SoftBodyVec3 q = subSoftBodyVec3(sim.restPositions[i], sim.restCenterOfMass);
|
|
532
|
+
|
|
533
|
+
A.m[0][0] += mass * p.x * q.x;
|
|
534
|
+
A.m[0][1] += mass * p.x * q.y;
|
|
535
|
+
A.m[0][2] += mass * p.x * q.z;
|
|
536
|
+
|
|
537
|
+
A.m[1][0] += mass * p.y * q.x;
|
|
538
|
+
A.m[1][1] += mass * p.y * q.y;
|
|
539
|
+
A.m[1][2] += mass * p.y * q.z;
|
|
540
|
+
|
|
541
|
+
A.m[2][0] += mass * p.z * q.x;
|
|
542
|
+
A.m[2][1] += mass * p.z * q.y;
|
|
543
|
+
A.m[2][2] += mass * p.z * q.z;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
const SoftBodyMat3 R = extractRotationPolar(A);
|
|
547
|
+
|
|
548
|
+
for (std::uint32_t i = 0; i < sim.nodeCount; ++i) {
|
|
549
|
+
if (sim.nodes[i].inverseMass <= 0.0) continue;
|
|
550
|
+
const SoftBodyVec3 q = subSoftBodyVec3(sim.restPositions[i], sim.restCenterOfMass);
|
|
551
|
+
const SoftBodyVec3 goal = addSoftBodyVec3(curCm, mat3MultiplyVec3(R, q));
|
|
552
|
+
sim.nodes[i].position = addSoftBodyVec3(sim.nodes[i].position,
|
|
553
|
+
scaleSoftBodyVec3(subSoftBodyVec3(goal, sim.nodes[i].position), kShape));
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
void applyPlasticity(SoftBodySimulation& sim) {
|
|
558
|
+
// Edge plasticity where declared
|
|
559
|
+
for (std::uint32_t e = 0; e < sim.edgeCount; ++e) {
|
|
560
|
+
auto& edge = sim.edges[e];
|
|
561
|
+
const double yieldThresh = (edge.yieldThreshold > 0.0)
|
|
562
|
+
? edge.yieldThreshold
|
|
563
|
+
: sim.params.edgeYieldThreshold;
|
|
564
|
+
const double flowRate = (edge.plasticityRate > 0.0)
|
|
565
|
+
? edge.plasticityRate
|
|
566
|
+
: sim.params.edgePlasticityRate;
|
|
567
|
+
|
|
568
|
+
const double currentLen = lengthSoftBodyVec3(
|
|
569
|
+
subSoftBodyVec3(sim.nodes[edge.nodeA].position, sim.nodes[edge.nodeB].position));
|
|
570
|
+
edge.currentLength = currentLen;
|
|
571
|
+
|
|
572
|
+
if (yieldThresh > 0.0 && flowRate > 0.0 && edge.restLength > 1e-12) {
|
|
573
|
+
const double strain = (currentLen - edge.restLength) / edge.restLength;
|
|
574
|
+
if (std::abs(strain) > yieldThresh) {
|
|
575
|
+
const double sign = (strain > 0.0) ? 1.0 : -1.0;
|
|
576
|
+
const double excess = std::abs(strain) - yieldThresh;
|
|
577
|
+
const double dPlastic = sign * excess * softBodyClamp(flowRate, 0.0, 1.0);
|
|
578
|
+
const double maxLimit = (sim.params.maxPlasticStrain > 0.0) ? sim.params.maxPlasticStrain : 0.5;
|
|
579
|
+
edge.plasticStrain = softBodyClamp(edge.plasticStrain + dPlastic, -maxLimit, maxLimit);
|
|
580
|
+
edge.restLength = std::max(0.1 * edge.restLength, edge.restLength * (1.0 + dPlastic));
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// Volumetric plasticity where declared
|
|
586
|
+
if (sim.params.volumetricYieldThreshold > 0.0 && sim.params.volumetricPlasticityRate > 0.0) {
|
|
587
|
+
for (std::uint32_t t = 0; t < sim.tetCount; ++t) {
|
|
588
|
+
auto& tet = sim.tetrahedra[t];
|
|
589
|
+
if (tet.restVolume > 1e-12) {
|
|
590
|
+
const double vStrain = (tet.currentVolume - tet.restVolume) / tet.restVolume;
|
|
591
|
+
if (std::abs(vStrain) > sim.params.volumetricYieldThreshold) {
|
|
592
|
+
const double sign = (vStrain > 0.0) ? 1.0 : -1.0;
|
|
593
|
+
const double excess = std::abs(vStrain) - sim.params.volumetricYieldThreshold;
|
|
594
|
+
const double dVol = sign * excess * sim.params.volumetricPlasticityRate * tet.restVolume;
|
|
595
|
+
tet.plasticVolumeStrain += dVol;
|
|
596
|
+
tet.restVolume = softBodyClamp(tet.restVolume + dVol, 0.2 * tet.restVolume, 5.0 * tet.restVolume);
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
void solveRigidAnchors(SoftBodySimulation& sim, std::span<Body> bodies, double dt) {
|
|
604
|
+
if (sim.anchorCount == 0) return;
|
|
605
|
+
const double invDt = (dt > 0.0) ? (1.0 / dt) : 1.0;
|
|
606
|
+
|
|
607
|
+
for (std::uint32_t a = 0; a < sim.anchorCount; ++a) {
|
|
608
|
+
auto& anchor = sim.anchors[a];
|
|
609
|
+
if (!anchor.active || anchor.nodeIndex >= sim.nodeCount) continue;
|
|
610
|
+
|
|
611
|
+
auto& node = sim.nodes[anchor.nodeIndex];
|
|
612
|
+
SoftBodyVec3 targetPos{};
|
|
613
|
+
|
|
614
|
+
if (anchor.bodyIndex >= 0 && static_cast<std::size_t>(anchor.bodyIndex) < bodies.size()) {
|
|
615
|
+
const auto& body = bodies[anchor.bodyIndex];
|
|
616
|
+
const SoftBodyQuat bodyQ{body.orientationX, body.orientationY, body.orientationZ, body.orientationW};
|
|
617
|
+
const SoftBodyVec3 rotOffset = rotateByQuat(bodyQ, anchor.localAttachment);
|
|
618
|
+
targetPos = addSoftBodyVec3({body.x, body.y, body.z}, rotOffset);
|
|
619
|
+
} else {
|
|
620
|
+
targetPos = anchor.localAttachment;
|
|
621
|
+
}
|
|
622
|
+
anchor.worldAttachment = targetPos;
|
|
623
|
+
|
|
624
|
+
const SoftBodyVec3 diff = subSoftBodyVec3(targetPos, node.position);
|
|
625
|
+
anchor.reactionForce = scaleSoftBodyVec3(diff, invDt * invDt / (node.inverseMass > 0.0 ? node.inverseMass : 1.0));
|
|
626
|
+
node.position = targetPos;
|
|
627
|
+
node.velocity = {0.0, 0.0, 0.0};
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
void solveGroundCollisions(SoftBodySimulation& sim, double dt) {
|
|
632
|
+
if (!sim.params.enableGround) return;
|
|
633
|
+
const double groundY = sim.params.groundY;
|
|
634
|
+
const double r = sim.params.collisionRadius;
|
|
635
|
+
const double friction = sim.params.groundFriction;
|
|
636
|
+
const double restitution = sim.params.groundRestitution;
|
|
637
|
+
|
|
638
|
+
for (std::uint32_t i = 0; i < sim.nodeCount; ++i) {
|
|
639
|
+
auto& node = sim.nodes[i];
|
|
640
|
+
if (node.inverseMass <= 0.0) continue;
|
|
641
|
+
|
|
642
|
+
if (node.position.y < groundY + r) {
|
|
643
|
+
const double penetration = (groundY + r) - node.position.y;
|
|
644
|
+
node.position.y = groundY + r;
|
|
645
|
+
|
|
646
|
+
if (dt > 0.0) {
|
|
647
|
+
const double deltaX = node.position.x - node.previousPosition.x;
|
|
648
|
+
const double deltaZ = node.position.z - node.previousPosition.z;
|
|
649
|
+
const double horizDisp = softBodySqrt(deltaX * deltaX + deltaZ * deltaZ);
|
|
650
|
+
if (horizDisp > 1e-12) {
|
|
651
|
+
const double frictionScale = std::max(0.0, 1.0 - (friction * penetration) / horizDisp);
|
|
652
|
+
node.position.x = node.previousPosition.x + deltaX * frictionScale;
|
|
653
|
+
node.position.z = node.previousPosition.z + deltaZ * frictionScale;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
if (node.velocity.y < 0.0) {
|
|
658
|
+
node.velocity.y = -restitution * node.velocity.y;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
SoftBodyStatus stepSoftBody(
|
|
665
|
+
SoftBodySimulation& sim,
|
|
666
|
+
std::span<Body> bodies,
|
|
667
|
+
double dt) {
|
|
668
|
+
if (dt <= 0.0 || sim.nodeCount < 4) return SoftBodyStatus::InvalidNodeCount;
|
|
669
|
+
if (sim.nodeCount > kSoftBodyMaxNodes || sim.tetCount > kSoftBodyMaxTetrahedra) {
|
|
670
|
+
return SoftBodyStatus::BufferOverflow;
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
const std::uint32_t substeps = std::max(1u, sim.params.substeps);
|
|
674
|
+
const double subDt = dt / static_cast<double>(substeps);
|
|
675
|
+
const double subInvDt = 1.0 / subDt;
|
|
676
|
+
const SoftBodyVec3 gravity{sim.params.gravityX, sim.params.gravityY, sim.params.gravityZ};
|
|
677
|
+
const double dampingFactor = softBodyClamp(1.0 - sim.params.damping, 0.0, 1.0);
|
|
678
|
+
|
|
679
|
+
for (std::uint32_t step = 0; step < substeps; ++step) {
|
|
680
|
+
// 1. Prediction step
|
|
681
|
+
for (std::uint32_t i = 0; i < sim.nodeCount; ++i) {
|
|
682
|
+
auto& node = sim.nodes[i];
|
|
683
|
+
if (node.inverseMass <= 0.0) {
|
|
684
|
+
node.previousPosition = node.position;
|
|
685
|
+
continue;
|
|
686
|
+
}
|
|
687
|
+
node.velocity = scaleSoftBodyVec3(node.velocity, dampingFactor);
|
|
688
|
+
node.velocity = addSoftBodyVec3(node.velocity, scaleSoftBodyVec3(gravity, subDt));
|
|
689
|
+
node.previousPosition = node.position;
|
|
690
|
+
node.position = addSoftBodyVec3(node.position, scaleSoftBodyVec3(node.velocity, subDt));
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// 2. Constraint solver iterations
|
|
694
|
+
const std::uint32_t iters = std::max(1u, sim.params.solverIterations);
|
|
695
|
+
for (std::uint32_t iter = 0; iter < iters; ++iter) {
|
|
696
|
+
handleInvertedTetrahedra(sim);
|
|
697
|
+
solveVolumePreservation(sim, subDt);
|
|
698
|
+
solvePressure(sim, subDt);
|
|
699
|
+
solveElasticEdges(sim, subDt);
|
|
700
|
+
solveShapeMatching(sim);
|
|
701
|
+
solveRigidAnchors(sim, bodies, subDt);
|
|
702
|
+
solveGroundCollisions(sim, subDt);
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
// 3. Velocity update
|
|
706
|
+
for (std::uint32_t i = 0; i < sim.nodeCount; ++i) {
|
|
707
|
+
auto& node = sim.nodes[i];
|
|
708
|
+
if (node.inverseMass <= 0.0) continue;
|
|
709
|
+
node.velocity = scaleSoftBodyVec3(subSoftBodyVec3(node.position, node.previousPosition), subInvDt);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
// 4. Update plasticity
|
|
713
|
+
applyPlasticity(sim);
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
return SoftBodyStatus::Success;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
SoftBodyStatus stepSoftBodyRaw(
|
|
720
|
+
double* nodeData,
|
|
721
|
+
std::uint32_t nodeCount,
|
|
722
|
+
double* tetData,
|
|
723
|
+
std::uint32_t tetCount,
|
|
724
|
+
double* faceData,
|
|
725
|
+
std::uint32_t faceCount,
|
|
726
|
+
double* edgeData,
|
|
727
|
+
std::uint32_t edgeCount,
|
|
728
|
+
double* anchorData,
|
|
729
|
+
std::uint32_t anchorCount,
|
|
730
|
+
double* bodyData,
|
|
731
|
+
std::uint32_t bodyCount,
|
|
732
|
+
const double* paramsData,
|
|
733
|
+
double dt) {
|
|
734
|
+
if (nodeData == nullptr || nodeCount < 4) return SoftBodyStatus::InvalidNodeCount;
|
|
735
|
+
if (nodeCount > kSoftBodyMaxNodes || tetCount > kSoftBodyMaxTetrahedra) {
|
|
736
|
+
return SoftBodyStatus::BufferOverflow;
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
static SoftBodySimulation sim{};
|
|
740
|
+
std::memset(&sim, 0, sizeof(SoftBodySimulation));
|
|
741
|
+
sim.params = SoftBodyParams{};
|
|
742
|
+
sim.nodeCount = nodeCount;
|
|
743
|
+
sim.tetCount = tetCount;
|
|
744
|
+
sim.faceCount = faceCount;
|
|
745
|
+
sim.edgeCount = edgeCount;
|
|
746
|
+
sim.anchorCount = anchorCount;
|
|
747
|
+
|
|
748
|
+
// Unpack params
|
|
749
|
+
if (paramsData != nullptr) {
|
|
750
|
+
sim.params.gravityX = paramsData[0];
|
|
751
|
+
sim.params.gravityY = paramsData[1];
|
|
752
|
+
sim.params.gravityZ = paramsData[2];
|
|
753
|
+
sim.params.damping = paramsData[3];
|
|
754
|
+
sim.params.collisionRadius = paramsData[4];
|
|
755
|
+
sim.params.groundY = paramsData[5];
|
|
756
|
+
sim.params.groundFriction = paramsData[6];
|
|
757
|
+
sim.params.groundRestitution = paramsData[7];
|
|
758
|
+
sim.params.enableGround = paramsData[8] > 0.5;
|
|
759
|
+
sim.params.pressure = paramsData[9];
|
|
760
|
+
sim.params.pressureStiffness = paramsData[10];
|
|
761
|
+
sim.params.volumePreservationStiffness = paramsData[11];
|
|
762
|
+
sim.params.shapeMatchingStiffness = paramsData[12];
|
|
763
|
+
sim.params.edgeYieldThreshold = paramsData[13];
|
|
764
|
+
sim.params.edgePlasticityRate = paramsData[14];
|
|
765
|
+
sim.params.inversionThreshold = paramsData[15];
|
|
766
|
+
sim.params.inversionRestorationStiffness = paramsData[16];
|
|
767
|
+
sim.params.solverIterations = static_cast<std::uint32_t>(paramsData[17]);
|
|
768
|
+
if (sim.params.solverIterations == 0) sim.params.solverIterations = 10;
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
// Unpack nodes
|
|
772
|
+
for (std::uint32_t i = 0; i < nodeCount; ++i) {
|
|
773
|
+
const std::uint32_t off = i * kSoftBodyNodeStride;
|
|
774
|
+
sim.nodes[i].position = {nodeData[off], nodeData[off + 1], nodeData[off + 2]};
|
|
775
|
+
sim.nodes[i].velocity = {nodeData[off + 3], nodeData[off + 4], nodeData[off + 5]};
|
|
776
|
+
sim.nodes[i].inverseMass = nodeData[off + 6];
|
|
777
|
+
sim.nodes[i].previousPosition = {nodeData[off + 7], nodeData[off + 8], nodeData[off + 9]};
|
|
778
|
+
sim.restPositions[i] = sim.nodes[i].position;
|
|
779
|
+
}
|
|
780
|
+
|
|
781
|
+
// Unpack tets
|
|
782
|
+
if (tetData != nullptr) {
|
|
783
|
+
for (std::uint32_t t = 0; t < tetCount; ++t) {
|
|
784
|
+
const std::uint32_t off = t * kSoftBodyTetStride;
|
|
785
|
+
sim.tetrahedra[t].node0 = static_cast<std::uint32_t>(tetData[off]);
|
|
786
|
+
sim.tetrahedra[t].node1 = static_cast<std::uint32_t>(tetData[off + 1]);
|
|
787
|
+
sim.tetrahedra[t].node2 = static_cast<std::uint32_t>(tetData[off + 2]);
|
|
788
|
+
sim.tetrahedra[t].node3 = static_cast<std::uint32_t>(tetData[off + 3]);
|
|
789
|
+
sim.tetrahedra[t].restVolume = tetData[off + 4];
|
|
790
|
+
sim.tetrahedra[t].currentVolume = tetData[off + 5];
|
|
791
|
+
sim.tetrahedra[t].volumeStiffness = tetData[off + 6];
|
|
792
|
+
sim.tetrahedra[t].inverted = tetData[off + 7] > 0.5;
|
|
793
|
+
sim.totalRestVolume += sim.tetrahedra[t].restVolume;
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
// Unpack faces
|
|
798
|
+
if (faceData != nullptr) {
|
|
799
|
+
for (std::uint32_t f = 0; f < faceCount; ++f) {
|
|
800
|
+
const std::uint32_t off = f * kSoftBodyFaceStride;
|
|
801
|
+
sim.faces[f].nodeA = static_cast<std::uint32_t>(faceData[off]);
|
|
802
|
+
sim.faces[f].nodeB = static_cast<std::uint32_t>(faceData[off + 1]);
|
|
803
|
+
sim.faces[f].nodeC = static_cast<std::uint32_t>(faceData[off + 2]);
|
|
804
|
+
sim.faces[f].area = faceData[off + 3];
|
|
805
|
+
sim.faces[f].normal = {faceData[off + 4], faceData[off + 5], faceData[off + 6]};
|
|
806
|
+
sim.faces[f].pressureMultiplier = faceData[off + 7];
|
|
807
|
+
}
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// Unpack edges
|
|
811
|
+
if (edgeData != nullptr) {
|
|
812
|
+
for (std::uint32_t e = 0; e < edgeCount; ++e) {
|
|
813
|
+
const std::uint32_t off = e * kSoftBodyEdgeStride;
|
|
814
|
+
sim.edges[e].nodeA = static_cast<std::uint32_t>(edgeData[off]);
|
|
815
|
+
sim.edges[e].nodeB = static_cast<std::uint32_t>(edgeData[off + 1]);
|
|
816
|
+
sim.edges[e].restLength = edgeData[off + 2];
|
|
817
|
+
sim.edges[e].currentLength = edgeData[off + 3];
|
|
818
|
+
sim.edges[e].stretchStiffness = edgeData[off + 4];
|
|
819
|
+
sim.edges[e].plasticStrain = edgeData[off + 5];
|
|
820
|
+
sim.edges[e].yieldThreshold = edgeData[off + 6];
|
|
821
|
+
sim.edges[e].plasticityRate = edgeData[off + 7];
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// Unpack anchors
|
|
826
|
+
if (anchorData != nullptr) {
|
|
827
|
+
for (std::uint32_t a = 0; a < anchorCount; ++a) {
|
|
828
|
+
const std::uint32_t off = a * kSoftBodyAnchorStride;
|
|
829
|
+
sim.anchors[a].nodeIndex = static_cast<std::uint32_t>(anchorData[off]);
|
|
830
|
+
sim.anchors[a].bodyIndex = static_cast<std::int32_t>(anchorData[off + 1]);
|
|
831
|
+
sim.anchors[a].localAttachment = {anchorData[off + 2], anchorData[off + 3], anchorData[off + 4]};
|
|
832
|
+
sim.anchors[a].worldAttachment = {anchorData[off + 5], anchorData[off + 6], anchorData[off + 7]};
|
|
833
|
+
sim.anchors[a].reactionForce = {anchorData[off + 8], anchorData[off + 9], anchorData[off + 10]};
|
|
834
|
+
sim.anchors[a].active = anchorData[off + 11] > 0.5;
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
// Unpack bodies
|
|
839
|
+
const std::uint32_t clampedBodyCount = std::min(bodyCount, kMaxRawBodies);
|
|
840
|
+
if (bodyData != nullptr && clampedBodyCount > 0) {
|
|
841
|
+
for (std::uint32_t i = 0; i < clampedBodyCount; ++i) {
|
|
842
|
+
const std::uint32_t off = i * kBodyStride;
|
|
843
|
+
sRawBodies[i].x = bodyData[off + 0];
|
|
844
|
+
sRawBodies[i].y = bodyData[off + 1];
|
|
845
|
+
sRawBodies[i].z = bodyData[off + 2];
|
|
846
|
+
sRawBodies[i].vx = bodyData[off + 3];
|
|
847
|
+
sRawBodies[i].vy = bodyData[off + 4];
|
|
848
|
+
sRawBodies[i].vz = bodyData[off + 5];
|
|
849
|
+
const std::uint32_t motionType = static_cast<std::uint32_t>(bodyData[off + 9]);
|
|
850
|
+
sRawBodies[i].dynamic = (motionType & kMotionKindMask) == 1;
|
|
851
|
+
sRawBodies[i].mass = bodyData[off + 18];
|
|
852
|
+
sRawBodies[i].orientationX = bodyData[off + 22];
|
|
853
|
+
sRawBodies[i].orientationY = bodyData[off + 23];
|
|
854
|
+
sRawBodies[i].orientationZ = bodyData[off + 24];
|
|
855
|
+
sRawBodies[i].orientationW = bodyData[off + 25];
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
std::span<Body> bodiesSpan(sRawBodies.data(), clampedBodyCount);
|
|
860
|
+
const SoftBodyStatus status = stepSoftBody(sim, bodiesSpan, dt);
|
|
861
|
+
if (status != SoftBodyStatus::Success) return status;
|
|
862
|
+
|
|
863
|
+
// Marshal nodes back
|
|
864
|
+
for (std::uint32_t i = 0; i < nodeCount; ++i) {
|
|
865
|
+
const std::uint32_t off = i * kSoftBodyNodeStride;
|
|
866
|
+
nodeData[off] = sim.nodes[i].position.x;
|
|
867
|
+
nodeData[off + 1] = sim.nodes[i].position.y;
|
|
868
|
+
nodeData[off + 2] = sim.nodes[i].position.z;
|
|
869
|
+
nodeData[off + 3] = sim.nodes[i].velocity.x;
|
|
870
|
+
nodeData[off + 4] = sim.nodes[i].velocity.y;
|
|
871
|
+
nodeData[off + 5] = sim.nodes[i].velocity.z;
|
|
872
|
+
nodeData[off + 6] = sim.nodes[i].inverseMass;
|
|
873
|
+
nodeData[off + 7] = sim.nodes[i].previousPosition.x;
|
|
874
|
+
nodeData[off + 8] = sim.nodes[i].previousPosition.y;
|
|
875
|
+
nodeData[off + 9] = sim.nodes[i].previousPosition.z;
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
// Marshal tets back
|
|
879
|
+
if (tetData != nullptr) {
|
|
880
|
+
for (std::uint32_t t = 0; t < tetCount; ++t) {
|
|
881
|
+
const std::uint32_t off = t * kSoftBodyTetStride;
|
|
882
|
+
tetData[off + 5] = sim.tetrahedra[t].currentVolume;
|
|
883
|
+
tetData[off + 7] = sim.tetrahedra[t].inverted ? 1.0 : 0.0;
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
// Marshal faces back
|
|
888
|
+
if (faceData != nullptr) {
|
|
889
|
+
for (std::uint32_t f = 0; f < faceCount; ++f) {
|
|
890
|
+
const std::uint32_t off = f * kSoftBodyFaceStride;
|
|
891
|
+
faceData[off + 3] = sim.faces[f].area;
|
|
892
|
+
faceData[off + 4] = sim.faces[f].normal.x;
|
|
893
|
+
faceData[off + 5] = sim.faces[f].normal.y;
|
|
894
|
+
faceData[off + 6] = sim.faces[f].normal.z;
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// Marshal edges back
|
|
899
|
+
if (edgeData != nullptr) {
|
|
900
|
+
for (std::uint32_t e = 0; e < edgeCount; ++e) {
|
|
901
|
+
const std::uint32_t off = e * kSoftBodyEdgeStride;
|
|
902
|
+
edgeData[off + 2] = sim.edges[e].restLength;
|
|
903
|
+
edgeData[off + 3] = sim.edges[e].currentLength;
|
|
904
|
+
edgeData[off + 5] = sim.edges[e].plasticStrain;
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
// Marshal anchors back
|
|
909
|
+
if (anchorData != nullptr) {
|
|
910
|
+
for (std::uint32_t a = 0; a < anchorCount; ++a) {
|
|
911
|
+
const std::uint32_t off = a * kSoftBodyAnchorStride;
|
|
912
|
+
anchorData[off + 5] = sim.anchors[a].worldAttachment.x;
|
|
913
|
+
anchorData[off + 6] = sim.anchors[a].worldAttachment.y;
|
|
914
|
+
anchorData[off + 7] = sim.anchors[a].worldAttachment.z;
|
|
915
|
+
anchorData[off + 8] = sim.anchors[a].reactionForce.x;
|
|
916
|
+
anchorData[off + 9] = sim.anchors[a].reactionForce.y;
|
|
917
|
+
anchorData[off + 10] = sim.anchors[a].reactionForce.z;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
return SoftBodyStatus::Success;
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
SoftBodyStatus createSoftBodyCube(
|
|
925
|
+
SoftBodySimulation& sim,
|
|
926
|
+
const SoftBodyVec3& center,
|
|
927
|
+
const SoftBodyVec3& halfExtents,
|
|
928
|
+
double totalMass) {
|
|
929
|
+
std::memset(&sim, 0, sizeof(SoftBodySimulation));
|
|
930
|
+
sim.params = SoftBodyParams{};
|
|
931
|
+
|
|
932
|
+
const double hx = halfExtents.x;
|
|
933
|
+
const double hy = halfExtents.y;
|
|
934
|
+
const double hz = halfExtents.z;
|
|
935
|
+
|
|
936
|
+
sim.nodeCount = 8;
|
|
937
|
+
const double nodeMass = (totalMass > 0.0) ? (totalMass / 8.0) : 0.125;
|
|
938
|
+
const double invMass = (nodeMass > 0.0) ? (1.0 / nodeMass) : 1.0;
|
|
939
|
+
|
|
940
|
+
// 8 vertices of cube
|
|
941
|
+
const SoftBodyVec3 corners[8] = {
|
|
942
|
+
{-hx, -hy, -hz},
|
|
943
|
+
{ hx, -hy, -hz},
|
|
944
|
+
{ hx, hy, -hz},
|
|
945
|
+
{-hx, hy, -hz},
|
|
946
|
+
{-hx, -hy, hz},
|
|
947
|
+
{ hx, -hy, hz},
|
|
948
|
+
{ hx, hy, hz},
|
|
949
|
+
{-hx, hy, hz},
|
|
950
|
+
};
|
|
951
|
+
|
|
952
|
+
for (std::uint32_t i = 0; i < 8; ++i) {
|
|
953
|
+
sim.nodes[i].position = addSoftBodyVec3(center, corners[i]);
|
|
954
|
+
sim.nodes[i].previousPosition = sim.nodes[i].position;
|
|
955
|
+
sim.nodes[i].velocity = {0.0, 0.0, 0.0};
|
|
956
|
+
sim.nodes[i].inverseMass = invMass;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
// 5-tetrahedron decomposition of a cube
|
|
960
|
+
sim.tetCount = 5;
|
|
961
|
+
sim.tetrahedra[0] = {0, 1, 3, 5, 0.0, 0.0, 1.0, 0.0, false};
|
|
962
|
+
sim.tetrahedra[1] = {1, 2, 3, 6, 0.0, 0.0, 1.0, 0.0, false};
|
|
963
|
+
sim.tetrahedra[2] = {0, 4, 5, 7, 0.0, 0.0, 1.0, 0.0, false};
|
|
964
|
+
sim.tetrahedra[3] = {3, 5, 6, 7, 0.0, 0.0, 1.0, 0.0, false};
|
|
965
|
+
sim.tetrahedra[4] = {0, 3, 5, 7, 0.0, 0.0, 1.0, 0.0, false};
|
|
966
|
+
|
|
967
|
+
// 12 boundary surface triangles (facing outward)
|
|
968
|
+
sim.faceCount = 12;
|
|
969
|
+
sim.faces[0] = {0, 2, 1, 0.0, {}, 1.0}; // -Z
|
|
970
|
+
sim.faces[1] = {0, 3, 2, 0.0, {}, 1.0};
|
|
971
|
+
sim.faces[2] = {4, 5, 6, 0.0, {}, 1.0}; // +Z
|
|
972
|
+
sim.faces[3] = {4, 6, 7, 0.0, {}, 1.0};
|
|
973
|
+
sim.faces[4] = {0, 1, 5, 0.0, {}, 1.0}; // -Y
|
|
974
|
+
sim.faces[5] = {0, 5, 4, 0.0, {}, 1.0};
|
|
975
|
+
sim.faces[6] = {2, 3, 7, 0.0, {}, 1.0}; // +Y
|
|
976
|
+
sim.faces[7] = {2, 7, 6, 0.0, {}, 1.0};
|
|
977
|
+
sim.faces[8] = {0, 4, 7, 0.0, {}, 1.0}; // -X
|
|
978
|
+
sim.faces[9] = {0, 7, 3, 0.0, {}, 1.0};
|
|
979
|
+
sim.faces[10] = {1, 2, 6, 0.0, {}, 1.0}; // +X
|
|
980
|
+
sim.faces[11] = {1, 6, 5, 0.0, {}, 1.0};
|
|
981
|
+
|
|
982
|
+
// Edges
|
|
983
|
+
constexpr std::uint32_t edgeIndices[18][2] = {
|
|
984
|
+
{0, 1}, {1, 2}, {2, 3}, {3, 0}, // -Z face
|
|
985
|
+
{4, 5}, {5, 6}, {6, 7}, {7, 4}, // +Z face
|
|
986
|
+
{0, 4}, {1, 5}, {2, 6}, {3, 7}, // connecting pillars
|
|
987
|
+
{0, 2}, {1, 3}, {4, 6}, {5, 7}, // face diagonals
|
|
988
|
+
{0, 5}, {3, 5}, // internal diagonals
|
|
989
|
+
};
|
|
990
|
+
|
|
991
|
+
sim.edgeCount = 18;
|
|
992
|
+
for (std::uint32_t e = 0; e < 18; ++e) {
|
|
993
|
+
sim.edges[e].nodeA = edgeIndices[e][0];
|
|
994
|
+
sim.edges[e].nodeB = edgeIndices[e][1];
|
|
995
|
+
sim.edges[e].stretchStiffness = 500.0;
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
return initSoftBody(sim);
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
} // namespace rundot::kinetix::physics
|
|
1002
|
+
|
|
1003
|
+
extern "C" {
|
|
1004
|
+
|
|
1005
|
+
std::uint32_t rundot_kinetix_physics_soft_body_abi_version() {
|
|
1006
|
+
return rundot::kinetix::physics::kSoftBodyAbiVersion;
|
|
1007
|
+
}
|
|
1008
|
+
|
|
1009
|
+
std::uint32_t rundot_kinetix_physics_soft_body_node_stride() {
|
|
1010
|
+
return rundot::kinetix::physics::kSoftBodyNodeStride;
|
|
1011
|
+
}
|
|
1012
|
+
|
|
1013
|
+
std::uint32_t rundot_kinetix_physics_soft_body_tet_stride() {
|
|
1014
|
+
return rundot::kinetix::physics::kSoftBodyTetStride;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
std::uint32_t rundot_kinetix_physics_soft_body_face_stride() {
|
|
1018
|
+
return rundot::kinetix::physics::kSoftBodyFaceStride;
|
|
1019
|
+
}
|
|
1020
|
+
|
|
1021
|
+
std::uint32_t rundot_kinetix_physics_soft_body_edge_stride() {
|
|
1022
|
+
return rundot::kinetix::physics::kSoftBodyEdgeStride;
|
|
1023
|
+
}
|
|
1024
|
+
|
|
1025
|
+
std::uint32_t rundot_kinetix_physics_soft_body_anchor_stride() {
|
|
1026
|
+
return rundot::kinetix::physics::kSoftBodyAnchorStride;
|
|
1027
|
+
}
|
|
1028
|
+
|
|
1029
|
+
std::uint32_t rundot_kinetix_physics_soft_body_params_stride() {
|
|
1030
|
+
return rundot::kinetix::physics::kSoftBodyParamsStride;
|
|
1031
|
+
}
|
|
1032
|
+
|
|
1033
|
+
std::uint32_t rundot_kinetix_physics_soft_body_max_nodes() {
|
|
1034
|
+
return rundot::kinetix::physics::kSoftBodyMaxNodes;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
std::uint32_t rundot_kinetix_physics_soft_body_max_tetrahedra() {
|
|
1038
|
+
return rundot::kinetix::physics::kSoftBodyMaxTetrahedra;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
std::uint32_t rundot_kinetix_physics_soft_body_max_edges() {
|
|
1042
|
+
return rundot::kinetix::physics::kSoftBodyMaxEdges;
|
|
1043
|
+
}
|
|
1044
|
+
|
|
1045
|
+
std::uint32_t rundot_kinetix_physics_soft_body_max_faces() {
|
|
1046
|
+
return rundot::kinetix::physics::kSoftBodyMaxFaces;
|
|
1047
|
+
}
|
|
1048
|
+
|
|
1049
|
+
std::int32_t rundot_kinetix_physics_soft_body_step(
|
|
1050
|
+
double* nodeData,
|
|
1051
|
+
std::uint32_t nodeCount,
|
|
1052
|
+
double* tetData,
|
|
1053
|
+
std::uint32_t tetCount,
|
|
1054
|
+
double* faceData,
|
|
1055
|
+
std::uint32_t faceCount,
|
|
1056
|
+
double* edgeData,
|
|
1057
|
+
std::uint32_t edgeCount,
|
|
1058
|
+
double* anchorData,
|
|
1059
|
+
std::uint32_t anchorCount,
|
|
1060
|
+
double* bodyData,
|
|
1061
|
+
std::uint32_t bodyCount,
|
|
1062
|
+
const double* paramsData,
|
|
1063
|
+
double dt) {
|
|
1064
|
+
return static_cast<std::int32_t>(rundot::kinetix::physics::stepSoftBodyRaw(
|
|
1065
|
+
nodeData, nodeCount,
|
|
1066
|
+
tetData, tetCount,
|
|
1067
|
+
faceData, faceCount,
|
|
1068
|
+
edgeData, edgeCount,
|
|
1069
|
+
anchorData, anchorCount,
|
|
1070
|
+
bodyData, bodyCount,
|
|
1071
|
+
paramsData, dt));
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
double rundot_kinetix_physics_soft_body_compute_volume(
|
|
1075
|
+
const double* nodeData,
|
|
1076
|
+
std::uint32_t nodeCount,
|
|
1077
|
+
const double* tetData,
|
|
1078
|
+
std::uint32_t tetCount) {
|
|
1079
|
+
if (nodeData == nullptr || tetData == nullptr || nodeCount < 4) return 0.0;
|
|
1080
|
+
using namespace rundot::kinetix::physics;
|
|
1081
|
+
|
|
1082
|
+
double total = 0.0;
|
|
1083
|
+
for (std::uint32_t t = 0; t < tetCount; ++t) {
|
|
1084
|
+
const std::uint32_t off = t * kSoftBodyTetStride;
|
|
1085
|
+
const auto n0 = static_cast<std::uint32_t>(tetData[off]);
|
|
1086
|
+
const auto n1 = static_cast<std::uint32_t>(tetData[off + 1]);
|
|
1087
|
+
const auto n2 = static_cast<std::uint32_t>(tetData[off + 2]);
|
|
1088
|
+
const auto n3 = static_cast<std::uint32_t>(tetData[off + 3]);
|
|
1089
|
+
|
|
1090
|
+
if (n0 < nodeCount && n1 < nodeCount && n2 < nodeCount && n3 < nodeCount) {
|
|
1091
|
+
const SoftBodyVec3 p0{nodeData[n0 * kSoftBodyNodeStride], nodeData[n0 * kSoftBodyNodeStride + 1], nodeData[n0 * kSoftBodyNodeStride + 2]};
|
|
1092
|
+
const SoftBodyVec3 p1{nodeData[n1 * kSoftBodyNodeStride], nodeData[n1 * kSoftBodyNodeStride + 1], nodeData[n1 * kSoftBodyNodeStride + 2]};
|
|
1093
|
+
const SoftBodyVec3 p2{nodeData[n2 * kSoftBodyNodeStride], nodeData[n2 * kSoftBodyNodeStride + 1], nodeData[n2 * kSoftBodyNodeStride + 2]};
|
|
1094
|
+
const SoftBodyVec3 p3{nodeData[n3 * kSoftBodyNodeStride], nodeData[n3 * kSoftBodyNodeStride + 1], nodeData[n3 * kSoftBodyNodeStride + 2]};
|
|
1095
|
+
total += computeTetVolume(p0, p1, p2, p3);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
return total;
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
std::int32_t rundot_kinetix_physics_soft_body_solve_volume(
|
|
1102
|
+
double* nodeData,
|
|
1103
|
+
std::uint32_t nodeCount,
|
|
1104
|
+
double* tetData,
|
|
1105
|
+
std::uint32_t tetCount,
|
|
1106
|
+
double volumeStiffness,
|
|
1107
|
+
double dt) {
|
|
1108
|
+
if (nodeData == nullptr || tetData == nullptr || nodeCount < 4 || dt <= 0.0) return -1;
|
|
1109
|
+
using namespace rundot::kinetix::physics;
|
|
1110
|
+
|
|
1111
|
+
const double compliance = (volumeStiffness > 0.0) ? (1.0 / (volumeStiffness * dt * dt)) : 0.0;
|
|
1112
|
+
|
|
1113
|
+
for (std::uint32_t t = 0; t < tetCount; ++t) {
|
|
1114
|
+
const std::uint32_t off = t * kSoftBodyTetStride;
|
|
1115
|
+
const auto i0 = static_cast<std::uint32_t>(tetData[off]);
|
|
1116
|
+
const auto i1 = static_cast<std::uint32_t>(tetData[off + 1]);
|
|
1117
|
+
const auto i2 = static_cast<std::uint32_t>(tetData[off + 2]);
|
|
1118
|
+
const auto i3 = static_cast<std::uint32_t>(tetData[off + 3]);
|
|
1119
|
+
const double restVol = tetData[off + 4];
|
|
1120
|
+
|
|
1121
|
+
if (i0 >= nodeCount || i1 >= nodeCount || i2 >= nodeCount || i3 >= nodeCount) continue;
|
|
1122
|
+
|
|
1123
|
+
SoftBodyVec3 p0{nodeData[i0 * 10], nodeData[i0 * 10 + 1], nodeData[i0 * 10 + 2]};
|
|
1124
|
+
SoftBodyVec3 p1{nodeData[i1 * 10], nodeData[i1 * 10 + 1], nodeData[i1 * 10 + 2]};
|
|
1125
|
+
SoftBodyVec3 p2{nodeData[i2 * 10], nodeData[i2 * 10 + 1], nodeData[i2 * 10 + 2]};
|
|
1126
|
+
SoftBodyVec3 p3{nodeData[i3 * 10], nodeData[i3 * 10 + 1], nodeData[i3 * 10 + 2]};
|
|
1127
|
+
|
|
1128
|
+
const double currentVol = computeTetVolume(p0, p1, p2, p3);
|
|
1129
|
+
tetData[off + 5] = currentVol;
|
|
1130
|
+
|
|
1131
|
+
const double C = currentVol - restVol;
|
|
1132
|
+
SoftBodyVec3 g0{}, g1{}, g2{}, g3{};
|
|
1133
|
+
computeTetGradients(p0, p1, p2, p3, g0, g1, g2, g3);
|
|
1134
|
+
|
|
1135
|
+
const double w0 = nodeData[i0 * 10 + 6];
|
|
1136
|
+
const double w1 = nodeData[i1 * 10 + 6];
|
|
1137
|
+
const double w2 = nodeData[i2 * 10 + 6];
|
|
1138
|
+
const double w3 = nodeData[i3 * 10 + 6];
|
|
1139
|
+
|
|
1140
|
+
const double denom = w0 * lengthSquaredSoftBodyVec3(g0)
|
|
1141
|
+
+ w1 * lengthSquaredSoftBodyVec3(g1)
|
|
1142
|
+
+ w2 * lengthSquaredSoftBodyVec3(g2)
|
|
1143
|
+
+ w3 * lengthSquaredSoftBodyVec3(g3)
|
|
1144
|
+
+ compliance;
|
|
1145
|
+
|
|
1146
|
+
if (denom > 1e-15) {
|
|
1147
|
+
const double deltaLambda = -C / denom;
|
|
1148
|
+
if (w0 > 0.0) {
|
|
1149
|
+
nodeData[i0 * 10 + 0] += g0.x * w0 * deltaLambda;
|
|
1150
|
+
nodeData[i0 * 10 + 1] += g0.y * w0 * deltaLambda;
|
|
1151
|
+
nodeData[i0 * 10 + 2] += g0.z * w0 * deltaLambda;
|
|
1152
|
+
}
|
|
1153
|
+
if (w1 > 0.0) {
|
|
1154
|
+
nodeData[i1 * 10 + 0] += g1.x * w1 * deltaLambda;
|
|
1155
|
+
nodeData[i1 * 10 + 1] += g1.y * w1 * deltaLambda;
|
|
1156
|
+
nodeData[i1 * 10 + 2] += g1.z * w1 * deltaLambda;
|
|
1157
|
+
}
|
|
1158
|
+
if (w2 > 0.0) {
|
|
1159
|
+
nodeData[i2 * 10 + 0] += g2.x * w2 * deltaLambda;
|
|
1160
|
+
nodeData[i2 * 10 + 1] += g2.y * w2 * deltaLambda;
|
|
1161
|
+
nodeData[i2 * 10 + 2] += g2.z * w2 * deltaLambda;
|
|
1162
|
+
}
|
|
1163
|
+
if (w3 > 0.0) {
|
|
1164
|
+
nodeData[i3 * 10 + 0] += g3.x * w3 * deltaLambda;
|
|
1165
|
+
nodeData[i3 * 10 + 1] += g3.y * w3 * deltaLambda;
|
|
1166
|
+
nodeData[i3 * 10 + 2] += g3.z * w3 * deltaLambda;
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
return 0;
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
std::int32_t rundot_kinetix_physics_soft_body_apply_pressure(
|
|
1175
|
+
double* nodeData,
|
|
1176
|
+
std::uint32_t nodeCount,
|
|
1177
|
+
const double* faceData,
|
|
1178
|
+
std::uint32_t faceCount,
|
|
1179
|
+
double pressure,
|
|
1180
|
+
double dt) {
|
|
1181
|
+
if (nodeData == nullptr || faceData == nullptr || nodeCount < 3 || dt <= 0.0) return -1;
|
|
1182
|
+
using namespace rundot::kinetix::physics;
|
|
1183
|
+
|
|
1184
|
+
const double dtSq = dt * dt;
|
|
1185
|
+
|
|
1186
|
+
for (std::uint32_t f = 0; f < faceCount; ++f) {
|
|
1187
|
+
const std::uint32_t off = f * kSoftBodyFaceStride;
|
|
1188
|
+
const auto iA = static_cast<std::uint32_t>(faceData[off]);
|
|
1189
|
+
const auto iB = static_cast<std::uint32_t>(faceData[off + 1]);
|
|
1190
|
+
const auto iC = static_cast<std::uint32_t>(faceData[off + 2]);
|
|
1191
|
+
const double multiplier = faceData[off + 7];
|
|
1192
|
+
|
|
1193
|
+
if (iA >= nodeCount || iB >= nodeCount || iC >= nodeCount) continue;
|
|
1194
|
+
|
|
1195
|
+
const SoftBodyVec3 pA{nodeData[iA * 10], nodeData[iA * 10 + 1], nodeData[iA * 10 + 2]};
|
|
1196
|
+
const SoftBodyVec3 pB{nodeData[iB * 10], nodeData[iB * 10 + 1], nodeData[iB * 10 + 2]};
|
|
1197
|
+
const SoftBodyVec3 pC{nodeData[iC * 10], nodeData[iC * 10 + 1], nodeData[iC * 10 + 2]};
|
|
1198
|
+
|
|
1199
|
+
double area = 0.0;
|
|
1200
|
+
SoftBodyVec3 normal{};
|
|
1201
|
+
computeFaceGeometry(pA, pB, pC, area, normal);
|
|
1202
|
+
|
|
1203
|
+
const double effPressure = pressure * multiplier;
|
|
1204
|
+
const SoftBodyVec3 force = scaleSoftBodyVec3(normal, effPressure * area);
|
|
1205
|
+
const SoftBodyVec3 forcePerVertex = scaleSoftBodyVec3(force, 1.0 / 3.0);
|
|
1206
|
+
|
|
1207
|
+
const double wA = nodeData[iA * 10 + 6];
|
|
1208
|
+
const double wB = nodeData[iB * 10 + 6];
|
|
1209
|
+
const double wC = nodeData[iC * 10 + 6];
|
|
1210
|
+
|
|
1211
|
+
if (wA > 0.0) {
|
|
1212
|
+
nodeData[iA * 10 + 0] += forcePerVertex.x * wA * dtSq;
|
|
1213
|
+
nodeData[iA * 10 + 1] += forcePerVertex.y * wA * dtSq;
|
|
1214
|
+
nodeData[iA * 10 + 2] += forcePerVertex.z * wA * dtSq;
|
|
1215
|
+
}
|
|
1216
|
+
if (wB > 0.0) {
|
|
1217
|
+
nodeData[iB * 10 + 0] += forcePerVertex.x * wB * dtSq;
|
|
1218
|
+
nodeData[iB * 10 + 1] += forcePerVertex.y * wB * dtSq;
|
|
1219
|
+
nodeData[iB * 10 + 2] += forcePerVertex.z * wB * dtSq;
|
|
1220
|
+
}
|
|
1221
|
+
if (wC > 0.0) {
|
|
1222
|
+
nodeData[iC * 10 + 0] += forcePerVertex.x * wC * dtSq;
|
|
1223
|
+
nodeData[iC * 10 + 1] += forcePerVertex.y * wC * dtSq;
|
|
1224
|
+
nodeData[iC * 10 + 2] += forcePerVertex.z * wC * dtSq;
|
|
1225
|
+
}
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
return 0;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
std::int32_t rundot_kinetix_physics_soft_body_handle_inversion(
|
|
1232
|
+
double* nodeData,
|
|
1233
|
+
std::uint32_t nodeCount,
|
|
1234
|
+
double* tetData,
|
|
1235
|
+
std::uint32_t tetCount,
|
|
1236
|
+
double threshold,
|
|
1237
|
+
double stiffness) {
|
|
1238
|
+
if (nodeData == nullptr || tetData == nullptr || nodeCount < 4) return -1;
|
|
1239
|
+
using namespace rundot::kinetix::physics;
|
|
1240
|
+
|
|
1241
|
+
const double kRestoration = (stiffness > 0.0) ? stiffness : 0.8;
|
|
1242
|
+
std::int32_t invertedCount = 0;
|
|
1243
|
+
|
|
1244
|
+
for (std::uint32_t t = 0; t < tetCount; ++t) {
|
|
1245
|
+
const std::uint32_t off = t * kSoftBodyTetStride;
|
|
1246
|
+
const auto i0 = static_cast<std::uint32_t>(tetData[off]);
|
|
1247
|
+
const auto i1 = static_cast<std::uint32_t>(tetData[off + 1]);
|
|
1248
|
+
const auto i2 = static_cast<std::uint32_t>(tetData[off + 2]);
|
|
1249
|
+
const auto i3 = static_cast<std::uint32_t>(tetData[off + 3]);
|
|
1250
|
+
const double restVol = tetData[off + 4];
|
|
1251
|
+
|
|
1252
|
+
if (i0 >= nodeCount || i1 >= nodeCount || i2 >= nodeCount || i3 >= nodeCount) continue;
|
|
1253
|
+
|
|
1254
|
+
SoftBodyVec3 p0{nodeData[i0 * 10], nodeData[i0 * 10 + 1], nodeData[i0 * 10 + 2]};
|
|
1255
|
+
SoftBodyVec3 p1{nodeData[i1 * 10], nodeData[i1 * 10 + 1], nodeData[i1 * 10 + 2]};
|
|
1256
|
+
SoftBodyVec3 p2{nodeData[i2 * 10], nodeData[i2 * 10 + 1], nodeData[i2 * 10 + 2]};
|
|
1257
|
+
SoftBodyVec3 p3{nodeData[i3 * 10], nodeData[i3 * 10 + 1], nodeData[i3 * 10 + 2]};
|
|
1258
|
+
|
|
1259
|
+
const double currentVol = computeTetVolume(p0, p1, p2, p3);
|
|
1260
|
+
tetData[off + 5] = currentVol;
|
|
1261
|
+
|
|
1262
|
+
const double minAllowed = (restVol > 0.0) ? std::max(threshold, 0.02 * restVol) : threshold;
|
|
1263
|
+
if (currentVol < minAllowed) {
|
|
1264
|
+
invertedCount++;
|
|
1265
|
+
tetData[off + 7] = 1.0;
|
|
1266
|
+
|
|
1267
|
+
SoftBodyVec3 faceNormal = crossSoftBodyVec3(subSoftBodyVec3(p1, p0), subSoftBodyVec3(p2, p0));
|
|
1268
|
+
const double faceAreaTimesTwo = lengthSoftBodyVec3(faceNormal);
|
|
1269
|
+
|
|
1270
|
+
if (faceAreaTimesTwo > 1e-12) {
|
|
1271
|
+
const SoftBodyVec3 unitNormal = scaleSoftBodyVec3(faceNormal, 1.0 / faceAreaTimesTwo);
|
|
1272
|
+
const double targetVol = (restVol > 0.0) ? (0.25 * restVol) : 0.01;
|
|
1273
|
+
const double hCurrent = dotSoftBodyVec3(subSoftBodyVec3(p3, p0), unitNormal);
|
|
1274
|
+
const double hTarget = 6.0 * targetVol / faceAreaTimesTwo;
|
|
1275
|
+
const double deficit = hTarget - hCurrent;
|
|
1276
|
+
|
|
1277
|
+
if (deficit > 0.0) {
|
|
1278
|
+
const double wOpp = nodeData[i3 * 10 + 6];
|
|
1279
|
+
const double wFace = (nodeData[i0 * 10 + 6] + nodeData[i1 * 10 + 6] + nodeData[i2 * 10 + 6]) / 3.0;
|
|
1280
|
+
const double totalW = wOpp + wFace;
|
|
1281
|
+
if (totalW > 1e-15) {
|
|
1282
|
+
const double pushOpp = (wOpp / totalW) * deficit * kRestoration;
|
|
1283
|
+
const double pushFace = (wFace / totalW) * deficit * kRestoration / 3.0;
|
|
1284
|
+
|
|
1285
|
+
nodeData[i3 * 10 + 0] += unitNormal.x * pushOpp;
|
|
1286
|
+
nodeData[i3 * 10 + 1] += unitNormal.y * pushOpp;
|
|
1287
|
+
nodeData[i3 * 10 + 2] += unitNormal.z * pushOpp;
|
|
1288
|
+
|
|
1289
|
+
if (nodeData[i0 * 10 + 6] > 0.0) {
|
|
1290
|
+
nodeData[i0 * 10 + 0] -= unitNormal.x * pushFace;
|
|
1291
|
+
nodeData[i0 * 10 + 1] -= unitNormal.y * pushFace;
|
|
1292
|
+
nodeData[i0 * 10 + 2] -= unitNormal.z * pushFace;
|
|
1293
|
+
}
|
|
1294
|
+
if (nodeData[i1 * 10 + 6] > 0.0) {
|
|
1295
|
+
nodeData[i1 * 10 + 0] -= unitNormal.x * pushFace;
|
|
1296
|
+
nodeData[i1 * 10 + 1] -= unitNormal.y * pushFace;
|
|
1297
|
+
nodeData[i1 * 10 + 2] -= unitNormal.z * pushFace;
|
|
1298
|
+
}
|
|
1299
|
+
if (nodeData[i2 * 10 + 6] > 0.0) {
|
|
1300
|
+
nodeData[i2 * 10 + 0] -= unitNormal.x * pushFace;
|
|
1301
|
+
nodeData[i2 * 10 + 1] -= unitNormal.y * pushFace;
|
|
1302
|
+
nodeData[i2 * 10 + 2] -= unitNormal.z * pushFace;
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
} else {
|
|
1308
|
+
tetData[off + 7] = 0.0;
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
|
|
1312
|
+
return invertedCount;
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
namespace {
|
|
1316
|
+
constexpr std::uint32_t kProbeValues = 64;
|
|
1317
|
+
static double gSoftBodyProbeBuffer[kProbeValues]{};
|
|
1318
|
+
static rundot::kinetix::physics::SoftBodySimulation sProbeSim{};
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
std::uint32_t rundot_soft_body_probe_size() {
|
|
1322
|
+
return kProbeValues;
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
const double* rundot_soft_body_probe(std::uint32_t scenario) {
|
|
1326
|
+
using namespace rundot::kinetix::physics;
|
|
1327
|
+
std::memset(gSoftBodyProbeBuffer, 0, sizeof(gSoftBodyProbeBuffer));
|
|
1328
|
+
|
|
1329
|
+
if (scenario == 0) {
|
|
1330
|
+
// Scenario 0: Volume preservation under compression
|
|
1331
|
+
createSoftBodyCube(sProbeSim, {0.0, 1.0, 0.0}, {0.5, 0.5, 0.5}, 1.0);
|
|
1332
|
+
sProbeSim.params.gravityY = 0.0;
|
|
1333
|
+
sProbeSim.params.volumePreservationStiffness = 500.0;
|
|
1334
|
+
sProbeSim.params.shapeMatchingStiffness = 0.5;
|
|
1335
|
+
|
|
1336
|
+
const double initialVolume = sProbeSim.totalRestVolume;
|
|
1337
|
+
|
|
1338
|
+
// Compress top nodes downwards
|
|
1339
|
+
for (std::uint32_t i = 0; i < 8; ++i) {
|
|
1340
|
+
if (sProbeSim.nodes[i].position.y > 1.0) {
|
|
1341
|
+
sProbeSim.nodes[i].position.y -= 0.3;
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
const double compressedVolume = computeTotalEnclosedVolume(sProbeSim);
|
|
1345
|
+
|
|
1346
|
+
// Step simulation for 30 steps
|
|
1347
|
+
for (int t = 0; t < 30; ++t) {
|
|
1348
|
+
stepSoftBody(sProbeSim, {}, 1.0 / 60.0);
|
|
1349
|
+
}
|
|
1350
|
+
const double restoredVolume = computeTotalEnclosedVolume(sProbeSim);
|
|
1351
|
+
|
|
1352
|
+
gSoftBodyProbeBuffer[0] = initialVolume;
|
|
1353
|
+
gSoftBodyProbeBuffer[1] = compressedVolume;
|
|
1354
|
+
gSoftBodyProbeBuffer[2] = restoredVolume;
|
|
1355
|
+
gSoftBodyProbeBuffer[3] = restoredVolume / (initialVolume > 0.0 ? initialVolume : 1.0);
|
|
1356
|
+
gSoftBodyProbeBuffer[4] = static_cast<double>(sProbeSim.invertedCount);
|
|
1357
|
+
} else if (scenario == 1) {
|
|
1358
|
+
// Scenario 1: Hydrostatic pressure inflation
|
|
1359
|
+
createSoftBodyCube(sProbeSim, {0.0, 1.0, 0.0}, {0.5, 0.5, 0.5}, 1.0);
|
|
1360
|
+
sProbeSim.params.gravityY = 0.0;
|
|
1361
|
+
sProbeSim.params.pressure = 20.0;
|
|
1362
|
+
sProbeSim.params.pressureStiffness = 50.0;
|
|
1363
|
+
sProbeSim.params.volumePreservationStiffness = 100.0;
|
|
1364
|
+
|
|
1365
|
+
const double initialVolume = sProbeSim.totalRestVolume;
|
|
1366
|
+
|
|
1367
|
+
for (int t = 0; t < 30; ++t) {
|
|
1368
|
+
stepSoftBody(sProbeSim, {}, 1.0 / 60.0);
|
|
1369
|
+
}
|
|
1370
|
+
const double inflatedVolume = computeTotalEnclosedVolume(sProbeSim);
|
|
1371
|
+
|
|
1372
|
+
gSoftBodyProbeBuffer[0] = initialVolume;
|
|
1373
|
+
gSoftBodyProbeBuffer[1] = inflatedVolume;
|
|
1374
|
+
gSoftBodyProbeBuffer[2] = inflatedVolume / (initialVolume > 0.0 ? initialVolume : 1.0);
|
|
1375
|
+
} else if (scenario == 2) {
|
|
1376
|
+
// Scenario 2: Inverted element handling
|
|
1377
|
+
createSoftBodyCube(sProbeSim, {0.0, 1.0, 0.0}, {0.5, 0.5, 0.5}, 1.0);
|
|
1378
|
+
sProbeSim.params.gravityY = 0.0;
|
|
1379
|
+
sProbeSim.params.inversionRestorationStiffness = 0.9;
|
|
1380
|
+
|
|
1381
|
+
// Force inverted tetrahedron by flipping node 3 past the opposing face
|
|
1382
|
+
const auto& tet0 = sProbeSim.tetrahedra[0];
|
|
1383
|
+
sProbeSim.nodes[tet0.node3].position = {0.0, 1.0, -1.0};
|
|
1384
|
+
|
|
1385
|
+
const double invertedVol = computeTetVolume(
|
|
1386
|
+
sProbeSim.nodes[tet0.node0].position,
|
|
1387
|
+
sProbeSim.nodes[tet0.node1].position,
|
|
1388
|
+
sProbeSim.nodes[tet0.node2].position,
|
|
1389
|
+
sProbeSim.nodes[tet0.node3].position);
|
|
1390
|
+
const double invertedCountBefore = (invertedVol <= 0.0) ? 1.0 : 0.0;
|
|
1391
|
+
|
|
1392
|
+
for (int t = 0; t < 20; ++t) {
|
|
1393
|
+
stepSoftBody(sProbeSim, {}, 1.0 / 60.0);
|
|
1394
|
+
}
|
|
1395
|
+
const double restoredVol = sProbeSim.tetrahedra[0].currentVolume;
|
|
1396
|
+
const auto invertedCountAfter = static_cast<double>(sProbeSim.invertedCount);
|
|
1397
|
+
|
|
1398
|
+
gSoftBodyProbeBuffer[0] = invertedVol;
|
|
1399
|
+
gSoftBodyProbeBuffer[1] = invertedCountBefore;
|
|
1400
|
+
gSoftBodyProbeBuffer[2] = restoredVol;
|
|
1401
|
+
gSoftBodyProbeBuffer[3] = invertedCountAfter;
|
|
1402
|
+
} else if (scenario == 3) {
|
|
1403
|
+
// Scenario 3: Plasticity where declared
|
|
1404
|
+
createSoftBodyCube(sProbeSim, {0.0, 1.0, 0.0}, {0.5, 0.5, 0.5}, 1.0);
|
|
1405
|
+
sProbeSim.params.gravityY = 0.0;
|
|
1406
|
+
sProbeSim.params.edgeYieldThreshold = 0.05; // 5% strain threshold
|
|
1407
|
+
sProbeSim.params.edgePlasticityRate = 0.5;
|
|
1408
|
+
|
|
1409
|
+
const double initialRestLength = sProbeSim.edges[0].restLength;
|
|
1410
|
+
|
|
1411
|
+
// Clamp left side at rest, clamp right side at +30% stretch (exceeding 5% yield threshold)
|
|
1412
|
+
for (std::uint32_t i = 0; i < sProbeSim.nodeCount; ++i) {
|
|
1413
|
+
if (sProbeSim.nodes[i].position.x <= 0.0) {
|
|
1414
|
+
sProbeSim.nodes[i].inverseMass = 0.0;
|
|
1415
|
+
} else {
|
|
1416
|
+
sProbeSim.nodes[i].position.x += 0.3;
|
|
1417
|
+
sProbeSim.nodes[i].previousPosition = sProbeSim.nodes[i].position;
|
|
1418
|
+
sProbeSim.nodes[i].inverseMass = 0.0;
|
|
1419
|
+
}
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
// Step with clamped ends to accumulate plastic strain
|
|
1423
|
+
for (int t = 0; t < 10; ++t) {
|
|
1424
|
+
stepSoftBody(sProbeSim, {}, 1.0 / 60.0);
|
|
1425
|
+
}
|
|
1426
|
+
|
|
1427
|
+
// Release right side so it relaxes to the new yielded rest length
|
|
1428
|
+
const double nodeMass = 1.0 / 8.0;
|
|
1429
|
+
for (std::uint32_t i = 0; i < sProbeSim.nodeCount; ++i) {
|
|
1430
|
+
if (sProbeSim.nodes[i].position.x > 0.0) {
|
|
1431
|
+
sProbeSim.nodes[i].inverseMass = 1.0 / nodeMass;
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
for (int t = 0; t < 10; ++t) {
|
|
1435
|
+
stepSoftBody(sProbeSim, {}, 1.0 / 60.0);
|
|
1436
|
+
}
|
|
1437
|
+
|
|
1438
|
+
const double finalRestLength = sProbeSim.edges[0].restLength;
|
|
1439
|
+
const double plasticStrain = sProbeSim.edges[0].plasticStrain;
|
|
1440
|
+
|
|
1441
|
+
gSoftBodyProbeBuffer[0] = initialRestLength;
|
|
1442
|
+
gSoftBodyProbeBuffer[1] = finalRestLength;
|
|
1443
|
+
gSoftBodyProbeBuffer[2] = plasticStrain;
|
|
1444
|
+
}
|
|
1445
|
+
|
|
1446
|
+
return gSoftBodyProbeBuffer;
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
}
|