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 89ceb5a commit 1a00426
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 47 deletions.
26 changes: 12 additions & 14 deletions engine/src/fixedstep/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
#include <cubos/engine/fixedstep/plugin.hpp>
#include <cubos/engine/fixedstep/accumulated_time.hpp>

#include <cubos/engine/fixedstep/plugin.hpp>

using namespace cubos::engine;

const float frametime = 1/60;


const float frametime = 0.01F;

void cubos::engine::fixedStepPlugin(Cubos& cubos)

Check warning on line 8 in engine/src/fixedstep/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/fixedstep/plugin.cpp#L8

Added line #L8 was not covered by tests
{
cubos.addResource<AccumulatedTime>();

Check warning on line 10 in engine/src/fixedstep/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/fixedstep/plugin.cpp#L10

Added line #L10 was not covered by tests

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

Check warning on line 14 in engine/src/fixedstep/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/fixedstep/plugin.cpp#L12-L14

Added lines #L12 - L14 were not covered by tests

cubos.tag("cubos.fixedStep").repeatWhile([](AccumulatedTime& timer) {
if (timer.value >= frametime) {
timer.value -= frametime;
return true;
}
return false;
});
if (timer.value >= frametime)

Check warning on line 17 in engine/src/fixedstep/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/fixedstep/plugin.cpp#L16-L17

Added lines #L16 - L17 were not covered by tests
{
timer.value -= frametime;
return true;

Check warning on line 20 in engine/src/fixedstep/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/fixedstep/plugin.cpp#L19-L20

Added lines #L19 - L20 were not covered by tests
}
return false;

Check warning on line 22 in engine/src/fixedstep/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/fixedstep/plugin.cpp#L22

Added line #L22 was not covered by tests
});
}
15 changes: 0 additions & 15 deletions engine/src/physics/physics_accumulator.hpp

This file was deleted.

21 changes: 3 additions & 18 deletions engine/src/physics/plugin.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#include <glm/glm.hpp>

#include <cubos/engine/collisions/plugin.hpp>
#include <cubos/engine/fixedstep/plugin.hpp>
#include <cubos/engine/physics/plugin.hpp>
#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"

using namespace cubos::engine;

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


void cubos::engine::physicsPlugin(Cubos& cubos)
{
cubos.addResource<FixedDeltaTime>();
cubos.addResource<Substeps>();
cubos.addResource<PhysicsAccumulator>();
cubos.addResource<Damping>();

cubos.addComponent<Velocity>();
Expand Down Expand Up @@ -106,10 +102,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
}
});


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

Check warning on line 105 in engine/src/physics/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/physics/plugin.cpp#L105

Added line #L105 was not covered by tests

cubos.system("apply impulses")
.tagged("cubos.physics.simulation.apply_impulses")
Expand All @@ -125,7 +118,7 @@ void cubos::engine::physicsPlugin(Cubos& cubos)

cubos.system("integrate position")
.tagged("cubos.physics.simulation.substeps.integrate")
.after("cubos.physics.simulation.prepare")
.after("accumulator")
.tagged("cubos.fixedStep")

Check warning on line 122 in engine/src/physics/plugin.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/physics/plugin.cpp#L121-L122

Added lines #L121 - L122 were not covered by tests
.call([](Query<Position&, PreviousPosition&, Velocity&, const Force&, const Mass&> query,
const Damping& damping, const FixedDeltaTime& fixedDeltaTime, const Substeps& substeps) {
Expand Down Expand Up @@ -202,12 +195,4 @@ void cubos::engine::physicsPlugin(Cubos& cubos)
impulse.clear();
}
});

cubos.system("decrease fixed-step accumulator")
.tagged("cubos.physics.simulation.decrease_accumulator")
.after("cubos.physics.simulation.clear_forces")
.tagged("cubos.fixedStep")
.call([](PhysicsAccumulator& accumulator, const FixedDeltaTime& fixedDeltaTime) {
accumulator.value -= fixedDeltaTime.value;
});
}

0 comments on commit 1a00426

Please sign in to comment.