Skip to content

Commit

Permalink
feat(engine): add fixedStep plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
joaomanita committed Feb 21, 2024
1 parent cfa6005 commit 89ceb5a
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 18 deletions.
2 changes: 2 additions & 0 deletions engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ set(CUBOS_ENGINE_SOURCE

"src/screen_picker/plugin.cpp"
"src/screen_picker/screen_picker.cpp"

"src/fixedstep/plugin.cpp"
)

# Create cubos engine
Expand Down
16 changes: 16 additions & 0 deletions engine/include/cubos/engine/fixedstep/accumulated_time.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// @file
/// @brief Resource @ref cubos::engine::AccumulatedTime.
/// @ingroup fixedstep-plugin

#pragma once

namespace cubos::engine
{
/// @brief Resource which holds the time passed without running fixedStep systems, incremented every frame
/// by deltaTime and decremented once the systems run.
/// @ingroup fixedstep-plugin
struct AccumulatedTime
{
float value = 0.0F;
};
} // namespace cubos::engine
23 changes: 23 additions & 0 deletions engine/include/cubos/engine/fixedstep/plugin.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// @dir
/// @brief @ref fixedstep-plugin plugin directory.

/// @file
/// @brief Plugin entry point.
/// @ingroup fixedstep-plugin

#pragma once

#include <cubos/engine/prelude.hpp>

namespace cubos::engine
{
/// @defgroup fixedstep-plugin FixedStep
/// @ingroup engine
/// @brief Adds cubos.fixedStep tag which makes its systems run at a fixed framerate.
///

/// @brief Plugin entry function.
/// @param cubos @b CUBOS. main class
/// @ingroup fixedstep-plugin
void fixedStepPlugin(Cubos& cubos);
} // namespace cubos::engine
26 changes: 26 additions & 0 deletions engine/src/fixedstep/plugin.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <cubos/engine/fixedstep/plugin.hpp>
#include <cubos/engine/fixedstep/accumulated_time.hpp>


using namespace cubos::engine;

const float frametime = 1/60;



void cubos::engine::fixedStepPlugin(Cubos& cubos)
{
cubos.addResource<AccumulatedTime>();

cubos.system("accumulate time resource").call([](AccumulatedTime& timer, const DeltaTime& dt) {
timer.value += dt.value;
});

cubos.tag("cubos.fixedStep").repeatWhile([](AccumulatedTime& timer) {
if (timer.value >= frametime) {
timer.value -= frametime;
return true;
}
return false;
});
}
28 changes: 10 additions & 18 deletions engine/src/physics/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <cubos/engine/physics/solver/solver.hpp>
#include <cubos/engine/settings/plugin.hpp>
#include <cubos/engine/transform/plugin.hpp>
#include <cubos/engine/fixedstep/plugin.hpp>

#include "physics_accumulator.hpp"

Expand Down Expand Up @@ -61,10 +62,6 @@ CUBOS_REFLECT_IMPL(PhysicsBundle)
.build();
}

static bool simulatePhysicsStep(PhysicsAccumulator& accumulator, const FixedDeltaTime& fixedDeltaTime)
{
return accumulator.value >= fixedDeltaTime.value;
}

void cubos::engine::physicsPlugin(Cubos& cubos)
{
Expand All @@ -84,6 +81,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
cubos.addPlugin(collisionsPlugin);
cubos.addPlugin(gravityPlugin);
cubos.addPlugin(solverPlugin);
cubos.addPlugin(fixedStepPlugin);

// add components to entities created with PhysicsBundle
cubos.system("unpack PhysicsBundle's")
Expand All @@ -108,22 +106,16 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
}
});

// executed every frame
cubos.system("increase fixed-step accumulator")
.tagged("cubos.physics.simulation.prepare")
.call(
[](PhysicsAccumulator& accumulator, const DeltaTime& deltaTime) { accumulator.value += deltaTime.value; });

cubos.tag("cubos.physics.apply_forces")
.after("cubos.physics.simulation.prepare")
.before("cubos.physics.simulation.substeps.integrate")
.runIf(simulatePhysicsStep);
.before("cubos.physics.simulation.substeps.integrate");

cubos.system("apply impulses")
.tagged("cubos.physics.simulation.apply_impulses")
.after("cubos.physics.apply_forces")
.before("cubos.physics.simulation.substeps.integrate")
.onlyIf(simulatePhysicsStep)
.tagged("cubos.fixedStep")
.call([](Query<Velocity&, const Impulse&, const Mass&> query) {
for (auto [velocity, impulse, mass] : query)
{
Expand All @@ -134,7 +126,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
cubos.system("integrate position")
.tagged("cubos.physics.simulation.substeps.integrate")
.after("cubos.physics.simulation.prepare")
.onlyIf(simulatePhysicsStep)
.tagged("cubos.fixedStep")
.call([](Query<Position&, PreviousPosition&, Velocity&, const Force&, const Mass&> query,
const Damping& damping, const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps) {
float subDeltaTime = fixedDeltaTime.value / (float)substeps.value;
Expand Down Expand Up @@ -162,7 +154,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
cubos.system("apply corrections to positions")
.tagged("cubos.physics.simulation.substeps.correct_position")
.after("cubos.physics.simulation.substeps.integrate")
.onlyIf(simulatePhysicsStep)
.tagged("cubos.fixedStep")
.call([](Query<Position&, AccumulatedCorrection&, Mass&> query) {
for (auto [position, correction, mass] : query)
{
Expand All @@ -178,7 +170,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
cubos.system("update velocities")
.tagged("cubos.physics.simulation.substeps.update_velocity")
.after("cubos.physics.simulation.substeps.correct_position")
.onlyIf(simulatePhysicsStep)
.tagged("cubos.fixedStep")
.call([](Query<const Position&, const PreviousPosition&, Velocity&> query, const FixedDeltaTime& fixedDeltaTime,
const Substeps& substeps) {
float subDeltaTime = fixedDeltaTime.value / (float)substeps.value;
Expand All @@ -192,7 +184,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
cubos.system("clear forces")
.tagged("cubos.physics.simulation.clear_forces")
.after("cubos.physics.simulation.substeps.update_velocity")
.onlyIf(simulatePhysicsStep)
.tagged("cubos.fixedStep")
.call([](Query<Force&> query) {
for (auto [force] : query)
{
Expand All @@ -203,7 +195,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
cubos.system("clear impulses")
.tagged("cubos.physics.simulation.clear_forces")
.after("cubos.physics.simulation.substeps.update_velocity")
.onlyIf(simulatePhysicsStep)
.tagged("cubos.fixedStep")
.call([](Query<Impulse&> query) {
for (auto [impulse] : query)
{
Expand All @@ -214,7 +206,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
cubos.system("decrease fixed-step accumulator")
.tagged("cubos.physics.simulation.decrease_accumulator")
.after("cubos.physics.simulation.clear_forces")
.onlyIf(simulatePhysicsStep)
.tagged("cubos.fixedStep")
.call([](PhysicsAccumulator& accumulator, const FixedDeltaTime& fixedDeltaTime) {
accumulator.value -= fixedDeltaTime.value;
});
Expand Down

0 comments on commit 89ceb5a

Please sign in to comment.