From 8798b34423e4cbb456933ec2806ba0f7f0657e21 Mon Sep 17 00:00:00 2001 From: Andrey Tazetdinov Date: Wed, 21 Oct 2026 11:28:33 +0200 Subject: [PATCH] move to namespace --- src/physics/BroadPhase.cpp | 4 +-- src/physics/PhysicsSolver.cpp | 14 ++++++---- src/physics/collisions.cpp | 4 ++- src/physics/collisions.hpp | 22 ++++++++------- src/physics/scheduling.cpp | 4 +-- src/physics/scheduling.hpp | 52 ++++++++++++++++++++--------------- src/physics/sensors.cpp | 4 ++- src/physics/sensors.hpp | 4 ++- 8 files changed, 63 insertions(+), 45 deletions(-) diff --git a/src/physics/BroadPhase.cpp b/src/physics/BroadPhase.cpp index 4b321a8de..521b12804 100644 --- a/src/physics/BroadPhase.cpp +++ b/src/physics/BroadPhase.cpp @@ -24,8 +24,8 @@ static glm::vec3 calc_nearby_reach( glm::vec3 reach( 0.5f, - hitbox.stepHeight * 2.0f + E * 8.0f + ext.y * 0.5f + - maxSolidHalfY + 0.5f + MAX_FIX * 2.0f, + hitbox.stepHeight * 2.0f + physics::E * 8.0f + ext.y * 0.5f + + maxSolidHalfY + 0.5f + physics::MAX_FIX * 2.0f, 0.5f ); reach += ext + budget; diff --git a/src/physics/PhysicsSolver.cpp b/src/physics/PhysicsSolver.cpp index 5bb937aeb..8ee956088 100644 --- a/src/physics/PhysicsSolver.cpp +++ b/src/physics/PhysicsSolver.cpp @@ -30,7 +30,7 @@ void PhysicsSolver::step( float dt = delta / static_cast(substeps); broadPhase.build(delta); for (auto hitbox : hitboxes) { - schedule_substep(*hitbox, gravity, 0, substeps, dt); + physics::schedule_substep(*hitbox, gravity, 0, substeps, dt); } for (uint i = 0; i < substeps; i++) { @@ -44,12 +44,14 @@ void PhysicsSolver::step( dt * static_cast(i + 1 - hitbox->doneSubsteps); while (hitbox->doneSubsteps <= i) { uint left = i + 1 - hitbox->doneSubsteps; - uint skip = calc_substep_stride(*hitbox, gravity, dt, left) - 1; + uint skip = + physics::calc_substep_stride(*hitbox, gravity, dt, left) - + 1; if (skip > 0) { - integrate(*hitbox, gravity, dt, skip); + physics::integrate(*hitbox, gravity, dt, skip); hitbox->doneSubsteps += skip; } - calc_substep( + physics::calc_substep( *hitbox, {this->chunks, broadPhase.getNearbySolids(j), @@ -61,7 +63,7 @@ void PhysicsSolver::step( broadPhase.update(j, dt * static_cast(substeps - i)); } - schedule_substep(*hitbox, gravity, i + 1, substeps, dt); + physics::schedule_substep(*hitbox, gravity, i + 1, substeps, dt); } } @@ -84,7 +86,7 @@ void PhysicsSolver::step( hitbox->groundVelocity *= 1.0f - delta; } - update_sensors(*hitbox, sensors); + physics::update_sensors(*hitbox, sensors); } } diff --git a/src/physics/collisions.cpp b/src/physics/collisions.cpp index ae41a1b8e..bac65c9e5 100644 --- a/src/physics/collisions.cpp +++ b/src/physics/collisions.cpp @@ -7,6 +7,8 @@ #define GLM_ENABLE_EXPERIMENTAL #include +using namespace physics; + static glm::vec3 calc_collsion_velocity_result( const Hitbox& a, const Hitbox& b ) { @@ -316,7 +318,7 @@ static void calc_collisions( } } -void calc_substep(Hitbox& hitbox, const CollisionContext& ctx) { +void physics::calc_substep(Hitbox& hitbox, const CollisionContext& ctx) { const auto& chunks = ctx.chunks; const auto& solids = ctx.solids; const auto& gravity = ctx.gravity; diff --git a/src/physics/collisions.hpp b/src/physics/collisions.hpp index 50970cf15..9cbee38af 100644 --- a/src/physics/collisions.hpp +++ b/src/physics/collisions.hpp @@ -9,15 +9,17 @@ class GlobalChunks; -inline constexpr float E = 0.03f; -inline constexpr float MAX_FIX = 0.1f; +namespace physics { + inline constexpr float E = 0.03f; + inline constexpr float MAX_FIX = 0.1f; -struct CollisionContext { - const GlobalChunks& chunks; - const std::vector& solids; - glm::vec3 gravity; - float dt; - uint wakeSubstep; -}; + struct CollisionContext { + const GlobalChunks& chunks; + const std::vector& solids; + glm::vec3 gravity; + float dt; + uint wakeSubstep; + }; -void calc_substep(Hitbox& hitbox, const CollisionContext& ctx); + void calc_substep(Hitbox& hitbox, const CollisionContext& ctx); +} diff --git a/src/physics/scheduling.cpp b/src/physics/scheduling.cpp index 833b837ea..4d6400f73 100644 --- a/src/physics/scheduling.cpp +++ b/src/physics/scheduling.cpp @@ -3,7 +3,7 @@ inline constexpr float MAX_SUBSTEP_MOVE = 0.03f; inline constexpr float MAX_SUBSTEP_SINK = 0.015f; -uint calc_substep_stride( +uint physics::calc_substep_stride( const Hitbox& hitbox, const glm::vec3& gravity, float dt, uint left ) { if (hitbox.crouching) { @@ -28,7 +28,7 @@ uint calc_substep_stride( return glm::max(1u, static_cast(stride)); } -void schedule_substep( +void physics::schedule_substep( Hitbox& hitbox, const glm::vec3& gravity, uint done, uint substeps, float dt ) { hitbox.doneSubsteps = done; diff --git a/src/physics/scheduling.hpp b/src/physics/scheduling.hpp index a2aa215a5..1caf69ac5 100644 --- a/src/physics/scheduling.hpp +++ b/src/physics/scheduling.hpp @@ -7,27 +7,35 @@ #include #include -inline void integrate( - Hitbox& box, const glm::vec3& gravity, float dt, uint substeps -) { - float n = static_cast(substeps); - auto accel = gravity * box.gravityScale; - box.position += box.velocity * (dt * n) + - accel * (dt * dt * n * (n + 2.0f) * 0.5f); - box.velocity += accel * (dt * n); -} - -inline void push_body(Hitbox& box, int axis, float value, uint wakeSubstep) { - if (box.velocity[axis] != value) { - box.velocity[axis] = value; - box.nextSubstep = std::min(box.nextSubstep, wakeSubstep); +namespace physics { + inline void integrate( + Hitbox& box, const glm::vec3& gravity, float dt, uint substeps + ) { + float n = static_cast(substeps); + auto accel = gravity * box.gravityScale; + box.position += box.velocity * (dt * n) + + accel * (dt * dt * n * (n + 2.0f) * 0.5f); + box.velocity += accel * (dt * n); } + + inline void push_body( + Hitbox& box, int axis, float value, uint wakeSubstep + ) { + if (box.velocity[axis] != value) { + box.velocity[axis] = value; + box.nextSubstep = std::min(box.nextSubstep, wakeSubstep); + } + } + + uint calc_substep_stride( + const Hitbox& hitbox, const glm::vec3& gravity, float dt, uint left + ); + + void schedule_substep( + Hitbox& hitbox, + const glm::vec3& gravity, + uint done, + uint substeps, + float dt + ); } - -uint calc_substep_stride( - const Hitbox& hitbox, const glm::vec3& gravity, float dt, uint left -); - -void schedule_substep( - Hitbox& hitbox, const glm::vec3& gravity, uint done, uint substeps, float dt -); diff --git a/src/physics/sensors.cpp b/src/physics/sensors.cpp index cf2a0ba84..641d1c09b 100644 --- a/src/physics/sensors.cpp +++ b/src/physics/sensors.cpp @@ -1,6 +1,8 @@ #include "sensors.hpp" -void update_sensors(Hitbox& hitbox, const std::vector& sensors) { +void physics::update_sensors( + Hitbox& hitbox, const std::vector& sensors +) { auto aabb = hitbox.getAABB(); for (size_t i = 0; i < sensors.size(); i++) { diff --git a/src/physics/sensors.hpp b/src/physics/sensors.hpp index 510aa1adb..c78eae81c 100644 --- a/src/physics/sensors.hpp +++ b/src/physics/sensors.hpp @@ -4,4 +4,6 @@ #include -void update_sensors(Hitbox& hitbox, const std::vector& sensors); +namespace physics { + void update_sensors(Hitbox& hitbox, const std::vector& sensors); +}