@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,248 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "physics2d.hpp"
|
|
4
|
+
#include "../deterministic_math.hpp"
|
|
5
|
+
|
|
6
|
+
#include <algorithm>
|
|
7
|
+
#include <array>
|
|
8
|
+
#include <cmath>
|
|
9
|
+
#include <cstddef>
|
|
10
|
+
#include <cstdint>
|
|
11
|
+
#include <cstring>
|
|
12
|
+
#include <type_traits>
|
|
13
|
+
|
|
14
|
+
namespace rundot::kinetix::physics2d {
|
|
15
|
+
|
|
16
|
+
constexpr std::uint32_t kCompoundAbiVersion = 1;
|
|
17
|
+
constexpr std::uint32_t kMaxCompoundChildren = 32;
|
|
18
|
+
constexpr double kDefaultCompoundDensity = 1.0;
|
|
19
|
+
constexpr double kCompoundEpsilon = 1e-12;
|
|
20
|
+
constexpr double kCompoundPi = 3.14159265358979323846;
|
|
21
|
+
|
|
22
|
+
struct alignas(8) Transform2D {
|
|
23
|
+
Vec2 position{0.0, 0.0};
|
|
24
|
+
double angle = 0.0;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
static_assert(std::is_standard_layout_v<Transform2D>);
|
|
28
|
+
static_assert(std::is_trivially_copyable_v<Transform2D>);
|
|
29
|
+
static_assert(sizeof(Transform2D) == 24);
|
|
30
|
+
|
|
31
|
+
struct alignas(8) Material2D {
|
|
32
|
+
double friction = 0.5;
|
|
33
|
+
double restitution = 0.0;
|
|
34
|
+
double density = 1.0;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
static_assert(std::is_standard_layout_v<Material2D>);
|
|
38
|
+
static_assert(std::is_trivially_copyable_v<Material2D>);
|
|
39
|
+
static_assert(sizeof(Material2D) == 24);
|
|
40
|
+
|
|
41
|
+
struct alignas(4) Filter2D {
|
|
42
|
+
std::uint32_t categoryBits = 0x0001;
|
|
43
|
+
std::uint32_t maskBits = 0xffff;
|
|
44
|
+
std::int32_t groupIndex = 0;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
static_assert(std::is_standard_layout_v<Filter2D>);
|
|
48
|
+
static_assert(std::is_trivially_copyable_v<Filter2D>);
|
|
49
|
+
static_assert(sizeof(Filter2D) == 12);
|
|
50
|
+
|
|
51
|
+
struct alignas(8) CompoundChild2D {
|
|
52
|
+
std::uint32_t id = 0;
|
|
53
|
+
ShapeType shape = ShapeType::Circle;
|
|
54
|
+
Transform2D localTransform{};
|
|
55
|
+
Material2D material{};
|
|
56
|
+
Filter2D filter{};
|
|
57
|
+
bool isSensor = false;
|
|
58
|
+
std::uint8_t padding[3]{};
|
|
59
|
+
double halfX = 0.5;
|
|
60
|
+
double halfY = 0.5;
|
|
61
|
+
double radius = 0.5;
|
|
62
|
+
double explicitMass = 0.0;
|
|
63
|
+
PolygonGeometry polygon{};
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
static_assert(std::is_standard_layout_v<CompoundChild2D>);
|
|
67
|
+
static_assert(std::is_trivially_copyable_v<CompoundChild2D>);
|
|
68
|
+
|
|
69
|
+
struct alignas(8) CompoundMassProperties2D {
|
|
70
|
+
double totalMass = 0.0;
|
|
71
|
+
double totalArea = 0.0;
|
|
72
|
+
Vec2 centerOfMass{0.0, 0.0};
|
|
73
|
+
double inertiaAboutCenterOfMass = 0.0;
|
|
74
|
+
double inertiaAboutOrigin = 0.0;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
static_assert(std::is_standard_layout_v<CompoundMassProperties2D>);
|
|
78
|
+
static_assert(std::is_trivially_copyable_v<CompoundMassProperties2D>);
|
|
79
|
+
static_assert(sizeof(CompoundMassProperties2D) == 48);
|
|
80
|
+
|
|
81
|
+
struct alignas(8) CompoundBody2D {
|
|
82
|
+
std::uint32_t id = 0;
|
|
83
|
+
BodyType type = BodyType::Dynamic;
|
|
84
|
+
Transform2D transform{};
|
|
85
|
+
Vec2 linearVelocity{0.0, 0.0};
|
|
86
|
+
double angularVelocity = 0.0;
|
|
87
|
+
Vec2 force{0.0, 0.0};
|
|
88
|
+
double torque = 0.0;
|
|
89
|
+
CompoundMassProperties2D massProperties{};
|
|
90
|
+
std::uint32_t childCount = 0;
|
|
91
|
+
std::uint32_t flags = 0;
|
|
92
|
+
CompoundChild2D children[kMaxCompoundChildren]{};
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
static_assert(std::is_standard_layout_v<CompoundBody2D>);
|
|
96
|
+
static_assert(std::is_trivially_copyable_v<CompoundBody2D>);
|
|
97
|
+
|
|
98
|
+
struct CompoundContactManifold2D {
|
|
99
|
+
std::uint32_t bodyIdA = 0;
|
|
100
|
+
std::uint32_t bodyIdB = 0;
|
|
101
|
+
std::uint32_t childIdA = 0;
|
|
102
|
+
std::uint32_t childIdB = 0;
|
|
103
|
+
std::uint32_t childIndexA = 0;
|
|
104
|
+
std::uint32_t childIndexB = 0;
|
|
105
|
+
bool isSensor = false;
|
|
106
|
+
double combinedFriction = 0.0;
|
|
107
|
+
double combinedRestitution = 0.0;
|
|
108
|
+
Manifold manifold{};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
static_assert(std::is_standard_layout_v<CompoundContactManifold2D>);
|
|
112
|
+
static_assert(std::is_trivially_copyable_v<CompoundContactManifold2D>);
|
|
113
|
+
|
|
114
|
+
struct alignas(8) CompoundRaycastHit2D {
|
|
115
|
+
std::uint32_t childId = 0;
|
|
116
|
+
std::uint32_t childIndex = 0;
|
|
117
|
+
double fraction = 0.0;
|
|
118
|
+
double distance = 0.0;
|
|
119
|
+
Vec2 point{0.0, 0.0};
|
|
120
|
+
Vec2 normal{0.0, 0.0};
|
|
121
|
+
bool isSensor = false;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
static_assert(std::is_standard_layout_v<CompoundRaycastHit2D>);
|
|
125
|
+
static_assert(std::is_trivially_copyable_v<CompoundRaycastHit2D>);
|
|
126
|
+
|
|
127
|
+
struct alignas(8) CompoundOverlapHit2D {
|
|
128
|
+
std::uint32_t childId = 0;
|
|
129
|
+
std::uint32_t childIndex = 0;
|
|
130
|
+
bool isSensor = false;
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
static_assert(std::is_standard_layout_v<CompoundOverlapHit2D>);
|
|
134
|
+
static_assert(std::is_trivially_copyable_v<CompoundOverlapHit2D>);
|
|
135
|
+
|
|
136
|
+
// Transform utilities
|
|
137
|
+
inline Vec2 rotateVec2(Vec2 v, double angle) {
|
|
138
|
+
const double c = rundot::detmath::det_cos(angle);
|
|
139
|
+
const double s = rundot::detmath::det_sin(angle);
|
|
140
|
+
return {v.x * c - v.y * s, v.x * s + v.y * c};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
inline Vec2 transformPoint(const Transform2D& t, Vec2 p) {
|
|
144
|
+
const Vec2 rotated = rotateVec2(p, t.angle);
|
|
145
|
+
return {t.position.x + rotated.x, t.position.y + rotated.y};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
inline Vec2 inverseTransformPoint(const Transform2D& t, Vec2 p) {
|
|
149
|
+
const Vec2 delta{p.x - t.position.x, p.y - t.position.y};
|
|
150
|
+
const double c = rundot::detmath::det_cos(-t.angle);
|
|
151
|
+
const double s = rundot::detmath::det_sin(-t.angle);
|
|
152
|
+
return {delta.x * c - delta.y * s, delta.x * s + delta.y * c};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
inline Transform2D multiplyTransforms(const Transform2D& parent, const Transform2D& child) {
|
|
156
|
+
Transform2D result{};
|
|
157
|
+
result.position = transformPoint(parent, child.position);
|
|
158
|
+
result.angle = parent.angle + child.angle;
|
|
159
|
+
return result;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
inline bool filterMatches(const Filter2D& a, const Filter2D& b) {
|
|
163
|
+
if (a.groupIndex != 0 && a.groupIndex == b.groupIndex) {
|
|
164
|
+
return a.groupIndex > 0;
|
|
165
|
+
}
|
|
166
|
+
return (a.maskBits & b.categoryBits) != 0 && (b.maskBits & a.categoryBits) != 0;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
inline double combineFriction(double frictionA, double frictionB) {
|
|
170
|
+
return std::sqrt(std::max(0.0, frictionA * frictionB));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
inline double combineRestitution(double restitutionA, double restitutionB) {
|
|
174
|
+
return std::min(restitutionA, restitutionB);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Core compound functions
|
|
178
|
+
bool computePrimitiveMass(
|
|
179
|
+
ShapeType shape,
|
|
180
|
+
double halfX,
|
|
181
|
+
double halfY,
|
|
182
|
+
double radius,
|
|
183
|
+
const PolygonGeometry* polygon,
|
|
184
|
+
double& outArea,
|
|
185
|
+
Vec2& outCentroid,
|
|
186
|
+
double& outUnitInertia);
|
|
187
|
+
|
|
188
|
+
bool computeCompoundMassProperties(
|
|
189
|
+
const CompoundChild2D* children,
|
|
190
|
+
std::uint32_t count,
|
|
191
|
+
double totalMassOverride,
|
|
192
|
+
CompoundMassProperties2D& outProps);
|
|
193
|
+
|
|
194
|
+
bool updateCompoundMass(CompoundBody2D& body, double totalMassOverride = 0.0);
|
|
195
|
+
|
|
196
|
+
void recenterCompoundChildren(CompoundChild2D* children, std::uint32_t count, Vec2 centerOfMass);
|
|
197
|
+
|
|
198
|
+
Transform2D computeChildWorldTransform(const Transform2D& bodyTransform, const Transform2D& childTransform);
|
|
199
|
+
|
|
200
|
+
bool computeChildAabb(const CompoundChild2D& child, const Transform2D& bodyTransform, BodyAabb2D& outAabb);
|
|
201
|
+
|
|
202
|
+
bool computeCompoundAabb(const CompoundBody2D& body, BodyAabb2D& outAabb);
|
|
203
|
+
|
|
204
|
+
std::uint32_t collideCompoundBodies(
|
|
205
|
+
const CompoundBody2D& bodyA,
|
|
206
|
+
const CompoundBody2D& bodyB,
|
|
207
|
+
CompoundContactManifold2D* outManifolds,
|
|
208
|
+
std::uint32_t maxManifolds);
|
|
209
|
+
|
|
210
|
+
std::uint32_t raycastCompound(
|
|
211
|
+
const CompoundBody2D& body,
|
|
212
|
+
double startX,
|
|
213
|
+
double startY,
|
|
214
|
+
double dirX,
|
|
215
|
+
double dirY,
|
|
216
|
+
double maxDist,
|
|
217
|
+
CompoundRaycastHit2D* outHits,
|
|
218
|
+
std::uint32_t maxHits);
|
|
219
|
+
|
|
220
|
+
std::uint32_t overlapCompound(
|
|
221
|
+
const CompoundBody2D& body,
|
|
222
|
+
ShapeType queryShape,
|
|
223
|
+
const Transform2D& queryTransform,
|
|
224
|
+
double queryHalfX,
|
|
225
|
+
double queryHalfY,
|
|
226
|
+
double queryRadius,
|
|
227
|
+
const PolygonGeometry* queryPolygon,
|
|
228
|
+
CompoundOverlapHit2D* outHits,
|
|
229
|
+
std::uint32_t maxHits);
|
|
230
|
+
|
|
231
|
+
} // namespace rundot::kinetix::physics2d
|
|
232
|
+
|
|
233
|
+
// C ABI exports for interop and verification
|
|
234
|
+
extern "C" {
|
|
235
|
+
std::uint32_t rundot_physics2d_compound_abi_version();
|
|
236
|
+
std::uint32_t rundot_physics2d_compound_child_size();
|
|
237
|
+
std::uint32_t rundot_physics2d_compound_body_size();
|
|
238
|
+
std::int32_t rundot_physics2d_compound_compute_mass(
|
|
239
|
+
const rundot::kinetix::physics2d::CompoundChild2D* children,
|
|
240
|
+
std::uint32_t childCount,
|
|
241
|
+
double totalMassOverride,
|
|
242
|
+
rundot::kinetix::physics2d::CompoundMassProperties2D* outProperties);
|
|
243
|
+
std::uint32_t rundot_physics2d_compound_collide(
|
|
244
|
+
const rundot::kinetix::physics2d::CompoundBody2D* bodyA,
|
|
245
|
+
const rundot::kinetix::physics2d::CompoundBody2D* bodyB,
|
|
246
|
+
rundot::kinetix::physics2d::CompoundContactManifold2D* outManifolds,
|
|
247
|
+
std::uint32_t maxManifolds);
|
|
248
|
+
}
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
#include "contour.hpp"
|
|
2
|
+
|
|
3
|
+
#include <cmath>
|
|
4
|
+
#include <algorithm>
|
|
5
|
+
|
|
6
|
+
namespace rundot::kinetix::physics2d {
|
|
7
|
+
|
|
8
|
+
namespace {
|
|
9
|
+
|
|
10
|
+
inline double deterministicSqrt(double v) noexcept {
|
|
11
|
+
return v <= 0.0 ? 0.0 : std::sqrt(v);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
inline double dot(const Vec2& a, const Vec2& b) noexcept {
|
|
15
|
+
return a.x * b.x + a.y * b.y;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
inline Vec2 add(const Vec2& a, const Vec2& b) noexcept {
|
|
19
|
+
return {a.x + b.x, a.y + b.y};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
inline Vec2 subtract(const Vec2& a, const Vec2& b) noexcept {
|
|
23
|
+
return {a.x - b.x, a.y - b.y};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
inline Vec2 scale(const Vec2& a, double s) noexcept {
|
|
27
|
+
return {a.x * s, a.y * s};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
inline double lengthSquared(const Vec2& v) noexcept {
|
|
31
|
+
return dot(v, v);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
inline double length(const Vec2& v) noexcept {
|
|
35
|
+
return deterministicSqrt(lengthSquared(v));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
inline double distanceSquared(const Vec2& a, const Vec2& b) noexcept {
|
|
39
|
+
return lengthSquared(subtract(a, b));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
inline Vec2 normalize(const Vec2& v, const Vec2& fallback = {0.0, 1.0}) noexcept {
|
|
43
|
+
const double len = length(v);
|
|
44
|
+
if (len <= 1e-12) return fallback;
|
|
45
|
+
return scale(v, 1.0 / len);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
} // namespace
|
|
49
|
+
|
|
50
|
+
ArcEndpoints arcEndpoints(const Vec2& center, double radius, ArcQuadrant quadrant) noexcept {
|
|
51
|
+
return arcEndpoints(center, radius, static_cast<std::uint32_t>(quadrant));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
ArcEndpoints arcEndpoints(const Vec2& center, double radius, std::uint32_t quadrant) noexcept {
|
|
55
|
+
ArcEndpoints res;
|
|
56
|
+
const double cx = center.x;
|
|
57
|
+
const double cy = center.y;
|
|
58
|
+
const double R = radius;
|
|
59
|
+
switch (quadrant) {
|
|
60
|
+
case 0: // NE
|
|
61
|
+
res.A = {cx + R, cy};
|
|
62
|
+
res.B = {cx, cy + R};
|
|
63
|
+
break;
|
|
64
|
+
case 1: // NW
|
|
65
|
+
res.A = {cx, cy + R};
|
|
66
|
+
res.B = {cx - R, cy};
|
|
67
|
+
break;
|
|
68
|
+
case 2: // SW
|
|
69
|
+
res.A = {cx - R, cy};
|
|
70
|
+
res.B = {cx, cy - R};
|
|
71
|
+
break;
|
|
72
|
+
case 3: // SE
|
|
73
|
+
default:
|
|
74
|
+
res.A = {cx, cy - R};
|
|
75
|
+
res.B = {cx + R, cy};
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
return res;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
bool inSector(const Vec2& d, ArcQuadrant quadrant) noexcept {
|
|
82
|
+
return inSector(d, static_cast<std::uint32_t>(quadrant));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
bool inSector(const Vec2& d, std::uint32_t quadrant) noexcept {
|
|
86
|
+
const double eps = 1e-9;
|
|
87
|
+
switch (quadrant) {
|
|
88
|
+
case 0: return d.x >= -eps && d.y >= -eps;
|
|
89
|
+
case 1: return d.x <= eps && d.y >= -eps;
|
|
90
|
+
case 2: return d.x <= eps && d.y <= eps;
|
|
91
|
+
case 3: return d.x >= -eps && d.y <= eps;
|
|
92
|
+
default: return false;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
Vec2 contourClosestPointOnSegment(const Vec2& p, const Vec2& a, const Vec2& b) noexcept {
|
|
97
|
+
const Vec2 ab = subtract(b, a);
|
|
98
|
+
const double lenSq = lengthSquared(ab);
|
|
99
|
+
if (lenSq <= 1e-12) return a;
|
|
100
|
+
const double t = std::clamp(dot(subtract(p, a), ab) / lenSq, 0.0, 1.0);
|
|
101
|
+
return add(a, scale(ab, t));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
Vec2 closestPointOnArc(
|
|
105
|
+
const Vec2& p,
|
|
106
|
+
const Vec2& center,
|
|
107
|
+
double radius,
|
|
108
|
+
std::uint32_t quadrant,
|
|
109
|
+
const Vec2& from,
|
|
110
|
+
const Vec2& to) noexcept {
|
|
111
|
+
const Vec2 d = subtract(p, center);
|
|
112
|
+
if (inSector(d, quadrant)) {
|
|
113
|
+
const double len = length(d);
|
|
114
|
+
if (len > 1e-12) {
|
|
115
|
+
return add(center, scale(d, radius / len));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const double distFromSq = distanceSquared(p, from);
|
|
119
|
+
const double distToSq = distanceSquared(p, to);
|
|
120
|
+
return distFromSq <= distToSq ? from : to;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
Vec2 closestPointOnEdge(const Vec2& p, const ContourEdge2D& edge) noexcept {
|
|
124
|
+
if (edge.kind == 0 || edge.kind == 2) {
|
|
125
|
+
return contourClosestPointOnSegment(p, edge.from, edge.to);
|
|
126
|
+
}
|
|
127
|
+
return closestPointOnArc(p, edge.center, edge.arcRadius, edge.quadrant, edge.from, edge.to);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
double segmentArcDistance(const Vec2& p1, const Vec2& p2, const ContourEdge2D& edge, double skin) noexcept {
|
|
132
|
+
const Vec2 cpFrom = contourClosestPointOnSegment(edge.from, p1, p2);
|
|
133
|
+
double minDist = length(subtract(cpFrom, edge.from));
|
|
134
|
+
|
|
135
|
+
const Vec2 cpTo = contourClosestPointOnSegment(edge.to, p1, p2);
|
|
136
|
+
const double distTo = length(subtract(cpTo, edge.to));
|
|
137
|
+
if (distTo < minDist) minDist = distTo;
|
|
138
|
+
|
|
139
|
+
const Vec2 cpCenter = contourClosestPointOnSegment(edge.center, p1, p2);
|
|
140
|
+
const Vec2 dCenter = subtract(cpCenter, edge.center);
|
|
141
|
+
if (inSector(dCenter, edge.quadrant)) {
|
|
142
|
+
const double d = std::abs(length(dCenter) - edge.arcRadius);
|
|
143
|
+
if (d < minDist) minDist = d;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const Vec2 dir = subtract(p2, p1);
|
|
147
|
+
const double L2 = lengthSquared(dir);
|
|
148
|
+
if (L2 > 1e-12) {
|
|
149
|
+
const Vec2 f = subtract(p1, edge.center);
|
|
150
|
+
const double a = L2;
|
|
151
|
+
const double b = 2.0 * dot(f, dir);
|
|
152
|
+
const double c = lengthSquared(f) - edge.arcRadius * edge.arcRadius;
|
|
153
|
+
const double disc = b * b - 4.0 * a * c;
|
|
154
|
+
if (disc >= 0.0) {
|
|
155
|
+
const double sqrtDisc = deterministicSqrt(disc);
|
|
156
|
+
const double t1 = (-b - sqrtDisc) / (2.0 * a);
|
|
157
|
+
const double t2 = (-b + sqrtDisc) / (2.0 * a);
|
|
158
|
+
if (t1 >= 0.0 && t1 <= 1.0) {
|
|
159
|
+
const Vec2 pt1 = add(p1, scale(dir, t1));
|
|
160
|
+
if (inSector(subtract(pt1, edge.center), edge.quadrant)) {
|
|
161
|
+
minDist = 0.0;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (minDist > 0.0 && t2 >= 0.0 && t2 <= 1.0) {
|
|
165
|
+
const Vec2 pt2 = add(p1, scale(dir, t2));
|
|
166
|
+
if (inSector(subtract(pt2, edge.center), edge.quadrant)) {
|
|
167
|
+
minDist = 0.0;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return minDist - skin;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
double edgeDistance(
|
|
177
|
+
const Vec2& center,
|
|
178
|
+
double radius,
|
|
179
|
+
const ContourEdge2D& edge,
|
|
180
|
+
double skin) noexcept {
|
|
181
|
+
const Vec2 cp = closestPointOnEdge(center, edge);
|
|
182
|
+
const double d = length(subtract(center, cp));
|
|
183
|
+
return d - radius - skin;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
bool rayEdge(
|
|
187
|
+
const Vec2& origin,
|
|
188
|
+
const Vec2& dir,
|
|
189
|
+
double maxT,
|
|
190
|
+
const ContourEdge2D& edge,
|
|
191
|
+
double skin,
|
|
192
|
+
double* hitT,
|
|
193
|
+
Vec2* hitNormal) noexcept {
|
|
194
|
+
if (edge.kind == 0) {
|
|
195
|
+
const Vec2 e = subtract(edge.to, edge.from);
|
|
196
|
+
const double L = length(e);
|
|
197
|
+
if (L <= 1e-12) return false;
|
|
198
|
+
const Vec2 t = scale(e, 1.0 / L);
|
|
199
|
+
const Vec2 n = {t.y, -t.x};
|
|
200
|
+
const double denom = dot(dir, n);
|
|
201
|
+
if (denom >= -1e-9) return false; // Must point toward edge
|
|
202
|
+
const double dist = dot(subtract(edge.from, origin), n) + skin;
|
|
203
|
+
const double t_hit = dist / denom;
|
|
204
|
+
if (t_hit < 0.0 || t_hit > maxT) return false;
|
|
205
|
+
const Vec2 hitP = add(origin, scale(dir, t_hit));
|
|
206
|
+
const double s = dot(subtract(hitP, edge.from), t);
|
|
207
|
+
if (s < -skin || s > L + skin) return false;
|
|
208
|
+
if (hitT != nullptr) *hitT = t_hit;
|
|
209
|
+
if (hitNormal != nullptr) *hitNormal = n;
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
if (edge.kind == 1) {
|
|
213
|
+
const bool isCcw = isArcCcw(edge);
|
|
214
|
+
const double effR = isCcw ? (edge.arcRadius + skin) : (edge.arcRadius - skin);
|
|
215
|
+
const Vec2 m = subtract(origin, edge.center);
|
|
216
|
+
const double b = dot(m, dir);
|
|
217
|
+
const double c = lengthSquared(m) - effR * effR;
|
|
218
|
+
const double disc = b * b - c;
|
|
219
|
+
if (disc < 0.0) return false;
|
|
220
|
+
const double sqrtDisc = deterministicSqrt(disc);
|
|
221
|
+
const double t1 = -b - sqrtDisc;
|
|
222
|
+
const double t2 = -b + sqrtDisc;
|
|
223
|
+
double hitT_val = -1.0;
|
|
224
|
+
Vec2 hitD{};
|
|
225
|
+
if (t1 >= 0.0 && t1 <= maxT) {
|
|
226
|
+
const Vec2 d1 = subtract(add(origin, scale(dir, t1)), edge.center);
|
|
227
|
+
if (inSector(d1, edge.quadrant)) {
|
|
228
|
+
hitT_val = t1;
|
|
229
|
+
hitD = d1;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (hitT_val < 0.0 && t2 >= 0.0 && t2 <= maxT) {
|
|
233
|
+
const Vec2 d2 = subtract(add(origin, scale(dir, t2)), edge.center);
|
|
234
|
+
if (inSector(d2, edge.quadrant)) {
|
|
235
|
+
hitT_val = t2;
|
|
236
|
+
hitD = d2;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
if (hitT_val < 0.0) return false;
|
|
240
|
+
if (hitT != nullptr) *hitT = hitT_val;
|
|
241
|
+
if (hitNormal != nullptr) {
|
|
242
|
+
const Vec2 rad = normalize(hitD, {1.0, 0.0});
|
|
243
|
+
*hitNormal = isCcw ? rad : scale(rad, -1.0);
|
|
244
|
+
}
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
int windingNumber(
|
|
251
|
+
const Vec2& p,
|
|
252
|
+
const ContourEdge2D* edges,
|
|
253
|
+
std::uint32_t edgeCount) noexcept {
|
|
254
|
+
int winding = 0;
|
|
255
|
+
for (std::uint32_t i = 0; i < edgeCount; ++i) {
|
|
256
|
+
const ContourEdge2D& edge = edges[i];
|
|
257
|
+
if (edge.kind == 2) continue; // skip ghost
|
|
258
|
+
const double minY = std::min(edge.from.y, edge.to.y);
|
|
259
|
+
const double maxY = std::max(edge.from.y, edge.to.y);
|
|
260
|
+
if (p.y < minY || p.y >= maxY) continue;
|
|
261
|
+
|
|
262
|
+
if (edge.kind == 0) { // Line
|
|
263
|
+
const double dy = edge.to.y - edge.from.y;
|
|
264
|
+
if (std::abs(dy) <= 1e-12) continue;
|
|
265
|
+
const double t = (p.y - edge.from.y) / dy;
|
|
266
|
+
const double x_int = edge.from.x + t * (edge.to.x - edge.from.x);
|
|
267
|
+
if (x_int > p.x) {
|
|
268
|
+
if (edge.to.y > edge.from.y) winding += 1;
|
|
269
|
+
else winding -= 1;
|
|
270
|
+
}
|
|
271
|
+
} else if (edge.kind == 1) { // Arc
|
|
272
|
+
const double dy = p.y - edge.center.y;
|
|
273
|
+
const double radicand = edge.arcRadius * edge.arcRadius - dy * dy;
|
|
274
|
+
if (radicand < 0.0) continue;
|
|
275
|
+
const double dx = deterministicSqrt(radicand);
|
|
276
|
+
const double x_int = (edge.quadrant == 0 || edge.quadrant == 3)
|
|
277
|
+
? edge.center.x + dx : edge.center.x - dx;
|
|
278
|
+
if (x_int > p.x) {
|
|
279
|
+
if (edge.to.y > edge.from.y) winding += 1;
|
|
280
|
+
else winding -= 1;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return winding;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
bool validateContourEdges(
|
|
288
|
+
const ContourHeader2D& header,
|
|
289
|
+
const ContourEdge2D* edges,
|
|
290
|
+
std::uint32_t edgeCount) noexcept {
|
|
291
|
+
if (edges == nullptr || edgeCount < header.edgeCount) {
|
|
292
|
+
return false;
|
|
293
|
+
}
|
|
294
|
+
return true;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
void computeEdgeVertexIndices(
|
|
298
|
+
const ContourHeader2D& header,
|
|
299
|
+
const ContourEdge2D* edges,
|
|
300
|
+
EdgeVertexIndices* indices) noexcept {
|
|
301
|
+
std::uint32_t currentGlobalVertex = 0;
|
|
302
|
+
std::uint32_t edgeIdx = 0;
|
|
303
|
+
for (std::uint32_t loopIdx = 0; loopIdx < header.loopCount; ++loopIdx) {
|
|
304
|
+
const std::uint32_t expectedLoopNum = loopIdx + 1;
|
|
305
|
+
const std::uint32_t loopStartEdge = edgeIdx;
|
|
306
|
+
const std::uint32_t totalEdges = header.reserved > 0 ? header.reserved : header.edgeCount;
|
|
307
|
+
while (edgeIdx < totalEdges && edges[edgeIdx].loopIndex == expectedLoopNum) {
|
|
308
|
+
if (edgeIdx >= kMaxContourTotalEdges) break;
|
|
309
|
+
edgeIdx++;
|
|
310
|
+
}
|
|
311
|
+
const std::uint32_t k = edgeIdx - loopStartEdge;
|
|
312
|
+
if (k == 0) continue;
|
|
313
|
+
const bool closed = (edges[loopStartEdge].flags & 1) != 0;
|
|
314
|
+
const std::uint32_t loopStartVertex = currentGlobalVertex;
|
|
315
|
+
for (std::uint32_t j = 0; j < k; ++j) {
|
|
316
|
+
const std::uint32_t e = loopStartEdge + j;
|
|
317
|
+
if (e >= kMaxContourTotalEdges) break;
|
|
318
|
+
indices[e].startVertex = loopStartVertex + j;
|
|
319
|
+
indices[e].endVertex = (closed && j == k - 1) ? loopStartVertex : (loopStartVertex + j + 1);
|
|
320
|
+
}
|
|
321
|
+
currentGlobalVertex += closed ? k : (k + 1);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
} // namespace rundot::kinetix::physics2d
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#include "physics2d.hpp"
|
|
4
|
+
|
|
5
|
+
#include <cmath>
|
|
6
|
+
#include <cstdint>
|
|
7
|
+
#include <algorithm>
|
|
8
|
+
|
|
9
|
+
namespace rundot::kinetix::physics2d {
|
|
10
|
+
|
|
11
|
+
enum class ArcQuadrant : std::uint32_t {
|
|
12
|
+
NE = 0,
|
|
13
|
+
NW = 1,
|
|
14
|
+
SW = 2,
|
|
15
|
+
SE = 3,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
enum class ContourEdgeKind : std::uint32_t {
|
|
19
|
+
Line = 0,
|
|
20
|
+
Arc = 1,
|
|
21
|
+
Ghost = 2,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
struct ArcEndpoints {
|
|
25
|
+
Vec2 A{};
|
|
26
|
+
Vec2 B{};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
// Pure deterministic geometry kernel functions (D3)
|
|
30
|
+
ArcEndpoints arcEndpoints(const Vec2& center, double radius, ArcQuadrant quadrant) noexcept;
|
|
31
|
+
ArcEndpoints arcEndpoints(const Vec2& center, double radius, std::uint32_t quadrant) noexcept;
|
|
32
|
+
|
|
33
|
+
inline bool isArcCcw(const ContourEdge2D& edge) noexcept {
|
|
34
|
+
const ArcEndpoints ends = arcEndpoints(edge.center, edge.arcRadius, edge.quadrant);
|
|
35
|
+
return std::abs(edge.from.x - ends.A.x) <= 1e-9 && std::abs(edge.from.y - ends.A.y) <= 1e-9;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
bool inSector(const Vec2& d, ArcQuadrant quadrant) noexcept;
|
|
39
|
+
bool inSector(const Vec2& d, std::uint32_t quadrant) noexcept;
|
|
40
|
+
|
|
41
|
+
Vec2 contourClosestPointOnSegment(const Vec2& p, const Vec2& a, const Vec2& b) noexcept;
|
|
42
|
+
|
|
43
|
+
Vec2 closestPointOnArc(
|
|
44
|
+
const Vec2& p,
|
|
45
|
+
const Vec2& center,
|
|
46
|
+
double radius,
|
|
47
|
+
std::uint32_t quadrant,
|
|
48
|
+
const Vec2& from,
|
|
49
|
+
const Vec2& to) noexcept;
|
|
50
|
+
|
|
51
|
+
Vec2 closestPointOnEdge(const Vec2& p, const ContourEdge2D& edge) noexcept;
|
|
52
|
+
|
|
53
|
+
double segmentArcDistance(const Vec2& p1, const Vec2& p2, const ContourEdge2D& edge, double skin) noexcept;
|
|
54
|
+
|
|
55
|
+
double edgeDistance(
|
|
56
|
+
const Vec2& center,
|
|
57
|
+
double radius,
|
|
58
|
+
const ContourEdge2D& edge,
|
|
59
|
+
double skin) noexcept;
|
|
60
|
+
|
|
61
|
+
bool rayEdge(
|
|
62
|
+
const Vec2& origin,
|
|
63
|
+
const Vec2& dir,
|
|
64
|
+
double maxT,
|
|
65
|
+
const ContourEdge2D& edge,
|
|
66
|
+
double skin,
|
|
67
|
+
double* hitT,
|
|
68
|
+
Vec2* hitNormal) noexcept;
|
|
69
|
+
|
|
70
|
+
int windingNumber(
|
|
71
|
+
const Vec2& p,
|
|
72
|
+
const ContourEdge2D* edges,
|
|
73
|
+
std::uint32_t edgeCount) noexcept;
|
|
74
|
+
|
|
75
|
+
inline bool validateContourHeader(
|
|
76
|
+
const ContourHeader2D& header,
|
|
77
|
+
std::uint32_t maxWorldEdges) noexcept {
|
|
78
|
+
if (header.tag != kContourHeaderTag) return false;
|
|
79
|
+
if (header.edgeCount < 1 || header.edgeCount > kMaxContourEdges) return false;
|
|
80
|
+
if (header.loopCount < 1 || header.loopCount > kMaxContourLoops) return false;
|
|
81
|
+
if (header.fill > 1) return false;
|
|
82
|
+
if (!std::isfinite(header.skin) || header.skin < 0.0) return false;
|
|
83
|
+
if (header.firstEdge > maxWorldEdges) return false;
|
|
84
|
+
if (header.edgeCount > maxWorldEdges - header.firstEdge) return false;
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
bool validateContourEdges(
|
|
89
|
+
const ContourHeader2D& header,
|
|
90
|
+
const ContourEdge2D* edges,
|
|
91
|
+
std::uint32_t edgeCount) noexcept;
|
|
92
|
+
|
|
93
|
+
// Narrowphase and contact candidate definitions (D4)
|
|
94
|
+
struct ContourCandidate {
|
|
95
|
+
Vec2 point{};
|
|
96
|
+
Vec2 supportPoint{};
|
|
97
|
+
double supportRadius = 0.0;
|
|
98
|
+
Vec2 normal{}; // free-side surface normal s (pointing away from solid terrain)
|
|
99
|
+
double depth = 0.0;
|
|
100
|
+
std::uint32_t featureId = 0;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
struct EdgeVertexIndices {
|
|
104
|
+
std::uint32_t startVertex = 0;
|
|
105
|
+
std::uint32_t endVertex = 0;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
void computeEdgeVertexIndices(
|
|
109
|
+
const ContourHeader2D& header,
|
|
110
|
+
const ContourEdge2D* edges,
|
|
111
|
+
EdgeVertexIndices* indices) noexcept;
|
|
112
|
+
|
|
113
|
+
const ContourHeader2D* contourHeaderFor(const PhysicsContext2D& context, const Body& body) noexcept;
|
|
114
|
+
const ContourEdge2D* contourEdgesFor(const PhysicsContext2D& context, const ContourHeader2D& header) noexcept;
|
|
115
|
+
|
|
116
|
+
bool onFreeSideContour(
|
|
117
|
+
const PhysicsContext2D& context,
|
|
118
|
+
const Body& shapeBody,
|
|
119
|
+
const ContourEdge2D& edge) noexcept;
|
|
120
|
+
|
|
121
|
+
bool onFreeSideContour(
|
|
122
|
+
const Body& shapeBody,
|
|
123
|
+
const ContourEdge2D& edge) noexcept;
|
|
124
|
+
|
|
125
|
+
Manifold reduceContourCandidates(
|
|
126
|
+
const ContourCandidate* candidates,
|
|
127
|
+
std::uint32_t count,
|
|
128
|
+
const ContourEdge2D* edges,
|
|
129
|
+
double skin);
|
|
130
|
+
|
|
131
|
+
Manifold computeSolidContourMTV(
|
|
132
|
+
const PhysicsContext2D& context,
|
|
133
|
+
const Body& shapeBody,
|
|
134
|
+
const Body& contourBody,
|
|
135
|
+
const ContourHeader2D& header,
|
|
136
|
+
const ContourEdge2D* edges);
|
|
137
|
+
|
|
138
|
+
Manifold collideShapeWithContour(
|
|
139
|
+
const PhysicsContext2D& context,
|
|
140
|
+
const Body& shapeBody,
|
|
141
|
+
const Body& contourBody);
|
|
142
|
+
|
|
143
|
+
} // namespace rundot::kinetix::physics2d
|