@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,2433 @@
|
|
|
1
|
+
#include "planar_deformable.hpp"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <array>
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
#include <cstdint>
|
|
8
|
+
#include <cstring>
|
|
9
|
+
#include <limits>
|
|
10
|
+
#include <span>
|
|
11
|
+
|
|
12
|
+
namespace rundot::kinetix::physics {
|
|
13
|
+
|
|
14
|
+
namespace {
|
|
15
|
+
|
|
16
|
+
constexpr double kPi = 3.14159265358979323846;
|
|
17
|
+
constexpr double kTwoPi = 6.28318530717958647692;
|
|
18
|
+
constexpr double kEpsilon = 1e-12;
|
|
19
|
+
|
|
20
|
+
inline double clampVal(double val, double minVal, double maxVal) {
|
|
21
|
+
if (val < minVal) return minVal;
|
|
22
|
+
if (val > maxVal) return maxVal;
|
|
23
|
+
return val;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
inline double minVal(double a, double b) {
|
|
27
|
+
return (a < b) ? a : b;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
inline double maxVal(double a, double b) {
|
|
31
|
+
return (a > b) ? a : b;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
inline std::uint32_t fnv1aChecksum(const std::uint8_t* data, std::size_t size) {
|
|
35
|
+
std::uint32_t hash = 2166136261u;
|
|
36
|
+
for (std::size_t i = 0; i < size; ++i) {
|
|
37
|
+
hash ^= static_cast<std::uint32_t>(data[i]);
|
|
38
|
+
hash *= 16777619u;
|
|
39
|
+
}
|
|
40
|
+
return hash;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
} // namespace
|
|
44
|
+
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
// Pure Deterministic Math Functions
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
PlanarVec2 addPlanarVec2(const PlanarVec2& a, const PlanarVec2& b) {
|
|
50
|
+
return {a.x + b.x, a.y + b.y};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
PlanarVec2 subPlanarVec2(const PlanarVec2& a, const PlanarVec2& b) {
|
|
54
|
+
return {a.x - b.x, a.y - b.y};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
PlanarVec2 scalePlanarVec2(const PlanarVec2& v, double s) {
|
|
58
|
+
return {v.x * s, v.y * s};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
double dotPlanarVec2(const PlanarVec2& a, const PlanarVec2& b) {
|
|
62
|
+
return a.x * b.x + a.y * b.y;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
double crossPlanarVec2(const PlanarVec2& a, const PlanarVec2& b) {
|
|
66
|
+
return a.x * b.y - a.y * b.x;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
PlanarVec2 crossScalarPlanarVec2(double s, const PlanarVec2& v) {
|
|
70
|
+
return {-s * v.y, s * v.x};
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
PlanarVec2 crossPlanarVec2Scalar(const PlanarVec2& v, double s) {
|
|
74
|
+
return {s * v.y, -s * v.x};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
double lengthSquaredPlanarVec2(const PlanarVec2& v) {
|
|
78
|
+
return v.x * v.x + v.y * v.y;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
double lengthPlanarVec2(const PlanarVec2& v) {
|
|
82
|
+
return std::sqrt(v.x * v.x + v.y * v.y);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
PlanarVec2 normalizePlanarVec2(const PlanarVec2& v) {
|
|
86
|
+
const double lenSq = lengthSquaredPlanarVec2(v);
|
|
87
|
+
if (lenSq < kEpsilon) {
|
|
88
|
+
return {0.0, 0.0};
|
|
89
|
+
}
|
|
90
|
+
const double invLen = 1.0 / std::sqrt(lenSq);
|
|
91
|
+
return {v.x * invLen, v.y * invLen};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
double distancePlanarVec2(const PlanarVec2& a, const PlanarVec2& b) {
|
|
95
|
+
return lengthPlanarVec2(subPlanarVec2(a, b));
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
double distanceSquaredPlanarVec2(const PlanarVec2& a, const PlanarVec2& b) {
|
|
99
|
+
return lengthSquaredPlanarVec2(subPlanarVec2(a, b));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
PlanarVec2 perpendicularPlanarVec2(const PlanarVec2& v) {
|
|
103
|
+
return {-v.y, v.x};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
PlanarVec2 rotatePlanarVec2(const PlanarVec2& v, double angle) {
|
|
107
|
+
const double c = std::cos(angle);
|
|
108
|
+
const double s = std::sin(angle);
|
|
109
|
+
return {v.x * c - v.y * s, v.x * s + v.y * c};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
PlanarVec2 unrotatePlanarVec2(const PlanarVec2& v, double angle) {
|
|
113
|
+
const double c = std::cos(angle);
|
|
114
|
+
const double s = std::sin(angle);
|
|
115
|
+
return {v.x * c + v.y * s, -v.x * s + v.y * c};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
PlanarMat2 multiplyPlanarMat2(const PlanarMat2& a, const PlanarMat2& b) {
|
|
119
|
+
PlanarMat2 r{};
|
|
120
|
+
r.m[0][0] = a.m[0][0] * b.m[0][0] + a.m[0][1] * b.m[1][0];
|
|
121
|
+
r.m[0][1] = a.m[0][0] * b.m[0][1] + a.m[0][1] * b.m[1][1];
|
|
122
|
+
r.m[1][0] = a.m[1][0] * b.m[0][0] + a.m[1][1] * b.m[1][0];
|
|
123
|
+
r.m[1][1] = a.m[1][0] * b.m[0][1] + a.m[1][1] * b.m[1][1];
|
|
124
|
+
return r;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
PlanarVec2 multiplyPlanarMat2Vec2(const PlanarMat2& m, const PlanarVec2& v) {
|
|
128
|
+
return {
|
|
129
|
+
m.m[0][0] * v.x + m.m[0][1] * v.y,
|
|
130
|
+
m.m[1][0] * v.x + m.m[1][1] * v.y
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
bool invertPlanarMat2(const PlanarMat2& m, PlanarMat2& outInverse) {
|
|
135
|
+
const double det = m.m[0][0] * m.m[1][1] - m.m[0][1] * m.m[1][0];
|
|
136
|
+
if (std::abs(det) < kEpsilon) {
|
|
137
|
+
outInverse = PlanarMat2::zero();
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
const double invDet = 1.0 / det;
|
|
141
|
+
outInverse.m[0][0] = m.m[1][1] * invDet;
|
|
142
|
+
outInverse.m[0][1] = -m.m[0][1] * invDet;
|
|
143
|
+
outInverse.m[1][0] = -m.m[1][0] * invDet;
|
|
144
|
+
outInverse.m[1][1] = m.m[0][0] * invDet;
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// ---------------------------------------------------------------------------
|
|
149
|
+
// Mass Model & Properties
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
PlanarMassProperties computePlanarMassProperties(
|
|
153
|
+
std::span<const PlanarDeformableNode> nodes) {
|
|
154
|
+
PlanarMassProperties props{};
|
|
155
|
+
if (nodes.empty()) {
|
|
156
|
+
return props;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
props.boundingBox.min = nodes[0].position;
|
|
160
|
+
props.boundingBox.max = nodes[0].position;
|
|
161
|
+
|
|
162
|
+
double totalMass = 0.0;
|
|
163
|
+
PlanarVec2 weightedCom{0.0, 0.0};
|
|
164
|
+
|
|
165
|
+
for (std::size_t i = 0; i < nodes.size(); ++i) {
|
|
166
|
+
const auto& node = nodes[i];
|
|
167
|
+
props.boundingBox.min.x = minVal(props.boundingBox.min.x, node.position.x - node.radius);
|
|
168
|
+
props.boundingBox.min.y = minVal(props.boundingBox.min.y, node.position.y - node.radius);
|
|
169
|
+
props.boundingBox.max.x = maxVal(props.boundingBox.max.x, node.position.x + node.radius);
|
|
170
|
+
props.boundingBox.max.y = maxVal(props.boundingBox.max.y, node.position.y + node.radius);
|
|
171
|
+
|
|
172
|
+
const double m = (node.mass > 0.0) ? node.mass : 0.0;
|
|
173
|
+
totalMass += m;
|
|
174
|
+
weightedCom.x += node.position.x * m;
|
|
175
|
+
weightedCom.y += node.position.y * m;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
props.totalMass = totalMass;
|
|
179
|
+
if (totalMass > kEpsilon) {
|
|
180
|
+
props.centerOfMass = {weightedCom.x / totalMass, weightedCom.y / totalMass};
|
|
181
|
+
double inertia = 0.0;
|
|
182
|
+
for (std::size_t i = 0; i < nodes.size(); ++i) {
|
|
183
|
+
const auto& node = nodes[i];
|
|
184
|
+
const double m = (node.mass > 0.0) ? node.mass : 0.0;
|
|
185
|
+
const double distSq = distanceSquaredPlanarVec2(node.position, props.centerOfMass);
|
|
186
|
+
inertia += m * distSq;
|
|
187
|
+
}
|
|
188
|
+
props.momentOfInertia = inertia;
|
|
189
|
+
} else {
|
|
190
|
+
props.centerOfMass = {0.0, 0.0};
|
|
191
|
+
props.momentOfInertia = 0.0;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return props;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
PlanarStatus distributeRopeSegmentMass(
|
|
198
|
+
std::span<PlanarDeformableNode> nodes,
|
|
199
|
+
std::span<const PlanarSegment> segments,
|
|
200
|
+
double linearDensity) {
|
|
201
|
+
if (nodes.empty() || segments.empty() || linearDensity <= 0.0) {
|
|
202
|
+
return PlanarStatus::InvalidInput;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
for (std::size_t i = 0; i < nodes.size(); ++i) {
|
|
206
|
+
nodes[i].mass = 0.0;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
for (std::size_t i = 0; i < segments.size(); ++i) {
|
|
210
|
+
const auto& seg = segments[i];
|
|
211
|
+
if (seg.nodeA >= nodes.size() || seg.nodeB >= nodes.size()) {
|
|
212
|
+
return PlanarStatus::InvalidInput;
|
|
213
|
+
}
|
|
214
|
+
const double segLength = (seg.restLength > 0.0)
|
|
215
|
+
? seg.restLength
|
|
216
|
+
: distancePlanarVec2(nodes[seg.nodeA].position, nodes[seg.nodeB].position);
|
|
217
|
+
const double segMass = segLength * linearDensity;
|
|
218
|
+
const double halfMass = segMass * 0.5;
|
|
219
|
+
|
|
220
|
+
nodes[seg.nodeA].mass += halfMass;
|
|
221
|
+
nodes[seg.nodeB].mass += halfMass;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
for (std::size_t i = 0; i < nodes.size(); ++i) {
|
|
225
|
+
if ((nodes[i].flags & static_cast<std::uint32_t>(PlanarNodeFlags::Pinned)) != 0) {
|
|
226
|
+
nodes[i].inverseMass = 0.0;
|
|
227
|
+
} else if (nodes[i].mass > kEpsilon) {
|
|
228
|
+
nodes[i].inverseMass = 1.0 / nodes[i].mass;
|
|
229
|
+
} else {
|
|
230
|
+
nodes[i].inverseMass = 1.0;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
return PlanarStatus::Success;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
PlanarStatus distributeClothTriangleMass(
|
|
238
|
+
std::span<PlanarDeformableNode> nodes,
|
|
239
|
+
std::span<const PlanarFace> faces,
|
|
240
|
+
double surfaceDensity) {
|
|
241
|
+
if (nodes.empty() || faces.empty() || surfaceDensity <= 0.0) {
|
|
242
|
+
return PlanarStatus::InvalidInput;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
for (std::size_t i = 0; i < nodes.size(); ++i) {
|
|
246
|
+
nodes[i].mass = 0.0;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
for (std::size_t i = 0; i < faces.size(); ++i) {
|
|
250
|
+
const auto& face = faces[i];
|
|
251
|
+
if (face.nodeA >= nodes.size() || face.nodeB >= nodes.size() || face.nodeC >= nodes.size()) {
|
|
252
|
+
return PlanarStatus::InvalidInput;
|
|
253
|
+
}
|
|
254
|
+
const double area = (face.restArea > 0.0)
|
|
255
|
+
? face.restArea
|
|
256
|
+
: std::abs(crossPlanarVec2(
|
|
257
|
+
subPlanarVec2(nodes[face.nodeB].position, nodes[face.nodeA].position),
|
|
258
|
+
subPlanarVec2(nodes[face.nodeC].position, nodes[face.nodeA].position))) * 0.5;
|
|
259
|
+
const double faceMass = area * surfaceDensity;
|
|
260
|
+
const double thirdMass = faceMass / 3.0;
|
|
261
|
+
|
|
262
|
+
nodes[face.nodeA].mass += thirdMass;
|
|
263
|
+
nodes[face.nodeB].mass += thirdMass;
|
|
264
|
+
nodes[face.nodeC].mass += thirdMass;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
for (std::size_t i = 0; i < nodes.size(); ++i) {
|
|
268
|
+
if ((nodes[i].flags & static_cast<std::uint32_t>(PlanarNodeFlags::Pinned)) != 0) {
|
|
269
|
+
nodes[i].inverseMass = 0.0;
|
|
270
|
+
} else if (nodes[i].mass > kEpsilon) {
|
|
271
|
+
nodes[i].inverseMass = 1.0 / nodes[i].mass;
|
|
272
|
+
} else {
|
|
273
|
+
nodes[i].inverseMass = 1.0;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return PlanarStatus::Success;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// ---------------------------------------------------------------------------
|
|
281
|
+
// Element Construction Helpers
|
|
282
|
+
// ---------------------------------------------------------------------------
|
|
283
|
+
|
|
284
|
+
PlanarStatus addRope2D(
|
|
285
|
+
PlanarDeformableWorld& world,
|
|
286
|
+
PlanarVec2 start,
|
|
287
|
+
PlanarVec2 end,
|
|
288
|
+
std::uint32_t segmentCount,
|
|
289
|
+
double totalMass,
|
|
290
|
+
double stretchStiffness,
|
|
291
|
+
double bendStiffness,
|
|
292
|
+
double radius,
|
|
293
|
+
std::uint32_t* outStartNodeIndex,
|
|
294
|
+
std::uint32_t* outEndNodeIndex) {
|
|
295
|
+
if (segmentCount == 0 || segmentCount >= kPlanarMaxSegments) {
|
|
296
|
+
return PlanarStatus::InvalidInput;
|
|
297
|
+
}
|
|
298
|
+
const std::uint32_t nodeCount = segmentCount + 1;
|
|
299
|
+
if (world.nodeCount + nodeCount > kPlanarMaxNodes ||
|
|
300
|
+
world.segmentCount + segmentCount > kPlanarMaxSegments ||
|
|
301
|
+
world.distanceConstraintCount + segmentCount > kPlanarMaxDistanceConstraints) {
|
|
302
|
+
return PlanarStatus::BufferOverflow;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
const std::uint32_t baseNode = world.nodeCount;
|
|
306
|
+
const PlanarVec2 dir = subPlanarVec2(end, start);
|
|
307
|
+
const double totalLen = lengthPlanarVec2(dir);
|
|
308
|
+
const double segLen = totalLen / static_cast<double>(segmentCount);
|
|
309
|
+
const double nodeMass = (totalMass > 0.0) ? (totalMass / static_cast<double>(nodeCount)) : 0.1;
|
|
310
|
+
const double invMass = (nodeMass > 0.0) ? (1.0 / nodeMass) : 10.0;
|
|
311
|
+
|
|
312
|
+
for (std::uint32_t i = 0; i < nodeCount; ++i) {
|
|
313
|
+
const double t = static_cast<double>(i) / static_cast<double>(segmentCount);
|
|
314
|
+
PlanarDeformableNode node{};
|
|
315
|
+
node.position = {start.x + dir.x * t, start.y + dir.y * t};
|
|
316
|
+
node.previousPosition = node.position;
|
|
317
|
+
node.velocity = {0.0, 0.0};
|
|
318
|
+
node.force = {0.0, 0.0};
|
|
319
|
+
node.mass = nodeMass;
|
|
320
|
+
node.inverseMass = invMass;
|
|
321
|
+
node.radius = (radius > 0.0) ? radius : 0.05;
|
|
322
|
+
node.damping = world.params.damping;
|
|
323
|
+
node.flags = static_cast<std::uint32_t>(PlanarNodeFlags::Rope);
|
|
324
|
+
|
|
325
|
+
world.nodes[world.nodeCount++] = node;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
for (std::uint32_t i = 0; i < segmentCount; ++i) {
|
|
329
|
+
const std::uint32_t na = baseNode + i;
|
|
330
|
+
const std::uint32_t nb = baseNode + i + 1;
|
|
331
|
+
|
|
332
|
+
PlanarSegment seg{};
|
|
333
|
+
seg.nodeA = na;
|
|
334
|
+
seg.nodeB = nb;
|
|
335
|
+
seg.restLength = segLen;
|
|
336
|
+
seg.radius = (radius > 0.0) ? radius : 0.05;
|
|
337
|
+
seg.tension = 0.0;
|
|
338
|
+
seg.broken = false;
|
|
339
|
+
world.segments[world.segmentCount++] = seg;
|
|
340
|
+
|
|
341
|
+
PlanarDistanceConstraint distConst{};
|
|
342
|
+
distConst.nodeA = na;
|
|
343
|
+
distConst.nodeB = nb;
|
|
344
|
+
distConst.restLength = segLen;
|
|
345
|
+
distConst.stiffness = clampVal(stretchStiffness, 0.0, 1.0);
|
|
346
|
+
distConst.damping = 0.01;
|
|
347
|
+
distConst.tension = 0.0;
|
|
348
|
+
distConst.breakThreshold = world.params.breakThresholdDefault;
|
|
349
|
+
distConst.broken = false;
|
|
350
|
+
world.distanceConstraints[world.distanceConstraintCount++] = distConst;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
if (bendStiffness > 0.0 && segmentCount >= 2) {
|
|
354
|
+
for (std::uint32_t i = 1; i < segmentCount; ++i) {
|
|
355
|
+
if (world.bendConstraintCount >= kPlanarMaxBendConstraints) break;
|
|
356
|
+
PlanarBendConstraint bend{};
|
|
357
|
+
bend.nodeA = baseNode + i - 1;
|
|
358
|
+
bend.nodeB = baseNode + i;
|
|
359
|
+
bend.nodeC = baseNode + i + 1;
|
|
360
|
+
bend.restAngle = kPi;
|
|
361
|
+
bend.stiffness = clampVal(bendStiffness, 0.0, 1.0);
|
|
362
|
+
bend.currentAngle = kPi;
|
|
363
|
+
bend.bendingMoment = 0.0;
|
|
364
|
+
bend.breakThreshold = world.params.breakThresholdDefault;
|
|
365
|
+
bend.broken = false;
|
|
366
|
+
world.bendConstraints[world.bendConstraintCount++] = bend;
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (outStartNodeIndex) *outStartNodeIndex = baseNode;
|
|
371
|
+
if (outEndNodeIndex) *outEndNodeIndex = baseNode + nodeCount - 1;
|
|
372
|
+
|
|
373
|
+
return PlanarStatus::Success;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
PlanarStatus addClothGrid2D(
|
|
377
|
+
PlanarDeformableWorld& world,
|
|
378
|
+
PlanarVec2 origin,
|
|
379
|
+
double width,
|
|
380
|
+
double height,
|
|
381
|
+
std::uint32_t cols,
|
|
382
|
+
std::uint32_t rows,
|
|
383
|
+
double totalMass,
|
|
384
|
+
double stretchStiffness,
|
|
385
|
+
double shearStiffness,
|
|
386
|
+
double bendStiffness,
|
|
387
|
+
double areaStiffness,
|
|
388
|
+
double radius) {
|
|
389
|
+
if (cols < 2 || rows < 2 || width <= 0.0 || height <= 0.0) {
|
|
390
|
+
return PlanarStatus::InvalidInput;
|
|
391
|
+
}
|
|
392
|
+
const std::uint32_t totalNodes = cols * rows;
|
|
393
|
+
if (world.nodeCount + totalNodes > kPlanarMaxNodes) {
|
|
394
|
+
return PlanarStatus::BufferOverflow;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const std::uint32_t baseNode = world.nodeCount;
|
|
398
|
+
const double dx = width / static_cast<double>(cols - 1);
|
|
399
|
+
const double dy = height / static_cast<double>(rows - 1);
|
|
400
|
+
const double nodeMass = (totalMass > 0.0) ? (totalMass / static_cast<double>(totalNodes)) : 0.1;
|
|
401
|
+
const double invMass = (nodeMass > 0.0) ? (1.0 / nodeMass) : 10.0;
|
|
402
|
+
const double diagLen = std::sqrt(dx * dx + dy * dy);
|
|
403
|
+
const double triRestArea = 0.5 * dx * dy;
|
|
404
|
+
|
|
405
|
+
// 1. Create nodes
|
|
406
|
+
for (std::uint32_t r = 0; r < rows; ++r) {
|
|
407
|
+
for (std::uint32_t c = 0; c < cols; ++c) {
|
|
408
|
+
PlanarDeformableNode node{};
|
|
409
|
+
node.position = {origin.x + static_cast<double>(c) * dx, origin.y + static_cast<double>(r) * dy};
|
|
410
|
+
node.previousPosition = node.position;
|
|
411
|
+
node.velocity = {0.0, 0.0};
|
|
412
|
+
node.force = {0.0, 0.0};
|
|
413
|
+
node.mass = nodeMass;
|
|
414
|
+
node.inverseMass = invMass;
|
|
415
|
+
node.radius = (radius > 0.0) ? radius : 0.05;
|
|
416
|
+
node.damping = world.params.damping;
|
|
417
|
+
node.flags = static_cast<std::uint32_t>(PlanarNodeFlags::Cloth);
|
|
418
|
+
|
|
419
|
+
world.nodes[world.nodeCount++] = node;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
auto getNodeIdx = [baseNode, cols](std::uint32_t c, std::uint32_t r) -> std::uint32_t {
|
|
424
|
+
return baseNode + r * cols + c;
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
// 2. Structural distance constraints (horizontal & vertical)
|
|
428
|
+
for (std::uint32_t r = 0; r < rows; ++r) {
|
|
429
|
+
for (std::uint32_t c = 0; c < cols; ++c) {
|
|
430
|
+
if (c + 1 < cols && world.distanceConstraintCount < kPlanarMaxDistanceConstraints) {
|
|
431
|
+
PlanarDistanceConstraint dc{};
|
|
432
|
+
dc.nodeA = getNodeIdx(c, r);
|
|
433
|
+
dc.nodeB = getNodeIdx(c + 1, r);
|
|
434
|
+
dc.restLength = dx;
|
|
435
|
+
dc.stiffness = clampVal(stretchStiffness, 0.0, 1.0);
|
|
436
|
+
dc.damping = 0.01;
|
|
437
|
+
dc.breakThreshold = world.params.breakThresholdDefault;
|
|
438
|
+
world.distanceConstraints[world.distanceConstraintCount++] = dc;
|
|
439
|
+
}
|
|
440
|
+
if (r + 1 < rows && world.distanceConstraintCount < kPlanarMaxDistanceConstraints) {
|
|
441
|
+
PlanarDistanceConstraint dc{};
|
|
442
|
+
dc.nodeA = getNodeIdx(c, r);
|
|
443
|
+
dc.nodeB = getNodeIdx(c, r + 1);
|
|
444
|
+
dc.restLength = dy;
|
|
445
|
+
dc.stiffness = clampVal(stretchStiffness, 0.0, 1.0);
|
|
446
|
+
dc.damping = 0.01;
|
|
447
|
+
dc.breakThreshold = world.params.breakThresholdDefault;
|
|
448
|
+
world.distanceConstraints[world.distanceConstraintCount++] = dc;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
// 3. Shear constraints (diagonals of quads)
|
|
454
|
+
if (shearStiffness > 0.0) {
|
|
455
|
+
for (std::uint32_t r = 0; r + 1 < rows; ++r) {
|
|
456
|
+
for (std::uint32_t c = 0; c + 1 < cols; ++c) {
|
|
457
|
+
if (world.shearConstraintCount + 2 > kPlanarMaxShearConstraints) break;
|
|
458
|
+
// Diagonal 1: (c, r) to (c+1, r+1)
|
|
459
|
+
PlanarShearConstraint sc1{};
|
|
460
|
+
sc1.nodeA = getNodeIdx(c, r);
|
|
461
|
+
sc1.nodeB = getNodeIdx(c + 1, r + 1);
|
|
462
|
+
sc1.restLength = diagLen;
|
|
463
|
+
sc1.stiffness = clampVal(shearStiffness, 0.0, 1.0);
|
|
464
|
+
sc1.currentLength = diagLen;
|
|
465
|
+
sc1.breakThreshold = world.params.breakThresholdDefault;
|
|
466
|
+
world.shearConstraints[world.shearConstraintCount++] = sc1;
|
|
467
|
+
|
|
468
|
+
// Diagonal 2: (c+1, r) to (c, r+1)
|
|
469
|
+
PlanarShearConstraint sc2{};
|
|
470
|
+
sc2.nodeA = getNodeIdx(c + 1, r);
|
|
471
|
+
sc2.nodeB = getNodeIdx(c, r + 1);
|
|
472
|
+
sc2.restLength = diagLen;
|
|
473
|
+
sc2.stiffness = clampVal(shearStiffness, 0.0, 1.0);
|
|
474
|
+
sc2.currentLength = diagLen;
|
|
475
|
+
sc2.breakThreshold = world.params.breakThresholdDefault;
|
|
476
|
+
world.shearConstraints[world.shearConstraintCount++] = sc2;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// 4. Bending constraints (across 2 segments in row / column)
|
|
482
|
+
if (bendStiffness > 0.0) {
|
|
483
|
+
// Horizontal bend
|
|
484
|
+
for (std::uint32_t r = 0; r < rows; ++r) {
|
|
485
|
+
for (std::uint32_t c = 1; c + 1 < cols; ++c) {
|
|
486
|
+
if (world.bendConstraintCount >= kPlanarMaxBendConstraints) break;
|
|
487
|
+
PlanarBendConstraint bc{};
|
|
488
|
+
bc.nodeA = getNodeIdx(c - 1, r);
|
|
489
|
+
bc.nodeB = getNodeIdx(c, r);
|
|
490
|
+
bc.nodeC = getNodeIdx(c + 1, r);
|
|
491
|
+
bc.restAngle = kPi;
|
|
492
|
+
bc.stiffness = clampVal(bendStiffness, 0.0, 1.0);
|
|
493
|
+
bc.currentAngle = kPi;
|
|
494
|
+
bc.breakThreshold = world.params.breakThresholdDefault;
|
|
495
|
+
world.bendConstraints[world.bendConstraintCount++] = bc;
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
// Vertical bend
|
|
499
|
+
for (std::uint32_t c = 0; c < cols; ++c) {
|
|
500
|
+
for (std::uint32_t r = 1; r + 1 < rows; ++r) {
|
|
501
|
+
if (world.bendConstraintCount >= kPlanarMaxBendConstraints) break;
|
|
502
|
+
PlanarBendConstraint bc{};
|
|
503
|
+
bc.nodeA = getNodeIdx(c, r - 1);
|
|
504
|
+
bc.nodeB = getNodeIdx(c, r);
|
|
505
|
+
bc.nodeC = getNodeIdx(c, r + 1);
|
|
506
|
+
bc.restAngle = kPi;
|
|
507
|
+
bc.stiffness = clampVal(bendStiffness, 0.0, 1.0);
|
|
508
|
+
bc.currentAngle = kPi;
|
|
509
|
+
bc.breakThreshold = world.params.breakThresholdDefault;
|
|
510
|
+
world.bendConstraints[world.bendConstraintCount++] = bc;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// 5. Triangular faces and Area preservation constraints
|
|
516
|
+
for (std::uint32_t r = 0; r + 1 < rows; ++r) {
|
|
517
|
+
for (std::uint32_t c = 0; c + 1 < cols; ++c) {
|
|
518
|
+
const std::uint32_t n00 = getNodeIdx(c, r);
|
|
519
|
+
const std::uint32_t n10 = getNodeIdx(c + 1, r);
|
|
520
|
+
const std::uint32_t n11 = getNodeIdx(c + 1, r + 1);
|
|
521
|
+
const std::uint32_t n01 = getNodeIdx(c, r + 1);
|
|
522
|
+
|
|
523
|
+
// Triangle 1: (n00, n10, n11)
|
|
524
|
+
if (world.faceCount < kPlanarMaxFaces) {
|
|
525
|
+
PlanarFace f1{};
|
|
526
|
+
f1.nodeA = n00;
|
|
527
|
+
f1.nodeB = n10;
|
|
528
|
+
f1.nodeC = n11;
|
|
529
|
+
f1.restArea = triRestArea;
|
|
530
|
+
f1.currentArea = triRestArea;
|
|
531
|
+
f1.normal = {0.0, 1.0};
|
|
532
|
+
world.faces[world.faceCount++] = f1;
|
|
533
|
+
}
|
|
534
|
+
// Triangle 2: (n00, n11, n01)
|
|
535
|
+
if (world.faceCount < kPlanarMaxFaces) {
|
|
536
|
+
PlanarFace f2{};
|
|
537
|
+
f2.nodeA = n00;
|
|
538
|
+
f2.nodeB = n11;
|
|
539
|
+
f2.nodeC = n01;
|
|
540
|
+
f2.restArea = triRestArea;
|
|
541
|
+
f2.currentArea = triRestArea;
|
|
542
|
+
f2.normal = {0.0, 1.0};
|
|
543
|
+
world.faces[world.faceCount++] = f2;
|
|
544
|
+
}
|
|
545
|
+
|
|
546
|
+
// Area constraints
|
|
547
|
+
if (areaStiffness > 0.0) {
|
|
548
|
+
if (world.areaConstraintCount < kPlanarMaxAreaConstraints) {
|
|
549
|
+
PlanarAreaConstraint ac1{};
|
|
550
|
+
ac1.nodeA = n00;
|
|
551
|
+
ac1.nodeB = n10;
|
|
552
|
+
ac1.nodeC = n11;
|
|
553
|
+
ac1.restArea = triRestArea;
|
|
554
|
+
ac1.currentArea = triRestArea;
|
|
555
|
+
ac1.stiffness = clampVal(areaStiffness, 0.0, 1.0);
|
|
556
|
+
ac1.breakThreshold = world.params.breakThresholdDefault;
|
|
557
|
+
world.areaConstraints[world.areaConstraintCount++] = ac1;
|
|
558
|
+
}
|
|
559
|
+
if (world.areaConstraintCount < kPlanarMaxAreaConstraints) {
|
|
560
|
+
PlanarAreaConstraint ac2{};
|
|
561
|
+
ac2.nodeA = n00;
|
|
562
|
+
ac2.nodeB = n11;
|
|
563
|
+
ac2.nodeC = n01;
|
|
564
|
+
ac2.restArea = triRestArea;
|
|
565
|
+
ac2.currentArea = triRestArea;
|
|
566
|
+
ac2.stiffness = clampVal(areaStiffness, 0.0, 1.0);
|
|
567
|
+
ac2.breakThreshold = world.params.breakThresholdDefault;
|
|
568
|
+
world.areaConstraints[world.areaConstraintCount++] = ac2;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
return PlanarStatus::Success;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
PlanarStatus addParticleEmitter2D(
|
|
578
|
+
PlanarDeformableWorld& world,
|
|
579
|
+
const PlanarEmitter& emitter,
|
|
580
|
+
std::uint32_t* outEmitterIndex) {
|
|
581
|
+
if (world.emitterCount >= kPlanarMaxEmitters) {
|
|
582
|
+
return PlanarStatus::BufferOverflow;
|
|
583
|
+
}
|
|
584
|
+
const std::uint32_t idx = world.emitterCount++;
|
|
585
|
+
world.emitters[idx] = emitter;
|
|
586
|
+
if (outEmitterIndex) *outEmitterIndex = idx;
|
|
587
|
+
return PlanarStatus::Success;
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
PlanarStatus addParticle2D(
|
|
591
|
+
PlanarDeformableWorld& world,
|
|
592
|
+
PlanarVec2 pos,
|
|
593
|
+
PlanarVec2 vel,
|
|
594
|
+
double radius,
|
|
595
|
+
double mass,
|
|
596
|
+
double lifetime,
|
|
597
|
+
std::uint32_t* outParticleIndex) {
|
|
598
|
+
std::uint32_t targetIdx = world.particleCount;
|
|
599
|
+
for (std::uint32_t i = 0; i < world.particleCount; ++i) {
|
|
600
|
+
if (!world.particles[i].active) {
|
|
601
|
+
targetIdx = i;
|
|
602
|
+
break;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
if (targetIdx >= kPlanarMaxParticles) {
|
|
607
|
+
return PlanarStatus::BufferOverflow;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
const double pMass = (mass > 0.0) ? mass : 0.1;
|
|
611
|
+
PlanarParticle p{};
|
|
612
|
+
p.position = pos;
|
|
613
|
+
p.previousPosition = pos;
|
|
614
|
+
p.velocity = vel;
|
|
615
|
+
p.force = {0.0, 0.0};
|
|
616
|
+
p.radius = (radius > 0.0) ? radius : 0.05;
|
|
617
|
+
p.mass = pMass;
|
|
618
|
+
p.inverseMass = 1.0 / pMass;
|
|
619
|
+
p.age = 0.0;
|
|
620
|
+
p.lifetime = (lifetime > 0.0) ? lifetime : 5.0;
|
|
621
|
+
p.active = true;
|
|
622
|
+
|
|
623
|
+
world.particles[targetIdx] = p;
|
|
624
|
+
if (targetIdx == world.particleCount) {
|
|
625
|
+
world.particleCount++;
|
|
626
|
+
}
|
|
627
|
+
world.activeParticleCount++;
|
|
628
|
+
|
|
629
|
+
if (outParticleIndex) *outParticleIndex = targetIdx;
|
|
630
|
+
return PlanarStatus::Success;
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
PlanarStatus addPinAnchor2D(
|
|
634
|
+
PlanarDeformableWorld& world,
|
|
635
|
+
std::uint32_t nodeIndex,
|
|
636
|
+
std::int32_t bodyIndex,
|
|
637
|
+
PlanarVec2 localAnchor,
|
|
638
|
+
double stiffness,
|
|
639
|
+
std::uint32_t* outAnchorIndex) {
|
|
640
|
+
if (nodeIndex >= world.nodeCount) {
|
|
641
|
+
return PlanarStatus::InvalidInput;
|
|
642
|
+
}
|
|
643
|
+
if (world.anchorCount >= kPlanarMaxAnchors) {
|
|
644
|
+
return PlanarStatus::BufferOverflow;
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
const std::uint32_t idx = world.anchorCount++;
|
|
648
|
+
PlanarAnchor anchor{};
|
|
649
|
+
anchor.nodeIndex = nodeIndex;
|
|
650
|
+
anchor.bodyIndex = bodyIndex;
|
|
651
|
+
anchor.localAnchor = localAnchor;
|
|
652
|
+
anchor.worldAnchor = world.nodes[nodeIndex].position;
|
|
653
|
+
anchor.stiffness = clampVal(stiffness, 0.0, 1.0);
|
|
654
|
+
anchor.reactionForce = 0.0;
|
|
655
|
+
anchor.active = true;
|
|
656
|
+
|
|
657
|
+
world.anchors[idx] = anchor;
|
|
658
|
+
if (outAnchorIndex) *outAnchorIndex = idx;
|
|
659
|
+
return PlanarStatus::Success;
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
// ---------------------------------------------------------------------------
|
|
663
|
+
// Collision Resolution with 2D Rigid Bodies
|
|
664
|
+
// ---------------------------------------------------------------------------
|
|
665
|
+
|
|
666
|
+
namespace {
|
|
667
|
+
|
|
668
|
+
void resolveNodeRigidContact(
|
|
669
|
+
PlanarVec2& nodePos,
|
|
670
|
+
PlanarVec2& nodeVel,
|
|
671
|
+
double nodeRadius,
|
|
672
|
+
double nodeInvMass,
|
|
673
|
+
PlanarRigidBody2D& body,
|
|
674
|
+
double restitution,
|
|
675
|
+
double friction) {
|
|
676
|
+
const PlanarVec2 bodyPos{body.x, body.y};
|
|
677
|
+
const PlanarVec2 relNode = subPlanarVec2(nodePos, bodyPos);
|
|
678
|
+
const PlanarVec2 localPos = unrotatePlanarVec2(relNode, body.angle);
|
|
679
|
+
|
|
680
|
+
double penetration = 0.0;
|
|
681
|
+
PlanarVec2 localNormal{0.0, 1.0};
|
|
682
|
+
|
|
683
|
+
switch (body.shape) {
|
|
684
|
+
case PlanarRigidShapeKind::Circle: {
|
|
685
|
+
const double r = body.radius;
|
|
686
|
+
const double dist = lengthPlanarVec2(localPos);
|
|
687
|
+
penetration = (r + nodeRadius) - dist;
|
|
688
|
+
if (penetration <= 0.0) return;
|
|
689
|
+
localNormal = (dist > kEpsilon) ? scalePlanarVec2(localPos, 1.0 / dist) : PlanarVec2{0.0, 1.0};
|
|
690
|
+
break;
|
|
691
|
+
}
|
|
692
|
+
case PlanarRigidShapeKind::Box: {
|
|
693
|
+
const double hx = body.halfX;
|
|
694
|
+
const double hy = body.halfY;
|
|
695
|
+
const PlanarVec2 clamped{
|
|
696
|
+
clampVal(localPos.x, -hx, hx),
|
|
697
|
+
clampVal(localPos.y, -hy, hy)
|
|
698
|
+
};
|
|
699
|
+
const PlanarVec2 diff = subPlanarVec2(localPos, clamped);
|
|
700
|
+
const double distSq = lengthSquaredPlanarVec2(diff);
|
|
701
|
+
|
|
702
|
+
if (distSq > kEpsilon) {
|
|
703
|
+
const double dist = std::sqrt(distSq);
|
|
704
|
+
penetration = nodeRadius - dist;
|
|
705
|
+
if (penetration <= 0.0) return;
|
|
706
|
+
localNormal = scalePlanarVec2(diff, 1.0 / dist);
|
|
707
|
+
} else {
|
|
708
|
+
const double dx = hx - std::abs(localPos.x);
|
|
709
|
+
const double dy = hy - std::abs(localPos.y);
|
|
710
|
+
if (dx < dy) {
|
|
711
|
+
penetration = dx + nodeRadius;
|
|
712
|
+
localNormal = {localPos.x >= 0.0 ? 1.0 : -1.0, 0.0};
|
|
713
|
+
} else {
|
|
714
|
+
penetration = dy + nodeRadius;
|
|
715
|
+
localNormal = {0.0, localPos.y >= 0.0 ? 1.0 : -1.0};
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
break;
|
|
719
|
+
}
|
|
720
|
+
case PlanarRigidShapeKind::Capsule: {
|
|
721
|
+
const double r = body.radius;
|
|
722
|
+
const double halfL = maxVal(0.0, body.halfX - r);
|
|
723
|
+
const double clampedX = clampVal(localPos.x, -halfL, halfL);
|
|
724
|
+
const PlanarVec2 axisPt{clampedX, 0.0};
|
|
725
|
+
const PlanarVec2 diff = subPlanarVec2(localPos, axisPt);
|
|
726
|
+
const double dist = lengthPlanarVec2(diff);
|
|
727
|
+
penetration = (r + nodeRadius) - dist;
|
|
728
|
+
if (penetration <= 0.0) return;
|
|
729
|
+
localNormal = (dist > kEpsilon) ? scalePlanarVec2(diff, 1.0 / dist) : PlanarVec2{0.0, 1.0};
|
|
730
|
+
break;
|
|
731
|
+
}
|
|
732
|
+
case PlanarRigidShapeKind::Ground: {
|
|
733
|
+
penetration = (bodyPos.y + body.halfY) - (nodePos.y - nodeRadius);
|
|
734
|
+
if (penetration <= 0.0) return;
|
|
735
|
+
localNormal = {0.0, 1.0};
|
|
736
|
+
break;
|
|
737
|
+
}
|
|
738
|
+
case PlanarRigidShapeKind::Polygon: {
|
|
739
|
+
if (body.vertexCount < 3) return;
|
|
740
|
+
double minPen = std::numeric_limits<double>::infinity();
|
|
741
|
+
PlanarVec2 bestNorm{0.0, 1.0};
|
|
742
|
+
|
|
743
|
+
for (std::uint32_t i = 0; i < body.vertexCount; ++i) {
|
|
744
|
+
const std::uint32_t j = (i + 1 < body.vertexCount) ? (i + 1) : 0;
|
|
745
|
+
const PlanarVec2 edge = subPlanarVec2(body.vertices[j], body.vertices[i]);
|
|
746
|
+
const PlanarVec2 norm = normalizePlanarVec2(perpendicularPlanarVec2(edge));
|
|
747
|
+
const double d = dotPlanarVec2(subPlanarVec2(localPos, body.vertices[i]), norm);
|
|
748
|
+
const double pen = nodeRadius - d;
|
|
749
|
+
if (pen <= 0.0) return; // Outside
|
|
750
|
+
if (pen < minPen) {
|
|
751
|
+
minPen = pen;
|
|
752
|
+
bestNorm = norm;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
penetration = minPen;
|
|
756
|
+
localNormal = bestNorm;
|
|
757
|
+
break;
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
|
|
761
|
+
// World normal and contact point
|
|
762
|
+
const PlanarVec2 worldNormal = rotatePlanarVec2(localNormal, body.angle);
|
|
763
|
+
const PlanarVec2 contactPoint = subPlanarVec2(nodePos, scalePlanarVec2(worldNormal, nodeRadius));
|
|
764
|
+
const PlanarVec2 rArm = subPlanarVec2(contactPoint, bodyPos);
|
|
765
|
+
|
|
766
|
+
// Position projection
|
|
767
|
+
const double bodyInvMass = body.isDynamic ? body.inverseMass : 0.0;
|
|
768
|
+
const double wSum = nodeInvMass + bodyInvMass;
|
|
769
|
+
if (wSum > kEpsilon) {
|
|
770
|
+
const double nodeFraction = nodeInvMass / wSum;
|
|
771
|
+
const double bodyFraction = bodyInvMass / wSum;
|
|
772
|
+
nodePos = addPlanarVec2(nodePos, scalePlanarVec2(worldNormal, penetration * nodeFraction));
|
|
773
|
+
if (body.isDynamic) {
|
|
774
|
+
body.x -= worldNormal.x * penetration * bodyFraction;
|
|
775
|
+
body.y -= worldNormal.y * penetration * bodyFraction;
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// Velocity impulse
|
|
780
|
+
const PlanarVec2 bodyLinVel{body.vx, body.vy};
|
|
781
|
+
const PlanarVec2 bodyAngLinVel{-body.angularVelocity * rArm.y, body.angularVelocity * rArm.x};
|
|
782
|
+
const PlanarVec2 bodyContactVel = addPlanarVec2(bodyLinVel, bodyAngLinVel);
|
|
783
|
+
const PlanarVec2 relVel = subPlanarVec2(nodeVel, bodyContactVel);
|
|
784
|
+
|
|
785
|
+
const double vn = dotPlanarVec2(relVel, worldNormal);
|
|
786
|
+
if (vn < 0.0) {
|
|
787
|
+
const double armCrossN = crossPlanarVec2(rArm, worldNormal);
|
|
788
|
+
const double bodyAngInertiaEff = body.isDynamic ? (armCrossN * armCrossN * body.inverseInertia) : 0.0;
|
|
789
|
+
const double effMassN = nodeInvMass + bodyInvMass + bodyAngInertiaEff;
|
|
790
|
+
|
|
791
|
+
if (effMassN > kEpsilon) {
|
|
792
|
+
const double effRestitution = minVal(restitution, body.restitution);
|
|
793
|
+
const double jn = maxVal(0.0, -(1.0 + effRestitution) * vn / effMassN);
|
|
794
|
+
|
|
795
|
+
// Tangent friction
|
|
796
|
+
const PlanarVec2 tangent = perpendicularPlanarVec2(worldNormal);
|
|
797
|
+
const double vt = dotPlanarVec2(relVel, tangent);
|
|
798
|
+
const double armCrossT = crossPlanarVec2(rArm, tangent);
|
|
799
|
+
const double bodyAngInertiaEffT = body.isDynamic ? (armCrossT * armCrossT * body.inverseInertia) : 0.0;
|
|
800
|
+
const double effMassT = nodeInvMass + bodyInvMass + bodyAngInertiaEffT;
|
|
801
|
+
|
|
802
|
+
double jt = 0.0;
|
|
803
|
+
if (effMassT > kEpsilon) {
|
|
804
|
+
jt = -vt / effMassT;
|
|
805
|
+
const double maxFriction = friction * body.friction * jn;
|
|
806
|
+
jt = clampVal(jt, -maxFriction, maxFriction);
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const PlanarVec2 totalImpulse = addPlanarVec2(
|
|
810
|
+
scalePlanarVec2(worldNormal, jn),
|
|
811
|
+
scalePlanarVec2(tangent, jt));
|
|
812
|
+
|
|
813
|
+
nodeVel = addPlanarVec2(nodeVel, scalePlanarVec2(totalImpulse, nodeInvMass));
|
|
814
|
+
|
|
815
|
+
if (body.isDynamic) {
|
|
816
|
+
body.vx -= totalImpulse.x * body.inverseMass;
|
|
817
|
+
body.vy -= totalImpulse.y * body.inverseMass;
|
|
818
|
+
const double torqueImpulse = crossPlanarVec2(rArm, totalImpulse);
|
|
819
|
+
body.angularVelocity -= torqueImpulse * body.inverseInertia;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
} // namespace
|
|
826
|
+
|
|
827
|
+
// ---------------------------------------------------------------------------
|
|
828
|
+
// Core Simulation Step
|
|
829
|
+
// ---------------------------------------------------------------------------
|
|
830
|
+
|
|
831
|
+
PlanarStatus stepPlanarDeformableWorld(
|
|
832
|
+
PlanarDeformableWorld& world,
|
|
833
|
+
std::span<PlanarRigidBody2D> bodies,
|
|
834
|
+
double dt) {
|
|
835
|
+
if (dt <= 0.0) {
|
|
836
|
+
return PlanarStatus::InvalidInput;
|
|
837
|
+
}
|
|
838
|
+
const std::uint32_t substeps = maxVal(1u, world.params.substeps);
|
|
839
|
+
const std::uint32_t iterations = maxVal(1u, world.params.solverIterations);
|
|
840
|
+
const double h = dt / static_cast<double>(substeps);
|
|
841
|
+
const double hSq = h * h;
|
|
842
|
+
|
|
843
|
+
for (std::uint32_t step = 0; step < substeps; ++step) {
|
|
844
|
+
// 1. Emitter emission
|
|
845
|
+
for (std::uint32_t e = 0; e < world.emitterCount; ++e) {
|
|
846
|
+
auto& emitter = world.emitters[e];
|
|
847
|
+
if (!emitter.enabled || emitter.rate <= 0.0) continue;
|
|
848
|
+
|
|
849
|
+
emitter.accumulator += emitter.rate * h;
|
|
850
|
+
while (emitter.accumulator >= 1.0 && world.activeParticleCount < kPlanarMaxParticles) {
|
|
851
|
+
emitter.accumulator -= 1.0;
|
|
852
|
+
|
|
853
|
+
PlanarVec2 emitPos = emitter.position;
|
|
854
|
+
switch (emitter.shape) {
|
|
855
|
+
case PlanarEmitterShape::Point:
|
|
856
|
+
break;
|
|
857
|
+
case PlanarEmitterShape::Line: {
|
|
858
|
+
const double t = world.rng.nextRange(-0.5, 0.5);
|
|
859
|
+
emitPos.x += emitter.size.x * t;
|
|
860
|
+
emitPos.y += emitter.size.y * t;
|
|
861
|
+
break;
|
|
862
|
+
}
|
|
863
|
+
case PlanarEmitterShape::Box: {
|
|
864
|
+
emitPos.x += world.rng.nextRange(-0.5, 0.5) * emitter.size.x;
|
|
865
|
+
emitPos.y += world.rng.nextRange(-0.5, 0.5) * emitter.size.y;
|
|
866
|
+
break;
|
|
867
|
+
}
|
|
868
|
+
case PlanarEmitterShape::Circle: {
|
|
869
|
+
const double ang = world.rng.nextRange(0.0, kTwoPi);
|
|
870
|
+
const double rad = std::sqrt(world.rng.nextDouble()) * emitter.size.x;
|
|
871
|
+
emitPos.x += std::cos(ang) * rad;
|
|
872
|
+
emitPos.y += std::sin(ang) * rad;
|
|
873
|
+
break;
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
const double angle = world.rng.nextRange(emitter.minAngle, emitter.maxAngle);
|
|
878
|
+
const double speed = world.rng.nextRange(emitter.minSpeed, emitter.maxSpeed);
|
|
879
|
+
const PlanarVec2 emitVel{std::cos(angle) * speed, std::sin(angle) * speed};
|
|
880
|
+
const double life = world.rng.nextRange(emitter.minLifetime, emitter.maxLifetime);
|
|
881
|
+
|
|
882
|
+
addParticle2D(world, emitPos, emitVel, emitter.particleRadius, emitter.particleMass, life);
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
|
|
886
|
+
// 2. Integration of external forces & prediction
|
|
887
|
+
const PlanarVec2 gravityAcc = world.params.gravity;
|
|
888
|
+
for (std::uint32_t i = 0; i < world.nodeCount; ++i) {
|
|
889
|
+
auto& node = world.nodes[i];
|
|
890
|
+
if (node.inverseMass <= 0.0) {
|
|
891
|
+
node.velocity = {0.0, 0.0};
|
|
892
|
+
node.previousPosition = node.position;
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// Aerodynamic drag / wind
|
|
897
|
+
PlanarVec2 aeroAcc{0.0, 0.0};
|
|
898
|
+
if (world.params.airDensity > 0.0) {
|
|
899
|
+
const PlanarVec2 vRel = subPlanarVec2(node.velocity, world.params.wind);
|
|
900
|
+
const double speedRel = lengthPlanarVec2(vRel);
|
|
901
|
+
if (speedRel > kEpsilon) {
|
|
902
|
+
const double dragMag = 0.5 * world.params.airDensity * speedRel * speedRel *
|
|
903
|
+
world.params.dragCoefficient * (2.0 * node.radius);
|
|
904
|
+
aeroAcc = scalePlanarVec2(vRel, -dragMag * node.inverseMass / speedRel);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
const PlanarVec2 totalAcc = addPlanarVec2(
|
|
909
|
+
addPlanarVec2(gravityAcc, aeroAcc),
|
|
910
|
+
scalePlanarVec2(node.force, node.inverseMass));
|
|
911
|
+
|
|
912
|
+
node.velocity = addPlanarVec2(node.velocity, scalePlanarVec2(totalAcc, h));
|
|
913
|
+
const double dampFactor = clampVal(1.0 - node.damping, 0.0, 1.0);
|
|
914
|
+
node.velocity = scalePlanarVec2(node.velocity, dampFactor);
|
|
915
|
+
|
|
916
|
+
node.previousPosition = node.position;
|
|
917
|
+
node.position = addPlanarVec2(node.position, scalePlanarVec2(node.velocity, h));
|
|
918
|
+
node.force = {0.0, 0.0};
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
for (std::uint32_t i = 0; i < world.particleCount; ++i) {
|
|
922
|
+
auto& p = world.particles[i];
|
|
923
|
+
if (!p.active) continue;
|
|
924
|
+
|
|
925
|
+
p.age += h;
|
|
926
|
+
if (p.age >= p.lifetime) {
|
|
927
|
+
p.active = false;
|
|
928
|
+
if (world.activeParticleCount > 0) world.activeParticleCount--;
|
|
929
|
+
continue;
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
p.velocity = addPlanarVec2(p.velocity, scalePlanarVec2(gravityAcc, h));
|
|
933
|
+
const double dampFactor = clampVal(1.0 - world.params.damping, 0.0, 1.0);
|
|
934
|
+
p.velocity = scalePlanarVec2(p.velocity, dampFactor);
|
|
935
|
+
|
|
936
|
+
p.previousPosition = p.position;
|
|
937
|
+
p.position = addPlanarVec2(p.position, scalePlanarVec2(p.velocity, h));
|
|
938
|
+
p.force = {0.0, 0.0};
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
// 3. Iterative Constraint Solver
|
|
942
|
+
for (std::uint32_t iter = 0; iter < iterations; ++iter) {
|
|
943
|
+
// 3A. Distance constraints
|
|
944
|
+
for (std::uint32_t c = 0; c < world.distanceConstraintCount; ++c) {
|
|
945
|
+
auto& dc = world.distanceConstraints[c];
|
|
946
|
+
if (dc.broken) continue;
|
|
947
|
+
|
|
948
|
+
auto& na = world.nodes[dc.nodeA];
|
|
949
|
+
auto& nb = world.nodes[dc.nodeB];
|
|
950
|
+
const PlanarVec2 delta = subPlanarVec2(na.position, nb.position);
|
|
951
|
+
const double d = lengthPlanarVec2(delta);
|
|
952
|
+
if (d < kEpsilon) continue;
|
|
953
|
+
|
|
954
|
+
const double diff = d - dc.restLength;
|
|
955
|
+
const double wSum = na.inverseMass + nb.inverseMass;
|
|
956
|
+
if (wSum <= kEpsilon) continue;
|
|
957
|
+
|
|
958
|
+
const PlanarVec2 norm = scalePlanarVec2(delta, 1.0 / d);
|
|
959
|
+
const double correction = diff * dc.stiffness / wSum;
|
|
960
|
+
|
|
961
|
+
na.position = subPlanarVec2(na.position, scalePlanarVec2(norm, correction * na.inverseMass));
|
|
962
|
+
nb.position = addPlanarVec2(nb.position, scalePlanarVec2(norm, correction * nb.inverseMass));
|
|
963
|
+
|
|
964
|
+
const double tension = std::abs(diff) * dc.stiffness / hSq;
|
|
965
|
+
dc.tension = tension;
|
|
966
|
+
world.maxTension = maxVal(world.maxTension, tension);
|
|
967
|
+
|
|
968
|
+
if (dc.breakThreshold > 0.0 && tension > dc.breakThreshold) {
|
|
969
|
+
dc.broken = true;
|
|
970
|
+
if (world.breakEventCount < kPlanarMaxBreakEvents) {
|
|
971
|
+
world.breakEvents[world.breakEventCount++] = {
|
|
972
|
+
world.id, c, PlanarBreakReason::TensionExceeded, tension
|
|
973
|
+
};
|
|
974
|
+
}
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
// 3B. Bend constraints
|
|
979
|
+
for (std::uint32_t c = 0; c < world.bendConstraintCount; ++c) {
|
|
980
|
+
auto& bc = world.bendConstraints[c];
|
|
981
|
+
if (bc.broken) continue;
|
|
982
|
+
|
|
983
|
+
auto& na = world.nodes[bc.nodeA];
|
|
984
|
+
auto& nb = world.nodes[bc.nodeB];
|
|
985
|
+
auto& nc = world.nodes[bc.nodeC];
|
|
986
|
+
|
|
987
|
+
const PlanarVec2 u = subPlanarVec2(na.position, nb.position);
|
|
988
|
+
const PlanarVec2 v = subPlanarVec2(nc.position, nb.position);
|
|
989
|
+
const double lenUSq = lengthSquaredPlanarVec2(u);
|
|
990
|
+
const double lenVSq = lengthSquaredPlanarVec2(v);
|
|
991
|
+
if (lenUSq < kEpsilon || lenVSq < kEpsilon) continue;
|
|
992
|
+
|
|
993
|
+
const double currentAngle = std::atan2(crossPlanarVec2(u, v), dotPlanarVec2(u, v));
|
|
994
|
+
double angleDiff = currentAngle - bc.restAngle;
|
|
995
|
+
while (angleDiff > kPi) angleDiff -= kTwoPi;
|
|
996
|
+
while (angleDiff < -kPi) angleDiff += kTwoPi;
|
|
997
|
+
|
|
998
|
+
const PlanarVec2 gradA = scalePlanarVec2(perpendicularPlanarVec2(u), 1.0 / lenUSq);
|
|
999
|
+
const PlanarVec2 gradC = scalePlanarVec2(perpendicularPlanarVec2(v), -1.0 / lenVSq);
|
|
1000
|
+
const PlanarVec2 gradB = scalePlanarVec2(addPlanarVec2(gradA, gradC), -1.0);
|
|
1001
|
+
|
|
1002
|
+
const double denom = na.inverseMass * lengthSquaredPlanarVec2(gradA) +
|
|
1003
|
+
nb.inverseMass * lengthSquaredPlanarVec2(gradB) +
|
|
1004
|
+
nc.inverseMass * lengthSquaredPlanarVec2(gradC);
|
|
1005
|
+
if (denom <= kEpsilon) continue;
|
|
1006
|
+
|
|
1007
|
+
const double lambda = - (angleDiff * bc.stiffness) / denom;
|
|
1008
|
+
na.position = addPlanarVec2(na.position, scalePlanarVec2(gradA, lambda * na.inverseMass));
|
|
1009
|
+
nb.position = addPlanarVec2(nb.position, scalePlanarVec2(gradB, lambda * nb.inverseMass));
|
|
1010
|
+
nc.position = addPlanarVec2(nc.position, scalePlanarVec2(gradC, lambda * nc.inverseMass));
|
|
1011
|
+
|
|
1012
|
+
bc.currentAngle = currentAngle;
|
|
1013
|
+
const double moment = std::abs(lambda) / hSq;
|
|
1014
|
+
bc.bendingMoment = moment;
|
|
1015
|
+
world.maxBendMoment = maxVal(world.maxBendMoment, moment);
|
|
1016
|
+
|
|
1017
|
+
if (bc.breakThreshold > 0.0 && moment > bc.breakThreshold) {
|
|
1018
|
+
bc.broken = true;
|
|
1019
|
+
if (world.breakEventCount < kPlanarMaxBreakEvents) {
|
|
1020
|
+
world.breakEvents[world.breakEventCount++] = {
|
|
1021
|
+
world.id, c, PlanarBreakReason::BendTorqueExceeded, moment
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
// 3C. Shear constraints
|
|
1028
|
+
for (std::uint32_t c = 0; c < world.shearConstraintCount; ++c) {
|
|
1029
|
+
auto& sc = world.shearConstraints[c];
|
|
1030
|
+
if (sc.broken) continue;
|
|
1031
|
+
|
|
1032
|
+
auto& na = world.nodes[sc.nodeA];
|
|
1033
|
+
auto& nb = world.nodes[sc.nodeB];
|
|
1034
|
+
const PlanarVec2 delta = subPlanarVec2(na.position, nb.position);
|
|
1035
|
+
const double d = lengthPlanarVec2(delta);
|
|
1036
|
+
if (d < kEpsilon) continue;
|
|
1037
|
+
|
|
1038
|
+
const double diff = d - sc.restLength;
|
|
1039
|
+
const double wSum = na.inverseMass + nb.inverseMass;
|
|
1040
|
+
if (wSum <= kEpsilon) continue;
|
|
1041
|
+
|
|
1042
|
+
const PlanarVec2 norm = scalePlanarVec2(delta, 1.0 / d);
|
|
1043
|
+
const double correction = diff * sc.stiffness / wSum;
|
|
1044
|
+
|
|
1045
|
+
na.position = subPlanarVec2(na.position, scalePlanarVec2(norm, correction * na.inverseMass));
|
|
1046
|
+
nb.position = addPlanarVec2(nb.position, scalePlanarVec2(norm, correction * nb.inverseMass));
|
|
1047
|
+
|
|
1048
|
+
sc.currentLength = d;
|
|
1049
|
+
const double shearForce = std::abs(diff) * sc.stiffness / hSq;
|
|
1050
|
+
sc.shearForce = shearForce;
|
|
1051
|
+
world.maxShearForce = maxVal(world.maxShearForce, shearForce);
|
|
1052
|
+
|
|
1053
|
+
if (sc.breakThreshold > 0.0 && shearForce > sc.breakThreshold) {
|
|
1054
|
+
sc.broken = true;
|
|
1055
|
+
if (world.breakEventCount < kPlanarMaxBreakEvents) {
|
|
1056
|
+
world.breakEvents[world.breakEventCount++] = {
|
|
1057
|
+
world.id, c, PlanarBreakReason::ShearExceeded, shearForce
|
|
1058
|
+
};
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
// 3D. Area constraints
|
|
1064
|
+
for (std::uint32_t c = 0; c < world.areaConstraintCount; ++c) {
|
|
1065
|
+
auto& ac = world.areaConstraints[c];
|
|
1066
|
+
if (ac.broken) continue;
|
|
1067
|
+
|
|
1068
|
+
auto& na = world.nodes[ac.nodeA];
|
|
1069
|
+
auto& nb = world.nodes[ac.nodeB];
|
|
1070
|
+
auto& nc = world.nodes[ac.nodeC];
|
|
1071
|
+
|
|
1072
|
+
const double currentArea = 0.5 * crossPlanarVec2(
|
|
1073
|
+
subPlanarVec2(nb.position, na.position),
|
|
1074
|
+
subPlanarVec2(nc.position, na.position));
|
|
1075
|
+
|
|
1076
|
+
const double areaDiff = currentArea - ac.restArea;
|
|
1077
|
+
const PlanarVec2 gradA = scalePlanarVec2(
|
|
1078
|
+
PlanarVec2{nb.position.y - nc.position.y, nc.position.x - nb.position.x}, 0.5);
|
|
1079
|
+
const PlanarVec2 gradB = scalePlanarVec2(
|
|
1080
|
+
PlanarVec2{nc.position.y - na.position.y, na.position.x - nc.position.x}, 0.5);
|
|
1081
|
+
const PlanarVec2 gradC = scalePlanarVec2(
|
|
1082
|
+
PlanarVec2{na.position.y - nb.position.y, nb.position.x - na.position.x}, 0.5);
|
|
1083
|
+
|
|
1084
|
+
const double denom = na.inverseMass * lengthSquaredPlanarVec2(gradA) +
|
|
1085
|
+
nb.inverseMass * lengthSquaredPlanarVec2(gradB) +
|
|
1086
|
+
nc.inverseMass * lengthSquaredPlanarVec2(gradC);
|
|
1087
|
+
if (denom <= kEpsilon) continue;
|
|
1088
|
+
|
|
1089
|
+
const double lambda = - (areaDiff * ac.stiffness) / denom;
|
|
1090
|
+
na.position = addPlanarVec2(na.position, scalePlanarVec2(gradA, lambda * na.inverseMass));
|
|
1091
|
+
nb.position = addPlanarVec2(nb.position, scalePlanarVec2(gradB, lambda * nb.inverseMass));
|
|
1092
|
+
nc.position = addPlanarVec2(nc.position, scalePlanarVec2(gradC, lambda * nc.inverseMass));
|
|
1093
|
+
|
|
1094
|
+
ac.currentArea = currentArea;
|
|
1095
|
+
ac.pressure = std::abs(lambda) / hSq;
|
|
1096
|
+
|
|
1097
|
+
if (ac.breakThreshold > 0.0 && ac.pressure > ac.breakThreshold) {
|
|
1098
|
+
ac.broken = true;
|
|
1099
|
+
if (world.breakEventCount < kPlanarMaxBreakEvents) {
|
|
1100
|
+
world.breakEvents[world.breakEventCount++] = {
|
|
1101
|
+
world.id, c, PlanarBreakReason::AreaExceeded, ac.pressure
|
|
1102
|
+
};
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
// 3E. Anchors
|
|
1108
|
+
for (std::uint32_t a = 0; a < world.anchorCount; ++a) {
|
|
1109
|
+
auto& anchor = world.anchors[a];
|
|
1110
|
+
if (!anchor.active || anchor.nodeIndex >= world.nodeCount) continue;
|
|
1111
|
+
|
|
1112
|
+
auto& node = world.nodes[anchor.nodeIndex];
|
|
1113
|
+
PlanarVec2 targetPos = anchor.worldAnchor;
|
|
1114
|
+
|
|
1115
|
+
if (anchor.bodyIndex >= 0 && static_cast<std::size_t>(anchor.bodyIndex) < bodies.size()) {
|
|
1116
|
+
auto& body = bodies[anchor.bodyIndex];
|
|
1117
|
+
targetPos = addPlanarVec2(
|
|
1118
|
+
PlanarVec2{body.x, body.y},
|
|
1119
|
+
rotatePlanarVec2(anchor.localAnchor, body.angle));
|
|
1120
|
+
anchor.worldAnchor = targetPos;
|
|
1121
|
+
|
|
1122
|
+
const PlanarVec2 diff = subPlanarVec2(targetPos, node.position);
|
|
1123
|
+
const double bodyInvMass = body.isDynamic ? body.inverseMass : 0.0;
|
|
1124
|
+
const double wSum = node.inverseMass + bodyInvMass;
|
|
1125
|
+
if (wSum > kEpsilon) {
|
|
1126
|
+
const PlanarVec2 correction = scalePlanarVec2(diff, anchor.stiffness / wSum);
|
|
1127
|
+
node.position = addPlanarVec2(node.position, scalePlanarVec2(correction, node.inverseMass));
|
|
1128
|
+
|
|
1129
|
+
if (body.isDynamic) {
|
|
1130
|
+
body.x -= correction.x * body.inverseMass;
|
|
1131
|
+
body.y -= correction.y * body.inverseMass;
|
|
1132
|
+
const PlanarVec2 rArm = subPlanarVec2(targetPos, PlanarVec2{body.x, body.y});
|
|
1133
|
+
const double torque = crossPlanarVec2(rArm, scalePlanarVec2(correction, -body.inverseMass / hSq));
|
|
1134
|
+
body.angularVelocity += torque * body.inverseInertia * h;
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
anchor.reactionForce = lengthPlanarVec2(diff) * anchor.stiffness * node.mass / hSq;
|
|
1138
|
+
} else {
|
|
1139
|
+
const PlanarVec2 diff = subPlanarVec2(targetPos, node.position);
|
|
1140
|
+
node.position = addPlanarVec2(node.position, scalePlanarVec2(diff, anchor.stiffness));
|
|
1141
|
+
anchor.reactionForce = lengthPlanarVec2(diff) * anchor.stiffness * node.mass / hSq;
|
|
1142
|
+
}
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
// 3F. Ground collision
|
|
1146
|
+
if (world.params.groundEnabled) {
|
|
1147
|
+
const double gy = world.params.groundY;
|
|
1148
|
+
for (std::uint32_t i = 0; i < world.nodeCount; ++i) {
|
|
1149
|
+
auto& node = world.nodes[i];
|
|
1150
|
+
const double floorY = gy + node.radius;
|
|
1151
|
+
if (node.position.y < floorY) {
|
|
1152
|
+
node.position.y = floorY;
|
|
1153
|
+
}
|
|
1154
|
+
}
|
|
1155
|
+
for (std::uint32_t i = 0; i < world.particleCount; ++i) {
|
|
1156
|
+
auto& p = world.particles[i];
|
|
1157
|
+
if (!p.active) continue;
|
|
1158
|
+
const double floorY = gy + p.radius;
|
|
1159
|
+
if (p.position.y < floorY) {
|
|
1160
|
+
p.position.y = floorY;
|
|
1161
|
+
}
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
// 3G. Particle-Particle collisions
|
|
1166
|
+
if (world.params.particleCollisionEnabled && world.activeParticleCount > 1) {
|
|
1167
|
+
for (std::uint32_t i = 0; i < world.particleCount; ++i) {
|
|
1168
|
+
auto& pi = world.particles[i];
|
|
1169
|
+
if (!pi.active) continue;
|
|
1170
|
+
|
|
1171
|
+
for (std::uint32_t j = i + 1; j < world.particleCount; ++j) {
|
|
1172
|
+
auto& pj = world.particles[j];
|
|
1173
|
+
if (!pj.active) continue;
|
|
1174
|
+
|
|
1175
|
+
const PlanarVec2 delta = subPlanarVec2(pi.position, pj.position);
|
|
1176
|
+
const double distSq = lengthSquaredPlanarVec2(delta);
|
|
1177
|
+
const double minDist = pi.radius + pj.radius;
|
|
1178
|
+
if (distSq < minDist * minDist && distSq > kEpsilon) {
|
|
1179
|
+
const double dist = std::sqrt(distSq);
|
|
1180
|
+
const double pen = minDist - dist;
|
|
1181
|
+
const PlanarVec2 norm = scalePlanarVec2(delta, 1.0 / dist);
|
|
1182
|
+
const double wSum = pi.inverseMass + pj.inverseMass;
|
|
1183
|
+
if (wSum > kEpsilon) {
|
|
1184
|
+
pi.position = addPlanarVec2(pi.position, scalePlanarVec2(norm, pen * (pi.inverseMass / wSum)));
|
|
1185
|
+
pj.position = subPlanarVec2(pj.position, scalePlanarVec2(norm, pen * (pj.inverseMass / wSum)));
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
// 3H. Rigid body collisions
|
|
1193
|
+
for (std::size_t b = 0; b < bodies.size(); ++b) {
|
|
1194
|
+
auto& body = bodies[b];
|
|
1195
|
+
for (std::uint32_t i = 0; i < world.nodeCount; ++i) {
|
|
1196
|
+
auto& node = world.nodes[i];
|
|
1197
|
+
resolveNodeRigidContact(
|
|
1198
|
+
node.position, node.velocity, node.radius, node.inverseMass,
|
|
1199
|
+
body, world.params.groundRestitution, world.params.groundFriction);
|
|
1200
|
+
}
|
|
1201
|
+
for (std::uint32_t i = 0; i < world.particleCount; ++i) {
|
|
1202
|
+
auto& p = world.particles[i];
|
|
1203
|
+
if (!p.active) continue;
|
|
1204
|
+
resolveNodeRigidContact(
|
|
1205
|
+
p.position, p.velocity, p.radius, p.inverseMass,
|
|
1206
|
+
body, world.params.particleRestitution, world.params.particleFriction);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
} // iterations
|
|
1210
|
+
|
|
1211
|
+
// 4. Velocity update from corrected positions
|
|
1212
|
+
const double invH = 1.0 / h;
|
|
1213
|
+
for (std::uint32_t i = 0; i < world.nodeCount; ++i) {
|
|
1214
|
+
auto& node = world.nodes[i];
|
|
1215
|
+
if (node.inverseMass > 0.0) {
|
|
1216
|
+
node.velocity = scalePlanarVec2(subPlanarVec2(node.position, node.previousPosition), invH);
|
|
1217
|
+
if (world.params.groundEnabled && node.position.y <= world.params.groundY + node.radius + 1e-5) {
|
|
1218
|
+
if (node.velocity.y < 0.0) {
|
|
1219
|
+
node.velocity.y = -node.velocity.y * world.params.groundRestitution;
|
|
1220
|
+
node.velocity.x *= clampVal(1.0 - world.params.groundFriction, 0.0, 1.0);
|
|
1221
|
+
}
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
}
|
|
1225
|
+
|
|
1226
|
+
for (std::uint32_t i = 0; i < world.particleCount; ++i) {
|
|
1227
|
+
auto& p = world.particles[i];
|
|
1228
|
+
if (!p.active) continue;
|
|
1229
|
+
p.velocity = scalePlanarVec2(subPlanarVec2(p.position, p.previousPosition), invH);
|
|
1230
|
+
if (world.params.groundEnabled && p.position.y <= world.params.groundY + p.radius + 1e-5) {
|
|
1231
|
+
if (p.velocity.y < 0.0) {
|
|
1232
|
+
p.velocity.y = -p.velocity.y * world.params.groundRestitution;
|
|
1233
|
+
p.velocity.x *= clampVal(1.0 - world.params.groundFriction, 0.0, 1.0);
|
|
1234
|
+
}
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
} // substeps
|
|
1238
|
+
|
|
1239
|
+
// 5. Energy and area telemetry
|
|
1240
|
+
double kinetic = 0.0;
|
|
1241
|
+
double potential = 0.0;
|
|
1242
|
+
for (std::uint32_t i = 0; i < world.nodeCount; ++i) {
|
|
1243
|
+
const auto& node = world.nodes[i];
|
|
1244
|
+
const double m = (node.mass > 0.0) ? node.mass : 0.0;
|
|
1245
|
+
kinetic += 0.5 * m * lengthSquaredPlanarVec2(node.velocity);
|
|
1246
|
+
potential += m * (-world.params.gravity.y) * (node.position.y - world.params.groundY);
|
|
1247
|
+
}
|
|
1248
|
+
for (std::uint32_t i = 0; i < world.particleCount; ++i) {
|
|
1249
|
+
const auto& p = world.particles[i];
|
|
1250
|
+
if (!p.active) continue;
|
|
1251
|
+
kinetic += 0.5 * p.mass * lengthSquaredPlanarVec2(p.velocity);
|
|
1252
|
+
potential += p.mass * (-world.params.gravity.y) * (p.position.y - world.params.groundY);
|
|
1253
|
+
}
|
|
1254
|
+
|
|
1255
|
+
double totalArea = 0.0;
|
|
1256
|
+
for (std::uint32_t f = 0; f < world.faceCount; ++f) {
|
|
1257
|
+
const auto& face = world.faces[f];
|
|
1258
|
+
const double area = 0.5 * std::abs(crossPlanarVec2(
|
|
1259
|
+
subPlanarVec2(world.nodes[face.nodeB].position, world.nodes[face.nodeA].position),
|
|
1260
|
+
subPlanarVec2(world.nodes[face.nodeC].position, world.nodes[face.nodeA].position)));
|
|
1261
|
+
totalArea += area;
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
world.totalKineticEnergy = kinetic;
|
|
1265
|
+
world.totalPotentialEnergy = potential;
|
|
1266
|
+
world.totalDeformedArea = totalArea;
|
|
1267
|
+
world.tick++;
|
|
1268
|
+
|
|
1269
|
+
return PlanarStatus::Success;
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
PlanarStatus stepPlanarDeformableRaw(
|
|
1273
|
+
double* nodeData,
|
|
1274
|
+
std::uint32_t nodeCount,
|
|
1275
|
+
double* distanceConstraintData,
|
|
1276
|
+
std::uint32_t distanceConstraintCount,
|
|
1277
|
+
double* bendConstraintData,
|
|
1278
|
+
std::uint32_t bendConstraintCount,
|
|
1279
|
+
double* shearConstraintData,
|
|
1280
|
+
std::uint32_t shearConstraintCount,
|
|
1281
|
+
double* areaConstraintData,
|
|
1282
|
+
std::uint32_t areaConstraintCount,
|
|
1283
|
+
double* anchorData,
|
|
1284
|
+
std::uint32_t anchorCount,
|
|
1285
|
+
double* particleData,
|
|
1286
|
+
std::uint32_t particleCount,
|
|
1287
|
+
double* emitterData,
|
|
1288
|
+
std::uint32_t emitterCount,
|
|
1289
|
+
double* rigidBodyData,
|
|
1290
|
+
std::uint32_t rigidBodyCount,
|
|
1291
|
+
const double* paramsData,
|
|
1292
|
+
double* breakEventsData,
|
|
1293
|
+
std::uint32_t* breakEventCount,
|
|
1294
|
+
double dt) {
|
|
1295
|
+
if (dt <= 0.0) return PlanarStatus::InvalidInput;
|
|
1296
|
+
if (nodeCount > kPlanarMaxNodes) return PlanarStatus::BufferOverflow;
|
|
1297
|
+
|
|
1298
|
+
PlanarDeformableWorld world{};
|
|
1299
|
+
if (paramsData) {
|
|
1300
|
+
world.params.gravity = {paramsData[0], paramsData[1]};
|
|
1301
|
+
world.params.damping = paramsData[2];
|
|
1302
|
+
world.params.airDensity = paramsData[3];
|
|
1303
|
+
world.params.wind = {paramsData[4], paramsData[5]};
|
|
1304
|
+
world.params.dragCoefficient = paramsData[6];
|
|
1305
|
+
world.params.liftCoefficient = paramsData[7];
|
|
1306
|
+
world.params.groundEnabled = (paramsData[8] > 0.5);
|
|
1307
|
+
world.params.groundY = paramsData[9];
|
|
1308
|
+
world.params.groundFriction = paramsData[10];
|
|
1309
|
+
world.params.groundRestitution = paramsData[11];
|
|
1310
|
+
world.params.solverIterations = static_cast<std::uint32_t>(paramsData[12]);
|
|
1311
|
+
world.params.substeps = static_cast<std::uint32_t>(paramsData[13]);
|
|
1312
|
+
world.params.particleCollisionEnabled = (paramsData[14] > 0.5);
|
|
1313
|
+
world.params.particleRestitution = paramsData[15];
|
|
1314
|
+
world.params.particleFriction = paramsData[16];
|
|
1315
|
+
world.params.breakThresholdDefault = paramsData[17];
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
// Load nodes
|
|
1319
|
+
world.nodeCount = nodeCount;
|
|
1320
|
+
for (std::uint32_t i = 0; i < nodeCount; ++i) {
|
|
1321
|
+
const double* ptr = nodeData + i * kPlanarNodeStride;
|
|
1322
|
+
auto& n = world.nodes[i];
|
|
1323
|
+
n.position = {ptr[0], ptr[1]};
|
|
1324
|
+
n.previousPosition = {ptr[2], ptr[3]};
|
|
1325
|
+
n.velocity = {ptr[4], ptr[5]};
|
|
1326
|
+
n.force = {ptr[6], ptr[7]};
|
|
1327
|
+
n.mass = ptr[8];
|
|
1328
|
+
n.inverseMass = ptr[9];
|
|
1329
|
+
n.radius = ptr[10];
|
|
1330
|
+
n.flags = static_cast<std::uint32_t>(ptr[11]);
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1333
|
+
// Load distance constraints
|
|
1334
|
+
world.distanceConstraintCount = distanceConstraintCount;
|
|
1335
|
+
for (std::uint32_t i = 0; i < distanceConstraintCount; ++i) {
|
|
1336
|
+
const double* ptr = distanceConstraintData + i * kPlanarDistanceConstraintStride;
|
|
1337
|
+
auto& dc = world.distanceConstraints[i];
|
|
1338
|
+
dc.nodeA = static_cast<std::uint32_t>(ptr[0]);
|
|
1339
|
+
dc.nodeB = static_cast<std::uint32_t>(ptr[1]);
|
|
1340
|
+
dc.restLength = ptr[2];
|
|
1341
|
+
dc.stiffness = ptr[3];
|
|
1342
|
+
dc.damping = ptr[4];
|
|
1343
|
+
dc.tension = ptr[5];
|
|
1344
|
+
dc.breakThreshold = ptr[6];
|
|
1345
|
+
dc.broken = (ptr[7] > 0.5);
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1348
|
+
// Load bend constraints
|
|
1349
|
+
world.bendConstraintCount = bendConstraintCount;
|
|
1350
|
+
for (std::uint32_t i = 0; i < bendConstraintCount; ++i) {
|
|
1351
|
+
const double* ptr = bendConstraintData + i * kPlanarBendConstraintStride;
|
|
1352
|
+
auto& bc = world.bendConstraints[i];
|
|
1353
|
+
bc.nodeA = static_cast<std::uint32_t>(ptr[0]);
|
|
1354
|
+
bc.nodeB = static_cast<std::uint32_t>(ptr[1]);
|
|
1355
|
+
bc.nodeC = static_cast<std::uint32_t>(ptr[2]);
|
|
1356
|
+
bc.restAngle = ptr[3];
|
|
1357
|
+
bc.stiffness = ptr[4];
|
|
1358
|
+
bc.bendingMoment = ptr[5];
|
|
1359
|
+
bc.breakThreshold = ptr[6];
|
|
1360
|
+
bc.broken = (ptr[7] > 0.5);
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
// Load shear constraints
|
|
1364
|
+
world.shearConstraintCount = shearConstraintCount;
|
|
1365
|
+
for (std::uint32_t i = 0; i < shearConstraintCount; ++i) {
|
|
1366
|
+
const double* ptr = shearConstraintData + i * kPlanarShearConstraintStride;
|
|
1367
|
+
auto& sc = world.shearConstraints[i];
|
|
1368
|
+
sc.nodeA = static_cast<std::uint32_t>(ptr[0]);
|
|
1369
|
+
sc.nodeB = static_cast<std::uint32_t>(ptr[1]);
|
|
1370
|
+
sc.restLength = ptr[2];
|
|
1371
|
+
sc.stiffness = ptr[3];
|
|
1372
|
+
sc.damping = ptr[4];
|
|
1373
|
+
sc.shearForce = ptr[5];
|
|
1374
|
+
sc.breakThreshold = ptr[6];
|
|
1375
|
+
sc.broken = (ptr[7] > 0.5);
|
|
1376
|
+
}
|
|
1377
|
+
|
|
1378
|
+
// Load area constraints
|
|
1379
|
+
world.areaConstraintCount = areaConstraintCount;
|
|
1380
|
+
for (std::uint32_t i = 0; i < areaConstraintCount; ++i) {
|
|
1381
|
+
const double* ptr = areaConstraintData + i * kPlanarAreaConstraintStride;
|
|
1382
|
+
auto& ac = world.areaConstraints[i];
|
|
1383
|
+
ac.nodeA = static_cast<std::uint32_t>(ptr[0]);
|
|
1384
|
+
ac.nodeB = static_cast<std::uint32_t>(ptr[1]);
|
|
1385
|
+
ac.nodeC = static_cast<std::uint32_t>(ptr[2]);
|
|
1386
|
+
ac.restArea = ptr[3];
|
|
1387
|
+
ac.currentArea = ptr[4];
|
|
1388
|
+
ac.stiffness = ptr[5];
|
|
1389
|
+
ac.pressure = ptr[6];
|
|
1390
|
+
ac.broken = (ptr[7] > 0.5);
|
|
1391
|
+
}
|
|
1392
|
+
|
|
1393
|
+
// Load anchors
|
|
1394
|
+
world.anchorCount = anchorCount;
|
|
1395
|
+
for (std::uint32_t i = 0; i < anchorCount; ++i) {
|
|
1396
|
+
const double* ptr = anchorData + i * kPlanarAnchorStride;
|
|
1397
|
+
auto& an = world.anchors[i];
|
|
1398
|
+
an.nodeIndex = static_cast<std::uint32_t>(ptr[0]);
|
|
1399
|
+
an.bodyIndex = static_cast<std::int32_t>(ptr[1]);
|
|
1400
|
+
an.localAnchor = {ptr[2], ptr[3]};
|
|
1401
|
+
an.worldAnchor = {ptr[4], ptr[5]};
|
|
1402
|
+
an.stiffness = ptr[6];
|
|
1403
|
+
an.reactionForce = ptr[7];
|
|
1404
|
+
an.active = (ptr[8] > 0.5);
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
// Load particles
|
|
1408
|
+
world.particleCount = particleCount;
|
|
1409
|
+
world.activeParticleCount = 0;
|
|
1410
|
+
for (std::uint32_t i = 0; i < particleCount; ++i) {
|
|
1411
|
+
const double* ptr = particleData + i * kPlanarParticleStride;
|
|
1412
|
+
auto& p = world.particles[i];
|
|
1413
|
+
p.position = {ptr[0], ptr[1]};
|
|
1414
|
+
p.previousPosition = {ptr[0], ptr[1]};
|
|
1415
|
+
p.velocity = {ptr[2], ptr[3]};
|
|
1416
|
+
p.radius = ptr[4];
|
|
1417
|
+
p.mass = ptr[5];
|
|
1418
|
+
p.inverseMass = ptr[6];
|
|
1419
|
+
p.age = ptr[7];
|
|
1420
|
+
p.lifetime = ptr[8];
|
|
1421
|
+
p.active = (ptr[9] > 0.5);
|
|
1422
|
+
if (p.active) world.activeParticleCount++;
|
|
1423
|
+
}
|
|
1424
|
+
|
|
1425
|
+
// Load emitters
|
|
1426
|
+
world.emitterCount = emitterCount;
|
|
1427
|
+
for (std::uint32_t i = 0; i < emitterCount; ++i) {
|
|
1428
|
+
const double* ptr = emitterData + i * kPlanarEmitterStride;
|
|
1429
|
+
auto& em = world.emitters[i];
|
|
1430
|
+
em.shape = static_cast<PlanarEmitterShape>(ptr[0]);
|
|
1431
|
+
em.position = {ptr[1], ptr[2]};
|
|
1432
|
+
em.size = {ptr[3], ptr[4]};
|
|
1433
|
+
em.minSpeed = ptr[5];
|
|
1434
|
+
em.maxSpeed = ptr[6];
|
|
1435
|
+
em.minAngle = ptr[7];
|
|
1436
|
+
em.maxAngle = ptr[8];
|
|
1437
|
+
em.minLifetime = ptr[9];
|
|
1438
|
+
em.maxLifetime = ptr[10];
|
|
1439
|
+
em.rate = ptr[11];
|
|
1440
|
+
em.accumulator = ptr[12];
|
|
1441
|
+
em.enabled = (ptr[13] > 0.5);
|
|
1442
|
+
}
|
|
1443
|
+
|
|
1444
|
+
// Load rigid bodies
|
|
1445
|
+
std::array<PlanarRigidBody2D, 64> bodies{};
|
|
1446
|
+
const std::uint32_t bCount = minVal(rigidBodyCount, 64u);
|
|
1447
|
+
for (std::uint32_t i = 0; i < bCount; ++i) {
|
|
1448
|
+
const double* ptr = rigidBodyData + i * kPlanarRigidBodyStride;
|
|
1449
|
+
auto& b = bodies[i];
|
|
1450
|
+
b.id = static_cast<std::uint32_t>(ptr[0]);
|
|
1451
|
+
b.shape = static_cast<PlanarRigidShapeKind>(ptr[1]);
|
|
1452
|
+
b.isDynamic = (ptr[2] > 0.5);
|
|
1453
|
+
b.x = ptr[3];
|
|
1454
|
+
b.y = ptr[4];
|
|
1455
|
+
b.angle = ptr[5];
|
|
1456
|
+
b.vx = ptr[6];
|
|
1457
|
+
b.vy = ptr[7];
|
|
1458
|
+
b.angularVelocity = ptr[8];
|
|
1459
|
+
b.mass = ptr[9];
|
|
1460
|
+
b.inverseMass = (b.mass > 0.0) ? (1.0 / b.mass) : 0.0;
|
|
1461
|
+
b.inertia = ptr[10];
|
|
1462
|
+
b.inverseInertia = (b.inertia > 0.0) ? (1.0 / b.inertia) : 0.0;
|
|
1463
|
+
b.halfX = ptr[11];
|
|
1464
|
+
b.halfY = ptr[12];
|
|
1465
|
+
b.radius = ptr[13];
|
|
1466
|
+
b.friction = ptr[14];
|
|
1467
|
+
b.restitution = ptr[15];
|
|
1468
|
+
}
|
|
1469
|
+
|
|
1470
|
+
// Step world
|
|
1471
|
+
const PlanarStatus status = stepPlanarDeformableWorld(world, std::span<PlanarRigidBody2D>(bodies.data(), bCount), dt);
|
|
1472
|
+
if (status != PlanarStatus::Success) return status;
|
|
1473
|
+
|
|
1474
|
+
// Write back nodes
|
|
1475
|
+
for (std::uint32_t i = 0; i < nodeCount; ++i) {
|
|
1476
|
+
double* ptr = nodeData + i * kPlanarNodeStride;
|
|
1477
|
+
const auto& n = world.nodes[i];
|
|
1478
|
+
ptr[0] = n.position.x;
|
|
1479
|
+
ptr[1] = n.position.y;
|
|
1480
|
+
ptr[2] = n.previousPosition.x;
|
|
1481
|
+
ptr[3] = n.previousPosition.y;
|
|
1482
|
+
ptr[4] = n.velocity.x;
|
|
1483
|
+
ptr[5] = n.velocity.y;
|
|
1484
|
+
ptr[6] = n.force.x;
|
|
1485
|
+
ptr[7] = n.force.y;
|
|
1486
|
+
ptr[8] = n.mass;
|
|
1487
|
+
ptr[9] = n.inverseMass;
|
|
1488
|
+
ptr[10] = n.radius;
|
|
1489
|
+
ptr[11] = static_cast<double>(n.flags);
|
|
1490
|
+
}
|
|
1491
|
+
|
|
1492
|
+
// Write back distance constraints
|
|
1493
|
+
for (std::uint32_t i = 0; i < distanceConstraintCount; ++i) {
|
|
1494
|
+
double* ptr = distanceConstraintData + i * kPlanarDistanceConstraintStride;
|
|
1495
|
+
const auto& dc = world.distanceConstraints[i];
|
|
1496
|
+
ptr[5] = dc.tension;
|
|
1497
|
+
ptr[7] = dc.broken ? 1.0 : 0.0;
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
// Write back bend constraints
|
|
1501
|
+
for (std::uint32_t i = 0; i < bendConstraintCount; ++i) {
|
|
1502
|
+
double* ptr = bendConstraintData + i * kPlanarBendConstraintStride;
|
|
1503
|
+
const auto& bc = world.bendConstraints[i];
|
|
1504
|
+
ptr[5] = bc.bendingMoment;
|
|
1505
|
+
ptr[7] = bc.broken ? 1.0 : 0.0;
|
|
1506
|
+
}
|
|
1507
|
+
|
|
1508
|
+
// Write back shear constraints
|
|
1509
|
+
for (std::uint32_t i = 0; i < shearConstraintCount; ++i) {
|
|
1510
|
+
double* ptr = shearConstraintData + i * kPlanarShearConstraintStride;
|
|
1511
|
+
const auto& sc = world.shearConstraints[i];
|
|
1512
|
+
ptr[5] = sc.shearForce;
|
|
1513
|
+
ptr[7] = sc.broken ? 1.0 : 0.0;
|
|
1514
|
+
}
|
|
1515
|
+
|
|
1516
|
+
// Write back area constraints
|
|
1517
|
+
for (std::uint32_t i = 0; i < areaConstraintCount; ++i) {
|
|
1518
|
+
double* ptr = areaConstraintData + i * kPlanarAreaConstraintStride;
|
|
1519
|
+
const auto& ac = world.areaConstraints[i];
|
|
1520
|
+
ptr[4] = ac.currentArea;
|
|
1521
|
+
ptr[6] = ac.pressure;
|
|
1522
|
+
ptr[7] = ac.broken ? 1.0 : 0.0;
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
// Write back anchors
|
|
1526
|
+
for (std::uint32_t i = 0; i < anchorCount; ++i) {
|
|
1527
|
+
double* ptr = anchorData + i * kPlanarAnchorStride;
|
|
1528
|
+
const auto& an = world.anchors[i];
|
|
1529
|
+
ptr[4] = an.worldAnchor.x;
|
|
1530
|
+
ptr[5] = an.worldAnchor.y;
|
|
1531
|
+
ptr[7] = an.reactionForce;
|
|
1532
|
+
ptr[8] = an.active ? 1.0 : 0.0;
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
// Write back particles
|
|
1536
|
+
for (std::uint32_t i = 0; i < particleCount; ++i) {
|
|
1537
|
+
double* ptr = particleData + i * kPlanarParticleStride;
|
|
1538
|
+
const auto& p = world.particles[i];
|
|
1539
|
+
ptr[0] = p.position.x;
|
|
1540
|
+
ptr[1] = p.position.y;
|
|
1541
|
+
ptr[2] = p.velocity.x;
|
|
1542
|
+
ptr[3] = p.velocity.y;
|
|
1543
|
+
ptr[7] = p.age;
|
|
1544
|
+
ptr[9] = p.active ? 1.0 : 0.0;
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
// Write back rigid bodies
|
|
1548
|
+
for (std::uint32_t i = 0; i < bCount; ++i) {
|
|
1549
|
+
double* ptr = rigidBodyData + i * kPlanarRigidBodyStride;
|
|
1550
|
+
const auto& b = bodies[i];
|
|
1551
|
+
ptr[3] = b.x;
|
|
1552
|
+
ptr[4] = b.y;
|
|
1553
|
+
ptr[5] = b.angle;
|
|
1554
|
+
ptr[6] = b.vx;
|
|
1555
|
+
ptr[7] = b.vy;
|
|
1556
|
+
ptr[8] = b.angularVelocity;
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1559
|
+
// Write back break events
|
|
1560
|
+
if (breakEventsData && breakEventCount) {
|
|
1561
|
+
const std::uint32_t maxEvents = *breakEventCount;
|
|
1562
|
+
const std::uint32_t copyCount = minVal(maxEvents, world.breakEventCount);
|
|
1563
|
+
for (std::uint32_t i = 0; i < copyCount; ++i) {
|
|
1564
|
+
double* ptr = breakEventsData + i * 4;
|
|
1565
|
+
const auto& ev = world.breakEvents[i];
|
|
1566
|
+
ptr[0] = static_cast<double>(ev.systemId);
|
|
1567
|
+
ptr[1] = static_cast<double>(ev.elementIndex);
|
|
1568
|
+
ptr[2] = static_cast<double>(ev.reason);
|
|
1569
|
+
ptr[3] = ev.measuredValue;
|
|
1570
|
+
}
|
|
1571
|
+
*breakEventCount = copyCount;
|
|
1572
|
+
}
|
|
1573
|
+
|
|
1574
|
+
return PlanarStatus::Success;
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
// ---------------------------------------------------------------------------
|
|
1578
|
+
// Queries
|
|
1579
|
+
// ---------------------------------------------------------------------------
|
|
1580
|
+
|
|
1581
|
+
PlanarStatus raycastPlanarDeformable(
|
|
1582
|
+
const PlanarDeformableWorld& world,
|
|
1583
|
+
const PlanarRaycastQuery& query,
|
|
1584
|
+
PlanarRaycastHit& outHit) {
|
|
1585
|
+
outHit = PlanarRaycastHit{};
|
|
1586
|
+
const double dirLenSq = lengthSquaredPlanarVec2(query.direction);
|
|
1587
|
+
if (dirLenSq < kEpsilon) return PlanarStatus::InvalidInput;
|
|
1588
|
+
|
|
1589
|
+
const PlanarVec2 dir = scalePlanarVec2(query.direction, 1.0 / std::sqrt(dirLenSq));
|
|
1590
|
+
double closestDist = query.maxDistance;
|
|
1591
|
+
bool found = false;
|
|
1592
|
+
|
|
1593
|
+
// 1. Ray vs Nodes
|
|
1594
|
+
if ((query.flags & 0x01) != 0) {
|
|
1595
|
+
for (std::uint32_t i = 0; i < world.nodeCount; ++i) {
|
|
1596
|
+
const auto& node = world.nodes[i];
|
|
1597
|
+
const PlanarVec2 m = subPlanarVec2(query.origin, node.position);
|
|
1598
|
+
const double b = dotPlanarVec2(m, dir);
|
|
1599
|
+
const double c = lengthSquaredPlanarVec2(m) - node.radius * node.radius;
|
|
1600
|
+
|
|
1601
|
+
if (c > 0.0 && b > 0.0) continue;
|
|
1602
|
+
const double discr = b * b - c;
|
|
1603
|
+
if (discr < 0.0) continue;
|
|
1604
|
+
|
|
1605
|
+
double t = -b - std::sqrt(discr);
|
|
1606
|
+
if (t < 0.0) t = 0.0;
|
|
1607
|
+
if (t <= closestDist) {
|
|
1608
|
+
closestDist = t;
|
|
1609
|
+
found = true;
|
|
1610
|
+
outHit.hit = true;
|
|
1611
|
+
outHit.distance = t;
|
|
1612
|
+
outHit.point = addPlanarVec2(query.origin, scalePlanarVec2(dir, t));
|
|
1613
|
+
outHit.normal = normalizePlanarVec2(subPlanarVec2(outHit.point, node.position));
|
|
1614
|
+
outHit.elementKind = PlanarElementKind::Node;
|
|
1615
|
+
outHit.elementIndex = i;
|
|
1616
|
+
outHit.nodeIndex = i;
|
|
1617
|
+
outHit.parameter = 0.0;
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
// 2. Ray vs Segments
|
|
1623
|
+
if ((query.flags & 0x02) != 0) {
|
|
1624
|
+
for (std::uint32_t i = 0; i < world.segmentCount; ++i) {
|
|
1625
|
+
const auto& seg = world.segments[i];
|
|
1626
|
+
if (seg.broken || seg.nodeA >= world.nodeCount || seg.nodeB >= world.nodeCount) continue;
|
|
1627
|
+
|
|
1628
|
+
const PlanarVec2 pA = world.nodes[seg.nodeA].position;
|
|
1629
|
+
const PlanarVec2 pB = world.nodes[seg.nodeB].position;
|
|
1630
|
+
const PlanarVec2 segVec = subPlanarVec2(pB, pA);
|
|
1631
|
+
const double segLen = lengthPlanarVec2(segVec);
|
|
1632
|
+
if (segLen < kEpsilon) continue;
|
|
1633
|
+
|
|
1634
|
+
const PlanarVec2 segDir = scalePlanarVec2(segVec, 1.0 / segLen);
|
|
1635
|
+
const PlanarVec2 segNorm = perpendicularPlanarVec2(segDir);
|
|
1636
|
+
|
|
1637
|
+
// Line-line intersection between ray and segment
|
|
1638
|
+
const double denom = crossPlanarVec2(dir, segDir);
|
|
1639
|
+
if (std::abs(denom) > kEpsilon) {
|
|
1640
|
+
const PlanarVec2 diff = subPlanarVec2(pA, query.origin);
|
|
1641
|
+
const double tRay = crossPlanarVec2(diff, segDir) / denom;
|
|
1642
|
+
const double uSeg = crossPlanarVec2(diff, dir) / denom;
|
|
1643
|
+
|
|
1644
|
+
if (tRay >= 0.0 && tRay <= closestDist && uSeg >= 0.0 && uSeg <= segLen) {
|
|
1645
|
+
closestDist = tRay;
|
|
1646
|
+
found = true;
|
|
1647
|
+
outHit.hit = true;
|
|
1648
|
+
outHit.distance = tRay;
|
|
1649
|
+
outHit.point = addPlanarVec2(query.origin, scalePlanarVec2(dir, tRay));
|
|
1650
|
+
outHit.normal = (dotPlanarVec2(dir, segNorm) < 0.0) ? segNorm : scalePlanarVec2(segNorm, -1.0);
|
|
1651
|
+
outHit.elementKind = PlanarElementKind::Segment;
|
|
1652
|
+
outHit.elementIndex = i;
|
|
1653
|
+
outHit.nodeIndex = seg.nodeA;
|
|
1654
|
+
outHit.parameter = uSeg / segLen;
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
// 3. Ray vs Faces
|
|
1661
|
+
if ((query.flags & 0x04) != 0) {
|
|
1662
|
+
for (std::uint32_t f = 0; f < world.faceCount; ++f) {
|
|
1663
|
+
const auto& face = world.faces[f];
|
|
1664
|
+
if (face.broken || face.nodeA >= world.nodeCount || face.nodeB >= world.nodeCount || face.nodeC >= world.nodeCount) continue;
|
|
1665
|
+
|
|
1666
|
+
const PlanarVec2 v0 = world.nodes[face.nodeA].position;
|
|
1667
|
+
const PlanarVec2 v1 = world.nodes[face.nodeB].position;
|
|
1668
|
+
const PlanarVec2 v2 = world.nodes[face.nodeC].position;
|
|
1669
|
+
|
|
1670
|
+
// Test intersection against 3 edges of triangle
|
|
1671
|
+
const std::array<std::pair<PlanarVec2, PlanarVec2>, 3> edges{{{v0, v1}, {v1, v2}, {v2, v0}}};
|
|
1672
|
+
for (const auto& edge : edges) {
|
|
1673
|
+
const PlanarVec2 eVec = subPlanarVec2(edge.second, edge.first);
|
|
1674
|
+
const double denom = crossPlanarVec2(dir, eVec);
|
|
1675
|
+
if (std::abs(denom) < kEpsilon) continue;
|
|
1676
|
+
|
|
1677
|
+
const PlanarVec2 diff = subPlanarVec2(edge.first, query.origin);
|
|
1678
|
+
const double tRay = crossPlanarVec2(diff, eVec) / denom;
|
|
1679
|
+
const double uEdge = crossPlanarVec2(diff, dir) / denom;
|
|
1680
|
+
|
|
1681
|
+
if (tRay >= 0.0 && tRay <= closestDist && uEdge >= 0.0 && uEdge <= 1.0) {
|
|
1682
|
+
closestDist = tRay;
|
|
1683
|
+
found = true;
|
|
1684
|
+
outHit.hit = true;
|
|
1685
|
+
outHit.distance = tRay;
|
|
1686
|
+
outHit.point = addPlanarVec2(query.origin, scalePlanarVec2(dir, tRay));
|
|
1687
|
+
outHit.normal = normalizePlanarVec2(perpendicularPlanarVec2(eVec));
|
|
1688
|
+
outHit.elementKind = PlanarElementKind::Face;
|
|
1689
|
+
outHit.elementIndex = f;
|
|
1690
|
+
outHit.nodeIndex = face.nodeA;
|
|
1691
|
+
outHit.parameter = uEdge;
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
}
|
|
1696
|
+
|
|
1697
|
+
// 4. Ray vs Particles
|
|
1698
|
+
if ((query.flags & 0x08) != 0) {
|
|
1699
|
+
for (std::uint32_t i = 0; i < world.particleCount; ++i) {
|
|
1700
|
+
const auto& p = world.particles[i];
|
|
1701
|
+
if (!p.active) continue;
|
|
1702
|
+
|
|
1703
|
+
const PlanarVec2 m = subPlanarVec2(query.origin, p.position);
|
|
1704
|
+
const double b = dotPlanarVec2(m, dir);
|
|
1705
|
+
const double c = lengthSquaredPlanarVec2(m) - p.radius * p.radius;
|
|
1706
|
+
|
|
1707
|
+
if (c > 0.0 && b > 0.0) continue;
|
|
1708
|
+
const double discr = b * b - c;
|
|
1709
|
+
if (discr < 0.0) continue;
|
|
1710
|
+
|
|
1711
|
+
double t = -b - std::sqrt(discr);
|
|
1712
|
+
if (t < 0.0) t = 0.0;
|
|
1713
|
+
if (t <= closestDist) {
|
|
1714
|
+
closestDist = t;
|
|
1715
|
+
found = true;
|
|
1716
|
+
outHit.hit = true;
|
|
1717
|
+
outHit.distance = t;
|
|
1718
|
+
outHit.point = addPlanarVec2(query.origin, scalePlanarVec2(dir, t));
|
|
1719
|
+
outHit.normal = normalizePlanarVec2(subPlanarVec2(outHit.point, p.position));
|
|
1720
|
+
outHit.elementKind = PlanarElementKind::Particle;
|
|
1721
|
+
outHit.elementIndex = i;
|
|
1722
|
+
outHit.nodeIndex = i;
|
|
1723
|
+
outHit.parameter = 0.0;
|
|
1724
|
+
}
|
|
1725
|
+
}
|
|
1726
|
+
}
|
|
1727
|
+
|
|
1728
|
+
return found ? PlanarStatus::Success : PlanarStatus::NoHit;
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
PlanarStatus closestPointPlanarDeformable(
|
|
1732
|
+
const PlanarDeformableWorld& world,
|
|
1733
|
+
const PlanarClosestPointQuery& query,
|
|
1734
|
+
PlanarClosestPointHit& outHit) {
|
|
1735
|
+
outHit = PlanarClosestPointHit{};
|
|
1736
|
+
double closestDist = query.maxDistance;
|
|
1737
|
+
bool found = false;
|
|
1738
|
+
|
|
1739
|
+
// Nodes
|
|
1740
|
+
if ((query.flags & 0x01) != 0) {
|
|
1741
|
+
for (std::uint32_t i = 0; i < world.nodeCount; ++i) {
|
|
1742
|
+
const auto& node = world.nodes[i];
|
|
1743
|
+
const double d = distancePlanarVec2(query.point, node.position) - node.radius;
|
|
1744
|
+
if (d < closestDist) {
|
|
1745
|
+
closestDist = maxVal(0.0, d);
|
|
1746
|
+
found = true;
|
|
1747
|
+
outHit.hit = true;
|
|
1748
|
+
outHit.distance = closestDist;
|
|
1749
|
+
outHit.normal = normalizePlanarVec2(subPlanarVec2(query.point, node.position));
|
|
1750
|
+
outHit.point = addPlanarVec2(node.position, scalePlanarVec2(outHit.normal, node.radius));
|
|
1751
|
+
outHit.elementKind = PlanarElementKind::Node;
|
|
1752
|
+
outHit.elementIndex = i;
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
// Segments
|
|
1758
|
+
if ((query.flags & 0x02) != 0) {
|
|
1759
|
+
for (std::uint32_t i = 0; i < world.segmentCount; ++i) {
|
|
1760
|
+
const auto& seg = world.segments[i];
|
|
1761
|
+
if (seg.broken || seg.nodeA >= world.nodeCount || seg.nodeB >= world.nodeCount) continue;
|
|
1762
|
+
|
|
1763
|
+
const PlanarVec2 pA = world.nodes[seg.nodeA].position;
|
|
1764
|
+
const PlanarVec2 pB = world.nodes[seg.nodeB].position;
|
|
1765
|
+
const PlanarVec2 ab = subPlanarVec2(pB, pA);
|
|
1766
|
+
const double abLenSq = lengthSquaredPlanarVec2(ab);
|
|
1767
|
+
if (abLenSq < kEpsilon) continue;
|
|
1768
|
+
|
|
1769
|
+
const double t = clampVal(dotPlanarVec2(subPlanarVec2(query.point, pA), ab) / abLenSq, 0.0, 1.0);
|
|
1770
|
+
const PlanarVec2 proj = addPlanarVec2(pA, scalePlanarVec2(ab, t));
|
|
1771
|
+
const double d = distancePlanarVec2(query.point, proj) - seg.radius;
|
|
1772
|
+
if (d < closestDist) {
|
|
1773
|
+
closestDist = maxVal(0.0, d);
|
|
1774
|
+
found = true;
|
|
1775
|
+
outHit.hit = true;
|
|
1776
|
+
outHit.distance = closestDist;
|
|
1777
|
+
outHit.normal = normalizePlanarVec2(subPlanarVec2(query.point, proj));
|
|
1778
|
+
outHit.point = addPlanarVec2(proj, scalePlanarVec2(outHit.normal, seg.radius));
|
|
1779
|
+
outHit.elementKind = PlanarElementKind::Segment;
|
|
1780
|
+
outHit.elementIndex = i;
|
|
1781
|
+
}
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
return found ? PlanarStatus::Success : PlanarStatus::NoHit;
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
PlanarStatus overlapPlanarDeformable(
|
|
1789
|
+
const PlanarDeformableWorld& world,
|
|
1790
|
+
const PlanarOverlapQuery& query,
|
|
1791
|
+
PlanarOverlapResult& outResult) {
|
|
1792
|
+
outResult = PlanarOverlapResult{};
|
|
1793
|
+
std::uint32_t hitCount = 0;
|
|
1794
|
+
double maxPen = 0.0;
|
|
1795
|
+
|
|
1796
|
+
for (std::uint32_t i = 0; i < world.nodeCount && hitCount < 128; ++i) {
|
|
1797
|
+
const auto& node = world.nodes[i];
|
|
1798
|
+
bool overlaps = false;
|
|
1799
|
+
double pen = 0.0;
|
|
1800
|
+
|
|
1801
|
+
if (query.shapeType == 0) {
|
|
1802
|
+
// Circle overlap
|
|
1803
|
+
const double r = query.halfExtents.x;
|
|
1804
|
+
const double dist = distancePlanarVec2(query.center, node.position);
|
|
1805
|
+
if (dist <= r + node.radius) {
|
|
1806
|
+
overlaps = true;
|
|
1807
|
+
pen = (r + node.radius) - dist;
|
|
1808
|
+
}
|
|
1809
|
+
} else {
|
|
1810
|
+
// Box / AABB overlap
|
|
1811
|
+
const PlanarVec2 rel = unrotatePlanarVec2(subPlanarVec2(node.position, query.center), query.angle);
|
|
1812
|
+
const double dx = std::abs(rel.x) - (query.halfExtents.x + node.radius);
|
|
1813
|
+
const double dy = std::abs(rel.y) - (query.halfExtents.y + node.radius);
|
|
1814
|
+
if (dx <= 0.0 && dy <= 0.0) {
|
|
1815
|
+
overlaps = true;
|
|
1816
|
+
pen = minVal(-dx, -dy);
|
|
1817
|
+
}
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1820
|
+
if (overlaps) {
|
|
1821
|
+
outResult.hitIndices[hitCount] = i;
|
|
1822
|
+
outResult.hitKinds[hitCount] = PlanarElementKind::Node;
|
|
1823
|
+
maxPen = maxVal(maxPen, pen);
|
|
1824
|
+
hitCount++;
|
|
1825
|
+
}
|
|
1826
|
+
}
|
|
1827
|
+
|
|
1828
|
+
outResult.count = hitCount;
|
|
1829
|
+
outResult.maxPenetration = maxPen;
|
|
1830
|
+
return PlanarStatus::Success;
|
|
1831
|
+
}
|
|
1832
|
+
|
|
1833
|
+
PlanarAABB computeAABBPlanarDeformable(
|
|
1834
|
+
const PlanarDeformableWorld& world) {
|
|
1835
|
+
PlanarAABB aabb{};
|
|
1836
|
+
if (world.nodeCount == 0 && world.activeParticleCount == 0) {
|
|
1837
|
+
return aabb;
|
|
1838
|
+
}
|
|
1839
|
+
|
|
1840
|
+
bool initialized = false;
|
|
1841
|
+
for (std::uint32_t i = 0; i < world.nodeCount; ++i) {
|
|
1842
|
+
const auto& n = world.nodes[i];
|
|
1843
|
+
if (!initialized) {
|
|
1844
|
+
aabb.min = {n.position.x - n.radius, n.position.y - n.radius};
|
|
1845
|
+
aabb.max = {n.position.x + n.radius, n.position.y + n.radius};
|
|
1846
|
+
initialized = true;
|
|
1847
|
+
} else {
|
|
1848
|
+
aabb.min.x = minVal(aabb.min.x, n.position.x - n.radius);
|
|
1849
|
+
aabb.min.y = minVal(aabb.min.y, n.position.y - n.radius);
|
|
1850
|
+
aabb.max.x = maxVal(aabb.max.x, n.position.x + n.radius);
|
|
1851
|
+
aabb.max.y = maxVal(aabb.max.y, n.position.y + n.radius);
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
|
|
1855
|
+
for (std::uint32_t i = 0; i < world.particleCount; ++i) {
|
|
1856
|
+
const auto& p = world.particles[i];
|
|
1857
|
+
if (!p.active) continue;
|
|
1858
|
+
if (!initialized) {
|
|
1859
|
+
aabb.min = {p.position.x - p.radius, p.position.y - p.radius};
|
|
1860
|
+
aabb.max = {p.position.x + p.radius, p.position.y + p.radius};
|
|
1861
|
+
initialized = true;
|
|
1862
|
+
} else {
|
|
1863
|
+
aabb.min.x = minVal(aabb.min.x, p.position.x - p.radius);
|
|
1864
|
+
aabb.min.y = minVal(aabb.min.y, p.position.y - p.radius);
|
|
1865
|
+
aabb.max.x = maxVal(aabb.max.x, p.position.x + p.radius);
|
|
1866
|
+
aabb.max.y = maxVal(aabb.max.y, p.position.y + p.radius);
|
|
1867
|
+
}
|
|
1868
|
+
}
|
|
1869
|
+
|
|
1870
|
+
return aabb;
|
|
1871
|
+
}
|
|
1872
|
+
|
|
1873
|
+
// ---------------------------------------------------------------------------
|
|
1874
|
+
// Rollback & State Snapshots
|
|
1875
|
+
// ---------------------------------------------------------------------------
|
|
1876
|
+
|
|
1877
|
+
PlanarStatus captureSnapshot(
|
|
1878
|
+
const PlanarDeformableWorld& world,
|
|
1879
|
+
std::span<std::uint8_t> outBuffer,
|
|
1880
|
+
std::uint32_t& outBytes) {
|
|
1881
|
+
const std::size_t headerSize = sizeof(PlanarSnapshotHeader);
|
|
1882
|
+
const std::size_t nodesBytes = world.nodeCount * sizeof(PlanarDeformableNode);
|
|
1883
|
+
const std::size_t distBytes = world.distanceConstraintCount * sizeof(PlanarDistanceConstraint);
|
|
1884
|
+
const std::size_t bendBytes = world.bendConstraintCount * sizeof(PlanarBendConstraint);
|
|
1885
|
+
const std::size_t shearBytes = world.shearConstraintCount * sizeof(PlanarShearConstraint);
|
|
1886
|
+
const std::size_t areaBytes = world.areaConstraintCount * sizeof(PlanarAreaConstraint);
|
|
1887
|
+
const std::size_t anchorBytes = world.anchorCount * sizeof(PlanarAnchor);
|
|
1888
|
+
const std::size_t particleBytes = world.particleCount * sizeof(PlanarParticle);
|
|
1889
|
+
const std::size_t emitterBytes = world.emitterCount * sizeof(PlanarEmitter);
|
|
1890
|
+
const std::size_t teleBytes = sizeof(double) * 6 + sizeof(std::uint64_t); // Telemetry + RNG state
|
|
1891
|
+
|
|
1892
|
+
const std::size_t payloadBytes = nodesBytes + distBytes + bendBytes + shearBytes +
|
|
1893
|
+
areaBytes + anchorBytes + particleBytes + emitterBytes + teleBytes;
|
|
1894
|
+
const std::size_t totalBytes = headerSize + payloadBytes;
|
|
1895
|
+
|
|
1896
|
+
if (outBuffer.size() < totalBytes) {
|
|
1897
|
+
return PlanarStatus::BufferOverflow;
|
|
1898
|
+
}
|
|
1899
|
+
|
|
1900
|
+
PlanarSnapshotHeader header{};
|
|
1901
|
+
header.magic[0] = 'P';
|
|
1902
|
+
header.magic[1] = 'D';
|
|
1903
|
+
header.magic[2] = 'E';
|
|
1904
|
+
header.magic[3] = 'F';
|
|
1905
|
+
header.version = kPlanarDeformableSnapshotVersion;
|
|
1906
|
+
header.tick = world.tick;
|
|
1907
|
+
header.nodeCount = world.nodeCount;
|
|
1908
|
+
header.segmentCount = world.segmentCount;
|
|
1909
|
+
header.faceCount = world.faceCount;
|
|
1910
|
+
header.distanceConstraintCount = world.distanceConstraintCount;
|
|
1911
|
+
header.bendConstraintCount = world.bendConstraintCount;
|
|
1912
|
+
header.shearConstraintCount = world.shearConstraintCount;
|
|
1913
|
+
header.areaConstraintCount = world.areaConstraintCount;
|
|
1914
|
+
header.anchorCount = world.anchorCount;
|
|
1915
|
+
header.particleCount = world.particleCount;
|
|
1916
|
+
header.activeParticleCount = world.activeParticleCount;
|
|
1917
|
+
header.emitterCount = world.emitterCount;
|
|
1918
|
+
header.payloadBytes = payloadBytes;
|
|
1919
|
+
|
|
1920
|
+
std::uint8_t* payloadPtr = outBuffer.data() + headerSize;
|
|
1921
|
+
std::size_t offset = 0;
|
|
1922
|
+
|
|
1923
|
+
if (nodesBytes > 0) {
|
|
1924
|
+
std::memcpy(payloadPtr + offset, world.nodes.data(), nodesBytes);
|
|
1925
|
+
offset += nodesBytes;
|
|
1926
|
+
}
|
|
1927
|
+
if (distBytes > 0) {
|
|
1928
|
+
std::memcpy(payloadPtr + offset, world.distanceConstraints.data(), distBytes);
|
|
1929
|
+
offset += distBytes;
|
|
1930
|
+
}
|
|
1931
|
+
if (bendBytes > 0) {
|
|
1932
|
+
std::memcpy(payloadPtr + offset, world.bendConstraints.data(), bendBytes);
|
|
1933
|
+
offset += bendBytes;
|
|
1934
|
+
}
|
|
1935
|
+
if (shearBytes > 0) {
|
|
1936
|
+
std::memcpy(payloadPtr + offset, world.shearConstraints.data(), shearBytes);
|
|
1937
|
+
offset += shearBytes;
|
|
1938
|
+
}
|
|
1939
|
+
if (areaBytes > 0) {
|
|
1940
|
+
std::memcpy(payloadPtr + offset, world.areaConstraints.data(), areaBytes);
|
|
1941
|
+
offset += areaBytes;
|
|
1942
|
+
}
|
|
1943
|
+
if (anchorBytes > 0) {
|
|
1944
|
+
std::memcpy(payloadPtr + offset, world.anchors.data(), anchorBytes);
|
|
1945
|
+
offset += anchorBytes;
|
|
1946
|
+
}
|
|
1947
|
+
if (particleBytes > 0) {
|
|
1948
|
+
std::memcpy(payloadPtr + offset, world.particles.data(), particleBytes);
|
|
1949
|
+
offset += particleBytes;
|
|
1950
|
+
}
|
|
1951
|
+
if (emitterBytes > 0) {
|
|
1952
|
+
std::memcpy(payloadPtr + offset, world.emitters.data(), emitterBytes);
|
|
1953
|
+
offset += emitterBytes;
|
|
1954
|
+
}
|
|
1955
|
+
|
|
1956
|
+
// Telemetry & RNG
|
|
1957
|
+
double telemetry[6] = {
|
|
1958
|
+
world.maxTension, world.maxBendMoment, world.maxShearForce,
|
|
1959
|
+
world.totalKineticEnergy, world.totalPotentialEnergy, world.totalDeformedArea
|
|
1960
|
+
};
|
|
1961
|
+
std::memcpy(payloadPtr + offset, telemetry, sizeof(telemetry));
|
|
1962
|
+
offset += sizeof(telemetry);
|
|
1963
|
+
std::memcpy(payloadPtr + offset, &world.rng.state, sizeof(world.rng.state));
|
|
1964
|
+
offset += sizeof(world.rng.state);
|
|
1965
|
+
|
|
1966
|
+
header.checksum = fnv1aChecksum(payloadPtr, payloadBytes);
|
|
1967
|
+
std::memcpy(outBuffer.data(), &header, headerSize);
|
|
1968
|
+
|
|
1969
|
+
outBytes = static_cast<std::uint32_t>(totalBytes);
|
|
1970
|
+
return PlanarStatus::Success;
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
PlanarStatus restoreSnapshot(
|
|
1974
|
+
PlanarDeformableWorld& world,
|
|
1975
|
+
std::span<const std::uint8_t> inBuffer) {
|
|
1976
|
+
const std::size_t headerSize = sizeof(PlanarSnapshotHeader);
|
|
1977
|
+
if (inBuffer.size() < headerSize) {
|
|
1978
|
+
return PlanarStatus::InvalidInput;
|
|
1979
|
+
}
|
|
1980
|
+
|
|
1981
|
+
PlanarSnapshotHeader header{};
|
|
1982
|
+
std::memcpy(&header, inBuffer.data(), headerSize);
|
|
1983
|
+
|
|
1984
|
+
if (header.magic[0] != 'P' || header.magic[1] != 'D' ||
|
|
1985
|
+
header.magic[2] != 'E' || header.magic[3] != 'F') {
|
|
1986
|
+
return PlanarStatus::SnapshotCorrupted;
|
|
1987
|
+
}
|
|
1988
|
+
if (header.version != kPlanarDeformableSnapshotVersion) {
|
|
1989
|
+
return PlanarStatus::SnapshotVersionMismatch;
|
|
1990
|
+
}
|
|
1991
|
+
if (inBuffer.size() < headerSize + header.payloadBytes) {
|
|
1992
|
+
return PlanarStatus::SnapshotCorrupted;
|
|
1993
|
+
}
|
|
1994
|
+
|
|
1995
|
+
const std::uint8_t* payloadPtr = inBuffer.data() + headerSize;
|
|
1996
|
+
const std::uint32_t computedChecksum = fnv1aChecksum(payloadPtr, header.payloadBytes);
|
|
1997
|
+
if (computedChecksum != header.checksum) {
|
|
1998
|
+
return PlanarStatus::SnapshotCorrupted;
|
|
1999
|
+
}
|
|
2000
|
+
|
|
2001
|
+
if (header.nodeCount > kPlanarMaxNodes ||
|
|
2002
|
+
header.distanceConstraintCount > kPlanarMaxDistanceConstraints ||
|
|
2003
|
+
header.bendConstraintCount > kPlanarMaxBendConstraints ||
|
|
2004
|
+
header.shearConstraintCount > kPlanarMaxShearConstraints ||
|
|
2005
|
+
header.areaConstraintCount > kPlanarMaxAreaConstraints ||
|
|
2006
|
+
header.anchorCount > kPlanarMaxAnchors ||
|
|
2007
|
+
header.particleCount > kPlanarMaxParticles ||
|
|
2008
|
+
header.emitterCount > kPlanarMaxEmitters) {
|
|
2009
|
+
return PlanarStatus::BufferOverflow;
|
|
2010
|
+
}
|
|
2011
|
+
|
|
2012
|
+
world.tick = header.tick;
|
|
2013
|
+
world.nodeCount = header.nodeCount;
|
|
2014
|
+
world.distanceConstraintCount = header.distanceConstraintCount;
|
|
2015
|
+
world.bendConstraintCount = header.bendConstraintCount;
|
|
2016
|
+
world.shearConstraintCount = header.shearConstraintCount;
|
|
2017
|
+
world.areaConstraintCount = header.areaConstraintCount;
|
|
2018
|
+
world.anchorCount = header.anchorCount;
|
|
2019
|
+
world.particleCount = header.particleCount;
|
|
2020
|
+
world.activeParticleCount = header.activeParticleCount;
|
|
2021
|
+
world.emitterCount = header.emitterCount;
|
|
2022
|
+
|
|
2023
|
+
std::size_t offset = 0;
|
|
2024
|
+
const std::size_t nodesBytes = header.nodeCount * sizeof(PlanarDeformableNode);
|
|
2025
|
+
if (nodesBytes > 0) {
|
|
2026
|
+
std::memcpy(world.nodes.data(), payloadPtr + offset, nodesBytes);
|
|
2027
|
+
offset += nodesBytes;
|
|
2028
|
+
}
|
|
2029
|
+
|
|
2030
|
+
const std::size_t distBytes = header.distanceConstraintCount * sizeof(PlanarDistanceConstraint);
|
|
2031
|
+
if (distBytes > 0) {
|
|
2032
|
+
std::memcpy(world.distanceConstraints.data(), payloadPtr + offset, distBytes);
|
|
2033
|
+
offset += distBytes;
|
|
2034
|
+
}
|
|
2035
|
+
|
|
2036
|
+
const std::size_t bendBytes = header.bendConstraintCount * sizeof(PlanarBendConstraint);
|
|
2037
|
+
if (bendBytes > 0) {
|
|
2038
|
+
std::memcpy(world.bendConstraints.data(), payloadPtr + offset, bendBytes);
|
|
2039
|
+
offset += bendBytes;
|
|
2040
|
+
}
|
|
2041
|
+
|
|
2042
|
+
const std::size_t shearBytes = header.shearConstraintCount * sizeof(PlanarShearConstraint);
|
|
2043
|
+
if (shearBytes > 0) {
|
|
2044
|
+
std::memcpy(world.shearConstraints.data(), payloadPtr + offset, shearBytes);
|
|
2045
|
+
offset += shearBytes;
|
|
2046
|
+
}
|
|
2047
|
+
|
|
2048
|
+
const std::size_t areaBytes = header.areaConstraintCount * sizeof(PlanarAreaConstraint);
|
|
2049
|
+
if (areaBytes > 0) {
|
|
2050
|
+
std::memcpy(world.areaConstraints.data(), payloadPtr + offset, areaBytes);
|
|
2051
|
+
offset += areaBytes;
|
|
2052
|
+
}
|
|
2053
|
+
|
|
2054
|
+
const std::size_t anchorBytes = header.anchorCount * sizeof(PlanarAnchor);
|
|
2055
|
+
if (anchorBytes > 0) {
|
|
2056
|
+
std::memcpy(world.anchors.data(), payloadPtr + offset, anchorBytes);
|
|
2057
|
+
offset += anchorBytes;
|
|
2058
|
+
}
|
|
2059
|
+
|
|
2060
|
+
const std::size_t particleBytes = header.particleCount * sizeof(PlanarParticle);
|
|
2061
|
+
if (particleBytes > 0) {
|
|
2062
|
+
std::memcpy(world.particles.data(), payloadPtr + offset, particleBytes);
|
|
2063
|
+
offset += particleBytes;
|
|
2064
|
+
}
|
|
2065
|
+
|
|
2066
|
+
const std::size_t emitterBytes = header.emitterCount * sizeof(PlanarEmitter);
|
|
2067
|
+
if (emitterBytes > 0) {
|
|
2068
|
+
std::memcpy(world.emitters.data(), payloadPtr + offset, emitterBytes);
|
|
2069
|
+
offset += emitterBytes;
|
|
2070
|
+
}
|
|
2071
|
+
|
|
2072
|
+
double telemetry[6]{};
|
|
2073
|
+
std::memcpy(telemetry, payloadPtr + offset, sizeof(telemetry));
|
|
2074
|
+
offset += sizeof(telemetry);
|
|
2075
|
+
world.maxTension = telemetry[0];
|
|
2076
|
+
world.maxBendMoment = telemetry[1];
|
|
2077
|
+
world.maxShearForce = telemetry[2];
|
|
2078
|
+
world.totalKineticEnergy = telemetry[3];
|
|
2079
|
+
world.totalPotentialEnergy = telemetry[4];
|
|
2080
|
+
world.totalDeformedArea = telemetry[5];
|
|
2081
|
+
|
|
2082
|
+
std::memcpy(&world.rng.state, payloadPtr + offset, sizeof(world.rng.state));
|
|
2083
|
+
|
|
2084
|
+
return PlanarStatus::Success;
|
|
2085
|
+
}
|
|
2086
|
+
|
|
2087
|
+
} // namespace rundot::kinetix::physics
|
|
2088
|
+
|
|
2089
|
+
// ---------------------------------------------------------------------------
|
|
2090
|
+
// C ABI Implementation
|
|
2091
|
+
// ---------------------------------------------------------------------------
|
|
2092
|
+
|
|
2093
|
+
extern "C" {
|
|
2094
|
+
|
|
2095
|
+
using namespace rundot::kinetix::physics;
|
|
2096
|
+
|
|
2097
|
+
std::uint32_t rundot_kinetix_ext_d18_abi_version() {
|
|
2098
|
+
return kPlanarDeformableAbiVersion;
|
|
2099
|
+
}
|
|
2100
|
+
|
|
2101
|
+
std::uint32_t rundot_kinetix_ext_d18_node_stride() {
|
|
2102
|
+
return kPlanarNodeStride;
|
|
2103
|
+
}
|
|
2104
|
+
|
|
2105
|
+
std::uint32_t rundot_kinetix_ext_d18_segment_stride() {
|
|
2106
|
+
return kPlanarSegmentStride;
|
|
2107
|
+
}
|
|
2108
|
+
|
|
2109
|
+
std::uint32_t rundot_kinetix_ext_d18_face_stride() {
|
|
2110
|
+
return kPlanarFaceStride;
|
|
2111
|
+
}
|
|
2112
|
+
|
|
2113
|
+
std::uint32_t rundot_kinetix_ext_d18_distance_constraint_stride() {
|
|
2114
|
+
return kPlanarDistanceConstraintStride;
|
|
2115
|
+
}
|
|
2116
|
+
|
|
2117
|
+
std::uint32_t rundot_kinetix_ext_d18_bend_constraint_stride() {
|
|
2118
|
+
return kPlanarBendConstraintStride;
|
|
2119
|
+
}
|
|
2120
|
+
|
|
2121
|
+
std::uint32_t rundot_kinetix_ext_d18_shear_constraint_stride() {
|
|
2122
|
+
return kPlanarShearConstraintStride;
|
|
2123
|
+
}
|
|
2124
|
+
|
|
2125
|
+
std::uint32_t rundot_kinetix_ext_d18_area_constraint_stride() {
|
|
2126
|
+
return kPlanarAreaConstraintStride;
|
|
2127
|
+
}
|
|
2128
|
+
|
|
2129
|
+
std::uint32_t rundot_kinetix_ext_d18_anchor_stride() {
|
|
2130
|
+
return kPlanarAnchorStride;
|
|
2131
|
+
}
|
|
2132
|
+
|
|
2133
|
+
std::uint32_t rundot_kinetix_ext_d18_particle_stride() {
|
|
2134
|
+
return kPlanarParticleStride;
|
|
2135
|
+
}
|
|
2136
|
+
|
|
2137
|
+
std::uint32_t rundot_kinetix_ext_d18_emitter_stride() {
|
|
2138
|
+
return kPlanarEmitterStride;
|
|
2139
|
+
}
|
|
2140
|
+
|
|
2141
|
+
std::uint32_t rundot_kinetix_ext_d18_params_stride() {
|
|
2142
|
+
return kPlanarParamsStride;
|
|
2143
|
+
}
|
|
2144
|
+
|
|
2145
|
+
std::uint32_t rundot_kinetix_ext_d18_rigid_body_stride() {
|
|
2146
|
+
return kPlanarRigidBodyStride;
|
|
2147
|
+
}
|
|
2148
|
+
|
|
2149
|
+
std::uint32_t rundot_kinetix_ext_d18_raycast_query_stride() {
|
|
2150
|
+
return kPlanarRaycastQueryStride;
|
|
2151
|
+
}
|
|
2152
|
+
|
|
2153
|
+
std::uint32_t rundot_kinetix_ext_d18_raycast_hit_stride() {
|
|
2154
|
+
return kPlanarRaycastHitStride;
|
|
2155
|
+
}
|
|
2156
|
+
|
|
2157
|
+
std::uint32_t rundot_kinetix_ext_d18_closest_point_query_stride() {
|
|
2158
|
+
return kPlanarClosestPointQueryStride;
|
|
2159
|
+
}
|
|
2160
|
+
|
|
2161
|
+
std::uint32_t rundot_kinetix_ext_d18_closest_point_hit_stride() {
|
|
2162
|
+
return kPlanarClosestPointHitStride;
|
|
2163
|
+
}
|
|
2164
|
+
|
|
2165
|
+
std::uint32_t rundot_kinetix_ext_d18_overlap_query_stride() {
|
|
2166
|
+
return kPlanarOverlapQueryStride;
|
|
2167
|
+
}
|
|
2168
|
+
|
|
2169
|
+
std::uint32_t rundot_kinetix_ext_d18_overlap_hit_stride() {
|
|
2170
|
+
return kPlanarOverlapHitStride;
|
|
2171
|
+
}
|
|
2172
|
+
|
|
2173
|
+
std::uint32_t rundot_kinetix_ext_d18_world_size() {
|
|
2174
|
+
return static_cast<std::uint32_t>(sizeof(PlanarDeformableWorld));
|
|
2175
|
+
}
|
|
2176
|
+
|
|
2177
|
+
std::int32_t rundot_kinetix_ext_d18_step(
|
|
2178
|
+
PlanarDeformableWorld* world,
|
|
2179
|
+
double* rigidBodiesData,
|
|
2180
|
+
std::uint32_t bodyCount,
|
|
2181
|
+
double dt) {
|
|
2182
|
+
if (!world || dt <= 0.0) return -1;
|
|
2183
|
+
|
|
2184
|
+
std::array<PlanarRigidBody2D, 64> bodies{};
|
|
2185
|
+
const std::uint32_t count = (bodyCount < 64) ? bodyCount : 64;
|
|
2186
|
+
if (rigidBodiesData && count > 0) {
|
|
2187
|
+
for (std::uint32_t i = 0; i < count; ++i) {
|
|
2188
|
+
const double* ptr = rigidBodiesData + i * kPlanarRigidBodyStride;
|
|
2189
|
+
auto& b = bodies[i];
|
|
2190
|
+
b.id = static_cast<std::uint32_t>(ptr[0]);
|
|
2191
|
+
b.shape = static_cast<PlanarRigidShapeKind>(ptr[1]);
|
|
2192
|
+
b.isDynamic = (ptr[2] > 0.5);
|
|
2193
|
+
b.x = ptr[3];
|
|
2194
|
+
b.y = ptr[4];
|
|
2195
|
+
b.angle = ptr[5];
|
|
2196
|
+
b.vx = ptr[6];
|
|
2197
|
+
b.vy = ptr[7];
|
|
2198
|
+
b.angularVelocity = ptr[8];
|
|
2199
|
+
b.mass = ptr[9];
|
|
2200
|
+
b.inverseMass = (b.mass > 0.0) ? (1.0 / b.mass) : 0.0;
|
|
2201
|
+
b.inertia = ptr[10];
|
|
2202
|
+
b.inverseInertia = (b.inertia > 0.0) ? (1.0 / b.inertia) : 0.0;
|
|
2203
|
+
b.halfX = ptr[11];
|
|
2204
|
+
b.halfY = ptr[12];
|
|
2205
|
+
b.radius = ptr[13];
|
|
2206
|
+
b.friction = ptr[14];
|
|
2207
|
+
b.restitution = ptr[15];
|
|
2208
|
+
}
|
|
2209
|
+
}
|
|
2210
|
+
|
|
2211
|
+
const PlanarStatus status = stepPlanarDeformableWorld(
|
|
2212
|
+
*world, std::span<PlanarRigidBody2D>(bodies.data(), count), dt);
|
|
2213
|
+
|
|
2214
|
+
if (rigidBodiesData && count > 0) {
|
|
2215
|
+
for (std::uint32_t i = 0; i < count; ++i) {
|
|
2216
|
+
double* ptr = rigidBodiesData + i * kPlanarRigidBodyStride;
|
|
2217
|
+
const auto& b = bodies[i];
|
|
2218
|
+
ptr[3] = b.x;
|
|
2219
|
+
ptr[4] = b.y;
|
|
2220
|
+
ptr[5] = b.angle;
|
|
2221
|
+
ptr[6] = b.vx;
|
|
2222
|
+
ptr[7] = b.vy;
|
|
2223
|
+
ptr[8] = b.angularVelocity;
|
|
2224
|
+
}
|
|
2225
|
+
}
|
|
2226
|
+
|
|
2227
|
+
return static_cast<std::int32_t>(status);
|
|
2228
|
+
}
|
|
2229
|
+
|
|
2230
|
+
std::int32_t rundot_kinetix_ext_d18_step_raw(
|
|
2231
|
+
double* nodeData,
|
|
2232
|
+
std::uint32_t nodeCount,
|
|
2233
|
+
double* distanceConstraintData,
|
|
2234
|
+
std::uint32_t distanceConstraintCount,
|
|
2235
|
+
double* bendConstraintData,
|
|
2236
|
+
std::uint32_t bendConstraintCount,
|
|
2237
|
+
double* shearConstraintData,
|
|
2238
|
+
std::uint32_t shearConstraintCount,
|
|
2239
|
+
double* areaConstraintData,
|
|
2240
|
+
std::uint32_t areaConstraintCount,
|
|
2241
|
+
double* anchorData,
|
|
2242
|
+
std::uint32_t anchorCount,
|
|
2243
|
+
double* particleData,
|
|
2244
|
+
std::uint32_t particleCount,
|
|
2245
|
+
double* emitterData,
|
|
2246
|
+
std::uint32_t emitterCount,
|
|
2247
|
+
double* rigidBodyData,
|
|
2248
|
+
std::uint32_t rigidBodyCount,
|
|
2249
|
+
const double* paramsData,
|
|
2250
|
+
double* breakEventsData,
|
|
2251
|
+
std::uint32_t* breakEventCount,
|
|
2252
|
+
double dt) {
|
|
2253
|
+
return static_cast<std::int32_t>(stepPlanarDeformableRaw(
|
|
2254
|
+
nodeData, nodeCount,
|
|
2255
|
+
distanceConstraintData, distanceConstraintCount,
|
|
2256
|
+
bendConstraintData, bendConstraintCount,
|
|
2257
|
+
shearConstraintData, shearConstraintCount,
|
|
2258
|
+
areaConstraintData, areaConstraintCount,
|
|
2259
|
+
anchorData, anchorCount,
|
|
2260
|
+
particleData, particleCount,
|
|
2261
|
+
emitterData, emitterCount,
|
|
2262
|
+
rigidBodyData, rigidBodyCount,
|
|
2263
|
+
paramsData, breakEventsData, breakEventCount,
|
|
2264
|
+
dt));
|
|
2265
|
+
}
|
|
2266
|
+
|
|
2267
|
+
std::int32_t rundot_kinetix_ext_d18_add_rope(
|
|
2268
|
+
PlanarDeformableWorld* world,
|
|
2269
|
+
double startX, double startY,
|
|
2270
|
+
double endX, double endY,
|
|
2271
|
+
std::uint32_t segmentCount,
|
|
2272
|
+
double totalMass,
|
|
2273
|
+
double stretchStiffness,
|
|
2274
|
+
double bendStiffness,
|
|
2275
|
+
double radius) {
|
|
2276
|
+
if (!world) return -1;
|
|
2277
|
+
return static_cast<std::int32_t>(addRope2D(
|
|
2278
|
+
*world, {startX, startY}, {endX, endY},
|
|
2279
|
+
segmentCount, totalMass, stretchStiffness, bendStiffness, radius));
|
|
2280
|
+
}
|
|
2281
|
+
|
|
2282
|
+
std::int32_t rundot_kinetix_ext_d18_add_cloth_grid(
|
|
2283
|
+
PlanarDeformableWorld* world,
|
|
2284
|
+
double originX, double originY,
|
|
2285
|
+
double width, double height,
|
|
2286
|
+
std::uint32_t cols, std::uint32_t rows,
|
|
2287
|
+
double totalMass,
|
|
2288
|
+
double stretchStiffness,
|
|
2289
|
+
double shearStiffness,
|
|
2290
|
+
double bendStiffness,
|
|
2291
|
+
double areaStiffness,
|
|
2292
|
+
double radius) {
|
|
2293
|
+
if (!world) return -1;
|
|
2294
|
+
return static_cast<std::int32_t>(addClothGrid2D(
|
|
2295
|
+
*world, {originX, originY}, width, height,
|
|
2296
|
+
cols, rows, totalMass, stretchStiffness, shearStiffness,
|
|
2297
|
+
bendStiffness, areaStiffness, radius));
|
|
2298
|
+
}
|
|
2299
|
+
|
|
2300
|
+
std::int32_t rundot_kinetix_ext_d18_add_particle(
|
|
2301
|
+
PlanarDeformableWorld* world,
|
|
2302
|
+
double posX, double posY,
|
|
2303
|
+
double velX, double velY,
|
|
2304
|
+
double radius, double mass, double lifetime) {
|
|
2305
|
+
if (!world) return -1;
|
|
2306
|
+
return static_cast<std::int32_t>(addParticle2D(
|
|
2307
|
+
*world, {posX, posY}, {velX, velY},
|
|
2308
|
+
radius, mass, lifetime));
|
|
2309
|
+
}
|
|
2310
|
+
|
|
2311
|
+
std::int32_t rundot_kinetix_ext_d18_raycast(
|
|
2312
|
+
const PlanarDeformableWorld* world,
|
|
2313
|
+
const double* queryData,
|
|
2314
|
+
double* hitData) {
|
|
2315
|
+
if (!world || !queryData || !hitData) return -1;
|
|
2316
|
+
PlanarRaycastQuery q{};
|
|
2317
|
+
q.origin = {queryData[0], queryData[1]};
|
|
2318
|
+
q.direction = {queryData[2], queryData[3]};
|
|
2319
|
+
q.maxDistance = queryData[4];
|
|
2320
|
+
q.flags = static_cast<std::uint32_t>(queryData[5]);
|
|
2321
|
+
|
|
2322
|
+
PlanarRaycastHit hit{};
|
|
2323
|
+
const PlanarStatus s = raycastPlanarDeformable(*world, q, hit);
|
|
2324
|
+
|
|
2325
|
+
hitData[0] = hit.hit ? 1.0 : 0.0;
|
|
2326
|
+
hitData[1] = hit.distance;
|
|
2327
|
+
hitData[2] = hit.point.x;
|
|
2328
|
+
hitData[3] = hit.point.y;
|
|
2329
|
+
hitData[4] = hit.normal.x;
|
|
2330
|
+
hitData[5] = hit.normal.y;
|
|
2331
|
+
hitData[6] = static_cast<double>(hit.elementKind);
|
|
2332
|
+
hitData[7] = static_cast<double>(hit.elementIndex);
|
|
2333
|
+
hitData[8] = static_cast<double>(hit.nodeIndex);
|
|
2334
|
+
hitData[9] = hit.parameter;
|
|
2335
|
+
|
|
2336
|
+
return static_cast<std::int32_t>(s);
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2339
|
+
std::int32_t rundot_kinetix_ext_d18_closest_point(
|
|
2340
|
+
const PlanarDeformableWorld* world,
|
|
2341
|
+
const double* queryData,
|
|
2342
|
+
double* hitData) {
|
|
2343
|
+
if (!world || !queryData || !hitData) return -1;
|
|
2344
|
+
PlanarClosestPointQuery q{};
|
|
2345
|
+
q.point = {queryData[0], queryData[1]};
|
|
2346
|
+
q.maxDistance = queryData[2];
|
|
2347
|
+
q.flags = static_cast<std::uint32_t>(queryData[3]);
|
|
2348
|
+
|
|
2349
|
+
PlanarClosestPointHit hit{};
|
|
2350
|
+
const PlanarStatus s = closestPointPlanarDeformable(*world, q, hit);
|
|
2351
|
+
|
|
2352
|
+
hitData[0] = hit.hit ? 1.0 : 0.0;
|
|
2353
|
+
hitData[1] = hit.distance;
|
|
2354
|
+
hitData[2] = hit.point.x;
|
|
2355
|
+
hitData[3] = hit.point.y;
|
|
2356
|
+
hitData[4] = hit.normal.x;
|
|
2357
|
+
hitData[5] = hit.normal.y;
|
|
2358
|
+
hitData[6] = static_cast<double>(hit.elementKind);
|
|
2359
|
+
hitData[7] = static_cast<double>(hit.elementIndex);
|
|
2360
|
+
|
|
2361
|
+
return static_cast<std::int32_t>(s);
|
|
2362
|
+
}
|
|
2363
|
+
|
|
2364
|
+
std::int32_t rundot_kinetix_ext_d18_overlap(
|
|
2365
|
+
const PlanarDeformableWorld* world,
|
|
2366
|
+
const double* queryData,
|
|
2367
|
+
double* resultData,
|
|
2368
|
+
std::uint32_t* outIndices,
|
|
2369
|
+
std::uint32_t maxIndices) {
|
|
2370
|
+
if (!world || !queryData || !resultData) return -1;
|
|
2371
|
+
PlanarOverlapQuery q{};
|
|
2372
|
+
q.shapeType = static_cast<std::uint32_t>(queryData[0]);
|
|
2373
|
+
q.center = {queryData[1], queryData[2]};
|
|
2374
|
+
q.halfExtents = {queryData[3], queryData[4]};
|
|
2375
|
+
q.angle = queryData[5];
|
|
2376
|
+
|
|
2377
|
+
PlanarOverlapResult res{};
|
|
2378
|
+
const PlanarStatus s = overlapPlanarDeformable(*world, q, res);
|
|
2379
|
+
|
|
2380
|
+
resultData[0] = static_cast<double>(res.count);
|
|
2381
|
+
resultData[1] = res.maxPenetration;
|
|
2382
|
+
resultData[2] = 0.0;
|
|
2383
|
+
resultData[3] = 0.0;
|
|
2384
|
+
|
|
2385
|
+
if (outIndices && maxIndices > 0) {
|
|
2386
|
+
const std::uint32_t toCopy = minVal(maxIndices, res.count);
|
|
2387
|
+
for (std::uint32_t i = 0; i < toCopy; ++i) {
|
|
2388
|
+
outIndices[i] = res.hitIndices[i];
|
|
2389
|
+
}
|
|
2390
|
+
}
|
|
2391
|
+
|
|
2392
|
+
return static_cast<std::int32_t>(s);
|
|
2393
|
+
}
|
|
2394
|
+
|
|
2395
|
+
std::int32_t rundot_kinetix_ext_d18_capture_snapshot(
|
|
2396
|
+
const PlanarDeformableWorld* world,
|
|
2397
|
+
std::uint8_t* outBuffer,
|
|
2398
|
+
std::uint32_t maxBytes,
|
|
2399
|
+
std::uint32_t* outBytes) {
|
|
2400
|
+
if (!world || !outBuffer || !outBytes) return -1;
|
|
2401
|
+
return static_cast<std::int32_t>(captureSnapshot(
|
|
2402
|
+
*world, std::span<std::uint8_t>(outBuffer, maxBytes), *outBytes));
|
|
2403
|
+
}
|
|
2404
|
+
|
|
2405
|
+
std::int32_t rundot_kinetix_ext_d18_restore_snapshot(
|
|
2406
|
+
PlanarDeformableWorld* world,
|
|
2407
|
+
const std::uint8_t* inBuffer,
|
|
2408
|
+
std::uint32_t byteCount) {
|
|
2409
|
+
if (!world || !inBuffer) return -1;
|
|
2410
|
+
return static_cast<std::int32_t>(restoreSnapshot(
|
|
2411
|
+
*world, std::span<const std::uint8_t>(inBuffer, byteCount)));
|
|
2412
|
+
}
|
|
2413
|
+
|
|
2414
|
+
std::int32_t rundot_kinetix_ext_d18_compute_mass_properties(
|
|
2415
|
+
const PlanarDeformableWorld* world,
|
|
2416
|
+
double* outProperties) {
|
|
2417
|
+
if (!world || !outProperties) return -1;
|
|
2418
|
+
const PlanarMassProperties props = computePlanarMassProperties(
|
|
2419
|
+
std::span<const PlanarDeformableNode>(world->nodes.data(), world->nodeCount));
|
|
2420
|
+
|
|
2421
|
+
outProperties[0] = props.totalMass;
|
|
2422
|
+
outProperties[1] = props.centerOfMass.x;
|
|
2423
|
+
outProperties[2] = props.centerOfMass.y;
|
|
2424
|
+
outProperties[3] = props.momentOfInertia;
|
|
2425
|
+
outProperties[4] = props.boundingBox.min.x;
|
|
2426
|
+
outProperties[5] = props.boundingBox.min.y;
|
|
2427
|
+
outProperties[6] = props.boundingBox.max.x;
|
|
2428
|
+
outProperties[7] = props.boundingBox.max.y;
|
|
2429
|
+
|
|
2430
|
+
return 0;
|
|
2431
|
+
}
|
|
2432
|
+
|
|
2433
|
+
}
|