From 1346bc1eb20cc6fdb456c1d1c2d155bb442f7ac8 Mon Sep 17 00:00:00 2001 From: fallenatlas Date: Tue, 13 Aug 2024 23:53:45 +0100 Subject: [PATCH] fix(physics): add missing components to complex physics sample and add more general physicsPrepareTag --- .../include/cubos/engine/physics/plugin.hpp | 1 + .../assets/scenes/red_cube.cubos | 35 +++++++++++++------ .../assets/scenes/white_cube.cubos | 35 +++++++++++++------ engine/src/physics/plugin.cpp | 13 ++++++- engine/src/physics/solver/plugin.cpp | 2 +- 5 files changed, 64 insertions(+), 22 deletions(-) diff --git a/engine/include/cubos/engine/physics/plugin.hpp b/engine/include/cubos/engine/physics/plugin.hpp index 54126b0e9..93441882f 100644 --- a/engine/include/cubos/engine/physics/plugin.hpp +++ b/engine/include/cubos/engine/physics/plugin.hpp @@ -57,6 +57,7 @@ namespace cubos::engine /// @brief Tag which should be used on all systems that modify velocity or apply forces or impulses. CUBOS_ENGINE_API extern Tag physicsApplyForcesTag; + extern Tag physicsPrepareTag; /// @brief Plugin entry function. /// @param cubos @b Cubos main class diff --git a/engine/samples/complex_physics/assets/scenes/red_cube.cubos b/engine/samples/complex_physics/assets/scenes/red_cube.cubos index 58b5a3968..9716dea41 100644 --- a/engine/samples/complex_physics/assets/scenes/red_cube.cubos +++ b/engine/samples/complex_physics/assets/scenes/red_cube.cubos @@ -7,9 +7,12 @@ "z": 0.0 }, "cubos::engine::BoxCollisionShape": { - "x": 0.5, - "y": 0.5, - "z": 0.5 + "box" : { + "x": 0.5, + "y": 0.5, + "z": 0.5 + }, + "changed" : true }, "cubos::engine::Collider": { "a": { @@ -38,18 +41,25 @@ } }, "cubos::engine::Force": { - "x": 0.0, - "y": 0.0, - "z": 0.0 + }, + "cubos::engine::Torque": { }, "cubos::engine::Impulse": { - "x": 0.0, - "y": 0.0, - "z": 0.0 + }, + "cubos::engine::AngularImpulse": { }, "cubos::engine::Mass": { "inverseMass": 0.02, - "mass": 50.0 + "mass": 50.0, + "changed": true + }, + "cubos::engine::Inertia": { + "autoUpdate": true + }, + "cubos::engine::CenterOfMass": { + "x": 0.0, + "y": 0.0, + "z": 0.0 }, "cubos::engine::PhysicsMaterial": { "friction": 0.02, @@ -81,6 +91,11 @@ "x": 0.0, "y": 0.0, "z": 0.0 + }, + "cubos::engine::AngularVelocity": { + "x": 0.0, + "y": 0.0, + "z": 0.0 } } }, diff --git a/engine/samples/complex_physics/assets/scenes/white_cube.cubos b/engine/samples/complex_physics/assets/scenes/white_cube.cubos index 3922efd6c..741b449b7 100644 --- a/engine/samples/complex_physics/assets/scenes/white_cube.cubos +++ b/engine/samples/complex_physics/assets/scenes/white_cube.cubos @@ -7,9 +7,12 @@ "z": 0.0 }, "cubos::engine::BoxCollisionShape": { - "x": 0.5, - "y": 0.5, - "z": 0.5 + "box" : { + "x": 0.5, + "y": 0.5, + "z": 0.5 + }, + "changed" : true }, "cubos::engine::Collider": { "a": { @@ -38,18 +41,25 @@ } }, "cubos::engine::Force": { - "x": 0.0, - "y": 0.0, - "z": 0.0 + }, + "cubos::engine::Torque": { }, "cubos::engine::Impulse": { - "x": 0.0, - "y": 0.0, - "z": 0.0 + }, + "cubos::engine::AngularImpulse": { }, "cubos::engine::Mass": { "inverseMass": 0.1, - "mass": 10.0 + "mass": 10.0, + "changed": true + }, + "cubos::engine::Inertia": { + "autoUpdate": true + }, + "cubos::engine::CenterOfMass": { + "x": 0.0, + "y": 0.0, + "z": 0.0 }, "cubos::engine::PhysicsMaterial": { "friction": 0.02, @@ -81,6 +91,11 @@ "x": 0.0, "y": 0.0, "z": 0.0 + }, + "cubos::engine::AngularVelocity": { + "x": 0.0, + "y": 0.0, + "z": 0.0 } } }, diff --git a/engine/src/physics/plugin.cpp b/engine/src/physics/plugin.cpp index ce895056e..ed4ba67da 100644 --- a/engine/src/physics/plugin.cpp +++ b/engine/src/physics/plugin.cpp @@ -5,7 +5,9 @@ #include #include +#include #include +#include #include #include @@ -41,6 +43,7 @@ CUBOS_REFLECT_IMPL(Inertia) return cubos::core::ecs::TypeBuilder("cubos::engine::Inertia") .withField("inertia", &Inertia::inertia) .withField("inverseInertia", &Inertia::inverseInertia) + .withField("autoUpdate", &Inertia::autoUpdate) .build(); } @@ -118,6 +121,8 @@ CUBOS_REFLECT_IMPL(Damping) return core::ecs::TypeBuilder("cubos::engine::Damping").build(); } +CUBOS_DEFINE_TAG(cubos::engine::physicsPrepareTag); + // Compute Inertia Tensor for box shape static glm::mat3 boxInertiaTensor(float mass, glm::vec3 dimensions) { @@ -132,8 +137,13 @@ static glm::mat3 boxInertiaTensor(float mass, glm::vec3 dimensions) void cubos::engine::physicsPlugin(Cubos& cubos) { + cubos.depends(fixedStepPlugin); + cubos.depends(collisionsPlugin); + cubos.plugin(physicsFixedSubstepPlugin); + cubos.tag(physicsPrepareTag).after(collisionsTag).tagged(fixedStepTag); + cubos.resource(); cubos.component(); @@ -144,6 +154,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos) cubos.component(); cubos.component(); cubos.component(); + cubos.component(); cubos.component(); cubos.component(); cubos.component(); @@ -187,7 +198,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos) // Should this be here? cubos.system("update inertia - box collider") - .tagged(physicsPrepareSolveTag) + .tagged(physicsPrepareTag) .call([](Query query) { for (auto [mass, shape, inertia] : query) { diff --git a/engine/src/physics/solver/plugin.cpp b/engine/src/physics/solver/plugin.cpp index d63d63f1a..015ce9313 100644 --- a/engine/src/physics/solver/plugin.cpp +++ b/engine/src/physics/solver/plugin.cpp @@ -29,7 +29,7 @@ void cubos::engine::physicsSolverPlugin(Cubos& cubos) // doesn't need to be after collisions but for now it should stay in case we do velocity based prediction for // collisions in the future - cubos.tag(physicsPrepareSolveTag).after(collisionsTag).tagged(fixedStepTag); + cubos.tag(physicsPrepareSolveTag).tagged(physicsPrepareTag); cubos.tag(physicsIntegrateVelocityTag) .after(physicsPrepareSolveTag)