From 3996c91c5503357396dfad6b69b75956be455d99 Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Sun, 1 Dec 2024 11:32:53 +0000 Subject: [PATCH] Fix 195 --- include/System/Physics/sPhysics.h | 60 +++++++++++++++++++++++++++---- src/System/Physics/sPhysics.cpp | 19 +++++++--- 2 files changed, 68 insertions(+), 11 deletions(-) diff --git a/include/System/Physics/sPhysics.h b/include/System/Physics/sPhysics.h index d9a3b408..0aefda4e 100644 --- a/include/System/Physics/sPhysics.h +++ b/include/System/Physics/sPhysics.h @@ -28,14 +28,19 @@ namespace Hop::System::Physics using Hop::System::Physics::sCollision; using Hop::World::AbstractWorld; - /* - System to update cPhysics components given forces - */ + /** + * @brief Physics system. + * + */ class sPhysics : public System { public: + /** + * @brief Construct a default physics system. + * + */ sPhysics() : dt(1.0/900.0), dtdt(dt*dt), @@ -45,13 +50,27 @@ namespace Hop::System::Physics subSamples(1) {} + /** + * @brief Update objects + * + * @param m object manager. + * @param collisions optional collision system. + * @param world optional world. + */ void step ( EntityComponentSystem * m, - sCollision * collisions, - AbstractWorld * world + sCollision * collisions = nullptr, + AbstractWorld * world = nullptr ); + /** + * @brief Set the strength and direction of gravity. + * + * @param g magnitude. + * @param nx direction in x. + * @param ny direction in y. + */ void setGravityForce ( double g, @@ -64,6 +83,15 @@ namespace Hop::System::Physics ngy = ny; } + /** + * @brief Apply a force to object i. + * + * @param m manager. + * @param i object to apply force to. + * @param fx force in x. + * @param fy force in y. + * @param global apply to all mesh components equally. + */ void applyForce( EntityComponentSystem * m, Id & i, @@ -72,12 +100,27 @@ namespace Hop::System::Physics bool global = false ); + /** + * @brief Apply a torque to object i. + * + * @param m manager. + * @param i object to apply torque to. + * @param tau torque. + */ void applyTorque( EntityComponentSystem * m, Id & i, double tau ); + /** + * @brief Apply a force to all objects. + * + * @param m manager. + * @param fx force in x. + * @param fy force in y. + * @param global apply to each objects mesh globally. + */ void applyForce( EntityComponentSystem * m, double fx, @@ -85,8 +128,11 @@ namespace Hop::System::Physics bool global = false ); - // automatically compute stable simulation parameters - // updating all objects + /** + * @brief Determine parameters for stable forces (resp. collisions). + * + * @param m + */ void stabaliseObjectParameters(Hop::Object::EntityComponentSystem * m); int lua_setTimeStep(lua_State * lua) diff --git a/src/System/Physics/sPhysics.cpp b/src/System/Physics/sPhysics.cpp index fffe49e8..c626cfca 100644 --- a/src/System/Physics/sPhysics.cpp +++ b/src/System/Physics/sPhysics.cpp @@ -13,12 +13,23 @@ namespace Hop::System::Physics AbstractWorld * world ) { - for (unsigned k = 0 ; k < subSamples; k++) + if (collisions != nullptr && world != nullptr) { + for (unsigned k = 0 ; k < subSamples; k++) + { - collisions->update(m, world); - gravityForce(m); - update(m); + collisions->update(m, world); + gravityForce(m); + update(m); + } + } + else + { + for (unsigned k = 0 ; k < subSamples; k++) + { + gravityForce(m); + update(m); + } } }