-
Notifications
You must be signed in to change notification settings - Fork 42
feat(physics): Interpolation #1563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /// @dir | ||
| /// @brief @ref interpolation-plugin plugin directory. | ||
|
|
||
| /// @file | ||
| /// @brief Plugin entry point. | ||
| /// @ingroup interpolation-plugin | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <glm/glm.hpp> | ||
| #include <glm/gtx/quaternion.hpp> | ||
|
|
||
| #include <cubos/core/reflection/reflect.hpp> | ||
| #include <cubos/core/reflection/type.hpp> | ||
|
|
||
| #include <cubos/engine/api.hpp> | ||
| #include <cubos/engine/prelude.hpp> | ||
|
|
||
| namespace cubos::engine | ||
| { | ||
|
|
||
| /// @brief Systems with this tag run once if this is fixed step update. | ||
| CUBOS_ENGINE_API extern Tag interpolationResetTag; | ||
|
|
||
| /// @brief Component which holds the the previous and next frames position, rotation and scale for interpolation. | ||
| /// @ingroup interpolation-plugin | ||
| struct CUBOS_ENGINE_API Interpolated | ||
| { | ||
| CUBOS_REFLECT; | ||
|
|
||
| glm::vec3 currentPosition; ///< Currently interpolated frame position. | ||
| // glm::quat currentRotation; ///< Currently interpolated frame rotation. | ||
| float currentScale; ///< Currently interpolated frame scale factor. | ||
|
|
||
| glm::vec3 previousPosition; ///< Previous frame position. | ||
| // glm::quat previousRotation; ///< Previous frame rotation. | ||
| float previousScale; ///< Previous frame scale factor. | ||
|
|
||
| glm::vec3 nextPosition; ///< Next frame position. | ||
| // glm::quat nextRotation; ///< Next frame rotation. | ||
| float nextScale; ///< Next frame scale factor. | ||
| }; | ||
|
|
||
| /// @ingroup interpolation-plugin | ||
| /// @brief Interpolates position, rotation and scale of entities with the @ref Interpolated component in between | ||
| /// fixed updates. | ||
|
|
||
| /// @brief Plugin entry function. | ||
| /// @param cubos @b Cubos main class | ||
| /// @ingroup interpolation-plugin | ||
| void interpolationPlugin(Cubos& cubos); | ||
| } // namespace cubos::engine | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,120 @@ | ||||||||||||||
| #include <glm/glm.hpp> | ||||||||||||||
|
|
||||||||||||||
| #include <cubos/core/ecs/reflection.hpp> | ||||||||||||||
| #include <cubos/core/reflection/external/glm.hpp> | ||||||||||||||
| #include <cubos/core/reflection/type.hpp> | ||||||||||||||
|
|
||||||||||||||
| #include <cubos/engine/fixed_step/plugin.hpp> | ||||||||||||||
| #include <cubos/engine/interpolation/plugin.hpp> | ||||||||||||||
| #include <cubos/engine/physics/plugin.hpp> | ||||||||||||||
| #include <cubos/engine/physics/solver/plugin.hpp> | ||||||||||||||
| #include <cubos/engine/transform/plugin.hpp> | ||||||||||||||
|
|
||||||||||||||
| using namespace cubos::engine; | ||||||||||||||
|
|
||||||||||||||
| CUBOS_DEFINE_TAG(cubos::engine::interpolationResetTag); | ||||||||||||||
|
|
||||||||||||||
| CUBOS_REFLECT_IMPL(cubos::engine::Interpolated) | ||||||||||||||
| { | ||||||||||||||
| return cubos::core::ecs::TypeBuilder<Interpolated>("cubos::engine::Interpolated").build(); | ||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason to not include reflection for the struct's fields? |
||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| void cubos::engine::interpolationPlugin(Cubos& cubos) | ||||||||||||||
| { | ||||||||||||||
| cubos.depends(fixedStepPlugin); | ||||||||||||||
| cubos.depends(transformPlugin); | ||||||||||||||
| cubos.depends(physicsPlugin); | ||||||||||||||
| cubos.depends(physicsSolverPlugin); | ||||||||||||||
|
|
||||||||||||||
| cubos.component<Interpolated>(); | ||||||||||||||
|
|
||||||||||||||
| cubos.tag(interpolationResetTag).runIf([](FixedAccumulatedTime& timer, const FixedDeltaTime& step) { | ||||||||||||||
| if (timer.value >= step.value) | ||||||||||||||
| { | ||||||||||||||
| return true; | ||||||||||||||
| } | ||||||||||||||
| return false; | ||||||||||||||
|
Comment on lines
+32
to
+36
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| cubos.observer("set up Interpolated") | ||||||||||||||
| .onAdd<Interpolated>() | ||||||||||||||
| .call([](Query<Entity, Interpolated&, const Position&, const Rotation&, const Scale&> query) { | ||||||||||||||
| for (auto [ent, interp, position, rotation, scale] : query) | ||||||||||||||
| { | ||||||||||||||
| interp.previousPosition = position.vec; | ||||||||||||||
| // interp.previousRotation = rotation.quat; | ||||||||||||||
| interp.previousScale = scale.factor; | ||||||||||||||
| interp.nextPosition = position.vec; | ||||||||||||||
| // interp.nextRotation = rotation.quat; | ||||||||||||||
| interp.nextScale = scale.factor; | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| cubos.system("set position to real value before physics update") | ||||||||||||||
| .tagged(interpolationResetTag) | ||||||||||||||
| .before(transformUpdateTag) | ||||||||||||||
| .call([](Query<Position&, const Rotation&, const Scale&, const Interpolated&> query) { | ||||||||||||||
| for (auto [position, rotation, scale, interpolated] : query) | ||||||||||||||
| { | ||||||||||||||
| position.vec = interpolated.nextPosition; | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| cubos.system("prepare before update") | ||||||||||||||
| .tagged(fixedStepTag) | ||||||||||||||
| .before(physicsFinalizePositionTag) | ||||||||||||||
| .call([](Query<const Position&, const Rotation&, const Scale&, Interpolated&> query) { | ||||||||||||||
| for (auto [position, rotation, scale, interpolated] : query) | ||||||||||||||
| { | ||||||||||||||
| interpolated.previousPosition = interpolated.nextPosition; | ||||||||||||||
| // interpolated.previousRotation = interpolated.nextRotation; | ||||||||||||||
| interpolated.previousScale = interpolated.nextScale; | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| cubos.system("do interpolation on Interpolated entities") | ||||||||||||||
| .after(fixedStepTag) | ||||||||||||||
| .call([](const FixedAccumulatedTime& acc, const DeltaTime& dt, const FixedDeltaTime& fdt, | ||||||||||||||
| Query<Position&, Rotation&, Scale&, Interpolated&> query) { | ||||||||||||||
| float alpha = (acc.value + dt.value()) / fdt.value; | ||||||||||||||
|
|
||||||||||||||
| for (auto [position, rotation, scale, interpolated] : query) | ||||||||||||||
| { | ||||||||||||||
|
|
||||||||||||||
| if (position.vec == interpolated.currentPosition) | ||||||||||||||
| { | ||||||||||||||
| position.vec = glm::mix(interpolated.previousPosition, interpolated.nextPosition, alpha); | ||||||||||||||
| } | ||||||||||||||
| else | ||||||||||||||
| { | ||||||||||||||
| interpolated.previousPosition = position.vec; | ||||||||||||||
| interpolated.nextPosition = position.vec; | ||||||||||||||
| } | ||||||||||||||
| interpolated.currentPosition = position.vec; | ||||||||||||||
|
|
||||||||||||||
| /* | ||||||||||||||
| if (rotation.quat == interpolated.currentRotation) | ||||||||||||||
| { | ||||||||||||||
| rotation.quat = glm::slerp(interpolated.previousRotation, interpolated.nextRotation, alpha); | ||||||||||||||
| } | ||||||||||||||
| else | ||||||||||||||
| { | ||||||||||||||
| interpolated.previousRotation = rotation.quat; | ||||||||||||||
| interpolated.nextRotation = rotation.quat; | ||||||||||||||
| } | ||||||||||||||
| interpolated.currentRotation = rotation.quat; | ||||||||||||||
| */ | ||||||||||||||
|
|
||||||||||||||
| if (scale.factor == interpolated.nextScale) | ||||||||||||||
| { | ||||||||||||||
| scale.factor = glm::mix(interpolated.previousScale, interpolated.nextScale, alpha); | ||||||||||||||
| } | ||||||||||||||
| else | ||||||||||||||
| { | ||||||||||||||
| interpolated.previousScale = scale.factor; | ||||||||||||||
| interpolated.nextScale = scale.factor; | ||||||||||||||
| } | ||||||||||||||
| interpolated.currentScale = scale.factor; | ||||||||||||||
| } | ||||||||||||||
| }); | ||||||||||||||
| } | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these comments meant to be here this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What could be different? That there is no rotation interpolation yet?