@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,1588 @@
|
|
|
1
|
+
#include "buoyancy.hpp"
|
|
2
|
+
|
|
3
|
+
#include <algorithm>
|
|
4
|
+
#include <array>
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <cstddef>
|
|
7
|
+
#include <vector>
|
|
8
|
+
|
|
9
|
+
namespace rundot::kinetix::physics {
|
|
10
|
+
namespace {
|
|
11
|
+
|
|
12
|
+
constexpr double kPi = 3.141592653589793238462643383279502884;
|
|
13
|
+
|
|
14
|
+
struct Polygon {
|
|
15
|
+
std::array<BuoyancyVector3, 6> points{};
|
|
16
|
+
std::size_t count = 0;
|
|
17
|
+
bool overflow = false;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
bool finite(double value) {
|
|
21
|
+
return std::isfinite(value);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
bool finite(const BuoyancyVector3& value) {
|
|
25
|
+
return finite(value.x) && finite(value.y) && finite(value.z);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
bool finite(const BuoyancyQuaternion& value) {
|
|
29
|
+
return finite(value.x) && finite(value.y) && finite(value.z) && finite(value.w);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
BuoyancyVector3 add(const BuoyancyVector3& first, const BuoyancyVector3& second) {
|
|
33
|
+
return {first.x + second.x, first.y + second.y, first.z + second.z};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
BuoyancyVector3 subtract(const BuoyancyVector3& first, const BuoyancyVector3& second) {
|
|
37
|
+
return {first.x - second.x, first.y - second.y, first.z - second.z};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
BuoyancyVector3 scale(const BuoyancyVector3& value, double factor) {
|
|
41
|
+
return {value.x * factor, value.y * factor, value.z * factor};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
double dot(const BuoyancyVector3& first, const BuoyancyVector3& second) {
|
|
45
|
+
return first.x * second.x + first.y * second.y + first.z * second.z;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
BuoyancyVector3 cross(const BuoyancyVector3& first, const BuoyancyVector3& second) {
|
|
49
|
+
return {
|
|
50
|
+
first.y * second.z - first.z * second.y,
|
|
51
|
+
first.z * second.x - first.x * second.z,
|
|
52
|
+
first.x * second.y - first.y * second.x,
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
bool samePoint(const BuoyancyVector3& first, const BuoyancyVector3& second) {
|
|
57
|
+
return first.x == second.x && first.y == second.y && first.z == second.z;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
void addUnique(Polygon& points, const BuoyancyVector3& point) {
|
|
61
|
+
for (std::size_t index = 0; index < points.count; index += 1) {
|
|
62
|
+
if (samePoint(points.points[index], point)) return;
|
|
63
|
+
}
|
|
64
|
+
if (points.count == points.points.size()) {
|
|
65
|
+
points.overflow = true;
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
points.points[points.count++] = point;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
BuoyancyVector3 rotate(
|
|
72
|
+
const BuoyancyQuaternion& orientation,
|
|
73
|
+
double inverseNormSquared,
|
|
74
|
+
const BuoyancyVector3& point) {
|
|
75
|
+
const double scale2 = 2.0 * inverseNormSquared;
|
|
76
|
+
const double xx = orientation.x * orientation.x * scale2;
|
|
77
|
+
const double yy = orientation.y * orientation.y * scale2;
|
|
78
|
+
const double zz = orientation.z * orientation.z * scale2;
|
|
79
|
+
const double xy = orientation.x * orientation.y * scale2;
|
|
80
|
+
const double xz = orientation.x * orientation.z * scale2;
|
|
81
|
+
const double yz = orientation.y * orientation.z * scale2;
|
|
82
|
+
const double wx = orientation.w * orientation.x * scale2;
|
|
83
|
+
const double wy = orientation.w * orientation.y * scale2;
|
|
84
|
+
const double wz = orientation.w * orientation.z * scale2;
|
|
85
|
+
return {
|
|
86
|
+
(1.0 - yy - zz) * point.x + (xy - wz) * point.y + (xz + wy) * point.z,
|
|
87
|
+
(xy + wz) * point.x + (1.0 - xx - zz) * point.y + (yz - wx) * point.z,
|
|
88
|
+
(xz - wy) * point.x + (yz + wx) * point.y + (1.0 - xx - yy) * point.z,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
BuoyancyVector3 planeIntersection(
|
|
93
|
+
const BuoyancyVector3& first,
|
|
94
|
+
const BuoyancyVector3& second,
|
|
95
|
+
double waterY) {
|
|
96
|
+
const double amount = (waterY - first.y) / (second.y - first.y);
|
|
97
|
+
return {
|
|
98
|
+
first.x + (second.x - first.x) * amount,
|
|
99
|
+
waterY,
|
|
100
|
+
first.z + (second.z - first.z) * amount,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
Polygon clipFace(const Polygon& face, double waterY) {
|
|
105
|
+
Polygon clipped;
|
|
106
|
+
if (face.count == 0) return clipped;
|
|
107
|
+
BuoyancyVector3 previous = face.points[face.count - 1];
|
|
108
|
+
bool previousInside = previous.y <= waterY;
|
|
109
|
+
for (std::size_t index = 0; index < face.count; index += 1) {
|
|
110
|
+
const BuoyancyVector3& current = face.points[index];
|
|
111
|
+
const bool currentInside = current.y <= waterY;
|
|
112
|
+
if (currentInside != previousInside) {
|
|
113
|
+
addUnique(clipped, planeIntersection(previous, current, waterY));
|
|
114
|
+
}
|
|
115
|
+
if (currentInside) addUnique(clipped, current);
|
|
116
|
+
previous = current;
|
|
117
|
+
previousInside = currentInside;
|
|
118
|
+
}
|
|
119
|
+
return clipped;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
Polygon capPolygon(
|
|
123
|
+
const std::array<BuoyancyVector3, 8>& vertices,
|
|
124
|
+
double waterY) {
|
|
125
|
+
constexpr std::array<std::array<std::size_t, 2>, 12> edges{{
|
|
126
|
+
{0, 1}, {1, 2}, {2, 3}, {3, 0},
|
|
127
|
+
{4, 5}, {5, 6}, {6, 7}, {7, 4},
|
|
128
|
+
{0, 4}, {1, 5}, {2, 6}, {3, 7},
|
|
129
|
+
}};
|
|
130
|
+
Polygon cap;
|
|
131
|
+
for (const auto& edge : edges) {
|
|
132
|
+
const BuoyancyVector3& first = vertices[edge[0]];
|
|
133
|
+
const BuoyancyVector3& second = vertices[edge[1]];
|
|
134
|
+
if (first.y == waterY) addUnique(cap, first);
|
|
135
|
+
if (second.y == waterY) addUnique(cap, second);
|
|
136
|
+
if ((first.y < waterY && second.y > waterY)
|
|
137
|
+
|| (first.y > waterY && second.y < waterY)) {
|
|
138
|
+
addUnique(cap, planeIntersection(first, second, waterY));
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (cap.count < 3) return {};
|
|
142
|
+
BuoyancyVector3 center{};
|
|
143
|
+
for (std::size_t index = 0; index < cap.count; index += 1) {
|
|
144
|
+
center = add(center, cap.points[index]);
|
|
145
|
+
}
|
|
146
|
+
center = scale(center, 1.0 / static_cast<double>(cap.count));
|
|
147
|
+
std::sort(cap.points.begin(), cap.points.begin() + cap.count, [¢er](const BuoyancyVector3& first, const BuoyancyVector3& second) {
|
|
148
|
+
const double firstX = first.x - center.x;
|
|
149
|
+
const double firstZ = first.z - center.z;
|
|
150
|
+
const double secondX = second.x - center.x;
|
|
151
|
+
const double secondZ = second.z - center.z;
|
|
152
|
+
const bool firstUpper = firstZ > 0.0 || (firstZ == 0.0 && firstX >= 0.0);
|
|
153
|
+
const bool secondUpper = secondZ > 0.0 || (secondZ == 0.0 && secondX >= 0.0);
|
|
154
|
+
if (firstUpper != secondUpper) return firstUpper;
|
|
155
|
+
const double turn = firstX * secondZ - firstZ * secondX;
|
|
156
|
+
if (turn != 0.0) return turn > 0.0;
|
|
157
|
+
return firstX * firstX + firstZ * firstZ < secondX * secondX + secondZ * secondZ;
|
|
158
|
+
});
|
|
159
|
+
return cap;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
BuoyancyStatus boxGeometry(
|
|
163
|
+
const BuoyancyVector3& center,
|
|
164
|
+
const BuoyancyQuaternion& orientation,
|
|
165
|
+
const BuoyancyVector3& halfSize,
|
|
166
|
+
double waterY,
|
|
167
|
+
SubmergedGeometry& output) {
|
|
168
|
+
if (!(halfSize.x > 0.0 && halfSize.y > 0.0 && halfSize.z > 0.0)) {
|
|
169
|
+
return BuoyancyStatus::InvalidInput;
|
|
170
|
+
}
|
|
171
|
+
const double normSquared = orientation.x * orientation.x
|
|
172
|
+
+ orientation.y * orientation.y
|
|
173
|
+
+ orientation.z * orientation.z
|
|
174
|
+
+ orientation.w * orientation.w;
|
|
175
|
+
if (!(normSquared > 0.0) || !finite(normSquared)) return BuoyancyStatus::InvalidInput;
|
|
176
|
+
const double inverseNormSquared = 1.0 / normSquared;
|
|
177
|
+
constexpr std::array<std::array<double, 3>, 8> signs{{
|
|
178
|
+
{-1.0, -1.0, -1.0}, {1.0, -1.0, -1.0},
|
|
179
|
+
{1.0, 1.0, -1.0}, {-1.0, 1.0, -1.0},
|
|
180
|
+
{-1.0, -1.0, 1.0}, {1.0, -1.0, 1.0},
|
|
181
|
+
{1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0},
|
|
182
|
+
}};
|
|
183
|
+
std::array<BuoyancyVector3, 8> vertices{};
|
|
184
|
+
double minimumY = waterY;
|
|
185
|
+
double maximumY = waterY;
|
|
186
|
+
for (std::size_t index = 0; index < vertices.size(); index += 1) {
|
|
187
|
+
const BuoyancyVector3 local{
|
|
188
|
+
signs[index][0] * halfSize.x,
|
|
189
|
+
signs[index][1] * halfSize.y,
|
|
190
|
+
signs[index][2] * halfSize.z,
|
|
191
|
+
};
|
|
192
|
+
vertices[index] = add(center, rotate(orientation, inverseNormSquared, local));
|
|
193
|
+
if (!finite(vertices[index])) return BuoyancyStatus::InvalidInput;
|
|
194
|
+
if (index == 0 || vertices[index].y < minimumY) minimumY = vertices[index].y;
|
|
195
|
+
if (index == 0 || vertices[index].y > maximumY) maximumY = vertices[index].y;
|
|
196
|
+
}
|
|
197
|
+
if (waterY <= minimumY) {
|
|
198
|
+
output = {0.0, center};
|
|
199
|
+
return BuoyancyStatus::Success;
|
|
200
|
+
}
|
|
201
|
+
const double fullVolume = 8.0 * halfSize.x * halfSize.y * halfSize.z;
|
|
202
|
+
if (!finite(fullVolume)) return BuoyancyStatus::InvalidInput;
|
|
203
|
+
if (waterY >= maximumY) {
|
|
204
|
+
output = {fullVolume, center};
|
|
205
|
+
return BuoyancyStatus::Success;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
constexpr std::array<std::array<std::size_t, 4>, 6> faceIndices{{
|
|
209
|
+
{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 5, 1},
|
|
210
|
+
{3, 2, 6, 7}, {0, 3, 7, 4}, {1, 5, 6, 2},
|
|
211
|
+
}};
|
|
212
|
+
std::array<Polygon, 7> faces{};
|
|
213
|
+
std::size_t faceCount = 0;
|
|
214
|
+
std::array<BuoyancyVector3, 36> referencePoints{};
|
|
215
|
+
std::size_t referenceCount = 0;
|
|
216
|
+
for (const auto& indices : faceIndices) {
|
|
217
|
+
Polygon face;
|
|
218
|
+
for (std::size_t index : indices) face.points[face.count++] = vertices[index];
|
|
219
|
+
Polygon clipped = clipFace(face, waterY);
|
|
220
|
+
if (clipped.overflow) return BuoyancyStatus::InvalidInput;
|
|
221
|
+
if (clipped.count >= 3) {
|
|
222
|
+
for (std::size_t index = 0; index < clipped.count; index += 1) {
|
|
223
|
+
if (referenceCount == referencePoints.size()) return BuoyancyStatus::InvalidInput;
|
|
224
|
+
referencePoints[referenceCount++] = clipped.points[index];
|
|
225
|
+
}
|
|
226
|
+
if (faceCount == faces.size()) return BuoyancyStatus::InvalidInput;
|
|
227
|
+
faces[faceCount++] = clipped;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
Polygon cap = capPolygon(vertices, waterY);
|
|
231
|
+
if (cap.overflow) return BuoyancyStatus::InvalidInput;
|
|
232
|
+
if (cap.count >= 3) {
|
|
233
|
+
for (std::size_t index = 0; index < cap.count; index += 1) {
|
|
234
|
+
if (referenceCount == referencePoints.size()) return BuoyancyStatus::InvalidInput;
|
|
235
|
+
referencePoints[referenceCount++] = cap.points[index];
|
|
236
|
+
}
|
|
237
|
+
if (faceCount == faces.size()) return BuoyancyStatus::InvalidInput;
|
|
238
|
+
faces[faceCount++] = cap;
|
|
239
|
+
}
|
|
240
|
+
if (referenceCount == 0) return BuoyancyStatus::InvalidInput;
|
|
241
|
+
BuoyancyVector3 reference{};
|
|
242
|
+
for (std::size_t index = 0; index < referenceCount; index += 1) {
|
|
243
|
+
reference = add(reference, referencePoints[index]);
|
|
244
|
+
}
|
|
245
|
+
reference = scale(reference, 1.0 / static_cast<double>(referenceCount));
|
|
246
|
+
|
|
247
|
+
double volume = 0.0;
|
|
248
|
+
BuoyancyVector3 moment{};
|
|
249
|
+
for (std::size_t faceIndex = 0; faceIndex < faceCount; faceIndex += 1) {
|
|
250
|
+
const Polygon& face = faces[faceIndex];
|
|
251
|
+
for (std::size_t index = 1; index + 1 < face.count; index += 1) {
|
|
252
|
+
const BuoyancyVector3 first = subtract(face.points[0], reference);
|
|
253
|
+
const BuoyancyVector3 second = subtract(face.points[index], reference);
|
|
254
|
+
const BuoyancyVector3 third = subtract(face.points[index + 1], reference);
|
|
255
|
+
const double signedSixVolume = dot(first, cross(second, third));
|
|
256
|
+
const double tetraVolume = std::abs(signedSixVolume) / 6.0;
|
|
257
|
+
const BuoyancyVector3 tetraCenter = scale(
|
|
258
|
+
add(add(reference, face.points[0]), add(face.points[index], face.points[index + 1])),
|
|
259
|
+
0.25);
|
|
260
|
+
volume += tetraVolume;
|
|
261
|
+
moment = add(moment, scale(tetraCenter, tetraVolume));
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
if (!(volume > 0.0) || !finite(volume) || !finite(moment)) {
|
|
265
|
+
return BuoyancyStatus::InvalidInput;
|
|
266
|
+
}
|
|
267
|
+
output = {volume, scale(moment, 1.0 / volume)};
|
|
268
|
+
if (!finite(output.center)) return BuoyancyStatus::InvalidInput;
|
|
269
|
+
return BuoyancyStatus::Success;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
BuoyancyStatus sphereGeometry(
|
|
273
|
+
const BuoyancyVector3& center,
|
|
274
|
+
double radius,
|
|
275
|
+
double waterY,
|
|
276
|
+
SubmergedGeometry& output) {
|
|
277
|
+
if (!(radius > 0.0)) return BuoyancyStatus::InvalidInput;
|
|
278
|
+
const double height = waterY - (center.y - radius);
|
|
279
|
+
if (height <= 0.0) {
|
|
280
|
+
output = {0.0, center};
|
|
281
|
+
return BuoyancyStatus::Success;
|
|
282
|
+
}
|
|
283
|
+
if (height >= 2.0 * radius) {
|
|
284
|
+
const double volume = (4.0 / 3.0) * kPi * radius * radius * radius;
|
|
285
|
+
if (!finite(volume)) return BuoyancyStatus::InvalidInput;
|
|
286
|
+
output = {volume, center};
|
|
287
|
+
return BuoyancyStatus::Success;
|
|
288
|
+
}
|
|
289
|
+
const double volume = kPi * height * height * (radius - height / 3.0);
|
|
290
|
+
const double remainingHeight = 2.0 * radius - height;
|
|
291
|
+
const double offsetY = -3.0 * remainingHeight * remainingHeight
|
|
292
|
+
/ (4.0 * (3.0 * radius - height));
|
|
293
|
+
output = {volume, {center.x, center.y + offsetY, center.z}};
|
|
294
|
+
if (!finite(volume) || !finite(output.center)) return BuoyancyStatus::InvalidInput;
|
|
295
|
+
return BuoyancyStatus::Success;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
} // namespace
|
|
299
|
+
|
|
300
|
+
BuoyancyVector3 perpendicularVector(const BuoyancyVector3& n) {
|
|
301
|
+
if (std::abs(n.x) < 0.9 && std::abs(n.x) > -0.9) {
|
|
302
|
+
const BuoyancyVector3 c = cross(n, {1.0, 0.0, 0.0});
|
|
303
|
+
const double lenSq = dot(c, c);
|
|
304
|
+
if (lenSq > 0.0) return scale(c, 1.0 / std::sqrt(lenSq));
|
|
305
|
+
}
|
|
306
|
+
const BuoyancyVector3 c = cross(n, {0.0, 1.0, 0.0});
|
|
307
|
+
const double lenSq = dot(c, c);
|
|
308
|
+
return lenSq > 0.0 ? scale(c, 1.0 / std::sqrt(lenSq)) : BuoyancyVector3{1.0, 0.0, 0.0};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
BuoyancyVector3 planeIntersectionWithPlane(
|
|
312
|
+
const BuoyancyVector3& first,
|
|
313
|
+
const BuoyancyVector3& second,
|
|
314
|
+
const BuoyancyVector3& normal,
|
|
315
|
+
double offset) {
|
|
316
|
+
const double dFirst = dot(normal, first) - offset;
|
|
317
|
+
const double dSecond = dot(normal, second) - offset;
|
|
318
|
+
const double denom = dSecond - dFirst;
|
|
319
|
+
const double amount = (denom != 0.0) ? (-dFirst / denom) : 0.0;
|
|
320
|
+
return add(first, scale(subtract(second, first), amount));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
Polygon clipFaceWithPlane(const Polygon& face, const BuoyancyVector3& normal, double offset) {
|
|
324
|
+
Polygon clipped;
|
|
325
|
+
if (face.count == 0) return clipped;
|
|
326
|
+
BuoyancyVector3 previous = face.points[face.count - 1];
|
|
327
|
+
double prevDist = dot(normal, previous) - offset;
|
|
328
|
+
bool previousInside = prevDist <= 0.0;
|
|
329
|
+
for (std::size_t index = 0; index < face.count; index += 1) {
|
|
330
|
+
const BuoyancyVector3& current = face.points[index];
|
|
331
|
+
const double currDist = dot(normal, current) - offset;
|
|
332
|
+
const bool currentInside = currDist <= 0.0;
|
|
333
|
+
if (currentInside != previousInside) {
|
|
334
|
+
addUnique(clipped, planeIntersectionWithPlane(previous, current, normal, offset));
|
|
335
|
+
}
|
|
336
|
+
if (currentInside) addUnique(clipped, current);
|
|
337
|
+
previous = current;
|
|
338
|
+
prevDist = currDist;
|
|
339
|
+
previousInside = currentInside;
|
|
340
|
+
}
|
|
341
|
+
return clipped;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
Polygon capPolygonWithPlane(
|
|
345
|
+
const std::array<BuoyancyVector3, 8>& vertices,
|
|
346
|
+
const BuoyancyVector3& normal,
|
|
347
|
+
double offset) {
|
|
348
|
+
constexpr std::array<std::array<std::size_t, 2>, 12> edges{{
|
|
349
|
+
{0, 1}, {1, 2}, {2, 3}, {3, 0},
|
|
350
|
+
{4, 5}, {5, 6}, {6, 7}, {7, 4},
|
|
351
|
+
{0, 4}, {1, 5}, {2, 6}, {3, 7},
|
|
352
|
+
}};
|
|
353
|
+
Polygon cap;
|
|
354
|
+
for (const auto& edge : edges) {
|
|
355
|
+
const BuoyancyVector3& first = vertices[edge[0]];
|
|
356
|
+
const BuoyancyVector3& second = vertices[edge[1]];
|
|
357
|
+
const double dFirst = dot(normal, first) - offset;
|
|
358
|
+
const double dSecond = dot(normal, second) - offset;
|
|
359
|
+
if (std::abs(dFirst) < 1e-12) addUnique(cap, first);
|
|
360
|
+
if (std::abs(dSecond) < 1e-12) addUnique(cap, second);
|
|
361
|
+
if ((dFirst < 0.0 && dSecond > 0.0) || (dFirst > 0.0 && dSecond < 0.0)) {
|
|
362
|
+
addUnique(cap, planeIntersectionWithPlane(first, second, normal, offset));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
if (cap.count < 3) return {};
|
|
366
|
+
BuoyancyVector3 center{};
|
|
367
|
+
for (std::size_t index = 0; index < cap.count; index += 1) {
|
|
368
|
+
center = add(center, cap.points[index]);
|
|
369
|
+
}
|
|
370
|
+
center = scale(center, 1.0 / static_cast<double>(cap.count));
|
|
371
|
+
|
|
372
|
+
const BuoyancyVector3 u = perpendicularVector(normal);
|
|
373
|
+
const BuoyancyVector3 v = cross(normal, u);
|
|
374
|
+
|
|
375
|
+
std::sort(cap.points.begin(), cap.points.begin() + cap.count,
|
|
376
|
+
[¢er, &u, &v](const BuoyancyVector3& first, const BuoyancyVector3& second) {
|
|
377
|
+
const BuoyancyVector3 d1 = subtract(first, center);
|
|
378
|
+
const BuoyancyVector3 d2 = subtract(second, center);
|
|
379
|
+
const double firstX = dot(u, d1);
|
|
380
|
+
const double firstY = dot(v, d1);
|
|
381
|
+
const double secondX = dot(u, d2);
|
|
382
|
+
const double secondY = dot(v, d2);
|
|
383
|
+
const bool firstUpper = firstY > 0.0 || (firstY == 0.0 && firstX >= 0.0);
|
|
384
|
+
const bool secondUpper = secondY > 0.0 || (secondY == 0.0 && secondX >= 0.0);
|
|
385
|
+
if (firstUpper != secondUpper) return firstUpper;
|
|
386
|
+
const double turn = firstX * secondY - firstY * secondX;
|
|
387
|
+
if (turn != 0.0) return turn > 0.0;
|
|
388
|
+
return firstX * firstX + firstY * firstY < secondX * secondX + secondY * secondY;
|
|
389
|
+
});
|
|
390
|
+
return cap;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
BuoyancyStatus boxGeometryWithPlaneAndBounds(
|
|
394
|
+
const BuoyancyVector3& center,
|
|
395
|
+
const BuoyancyQuaternion& orientation,
|
|
396
|
+
const BuoyancyVector3& halfSize,
|
|
397
|
+
const FluidPlane3D& plane,
|
|
398
|
+
const FluidBounds3D& bounds,
|
|
399
|
+
SubmergedGeometry& output) {
|
|
400
|
+
if (!(halfSize.x > 0.0 && halfSize.y > 0.0 && halfSize.z > 0.0)) {
|
|
401
|
+
return BuoyancyStatus::InvalidInput;
|
|
402
|
+
}
|
|
403
|
+
const double normSquared = orientation.x * orientation.x
|
|
404
|
+
+ orientation.y * orientation.y
|
|
405
|
+
+ orientation.z * orientation.z
|
|
406
|
+
+ orientation.w * orientation.w;
|
|
407
|
+
if (!(normSquared > 0.0) || !finite(normSquared)) return BuoyancyStatus::InvalidInput;
|
|
408
|
+
const double inverseNormSquared = 1.0 / normSquared;
|
|
409
|
+
|
|
410
|
+
constexpr std::array<std::array<double, 3>, 8> signs{{
|
|
411
|
+
{-1.0, -1.0, -1.0}, {1.0, -1.0, -1.0},
|
|
412
|
+
{1.0, 1.0, -1.0}, {-1.0, 1.0, -1.0},
|
|
413
|
+
{-1.0, -1.0, 1.0}, {1.0, -1.0, 1.0},
|
|
414
|
+
{1.0, 1.0, 1.0}, {-1.0, 1.0, 1.0},
|
|
415
|
+
}};
|
|
416
|
+
std::array<BuoyancyVector3, 8> vertices{};
|
|
417
|
+
BuoyancyVector3 boxMin{};
|
|
418
|
+
BuoyancyVector3 boxMax{};
|
|
419
|
+
for (std::size_t index = 0; index < vertices.size(); index += 1) {
|
|
420
|
+
const BuoyancyVector3 local{
|
|
421
|
+
signs[index][0] * halfSize.x,
|
|
422
|
+
signs[index][1] * halfSize.y,
|
|
423
|
+
signs[index][2] * halfSize.z,
|
|
424
|
+
};
|
|
425
|
+
vertices[index] = add(center, rotate(orientation, inverseNormSquared, local));
|
|
426
|
+
if (!finite(vertices[index])) return BuoyancyStatus::InvalidInput;
|
|
427
|
+
if (index == 0) {
|
|
428
|
+
boxMin = vertices[index];
|
|
429
|
+
boxMax = vertices[index];
|
|
430
|
+
} else {
|
|
431
|
+
boxMin.x = std::min(boxMin.x, vertices[index].x);
|
|
432
|
+
boxMin.y = std::min(boxMin.y, vertices[index].y);
|
|
433
|
+
boxMin.z = std::min(boxMin.z, vertices[index].z);
|
|
434
|
+
boxMax.x = std::max(boxMax.x, vertices[index].x);
|
|
435
|
+
boxMax.y = std::max(boxMax.y, vertices[index].y);
|
|
436
|
+
boxMax.z = std::max(boxMax.z, vertices[index].z);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
const bool hasBounds = (bounds.minX < bounds.maxX && bounds.minY < bounds.maxY && bounds.minZ < bounds.maxZ);
|
|
441
|
+
if (hasBounds) {
|
|
442
|
+
if (boxMax.x <= bounds.minX || boxMin.x >= bounds.maxX
|
|
443
|
+
|| boxMax.y <= bounds.minY || boxMin.y >= bounds.maxY
|
|
444
|
+
|| boxMax.z <= bounds.minZ || boxMin.z >= bounds.maxZ) {
|
|
445
|
+
output = {0.0, center};
|
|
446
|
+
return BuoyancyStatus::Success;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
const BuoyancyVector3 normal = plane.enabled ? plane.normal : BuoyancyVector3{0.0, 1.0, 0.0};
|
|
451
|
+
const double offset = plane.enabled ? plane.offset : (hasBounds ? bounds.maxY : boxMax.y);
|
|
452
|
+
|
|
453
|
+
double minimumDist = dot(normal, vertices[0]) - offset;
|
|
454
|
+
double maximumDist = minimumDist;
|
|
455
|
+
for (std::size_t index = 1; index < vertices.size(); index += 1) {
|
|
456
|
+
const double d = dot(normal, vertices[index]) - offset;
|
|
457
|
+
if (d < minimumDist) minimumDist = d;
|
|
458
|
+
if (d > maximumDist) maximumDist = d;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
if (minimumDist >= 0.0) {
|
|
462
|
+
output = {0.0, center};
|
|
463
|
+
return BuoyancyStatus::Success;
|
|
464
|
+
}
|
|
465
|
+
const double fullVolume = 8.0 * halfSize.x * halfSize.y * halfSize.z;
|
|
466
|
+
if (!finite(fullVolume)) return BuoyancyStatus::InvalidInput;
|
|
467
|
+
if (maximumDist <= 0.0) {
|
|
468
|
+
output = {fullVolume, center};
|
|
469
|
+
return BuoyancyStatus::Success;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
constexpr std::array<std::array<std::size_t, 4>, 6> faceIndices{{
|
|
473
|
+
{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 5, 1},
|
|
474
|
+
{3, 2, 6, 7}, {0, 3, 7, 4}, {1, 5, 6, 2},
|
|
475
|
+
}};
|
|
476
|
+
std::array<Polygon, 7> faces{};
|
|
477
|
+
std::size_t faceCount = 0;
|
|
478
|
+
std::array<BuoyancyVector3, 36> referencePoints{};
|
|
479
|
+
std::size_t referenceCount = 0;
|
|
480
|
+
for (const auto& indices : faceIndices) {
|
|
481
|
+
Polygon face;
|
|
482
|
+
for (std::size_t index : indices) face.points[face.count++] = vertices[index];
|
|
483
|
+
Polygon clipped = clipFaceWithPlane(face, normal, offset);
|
|
484
|
+
if (clipped.overflow) return BuoyancyStatus::InvalidInput;
|
|
485
|
+
if (clipped.count >= 3) {
|
|
486
|
+
for (std::size_t index = 0; index < clipped.count; index += 1) {
|
|
487
|
+
if (referenceCount == referencePoints.size()) return BuoyancyStatus::InvalidInput;
|
|
488
|
+
referencePoints[referenceCount++] = clipped.points[index];
|
|
489
|
+
}
|
|
490
|
+
if (faceCount == faces.size()) return BuoyancyStatus::InvalidInput;
|
|
491
|
+
faces[faceCount++] = clipped;
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
Polygon cap = capPolygonWithPlane(vertices, normal, offset);
|
|
495
|
+
if (cap.overflow) return BuoyancyStatus::InvalidInput;
|
|
496
|
+
if (cap.count >= 3) {
|
|
497
|
+
for (std::size_t index = 0; index < cap.count; index += 1) {
|
|
498
|
+
if (referenceCount == referencePoints.size()) return BuoyancyStatus::InvalidInput;
|
|
499
|
+
referencePoints[referenceCount++] = cap.points[index];
|
|
500
|
+
}
|
|
501
|
+
if (faceCount == faces.size()) return BuoyancyStatus::InvalidInput;
|
|
502
|
+
faces[faceCount++] = cap;
|
|
503
|
+
}
|
|
504
|
+
if (referenceCount == 0) return BuoyancyStatus::InvalidInput;
|
|
505
|
+
BuoyancyVector3 reference{};
|
|
506
|
+
for (std::size_t index = 0; index < referenceCount; index += 1) {
|
|
507
|
+
reference = add(reference, referencePoints[index]);
|
|
508
|
+
}
|
|
509
|
+
reference = scale(reference, 1.0 / static_cast<double>(referenceCount));
|
|
510
|
+
|
|
511
|
+
double volume = 0.0;
|
|
512
|
+
BuoyancyVector3 moment{};
|
|
513
|
+
for (std::size_t faceIndex = 0; faceIndex < faceCount; faceIndex += 1) {
|
|
514
|
+
const Polygon& face = faces[faceIndex];
|
|
515
|
+
for (std::size_t index = 1; index + 1 < face.count; index += 1) {
|
|
516
|
+
const BuoyancyVector3 first = subtract(face.points[0], reference);
|
|
517
|
+
const BuoyancyVector3 second = subtract(face.points[index], reference);
|
|
518
|
+
const BuoyancyVector3 third = subtract(face.points[index + 1], reference);
|
|
519
|
+
const double signedSixVolume = dot(first, cross(second, third));
|
|
520
|
+
const double tetraVolume = std::abs(signedSixVolume) / 6.0;
|
|
521
|
+
const BuoyancyVector3 tetraCenter = scale(
|
|
522
|
+
add(add(reference, face.points[0]), add(face.points[index], face.points[index + 1])),
|
|
523
|
+
0.25);
|
|
524
|
+
volume += tetraVolume;
|
|
525
|
+
moment = add(moment, scale(tetraCenter, tetraVolume));
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
if (!(volume > 0.0) || !finite(volume) || !finite(moment)) {
|
|
529
|
+
return BuoyancyStatus::InvalidInput;
|
|
530
|
+
}
|
|
531
|
+
output = {volume, scale(moment, 1.0 / volume)};
|
|
532
|
+
if (!finite(output.center)) return BuoyancyStatus::InvalidInput;
|
|
533
|
+
return BuoyancyStatus::Success;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
BuoyancyStatus sphereGeometryWithPlaneAndBounds(
|
|
537
|
+
const BuoyancyVector3& center,
|
|
538
|
+
double radius,
|
|
539
|
+
const FluidPlane3D& plane,
|
|
540
|
+
const FluidBounds3D& bounds,
|
|
541
|
+
SubmergedGeometry& output) {
|
|
542
|
+
if (!(radius > 0.0) || !finite(radius) || !finite(center)) return BuoyancyStatus::InvalidInput;
|
|
543
|
+
const bool hasBounds = (bounds.minX < bounds.maxX && bounds.minY < bounds.maxY && bounds.minZ < bounds.maxZ);
|
|
544
|
+
if (hasBounds) {
|
|
545
|
+
if (center.x + radius <= bounds.minX || center.x - radius >= bounds.maxX
|
|
546
|
+
|| center.y + radius <= bounds.minY || center.y - radius >= bounds.maxY
|
|
547
|
+
|| center.z + radius <= bounds.minZ || center.z - radius >= bounds.maxZ) {
|
|
548
|
+
output = {0.0, center};
|
|
549
|
+
return BuoyancyStatus::Success;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
const BuoyancyVector3 normal = plane.enabled ? plane.normal : BuoyancyVector3{0.0, 1.0, 0.0};
|
|
554
|
+
const double offset = plane.enabled ? plane.offset : (hasBounds ? bounds.maxY : center.y + radius);
|
|
555
|
+
|
|
556
|
+
const double dist = dot(normal, center) - offset;
|
|
557
|
+
if (dist >= radius) {
|
|
558
|
+
output = {0.0, center};
|
|
559
|
+
return BuoyancyStatus::Success;
|
|
560
|
+
}
|
|
561
|
+
const double fullVolume = (4.0 / 3.0) * kPi * radius * radius * radius;
|
|
562
|
+
if (dist <= -radius) {
|
|
563
|
+
output = {fullVolume, center};
|
|
564
|
+
return BuoyancyStatus::Success;
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
const double height = radius - dist;
|
|
568
|
+
const double volume = kPi * height * height * (radius - height / 3.0);
|
|
569
|
+
const double remainingHeight = 2.0 * radius - height;
|
|
570
|
+
const double offsetMag = -3.0 * remainingHeight * remainingHeight
|
|
571
|
+
/ (4.0 * (3.0 * radius - height));
|
|
572
|
+
output = {volume, add(center, scale(normal, offsetMag))};
|
|
573
|
+
if (!finite(volume) || !finite(output.center)) return BuoyancyStatus::InvalidInput;
|
|
574
|
+
return BuoyancyStatus::Success;
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
BuoyancyStatus calculateSubmergedGeometry(
|
|
578
|
+
ShapeKind shape,
|
|
579
|
+
const BuoyancyVector3& shapeCenter,
|
|
580
|
+
const BuoyancyQuaternion& orientation,
|
|
581
|
+
const BuoyancyVector3& shapeSize,
|
|
582
|
+
double waterY,
|
|
583
|
+
SubmergedGeometry& output) {
|
|
584
|
+
output = {};
|
|
585
|
+
if (!finite(shapeCenter) || !finite(orientation) || !finite(shapeSize) || !finite(waterY)) {
|
|
586
|
+
return BuoyancyStatus::InvalidInput;
|
|
587
|
+
}
|
|
588
|
+
switch (shape) {
|
|
589
|
+
case ShapeKind::Box:
|
|
590
|
+
return boxGeometry(shapeCenter, orientation, shapeSize, waterY, output);
|
|
591
|
+
case ShapeKind::Sphere:
|
|
592
|
+
return sphereGeometry(shapeCenter, shapeSize.x, waterY, output);
|
|
593
|
+
case ShapeKind::Capsule:
|
|
594
|
+
case ShapeKind::Hull:
|
|
595
|
+
case ShapeKind::Mesh:
|
|
596
|
+
case ShapeKind::Compound:
|
|
597
|
+
case ShapeKind::Heightfield:
|
|
598
|
+
case ShapeKind::Cylinder:
|
|
599
|
+
case ShapeKind::Cone:
|
|
600
|
+
case ShapeKind::TaperedCapsule:
|
|
601
|
+
case ShapeKind::RoundedConvex:
|
|
602
|
+
case ShapeKind::MultiSphere:
|
|
603
|
+
return BuoyancyStatus::UnsupportedShape;
|
|
604
|
+
}
|
|
605
|
+
return BuoyancyStatus::UnsupportedShape;
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
BuoyancyStatus calculateBuoyancy(const BuoyancyInput& input, BuoyancyResult& output) {
|
|
609
|
+
output = {};
|
|
610
|
+
if (!(input.density > 0.0) || !finite(input.density)
|
|
611
|
+
|| !finite(input.gravity) || !finite(input.bodyCenter)) {
|
|
612
|
+
return BuoyancyStatus::InvalidInput;
|
|
613
|
+
}
|
|
614
|
+
const BuoyancyStatus status = calculateSubmergedGeometry(
|
|
615
|
+
input.shape,
|
|
616
|
+
input.shapeCenter,
|
|
617
|
+
input.orientation,
|
|
618
|
+
input.shapeSize,
|
|
619
|
+
input.waterY,
|
|
620
|
+
output.submerged);
|
|
621
|
+
if (status != BuoyancyStatus::Success) return status;
|
|
622
|
+
output.force = scale(input.gravity, -input.density * output.submerged.volume);
|
|
623
|
+
output.torque = cross(subtract(output.submerged.center, input.bodyCenter), output.force);
|
|
624
|
+
if (!finite(output.force) || !finite(output.torque)) {
|
|
625
|
+
output = {};
|
|
626
|
+
return BuoyancyStatus::InvalidInput;
|
|
627
|
+
}
|
|
628
|
+
return BuoyancyStatus::Success;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
BuoyancyStatus validateFluidVolume(const FluidVolume3D& volume) {
|
|
632
|
+
if (volume.id == 0) return BuoyancyStatus::InvalidInput;
|
|
633
|
+
const double values[] = {
|
|
634
|
+
volume.bounds.minX, volume.bounds.maxX,
|
|
635
|
+
volume.bounds.minY, volume.bounds.maxY,
|
|
636
|
+
volume.bounds.minZ, volume.bounds.maxZ,
|
|
637
|
+
volume.density, volume.linearDrag, volume.angularDrag,
|
|
638
|
+
volume.flowVelocity.x, volume.flowVelocity.y, volume.flowVelocity.z,
|
|
639
|
+
};
|
|
640
|
+
for (double val : values) {
|
|
641
|
+
if (!finite(val)) return BuoyancyStatus::InvalidInput;
|
|
642
|
+
}
|
|
643
|
+
if (volume.bounds.minX >= volume.bounds.maxX
|
|
644
|
+
|| volume.bounds.minY >= volume.bounds.maxY
|
|
645
|
+
|| volume.bounds.minZ >= volume.bounds.maxZ) {
|
|
646
|
+
return BuoyancyStatus::InvalidBounds;
|
|
647
|
+
}
|
|
648
|
+
if (volume.bounds.maxX - volume.bounds.minX < 1e-6
|
|
649
|
+
|| volume.bounds.maxY - volume.bounds.minY < 1e-6
|
|
650
|
+
|| volume.bounds.maxZ - volume.bounds.minZ < 1e-6) {
|
|
651
|
+
return BuoyancyStatus::InvalidBounds;
|
|
652
|
+
}
|
|
653
|
+
if (volume.plane.enabled) {
|
|
654
|
+
if (!finite(volume.plane.normal) || !finite(volume.plane.offset)) {
|
|
655
|
+
return BuoyancyStatus::InvalidPlaneNormal;
|
|
656
|
+
}
|
|
657
|
+
const double lenSq = dot(volume.plane.normal, volume.plane.normal);
|
|
658
|
+
if (!finite(lenSq) || std::abs(lenSq - 1.0) > 1e-12) {
|
|
659
|
+
return BuoyancyStatus::InvalidPlaneNormal;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
if (volume.density < 0.0) return BuoyancyStatus::InvalidDensity;
|
|
663
|
+
if (volume.linearDrag < 0.0 || volume.angularDrag < 0.0) {
|
|
664
|
+
return BuoyancyStatus::InvalidInput;
|
|
665
|
+
}
|
|
666
|
+
return BuoyancyStatus::Success;
|
|
667
|
+
}
|
|
668
|
+
|
|
669
|
+
BuoyancyStatus validateFluidState(const FluidState3D& state) {
|
|
670
|
+
if (state.volumeCount > kMaxFluidVolumes3D) return BuoyancyStatus::InvalidInput;
|
|
671
|
+
if (state.overlapPolicy != FluidOverlapPolicy::RejectOverlap
|
|
672
|
+
&& state.overlapPolicy != FluidOverlapPolicy::PriorityPartition
|
|
673
|
+
&& state.overlapPolicy != FluidOverlapPolicy::Accumulate) {
|
|
674
|
+
return BuoyancyStatus::InvalidInput;
|
|
675
|
+
}
|
|
676
|
+
for (std::uint32_t i = 0; i < state.volumeCount; ++i) {
|
|
677
|
+
const BuoyancyStatus s = validateFluidVolume(state.volumes[i]);
|
|
678
|
+
if (s != BuoyancyStatus::Success) return s;
|
|
679
|
+
if (i > 0 && state.volumes[i - 1].id >= state.volumes[i].id) {
|
|
680
|
+
return BuoyancyStatus::InvalidInput;
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
for (std::uint32_t i = 0; i < state.volumeCount; ++i) {
|
|
684
|
+
for (std::uint32_t j = i + 1; j < state.volumeCount; ++j) {
|
|
685
|
+
const auto& a = state.volumes[i].bounds;
|
|
686
|
+
const auto& b = state.volumes[j].bounds;
|
|
687
|
+
const bool overlap = (std::min(a.maxX, b.maxX) > std::max(a.minX, b.minX))
|
|
688
|
+
&& (std::min(a.maxY, b.maxY) > std::max(a.minY, b.minY))
|
|
689
|
+
&& (std::min(a.maxZ, b.maxZ) > std::max(a.minZ, b.minZ));
|
|
690
|
+
if (overlap && state.overlapPolicy == FluidOverlapPolicy::RejectOverlap) {
|
|
691
|
+
return BuoyancyStatus::OverlapUnsupported;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
return BuoyancyStatus::Success;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
BuoyancyStatus calculateSubmergedGeometryWithPlane(
|
|
699
|
+
ShapeKind shape,
|
|
700
|
+
const BuoyancyVector3& shapeCenter,
|
|
701
|
+
const BuoyancyQuaternion& orientation,
|
|
702
|
+
const BuoyancyVector3& shapeSize,
|
|
703
|
+
const FluidPlane3D& plane,
|
|
704
|
+
const FluidBounds3D& bounds,
|
|
705
|
+
SubmergedGeometry& output) {
|
|
706
|
+
output = {};
|
|
707
|
+
if (!finite(shapeCenter) || !finite(orientation) || !finite(shapeSize)) {
|
|
708
|
+
return BuoyancyStatus::InvalidInput;
|
|
709
|
+
}
|
|
710
|
+
switch (shape) {
|
|
711
|
+
case ShapeKind::Box:
|
|
712
|
+
return boxGeometryWithPlaneAndBounds(shapeCenter, orientation, shapeSize, plane, bounds, output);
|
|
713
|
+
case ShapeKind::Sphere:
|
|
714
|
+
return sphereGeometryWithPlaneAndBounds(shapeCenter, shapeSize.x, plane, bounds, output);
|
|
715
|
+
case ShapeKind::Capsule:
|
|
716
|
+
case ShapeKind::Hull:
|
|
717
|
+
case ShapeKind::Mesh:
|
|
718
|
+
case ShapeKind::Compound:
|
|
719
|
+
case ShapeKind::Heightfield:
|
|
720
|
+
case ShapeKind::Cylinder:
|
|
721
|
+
case ShapeKind::Cone:
|
|
722
|
+
case ShapeKind::TaperedCapsule:
|
|
723
|
+
case ShapeKind::RoundedConvex:
|
|
724
|
+
case ShapeKind::MultiSphere:
|
|
725
|
+
return BuoyancyStatus::UnsupportedShape;
|
|
726
|
+
}
|
|
727
|
+
return BuoyancyStatus::UnsupportedShape;
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
BuoyancyStatus finishFluidForces(
|
|
731
|
+
const FluidBodyInput3D& body,
|
|
732
|
+
const FluidVolume3D& volume,
|
|
733
|
+
const BuoyancyVector3& gravity,
|
|
734
|
+
double fullVolume,
|
|
735
|
+
FluidForcesResult3D& output) {
|
|
736
|
+
if (output.submerged.volume <= 0.0) {
|
|
737
|
+
output.submerged = {0.0, body.position};
|
|
738
|
+
return BuoyancyStatus::Success;
|
|
739
|
+
}
|
|
740
|
+
const double normSq = body.orientation.x * body.orientation.x
|
|
741
|
+
+ body.orientation.y * body.orientation.y
|
|
742
|
+
+ body.orientation.z * body.orientation.z
|
|
743
|
+
+ body.orientation.w * body.orientation.w;
|
|
744
|
+
if (!(normSq > 0.0) || !finite(normSq)) return BuoyancyStatus::InvalidInput;
|
|
745
|
+
const BuoyancyVector3 centerOfMassWorld = add(
|
|
746
|
+
body.position,
|
|
747
|
+
rotate(body.orientation, 1.0 / normSq, body.centerOfMass));
|
|
748
|
+
const BuoyancyVector3 effectiveGravity = scale(gravity, body.gravityScale);
|
|
749
|
+
output.buoyantForce = scale(
|
|
750
|
+
effectiveGravity, -volume.density * output.submerged.volume);
|
|
751
|
+
const BuoyancyVector3 arm = subtract(
|
|
752
|
+
output.submerged.center, centerOfMassWorld);
|
|
753
|
+
const double submergedFraction = fullVolume > 0.0
|
|
754
|
+
? std::min(1.0, output.submerged.volume / fullVolume) : 0.0;
|
|
755
|
+
if (submergedFraction >= 0.999999999
|
|
756
|
+
&& dot(body.centerOfMass, body.centerOfMass) < 1e-12) {
|
|
757
|
+
output.buoyantTorque = {};
|
|
758
|
+
} else {
|
|
759
|
+
output.buoyantTorque = cross(arm, output.buoyantForce);
|
|
760
|
+
}
|
|
761
|
+
const BuoyancyVector3 pointVelocity = add(
|
|
762
|
+
body.linearVelocity, cross(body.angularVelocity, arm));
|
|
763
|
+
output.relativeVelocity = subtract(pointVelocity, volume.flowVelocity);
|
|
764
|
+
const double dragCoefficient = volume.density * output.submerged.volume
|
|
765
|
+
* volume.linearDrag;
|
|
766
|
+
output.dragForce = scale(output.relativeVelocity, -dragCoefficient);
|
|
767
|
+
const BuoyancyVector3 dragTorqueAtPoint = cross(arm, output.dragForce);
|
|
768
|
+
const BuoyancyVector3 angularDamping = scale(
|
|
769
|
+
body.angularVelocity, -submergedFraction * volume.angularDrag);
|
|
770
|
+
output.dragTorque = add(dragTorqueAtPoint, angularDamping);
|
|
771
|
+
output.totalForce = add(output.buoyantForce, output.dragForce);
|
|
772
|
+
output.totalTorque = add(output.buoyantTorque, output.dragTorque);
|
|
773
|
+
if (!finite(output.totalForce) || !finite(output.totalTorque)) {
|
|
774
|
+
return BuoyancyStatus::InvalidInput;
|
|
775
|
+
}
|
|
776
|
+
const BuoyancyVector3 gravitationalForce = scale(
|
|
777
|
+
effectiveGravity, body.mass);
|
|
778
|
+
const BuoyancyVector3 netForce = add(
|
|
779
|
+
output.buoyantForce, gravitationalForce);
|
|
780
|
+
const bool equilibrium = dot(netForce, netForce) < 1e-6
|
|
781
|
+
&& dot(output.buoyantTorque, output.buoyantTorque) < 1e-6
|
|
782
|
+
&& dot(output.relativeVelocity, output.relativeVelocity) < 1e-4
|
|
783
|
+
&& dot(body.angularVelocity, body.angularVelocity) < 1e-4;
|
|
784
|
+
output.isInEquilibrium = equilibrium;
|
|
785
|
+
output.shouldWake = body.sleeping
|
|
786
|
+
&& (!equilibrium || dot(volume.flowVelocity, volume.flowVelocity) > 1e-6);
|
|
787
|
+
return BuoyancyStatus::Success;
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
BuoyancyStatus calculateFluidForces(
|
|
791
|
+
const FluidBodyInput3D& body,
|
|
792
|
+
const FluidVolume3D& volume,
|
|
793
|
+
const BuoyancyVector3& gravity,
|
|
794
|
+
FluidForcesResult3D& output) {
|
|
795
|
+
output = {};
|
|
796
|
+
if (!finite(gravity) || !finite(body.position) || !finite(body.orientation)
|
|
797
|
+
|| !finite(body.linearVelocity) || !finite(body.angularVelocity)) {
|
|
798
|
+
return BuoyancyStatus::InvalidInput;
|
|
799
|
+
}
|
|
800
|
+
const BuoyancyStatus volStatus = validateFluidVolume(volume);
|
|
801
|
+
if (volStatus != BuoyancyStatus::Success) return volStatus;
|
|
802
|
+
|
|
803
|
+
const bool filterEnabled = volume.layer != 0 || volume.mask != 0;
|
|
804
|
+
if (filterEnabled
|
|
805
|
+
&& ((volume.mask & body.layer) == 0 || (body.mask & volume.layer) == 0)) {
|
|
806
|
+
return BuoyancyStatus::Success;
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
const BuoyancyStatus geomStatus = calculateSubmergedGeometryWithPlane(
|
|
810
|
+
body.shape,
|
|
811
|
+
body.position,
|
|
812
|
+
body.orientation,
|
|
813
|
+
body.size,
|
|
814
|
+
volume.plane,
|
|
815
|
+
volume.bounds,
|
|
816
|
+
output.submerged);
|
|
817
|
+
if (geomStatus != BuoyancyStatus::Success) return geomStatus;
|
|
818
|
+
const double fullVol = (body.shape == ShapeKind::Box)
|
|
819
|
+
? (8.0 * body.size.x * body.size.y * body.size.z)
|
|
820
|
+
: ((4.0 / 3.0) * kPi * body.size.x * body.size.x * body.size.x);
|
|
821
|
+
return finishFluidForces(body, volume, gravity, fullVol, output);
|
|
822
|
+
}
|
|
823
|
+
|
|
824
|
+
BuoyancyStatus validateVoxelBoxes(std::span<const VoxelBoxShape3D> boxes) {
|
|
825
|
+
if (boxes.empty()) return BuoyancyStatus::InvalidInput;
|
|
826
|
+
for (std::size_t index = 0; index < boxes.size(); ++index) {
|
|
827
|
+
const VoxelBoxShape3D& box = boxes[index];
|
|
828
|
+
if (!finite(box.halfSize) || !finite(box.offset)
|
|
829
|
+
|| !(box.halfSize.x > 0.0) || !(box.halfSize.y > 0.0)
|
|
830
|
+
|| !(box.halfSize.z > 0.0)) return BuoyancyStatus::InvalidInput;
|
|
831
|
+
for (std::size_t otherIndex = 0; otherIndex < index; ++otherIndex) {
|
|
832
|
+
const VoxelBoxShape3D& other = boxes[otherIndex];
|
|
833
|
+
constexpr double kEps = 1e-9;
|
|
834
|
+
const bool overlap = box.offset.x - box.halfSize.x
|
|
835
|
+
< other.offset.x + other.halfSize.x - kEps
|
|
836
|
+
&& other.offset.x - other.halfSize.x
|
|
837
|
+
< box.offset.x + box.halfSize.x - kEps
|
|
838
|
+
&& box.offset.y - box.halfSize.y
|
|
839
|
+
< other.offset.y + other.halfSize.y - kEps
|
|
840
|
+
&& other.offset.y - other.halfSize.y
|
|
841
|
+
< box.offset.y + box.halfSize.y - kEps
|
|
842
|
+
&& box.offset.z - box.halfSize.z
|
|
843
|
+
< other.offset.z + other.halfSize.z - kEps
|
|
844
|
+
&& other.offset.z - other.halfSize.z
|
|
845
|
+
< box.offset.z + box.halfSize.z - kEps;
|
|
846
|
+
if (overlap) return BuoyancyStatus::OverlapUnsupported;
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
return BuoyancyStatus::Success;
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
template <typename Calculate>
|
|
853
|
+
BuoyancyStatus calculateVoxelGeometry(
|
|
854
|
+
const BuoyancyVector3& position,
|
|
855
|
+
const BuoyancyQuaternion& orientation,
|
|
856
|
+
std::span<const VoxelBoxShape3D> boxes,
|
|
857
|
+
bool validateBoxes,
|
|
858
|
+
Calculate calculate,
|
|
859
|
+
SubmergedGeometry& output,
|
|
860
|
+
double& fullVolume) {
|
|
861
|
+
output = {};
|
|
862
|
+
fullVolume = 0.0;
|
|
863
|
+
if (validateBoxes) {
|
|
864
|
+
const BuoyancyStatus validation = validateVoxelBoxes(boxes);
|
|
865
|
+
if (validation != BuoyancyStatus::Success) return validation;
|
|
866
|
+
}
|
|
867
|
+
const double normSquared = orientation.x * orientation.x
|
|
868
|
+
+ orientation.y * orientation.y + orientation.z * orientation.z
|
|
869
|
+
+ orientation.w * orientation.w;
|
|
870
|
+
if (!(normSquared > 0.0) || !finite(normSquared) || !finite(position)) {
|
|
871
|
+
return BuoyancyStatus::InvalidInput;
|
|
872
|
+
}
|
|
873
|
+
BuoyancyVector3 centerMoment{};
|
|
874
|
+
for (const VoxelBoxShape3D& box : boxes) {
|
|
875
|
+
const BuoyancyVector3 center = add(position,
|
|
876
|
+
rotate(orientation, 1.0 / normSquared, box.offset));
|
|
877
|
+
SubmergedGeometry child{};
|
|
878
|
+
const BuoyancyStatus status = calculate(center, box.halfSize, child);
|
|
879
|
+
if (status != BuoyancyStatus::Success) return status;
|
|
880
|
+
fullVolume += 8.0 * box.halfSize.x * box.halfSize.y * box.halfSize.z;
|
|
881
|
+
output.volume += child.volume;
|
|
882
|
+
centerMoment = add(centerMoment, scale(child.center, child.volume));
|
|
883
|
+
}
|
|
884
|
+
if (!finite(fullVolume) || !finite(output.volume)) {
|
|
885
|
+
return BuoyancyStatus::NumericRange;
|
|
886
|
+
}
|
|
887
|
+
output.center = output.volume > 0.0
|
|
888
|
+
? scale(centerMoment, 1.0 / output.volume) : position;
|
|
889
|
+
return finite(output.center)
|
|
890
|
+
? BuoyancyStatus::Success : BuoyancyStatus::NumericRange;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
BuoyancyStatus calculateVoxelBuoyancy(
|
|
894
|
+
const BuoyancyInput& input,
|
|
895
|
+
std::span<const VoxelBoxShape3D> boxes,
|
|
896
|
+
BuoyancyResult& output) {
|
|
897
|
+
output = {};
|
|
898
|
+
if (!(input.density > 0.0) || !finite(input.density)
|
|
899
|
+
|| !finite(input.gravity) || !finite(input.bodyCenter)
|
|
900
|
+
|| !finite(input.waterY)) return BuoyancyStatus::InvalidInput;
|
|
901
|
+
double fullVolume = 0.0;
|
|
902
|
+
const BuoyancyStatus status = calculateVoxelGeometry(
|
|
903
|
+
input.shapeCenter, input.orientation, boxes, true,
|
|
904
|
+
[&](const BuoyancyVector3& center, const BuoyancyVector3& halfSize,
|
|
905
|
+
SubmergedGeometry& child) {
|
|
906
|
+
return calculateSubmergedGeometry(ShapeKind::Box, center,
|
|
907
|
+
input.orientation, halfSize, input.waterY, child);
|
|
908
|
+
}, output.submerged, fullVolume);
|
|
909
|
+
(void) fullVolume;
|
|
910
|
+
if (status != BuoyancyStatus::Success) return status;
|
|
911
|
+
output.force = scale(input.gravity,
|
|
912
|
+
-input.density * output.submerged.volume);
|
|
913
|
+
output.torque = cross(
|
|
914
|
+
subtract(output.submerged.center, input.bodyCenter), output.force);
|
|
915
|
+
return finite(output.force) && finite(output.torque)
|
|
916
|
+
? BuoyancyStatus::Success : BuoyancyStatus::NumericRange;
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
BuoyancyStatus calculateVoxelFluidForcesInternal(
|
|
920
|
+
const FluidBodyInput3D& body,
|
|
921
|
+
std::span<const VoxelBoxShape3D> boxes,
|
|
922
|
+
const FluidVolume3D& volume,
|
|
923
|
+
const BuoyancyVector3& gravity,
|
|
924
|
+
FluidForcesResult3D& output,
|
|
925
|
+
bool validateBoxes) {
|
|
926
|
+
output = {};
|
|
927
|
+
if (!finite(gravity) || !finite(body.position)
|
|
928
|
+
|| !finite(body.orientation) || !finite(body.linearVelocity)
|
|
929
|
+
|| !finite(body.angularVelocity)) return BuoyancyStatus::InvalidInput;
|
|
930
|
+
const BuoyancyStatus volumeStatus = validateFluidVolume(volume);
|
|
931
|
+
if (volumeStatus != BuoyancyStatus::Success) return volumeStatus;
|
|
932
|
+
const bool filterEnabled = volume.layer != 0 || volume.mask != 0;
|
|
933
|
+
if (filterEnabled && ((volume.mask & body.layer) == 0
|
|
934
|
+
|| (body.mask & volume.layer) == 0)) return BuoyancyStatus::Success;
|
|
935
|
+
double fullVolume = 0.0;
|
|
936
|
+
const BuoyancyStatus status = calculateVoxelGeometry(
|
|
937
|
+
body.position, body.orientation, boxes, validateBoxes,
|
|
938
|
+
[&](const BuoyancyVector3& center, const BuoyancyVector3& halfSize,
|
|
939
|
+
SubmergedGeometry& child) {
|
|
940
|
+
return calculateSubmergedGeometryWithPlane(ShapeKind::Box, center,
|
|
941
|
+
body.orientation, halfSize, volume.plane, volume.bounds, child);
|
|
942
|
+
}, output.submerged, fullVolume);
|
|
943
|
+
if (status != BuoyancyStatus::Success) return status;
|
|
944
|
+
return finishFluidForces(body, volume, gravity, fullVolume, output);
|
|
945
|
+
}
|
|
946
|
+
|
|
947
|
+
BuoyancyStatus calculateVoxelFluidForces(
|
|
948
|
+
const FluidBodyInput3D& body,
|
|
949
|
+
std::span<const VoxelBoxShape3D> boxes,
|
|
950
|
+
const FluidVolume3D& volume,
|
|
951
|
+
const BuoyancyVector3& gravity,
|
|
952
|
+
FluidForcesResult3D& output) {
|
|
953
|
+
return calculateVoxelFluidForcesInternal(
|
|
954
|
+
body, boxes, volume, gravity, output, true);
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
BuoyancyStatus calculateValidatedVoxelFluidForces(
|
|
958
|
+
const FluidBodyInput3D& body,
|
|
959
|
+
std::span<const VoxelBoxShape3D> boxes,
|
|
960
|
+
const FluidVolume3D& volume,
|
|
961
|
+
const BuoyancyVector3& gravity,
|
|
962
|
+
FluidForcesResult3D& output) {
|
|
963
|
+
return calculateVoxelFluidForcesInternal(
|
|
964
|
+
body, boxes, volume, gravity, output, false);
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
BuoyancyStatus calculateVoxelFluidStateForces(
|
|
968
|
+
const FluidBodyInput3D& body,
|
|
969
|
+
std::span<const VoxelBoxShape3D> boxes,
|
|
970
|
+
const FluidState3D& state,
|
|
971
|
+
const BuoyancyVector3& gravity,
|
|
972
|
+
FluidForcesResult3D& output) {
|
|
973
|
+
output = {};
|
|
974
|
+
const BuoyancyStatus stateStatus = validateFluidState(state);
|
|
975
|
+
if (stateStatus != BuoyancyStatus::Success) return stateStatus;
|
|
976
|
+
const BuoyancyStatus boxesStatus = validateVoxelBoxes(boxes);
|
|
977
|
+
if (boxesStatus != BuoyancyStatus::Success) return boxesStatus;
|
|
978
|
+
double maximumVolume = 0.0;
|
|
979
|
+
for (const VoxelBoxShape3D& box : boxes) {
|
|
980
|
+
maximumVolume += 8.0 * box.halfSize.x * box.halfSize.y * box.halfSize.z;
|
|
981
|
+
}
|
|
982
|
+
BuoyancyVector3 centerMoment{};
|
|
983
|
+
BuoyancyVector3 velocityMoment{};
|
|
984
|
+
bool equilibrium = true;
|
|
985
|
+
for (std::uint32_t index = 0; index < state.volumeCount; ++index) {
|
|
986
|
+
FluidForcesResult3D current{};
|
|
987
|
+
const BuoyancyStatus status = calculateVoxelFluidForces(
|
|
988
|
+
body, boxes, state.volumes[index], gravity, current);
|
|
989
|
+
if (status != BuoyancyStatus::Success) return status;
|
|
990
|
+
if (state.overlapPolicy == FluidOverlapPolicy::PriorityPartition
|
|
991
|
+
&& output.submerged.volume < maximumVolume
|
|
992
|
+
&& current.submerged.volume > maximumVolume - output.submerged.volume) {
|
|
993
|
+
const double remaining = maximumVolume - output.submerged.volume;
|
|
994
|
+
const double factor = remaining / current.submerged.volume;
|
|
995
|
+
current.submerged.volume = remaining;
|
|
996
|
+
current.buoyantForce = scale(current.buoyantForce, factor);
|
|
997
|
+
current.buoyantTorque = scale(current.buoyantTorque, factor);
|
|
998
|
+
current.dragForce = scale(current.dragForce, factor);
|
|
999
|
+
current.dragTorque = scale(current.dragTorque, factor);
|
|
1000
|
+
current.totalForce = add(current.buoyantForce, current.dragForce);
|
|
1001
|
+
current.totalTorque = add(current.buoyantTorque, current.dragTorque);
|
|
1002
|
+
}
|
|
1003
|
+
if (state.overlapPolicy == FluidOverlapPolicy::PriorityPartition
|
|
1004
|
+
&& output.submerged.volume >= maximumVolume) continue;
|
|
1005
|
+
output.buoyantForce = add(output.buoyantForce, current.buoyantForce);
|
|
1006
|
+
output.buoyantTorque = add(output.buoyantTorque, current.buoyantTorque);
|
|
1007
|
+
output.dragForce = add(output.dragForce, current.dragForce);
|
|
1008
|
+
output.dragTorque = add(output.dragTorque, current.dragTorque);
|
|
1009
|
+
output.totalForce = add(output.totalForce, current.totalForce);
|
|
1010
|
+
output.totalTorque = add(output.totalTorque, current.totalTorque);
|
|
1011
|
+
centerMoment = add(centerMoment,
|
|
1012
|
+
scale(current.submerged.center, current.submerged.volume));
|
|
1013
|
+
velocityMoment = add(velocityMoment,
|
|
1014
|
+
scale(current.relativeVelocity, current.submerged.volume));
|
|
1015
|
+
output.submerged.volume += current.submerged.volume;
|
|
1016
|
+
output.shouldWake = output.shouldWake || current.shouldWake;
|
|
1017
|
+
if (current.submerged.volume > 0.0 && !current.isInEquilibrium) {
|
|
1018
|
+
equilibrium = false;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
if (output.submerged.volume > 0.0) {
|
|
1022
|
+
const double inverseVolume = 1.0 / output.submerged.volume;
|
|
1023
|
+
output.submerged.center = scale(centerMoment, inverseVolume);
|
|
1024
|
+
output.relativeVelocity = scale(velocityMoment, inverseVolume);
|
|
1025
|
+
output.isInEquilibrium = equilibrium;
|
|
1026
|
+
} else {
|
|
1027
|
+
output.submerged.center = body.position;
|
|
1028
|
+
}
|
|
1029
|
+
return BuoyancyStatus::Success;
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
BuoyancyStatus stepBodyInFluids(
|
|
1033
|
+
FluidBodyInput3D& body,
|
|
1034
|
+
const FluidState3D& state,
|
|
1035
|
+
const BuoyancyVector3& gravity,
|
|
1036
|
+
double dt,
|
|
1037
|
+
FluidForcesResult3D* stepResult) {
|
|
1038
|
+
if (dt <= 0.0 || !finite(dt)) return BuoyancyStatus::InvalidInput;
|
|
1039
|
+
const BuoyancyStatus stateStatus = validateFluidState(state);
|
|
1040
|
+
if (stateStatus != BuoyancyStatus::Success) return stateStatus;
|
|
1041
|
+
|
|
1042
|
+
FluidForcesResult3D totalResult{};
|
|
1043
|
+
double maxVolume = 0.0;
|
|
1044
|
+
BuoyancyVector3 centerMoment{};
|
|
1045
|
+
BuoyancyVector3 relativeVelocityMoment{};
|
|
1046
|
+
bool allSubmergedVolumesInEquilibrium = true;
|
|
1047
|
+
if (body.shape == ShapeKind::Box) {
|
|
1048
|
+
maxVolume = 8.0 * body.size.x * body.size.y * body.size.z;
|
|
1049
|
+
} else if (body.shape == ShapeKind::Sphere) {
|
|
1050
|
+
maxVolume = (4.0 / 3.0) * kPi * body.size.x * body.size.x * body.size.x;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
for (std::uint32_t i = 0; i < state.volumeCount; ++i) {
|
|
1054
|
+
FluidForcesResult3D res{};
|
|
1055
|
+
const BuoyancyStatus s = calculateFluidForces(body, state.volumes[i], gravity, res);
|
|
1056
|
+
if (s != BuoyancyStatus::Success) return s;
|
|
1057
|
+
|
|
1058
|
+
if (state.overlapPolicy == FluidOverlapPolicy::PriorityPartition && maxVolume > 0.0) {
|
|
1059
|
+
if (totalResult.submerged.volume >= maxVolume - 1e-9) {
|
|
1060
|
+
continue;
|
|
1061
|
+
}
|
|
1062
|
+
const double remainingCapacity = maxVolume - totalResult.submerged.volume;
|
|
1063
|
+
if (res.submerged.volume > remainingCapacity) {
|
|
1064
|
+
const double factor = remainingCapacity / res.submerged.volume;
|
|
1065
|
+
res.submerged.volume = remainingCapacity;
|
|
1066
|
+
res.buoyantForce = scale(res.buoyantForce, factor);
|
|
1067
|
+
res.buoyantTorque = scale(res.buoyantTorque, factor);
|
|
1068
|
+
res.dragForce = scale(res.dragForce, factor);
|
|
1069
|
+
res.dragTorque = scale(res.dragTorque, factor);
|
|
1070
|
+
res.totalForce = add(res.buoyantForce, res.dragForce);
|
|
1071
|
+
res.totalTorque = add(res.buoyantTorque, res.dragTorque);
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
totalResult.buoyantForce = add(totalResult.buoyantForce, res.buoyantForce);
|
|
1076
|
+
totalResult.buoyantTorque = add(totalResult.buoyantTorque, res.buoyantTorque);
|
|
1077
|
+
totalResult.dragForce = add(totalResult.dragForce, res.dragForce);
|
|
1078
|
+
totalResult.dragTorque = add(totalResult.dragTorque, res.dragTorque);
|
|
1079
|
+
totalResult.totalForce = add(totalResult.totalForce, res.totalForce);
|
|
1080
|
+
totalResult.totalTorque = add(totalResult.totalTorque, res.totalTorque);
|
|
1081
|
+
centerMoment = add(centerMoment, scale(res.submerged.center, res.submerged.volume));
|
|
1082
|
+
relativeVelocityMoment = add(
|
|
1083
|
+
relativeVelocityMoment,
|
|
1084
|
+
scale(res.relativeVelocity, res.submerged.volume));
|
|
1085
|
+
totalResult.submerged.volume += res.submerged.volume;
|
|
1086
|
+
if (res.shouldWake) totalResult.shouldWake = true;
|
|
1087
|
+
if (res.submerged.volume > 0.0 && !res.isInEquilibrium) {
|
|
1088
|
+
allSubmergedVolumesInEquilibrium = false;
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
if (totalResult.submerged.volume > 0.0) {
|
|
1092
|
+
const double inverseVolume = 1.0 / totalResult.submerged.volume;
|
|
1093
|
+
totalResult.submerged.center = scale(centerMoment, inverseVolume);
|
|
1094
|
+
totalResult.relativeVelocity = scale(relativeVelocityMoment, inverseVolume);
|
|
1095
|
+
totalResult.isInEquilibrium = allSubmergedVolumesInEquilibrium;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
if (body.sleeping) {
|
|
1099
|
+
if (totalResult.shouldWake) {
|
|
1100
|
+
body.sleeping = false;
|
|
1101
|
+
} else {
|
|
1102
|
+
if (stepResult != nullptr) *stepResult = totalResult;
|
|
1103
|
+
return BuoyancyStatus::Success;
|
|
1104
|
+
}
|
|
1105
|
+
}
|
|
1106
|
+
|
|
1107
|
+
if (body.mass > 0.0) {
|
|
1108
|
+
const BuoyancyVector3 accel = scale(totalResult.totalForce, 1.0 / body.mass);
|
|
1109
|
+
body.linearVelocity = add(body.linearVelocity, scale(accel, dt));
|
|
1110
|
+
body.position = add(body.position, scale(body.linearVelocity, dt));
|
|
1111
|
+
}
|
|
1112
|
+
if (body.inertiaDiag.x > 0.0 && body.inertiaDiag.y > 0.0 && body.inertiaDiag.z > 0.0) {
|
|
1113
|
+
const BuoyancyVector3 angAccel{
|
|
1114
|
+
totalResult.totalTorque.x / body.inertiaDiag.x,
|
|
1115
|
+
totalResult.totalTorque.y / body.inertiaDiag.y,
|
|
1116
|
+
totalResult.totalTorque.z / body.inertiaDiag.z,
|
|
1117
|
+
};
|
|
1118
|
+
body.angularVelocity = add(body.angularVelocity, scale(angAccel, dt));
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
if (!body.sleeping && totalResult.isInEquilibrium) {
|
|
1122
|
+
body.sleeping = true;
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
if (stepResult != nullptr) *stepResult = totalResult;
|
|
1126
|
+
return BuoyancyStatus::Success;
|
|
1127
|
+
}
|
|
1128
|
+
|
|
1129
|
+
} // namespace rundot::kinetix::physics
|
|
1130
|
+
|
|
1131
|
+
namespace {
|
|
1132
|
+
|
|
1133
|
+
bool parseVoxelBoxes(
|
|
1134
|
+
const double* values,
|
|
1135
|
+
std::uint32_t valueCount,
|
|
1136
|
+
std::vector<rundot::kinetix::physics::VoxelBoxShape3D>& output) {
|
|
1137
|
+
using rundot::kinetix::physics::VoxelBoxShape3D;
|
|
1138
|
+
if (values == nullptr || valueCount == 0 || valueCount % 6 != 0) return false;
|
|
1139
|
+
output.resize(valueCount / 6);
|
|
1140
|
+
for (std::size_t index = 0; index < output.size(); ++index) {
|
|
1141
|
+
const double* record = values + index * 6;
|
|
1142
|
+
for (std::size_t field = 0; field < 6; ++field) {
|
|
1143
|
+
if (!std::isfinite(record[field])) return false;
|
|
1144
|
+
}
|
|
1145
|
+
output[index] = VoxelBoxShape3D{
|
|
1146
|
+
{record[0], record[1], record[2]},
|
|
1147
|
+
{record[3], record[4], record[5]},
|
|
1148
|
+
};
|
|
1149
|
+
}
|
|
1150
|
+
return true;
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
void writeFluidResult(
|
|
1154
|
+
const rundot::kinetix::physics::FluidForcesResult3D& result,
|
|
1155
|
+
double* output) {
|
|
1156
|
+
const double values[rundot::kinetix::physics::kFluidForcesOutputStride] = {
|
|
1157
|
+
result.submerged.volume,
|
|
1158
|
+
result.submerged.center.x, result.submerged.center.y,
|
|
1159
|
+
result.submerged.center.z,
|
|
1160
|
+
result.buoyantForce.x, result.buoyantForce.y, result.buoyantForce.z,
|
|
1161
|
+
result.buoyantTorque.x, result.buoyantTorque.y, result.buoyantTorque.z,
|
|
1162
|
+
result.dragForce.x, result.dragForce.y, result.dragForce.z,
|
|
1163
|
+
result.dragTorque.x, result.dragTorque.y, result.dragTorque.z,
|
|
1164
|
+
result.totalForce.x, result.totalForce.y, result.totalForce.z,
|
|
1165
|
+
result.totalTorque.x, result.totalTorque.y, result.totalTorque.z,
|
|
1166
|
+
result.relativeVelocity.x, result.relativeVelocity.y,
|
|
1167
|
+
result.relativeVelocity.z,
|
|
1168
|
+
result.shouldWake ? 1.0 : 0.0,
|
|
1169
|
+
result.isInEquilibrium ? 1.0 : 0.0,
|
|
1170
|
+
};
|
|
1171
|
+
std::copy(std::begin(values), std::end(values), output);
|
|
1172
|
+
}
|
|
1173
|
+
|
|
1174
|
+
} // namespace
|
|
1175
|
+
|
|
1176
|
+
extern "C" std::int32_t rundot_kinetix_physics_buoyancy(
|
|
1177
|
+
const double* input,
|
|
1178
|
+
std::uint32_t input_values,
|
|
1179
|
+
double* output,
|
|
1180
|
+
std::uint32_t output_values) {
|
|
1181
|
+
using namespace rundot::kinetix::physics;
|
|
1182
|
+
if (output == nullptr || output_values < kBuoyancyOutputStride) {
|
|
1183
|
+
return static_cast<std::int32_t>(BuoyancyStatus::OutputTooSmall);
|
|
1184
|
+
}
|
|
1185
|
+
std::fill(output, output + kBuoyancyOutputStride, 0.0);
|
|
1186
|
+
if (input == nullptr || input_values != kBuoyancyInputStride) {
|
|
1187
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1188
|
+
}
|
|
1189
|
+
for (std::uint32_t index = 0; index < input_values; index += 1) {
|
|
1190
|
+
if (!std::isfinite(input[index])) {
|
|
1191
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1192
|
+
}
|
|
1193
|
+
}
|
|
1194
|
+
const double shapeValue = input[0];
|
|
1195
|
+
if (shapeValue < 0.0 || shapeValue > 6.0
|
|
1196
|
+
|| shapeValue != static_cast<double>(static_cast<std::uint32_t>(shapeValue))) {
|
|
1197
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1198
|
+
}
|
|
1199
|
+
BuoyancyInput parsed{
|
|
1200
|
+
static_cast<ShapeKind>(static_cast<std::uint32_t>(shapeValue)),
|
|
1201
|
+
{input[1], input[2], input[3]},
|
|
1202
|
+
{input[4], input[5], input[6], input[7]},
|
|
1203
|
+
{input[8], input[9], input[10]},
|
|
1204
|
+
input[11],
|
|
1205
|
+
input[12],
|
|
1206
|
+
{input[13], input[14], input[15]},
|
|
1207
|
+
{input[16], input[17], input[18]},
|
|
1208
|
+
};
|
|
1209
|
+
const double normSquared = parsed.orientation.x * parsed.orientation.x
|
|
1210
|
+
+ parsed.orientation.y * parsed.orientation.y
|
|
1211
|
+
+ parsed.orientation.z * parsed.orientation.z
|
|
1212
|
+
+ parsed.orientation.w * parsed.orientation.w;
|
|
1213
|
+
if (!(normSquared > 0.0) || !std::isfinite(normSquared)) {
|
|
1214
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1215
|
+
}
|
|
1216
|
+
parsed.bodyCenter = add(
|
|
1217
|
+
parsed.shapeCenter,
|
|
1218
|
+
rotate(parsed.orientation, 1.0 / normSquared, parsed.bodyCenter));
|
|
1219
|
+
BuoyancyResult result;
|
|
1220
|
+
const BuoyancyStatus status = calculateBuoyancy(parsed, result);
|
|
1221
|
+
if (status != BuoyancyStatus::Success) return static_cast<std::int32_t>(status);
|
|
1222
|
+
output[0] = result.submerged.volume;
|
|
1223
|
+
output[1] = result.submerged.center.x;
|
|
1224
|
+
output[2] = result.submerged.center.y;
|
|
1225
|
+
output[3] = result.submerged.center.z;
|
|
1226
|
+
output[4] = result.force.x;
|
|
1227
|
+
output[5] = result.force.y;
|
|
1228
|
+
output[6] = result.force.z;
|
|
1229
|
+
output[7] = result.torque.x;
|
|
1230
|
+
output[8] = result.torque.y;
|
|
1231
|
+
output[9] = result.torque.z;
|
|
1232
|
+
return static_cast<std::int32_t>(status);
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
extern "C" std::int32_t rundot_kinetix_physics_voxel_buoyancy(
|
|
1236
|
+
const double* input,
|
|
1237
|
+
std::uint32_t input_values,
|
|
1238
|
+
const double* children,
|
|
1239
|
+
std::uint32_t child_values,
|
|
1240
|
+
double* output,
|
|
1241
|
+
std::uint32_t output_values) {
|
|
1242
|
+
using namespace rundot::kinetix::physics;
|
|
1243
|
+
if (output == nullptr || output_values < kBuoyancyOutputStride) {
|
|
1244
|
+
return static_cast<std::int32_t>(BuoyancyStatus::OutputTooSmall);
|
|
1245
|
+
}
|
|
1246
|
+
if (input == nullptr || input_values != kBuoyancyInputStride) {
|
|
1247
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1248
|
+
}
|
|
1249
|
+
for (std::uint32_t index = 0; index < input_values; ++index) {
|
|
1250
|
+
if (!std::isfinite(input[index])) {
|
|
1251
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
std::vector<VoxelBoxShape3D> boxes;
|
|
1255
|
+
if (!parseVoxelBoxes(children, child_values, boxes)) {
|
|
1256
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1257
|
+
}
|
|
1258
|
+
BuoyancyInput parsed{
|
|
1259
|
+
ShapeKind::Compound,
|
|
1260
|
+
{input[1], input[2], input[3]},
|
|
1261
|
+
{input[4], input[5], input[6], input[7]},
|
|
1262
|
+
{},
|
|
1263
|
+
input[11],
|
|
1264
|
+
input[12],
|
|
1265
|
+
{input[13], input[14], input[15]},
|
|
1266
|
+
{input[16], input[17], input[18]},
|
|
1267
|
+
};
|
|
1268
|
+
const double normSquared = parsed.orientation.x * parsed.orientation.x
|
|
1269
|
+
+ parsed.orientation.y * parsed.orientation.y
|
|
1270
|
+
+ parsed.orientation.z * parsed.orientation.z
|
|
1271
|
+
+ parsed.orientation.w * parsed.orientation.w;
|
|
1272
|
+
if (!(normSquared > 0.0) || !std::isfinite(normSquared)) {
|
|
1273
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1274
|
+
}
|
|
1275
|
+
parsed.bodyCenter = add(parsed.shapeCenter,
|
|
1276
|
+
rotate(parsed.orientation, 1.0 / normSquared, parsed.bodyCenter));
|
|
1277
|
+
BuoyancyResult result{};
|
|
1278
|
+
const BuoyancyStatus status = calculateVoxelBuoyancy(parsed, boxes, result);
|
|
1279
|
+
if (status != BuoyancyStatus::Success) {
|
|
1280
|
+
return static_cast<std::int32_t>(status);
|
|
1281
|
+
}
|
|
1282
|
+
const double values[kBuoyancyOutputStride] = {
|
|
1283
|
+
result.submerged.volume,
|
|
1284
|
+
result.submerged.center.x, result.submerged.center.y,
|
|
1285
|
+
result.submerged.center.z,
|
|
1286
|
+
result.force.x, result.force.y, result.force.z,
|
|
1287
|
+
result.torque.x, result.torque.y, result.torque.z,
|
|
1288
|
+
};
|
|
1289
|
+
std::copy(std::begin(values), std::end(values), output);
|
|
1290
|
+
return static_cast<std::int32_t>(BuoyancyStatus::Success);
|
|
1291
|
+
}
|
|
1292
|
+
|
|
1293
|
+
extern "C" std::uint32_t rundot_kinetix_physics_buoyancy_abi_version() {
|
|
1294
|
+
return 1;
|
|
1295
|
+
}
|
|
1296
|
+
|
|
1297
|
+
extern "C" std::uint32_t rundot_kinetix_physics_buoyancy_input_stride() {
|
|
1298
|
+
return rundot::kinetix::physics::kBuoyancyInputStride;
|
|
1299
|
+
}
|
|
1300
|
+
|
|
1301
|
+
extern "C" std::uint32_t rundot_kinetix_physics_buoyancy_output_stride() {
|
|
1302
|
+
return rundot::kinetix::physics::kBuoyancyOutputStride;
|
|
1303
|
+
}
|
|
1304
|
+
|
|
1305
|
+
extern "C" std::uint32_t rundot_kinetix_physics_fluid_abi_version() {
|
|
1306
|
+
return 1;
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
extern "C" std::uint32_t rundot_kinetix_physics_fluid_forces_input_stride() {
|
|
1310
|
+
return rundot::kinetix::physics::kFluidForcesInputStride;
|
|
1311
|
+
}
|
|
1312
|
+
|
|
1313
|
+
extern "C" std::uint32_t rundot_kinetix_physics_fluid_forces_output_stride() {
|
|
1314
|
+
return rundot::kinetix::physics::kFluidForcesOutputStride;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
extern "C" std::uint32_t rundot_kinetix_physics_fluid_volume_input_stride() {
|
|
1318
|
+
return rundot::kinetix::physics::kFluidVolumeInputStride;
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
extern "C" std::uint32_t rundot_kinetix_physics_fluid_state_forces_input_stride() {
|
|
1322
|
+
return rundot::kinetix::physics::kFluidStateForcesInputStride;
|
|
1323
|
+
}
|
|
1324
|
+
|
|
1325
|
+
extern "C" std::int32_t rundot_kinetix_physics_fluid_forces(
|
|
1326
|
+
const double* input,
|
|
1327
|
+
std::uint32_t input_values,
|
|
1328
|
+
double* output,
|
|
1329
|
+
std::uint32_t output_values) {
|
|
1330
|
+
using namespace rundot::kinetix::physics;
|
|
1331
|
+
if (output == nullptr || output_values < kFluidForcesOutputStride) {
|
|
1332
|
+
return static_cast<std::int32_t>(BuoyancyStatus::OutputTooSmall);
|
|
1333
|
+
}
|
|
1334
|
+
if (input == nullptr || input_values != kFluidForcesInputStride) {
|
|
1335
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1336
|
+
}
|
|
1337
|
+
for (std::uint32_t index = 0; index < input_values; index += 1) {
|
|
1338
|
+
if (!std::isfinite(input[index])) {
|
|
1339
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
const auto u32 = [](double value, std::uint32_t& result) {
|
|
1343
|
+
if (!std::isfinite(value) || value < 0.0 || value > 4294967295.0
|
|
1344
|
+
|| std::floor(value) != value) return false;
|
|
1345
|
+
result = static_cast<std::uint32_t>(value);
|
|
1346
|
+
return true;
|
|
1347
|
+
};
|
|
1348
|
+
std::uint32_t shape = 0;
|
|
1349
|
+
std::uint32_t booleanValue = 0;
|
|
1350
|
+
FluidBodyInput3D body{};
|
|
1351
|
+
FluidVolume3D volume{};
|
|
1352
|
+
if (!u32(input[0], shape) || shape > static_cast<std::uint32_t>(ShapeKind::Sphere)
|
|
1353
|
+
|| !u32(input[25], booleanValue) || booleanValue > 1
|
|
1354
|
+
|| !u32(input[26], body.layer) || !u32(input[27], body.mask)
|
|
1355
|
+
|| !u32(input[28], volume.id) || !u32(input[35], booleanValue) || booleanValue > 1
|
|
1356
|
+
|| !u32(input[46], volume.layer) || !u32(input[47], volume.mask)) {
|
|
1357
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1358
|
+
}
|
|
1359
|
+
body.shape = static_cast<ShapeKind>(shape);
|
|
1360
|
+
body.position = {input[1], input[2], input[3]};
|
|
1361
|
+
body.orientation = {input[4], input[5], input[6], input[7]};
|
|
1362
|
+
body.size = {input[8], input[9], input[10]};
|
|
1363
|
+
body.centerOfMass = {input[11], input[12], input[13]};
|
|
1364
|
+
body.linearVelocity = {input[14], input[15], input[16]};
|
|
1365
|
+
body.angularVelocity = {input[17], input[18], input[19]};
|
|
1366
|
+
body.mass = input[20];
|
|
1367
|
+
body.inertiaDiag = {input[21], input[22], input[23]};
|
|
1368
|
+
body.gravityScale = input[24];
|
|
1369
|
+
body.sleeping = input[25] != 0.0;
|
|
1370
|
+
volume.bounds = {input[29], input[30], input[31], input[32], input[33], input[34]};
|
|
1371
|
+
volume.plane.enabled = input[35] != 0.0;
|
|
1372
|
+
volume.plane.normal = {input[36], input[37], input[38]};
|
|
1373
|
+
volume.plane.offset = input[39];
|
|
1374
|
+
volume.density = input[40];
|
|
1375
|
+
volume.linearDrag = input[41];
|
|
1376
|
+
volume.angularDrag = input[42];
|
|
1377
|
+
volume.flowVelocity = {input[43], input[44], input[45]};
|
|
1378
|
+
const BuoyancyVector3 gravity{input[48], input[49], input[50]};
|
|
1379
|
+
FluidForcesResult3D result{};
|
|
1380
|
+
const BuoyancyStatus status = calculateFluidForces(body, volume, gravity, result);
|
|
1381
|
+
if (status != BuoyancyStatus::Success) return static_cast<std::int32_t>(status);
|
|
1382
|
+
const double values[kFluidForcesOutputStride] = {
|
|
1383
|
+
result.submerged.volume,
|
|
1384
|
+
result.submerged.center.x, result.submerged.center.y, result.submerged.center.z,
|
|
1385
|
+
result.buoyantForce.x, result.buoyantForce.y, result.buoyantForce.z,
|
|
1386
|
+
result.buoyantTorque.x, result.buoyantTorque.y, result.buoyantTorque.z,
|
|
1387
|
+
result.dragForce.x, result.dragForce.y, result.dragForce.z,
|
|
1388
|
+
result.dragTorque.x, result.dragTorque.y, result.dragTorque.z,
|
|
1389
|
+
result.totalForce.x, result.totalForce.y, result.totalForce.z,
|
|
1390
|
+
result.totalTorque.x, result.totalTorque.y, result.totalTorque.z,
|
|
1391
|
+
result.relativeVelocity.x, result.relativeVelocity.y, result.relativeVelocity.z,
|
|
1392
|
+
result.shouldWake ? 1.0 : 0.0,
|
|
1393
|
+
result.isInEquilibrium ? 1.0 : 0.0,
|
|
1394
|
+
};
|
|
1395
|
+
for (std::uint32_t index = 0; index < kFluidForcesOutputStride; index += 1) {
|
|
1396
|
+
output[index] = values[index];
|
|
1397
|
+
}
|
|
1398
|
+
return static_cast<std::int32_t>(BuoyancyStatus::Success);
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
extern "C" std::int32_t rundot_kinetix_physics_voxel_fluid_forces(
|
|
1402
|
+
const double* input,
|
|
1403
|
+
std::uint32_t input_values,
|
|
1404
|
+
const double* children,
|
|
1405
|
+
std::uint32_t child_values,
|
|
1406
|
+
double* output,
|
|
1407
|
+
std::uint32_t output_values) {
|
|
1408
|
+
using namespace rundot::kinetix::physics;
|
|
1409
|
+
if (output == nullptr || output_values < kFluidForcesOutputStride) {
|
|
1410
|
+
return static_cast<std::int32_t>(BuoyancyStatus::OutputTooSmall);
|
|
1411
|
+
}
|
|
1412
|
+
if (input == nullptr || input_values != kFluidForcesInputStride) {
|
|
1413
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1414
|
+
}
|
|
1415
|
+
for (std::uint32_t index = 0; index < input_values; ++index) {
|
|
1416
|
+
if (!std::isfinite(input[index])) {
|
|
1417
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1418
|
+
}
|
|
1419
|
+
}
|
|
1420
|
+
std::vector<VoxelBoxShape3D> boxes;
|
|
1421
|
+
if (!parseVoxelBoxes(children, child_values, boxes)) {
|
|
1422
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1423
|
+
}
|
|
1424
|
+
const auto u32 = [](double value, std::uint32_t& result) {
|
|
1425
|
+
if (value < 0.0 || value > 4294967295.0
|
|
1426
|
+
|| std::floor(value) != value) return false;
|
|
1427
|
+
result = static_cast<std::uint32_t>(value);
|
|
1428
|
+
return true;
|
|
1429
|
+
};
|
|
1430
|
+
std::uint32_t ignoredShape = 0;
|
|
1431
|
+
std::uint32_t booleanValue = 0;
|
|
1432
|
+
FluidBodyInput3D body{};
|
|
1433
|
+
FluidVolume3D volume{};
|
|
1434
|
+
if (!u32(input[0], ignoredShape)
|
|
1435
|
+
|| !u32(input[25], booleanValue) || booleanValue > 1
|
|
1436
|
+
|| !u32(input[26], body.layer) || !u32(input[27], body.mask)
|
|
1437
|
+
|| !u32(input[28], volume.id)
|
|
1438
|
+
|| !u32(input[35], booleanValue) || booleanValue > 1
|
|
1439
|
+
|| !u32(input[46], volume.layer) || !u32(input[47], volume.mask)) {
|
|
1440
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1441
|
+
}
|
|
1442
|
+
body.shape = ShapeKind::Compound;
|
|
1443
|
+
body.position = {input[1], input[2], input[3]};
|
|
1444
|
+
body.orientation = {input[4], input[5], input[6], input[7]};
|
|
1445
|
+
body.centerOfMass = {input[11], input[12], input[13]};
|
|
1446
|
+
body.linearVelocity = {input[14], input[15], input[16]};
|
|
1447
|
+
body.angularVelocity = {input[17], input[18], input[19]};
|
|
1448
|
+
body.mass = input[20];
|
|
1449
|
+
body.inertiaDiag = {input[21], input[22], input[23]};
|
|
1450
|
+
body.gravityScale = input[24];
|
|
1451
|
+
body.sleeping = input[25] != 0.0;
|
|
1452
|
+
volume.bounds = {
|
|
1453
|
+
input[29], input[30], input[31], input[32], input[33], input[34]};
|
|
1454
|
+
volume.plane.enabled = input[35] != 0.0;
|
|
1455
|
+
volume.plane.normal = {input[36], input[37], input[38]};
|
|
1456
|
+
volume.plane.offset = input[39];
|
|
1457
|
+
volume.density = input[40];
|
|
1458
|
+
volume.linearDrag = input[41];
|
|
1459
|
+
volume.angularDrag = input[42];
|
|
1460
|
+
volume.flowVelocity = {input[43], input[44], input[45]};
|
|
1461
|
+
const BuoyancyVector3 gravity{input[48], input[49], input[50]};
|
|
1462
|
+
FluidForcesResult3D result{};
|
|
1463
|
+
const BuoyancyStatus status = calculateVoxelFluidForces(
|
|
1464
|
+
body, boxes, volume, gravity, result);
|
|
1465
|
+
if (status != BuoyancyStatus::Success) {
|
|
1466
|
+
return static_cast<std::int32_t>(status);
|
|
1467
|
+
}
|
|
1468
|
+
writeFluidResult(result, output);
|
|
1469
|
+
return static_cast<std::int32_t>(BuoyancyStatus::Success);
|
|
1470
|
+
}
|
|
1471
|
+
|
|
1472
|
+
extern "C" std::int32_t rundot_kinetix_physics_fluid_state_forces(
|
|
1473
|
+
const double* input,
|
|
1474
|
+
std::uint32_t input_values,
|
|
1475
|
+
double* output,
|
|
1476
|
+
std::uint32_t output_values) {
|
|
1477
|
+
using namespace rundot::kinetix::physics;
|
|
1478
|
+
if (output == nullptr || output_values < kFluidForcesOutputStride) {
|
|
1479
|
+
return static_cast<std::int32_t>(BuoyancyStatus::OutputTooSmall);
|
|
1480
|
+
}
|
|
1481
|
+
if (input == nullptr || input_values != kFluidStateForcesInputStride) {
|
|
1482
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1483
|
+
}
|
|
1484
|
+
for (std::uint32_t index = 0; index < input_values; index += 1) {
|
|
1485
|
+
if (!std::isfinite(input[index])) {
|
|
1486
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
const auto u32 = [](double value, std::uint32_t& result) {
|
|
1490
|
+
if (value < 0.0 || value > 4294967295.0 || std::floor(value) != value) return false;
|
|
1491
|
+
result = static_cast<std::uint32_t>(value);
|
|
1492
|
+
return true;
|
|
1493
|
+
};
|
|
1494
|
+
std::uint32_t shape = 0;
|
|
1495
|
+
std::uint32_t booleanValue = 0;
|
|
1496
|
+
std::uint32_t policy = 0;
|
|
1497
|
+
FluidBodyInput3D body{};
|
|
1498
|
+
FluidState3D state{};
|
|
1499
|
+
if (!u32(input[0], shape) || shape > static_cast<std::uint32_t>(ShapeKind::Sphere)
|
|
1500
|
+
|| !u32(input[25], booleanValue) || booleanValue > 1
|
|
1501
|
+
|| !u32(input[26], body.layer) || !u32(input[27], body.mask)
|
|
1502
|
+
|| !u32(input[28], policy) || policy > static_cast<std::uint32_t>(FluidOverlapPolicy::Accumulate)
|
|
1503
|
+
|| !u32(input[29], state.volumeCount) || state.volumeCount > kMaxFluidVolumes3D) {
|
|
1504
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1505
|
+
}
|
|
1506
|
+
body.shape = static_cast<ShapeKind>(shape);
|
|
1507
|
+
body.position = {input[1], input[2], input[3]};
|
|
1508
|
+
body.orientation = {input[4], input[5], input[6], input[7]};
|
|
1509
|
+
body.size = {input[8], input[9], input[10]};
|
|
1510
|
+
body.centerOfMass = {input[11], input[12], input[13]};
|
|
1511
|
+
body.linearVelocity = {input[14], input[15], input[16]};
|
|
1512
|
+
body.angularVelocity = {input[17], input[18], input[19]};
|
|
1513
|
+
body.mass = input[20];
|
|
1514
|
+
body.inertiaDiag = {input[21], input[22], input[23]};
|
|
1515
|
+
body.gravityScale = input[24];
|
|
1516
|
+
body.sleeping = input[25] != 0.0;
|
|
1517
|
+
state.overlapPolicy = static_cast<FluidOverlapPolicy>(policy);
|
|
1518
|
+
for (std::uint32_t index = 0; index < state.volumeCount; index += 1) {
|
|
1519
|
+
const std::uint32_t base = 30 + index * kFluidVolumeInputStride;
|
|
1520
|
+
FluidVolume3D& volume = state.volumes[index];
|
|
1521
|
+
if (!u32(input[base], volume.id)
|
|
1522
|
+
|| !u32(input[base + 7], booleanValue) || booleanValue > 1
|
|
1523
|
+
|| !u32(input[base + 18], volume.layer)
|
|
1524
|
+
|| !u32(input[base + 19], volume.mask)) {
|
|
1525
|
+
return static_cast<std::int32_t>(BuoyancyStatus::InvalidInput);
|
|
1526
|
+
}
|
|
1527
|
+
volume.bounds = {
|
|
1528
|
+
input[base + 1], input[base + 2], input[base + 3],
|
|
1529
|
+
input[base + 4], input[base + 5], input[base + 6],
|
|
1530
|
+
};
|
|
1531
|
+
volume.plane.enabled = input[base + 7] != 0.0;
|
|
1532
|
+
volume.plane.normal = {input[base + 8], input[base + 9], input[base + 10]};
|
|
1533
|
+
volume.plane.offset = input[base + 11];
|
|
1534
|
+
volume.density = input[base + 12];
|
|
1535
|
+
volume.linearDrag = input[base + 13];
|
|
1536
|
+
volume.angularDrag = input[base + 14];
|
|
1537
|
+
volume.flowVelocity = {input[base + 15], input[base + 16], input[base + 17]};
|
|
1538
|
+
}
|
|
1539
|
+
const BuoyancyVector3 gravity{input[350], input[351], input[352]};
|
|
1540
|
+
FluidForcesResult3D result{};
|
|
1541
|
+
const BuoyancyStatus status = stepBodyInFluids(body, state, gravity, 1.0, &result);
|
|
1542
|
+
if (status != BuoyancyStatus::Success) return static_cast<std::int32_t>(status);
|
|
1543
|
+
const double values[kFluidForcesOutputStride] = {
|
|
1544
|
+
result.submerged.volume,
|
|
1545
|
+
result.submerged.center.x, result.submerged.center.y, result.submerged.center.z,
|
|
1546
|
+
result.buoyantForce.x, result.buoyantForce.y, result.buoyantForce.z,
|
|
1547
|
+
result.buoyantTorque.x, result.buoyantTorque.y, result.buoyantTorque.z,
|
|
1548
|
+
result.dragForce.x, result.dragForce.y, result.dragForce.z,
|
|
1549
|
+
result.dragTorque.x, result.dragTorque.y, result.dragTorque.z,
|
|
1550
|
+
result.totalForce.x, result.totalForce.y, result.totalForce.z,
|
|
1551
|
+
result.totalTorque.x, result.totalTorque.y, result.totalTorque.z,
|
|
1552
|
+
result.relativeVelocity.x, result.relativeVelocity.y, result.relativeVelocity.z,
|
|
1553
|
+
result.shouldWake ? 1.0 : 0.0,
|
|
1554
|
+
result.isInEquilibrium ? 1.0 : 0.0,
|
|
1555
|
+
};
|
|
1556
|
+
for (std::uint32_t index = 0; index < kFluidForcesOutputStride; index += 1) {
|
|
1557
|
+
output[index] = values[index];
|
|
1558
|
+
}
|
|
1559
|
+
return static_cast<std::int32_t>(BuoyancyStatus::Success);
|
|
1560
|
+
}
|
|
1561
|
+
|
|
1562
|
+
extern "C" std::int32_t rundot_kinetix_physics_validate_fluid_volume(
|
|
1563
|
+
const rundot::kinetix::physics::FluidVolume3D* volume) {
|
|
1564
|
+
if (volume == nullptr) {
|
|
1565
|
+
return static_cast<std::int32_t>(rundot::kinetix::physics::BuoyancyStatus::InvalidInput);
|
|
1566
|
+
}
|
|
1567
|
+
return static_cast<std::int32_t>(rundot::kinetix::physics::validateFluidVolume(*volume));
|
|
1568
|
+
}
|
|
1569
|
+
|
|
1570
|
+
extern "C" std::int32_t rundot_kinetix_physics_validate_fluid_state(
|
|
1571
|
+
const rundot::kinetix::physics::FluidState3D* state) {
|
|
1572
|
+
if (state == nullptr) {
|
|
1573
|
+
return static_cast<std::int32_t>(rundot::kinetix::physics::BuoyancyStatus::InvalidInput);
|
|
1574
|
+
}
|
|
1575
|
+
return static_cast<std::int32_t>(rundot::kinetix::physics::validateFluidState(*state));
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
extern "C" std::int32_t rundot_kinetix_physics_calculate_fluid_forces(
|
|
1579
|
+
const rundot::kinetix::physics::FluidBodyInput3D* body,
|
|
1580
|
+
const rundot::kinetix::physics::FluidVolume3D* volume,
|
|
1581
|
+
const double* gravityXyz,
|
|
1582
|
+
rundot::kinetix::physics::FluidForcesResult3D* output) {
|
|
1583
|
+
if (body == nullptr || volume == nullptr || gravityXyz == nullptr || output == nullptr) {
|
|
1584
|
+
return static_cast<std::int32_t>(rundot::kinetix::physics::BuoyancyStatus::InvalidInput);
|
|
1585
|
+
}
|
|
1586
|
+
rundot::kinetix::physics::BuoyancyVector3 gravity{gravityXyz[0], gravityXyz[1], gravityXyz[2]};
|
|
1587
|
+
return static_cast<std::int32_t>(rundot::kinetix::physics::calculateFluidForces(*body, *volume, gravity, *output));
|
|
1588
|
+
}
|