From 9846ad289a616557799a9c208e4c7463b896af9d Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 26 Oct 2026 19:30:53 +0300 Subject: [PATCH 1/2] feat: entity rotation interpolation --- src/animation/rigging.cpp | 20 ++++---- src/animation/rigging.hpp | 1 + .../scripting/lua/libs/lib__skeleton.cpp | 4 +- .../scripting/lua/libs/lib__transform.cpp | 6 ++- src/objects/Entities.cpp | 3 ++ src/objects/Entity.cpp | 11 ++++- src/objects/Entity.hpp | 3 ++ src/objects/Transform.cpp | 1 + src/util/Interpolation.hpp | 48 +++++++++++++++++++ 9 files changed, 82 insertions(+), 15 deletions(-) diff --git a/src/animation/rigging.cpp b/src/animation/rigging.cpp index 18cfa770e..ee1448255 100644 --- a/src/animation/rigging.cpp +++ b/src/animation/rigging.cpp @@ -160,17 +160,17 @@ void SkeletonConfig::update( const glm::vec3& position, const glm::vec3& scale ) const { - if (skeleton.interpolation.isEnabled()) { - const auto& interpolation = skeleton.interpolation; - update( - 0, - skeleton, - root.get(), - build_matrix(rotation, interpolation.getCurrent(), scale) - ); - } else { - update(0, skeleton, root.get(), build_matrix(rotation, position, scale)); + glm::vec3 pos = position; + const auto& interpolation = skeleton.interpolation; + if (interpolation.isEnabled() && interpolation.isRefreshed()) { + pos = interpolation.getCurrent(); } + glm::mat3 rot = rotation; + const auto& rotationInterpolation = skeleton.rotationInterpolation; + if (rotationInterpolation.isEnabled() && rotationInterpolation.isRefreshed()) { + rot = glm::mat3_cast(rotationInterpolation.getCurrent()); + } + update(0, skeleton, root.get(), build_matrix(rot, pos, scale)); } void SkeletonConfig::render( diff --git a/src/animation/rigging.hpp b/src/animation/rigging.hpp index e1653542a..2423f5c25 100644 --- a/src/animation/rigging.hpp +++ b/src/animation/rigging.hpp @@ -101,6 +101,7 @@ namespace rigging { std::vector boneTints; util::VecInterpolation<3, float> interpolation {false}; + util::RotationInterpolation rotationInterpolation {false}; Skeleton(std::shared_ptr config); diff --git a/src/logic/scripting/lua/libs/lib__skeleton.cpp b/src/logic/scripting/lua/libs/lib__skeleton.cpp index b3edc32e7..2c78d1a8a 100644 --- a/src/logic/scripting/lua/libs/lib__skeleton.cpp +++ b/src/logic/scripting/lua/libs/lib__skeleton.cpp @@ -201,7 +201,9 @@ static int l_is_interpolated(lua::State* L) { static int l_set_interpolated(lua::State* L) { if (auto skeleton = get_skeleton(L)) { - skeleton->interpolation.setEnabled(lua::toboolean(L, 2)); + bool enabled = lua::toboolean(L, 2); + skeleton->interpolation.setEnabled(enabled); + skeleton->rotationInterpolation.setEnabled(enabled); } return 0; } diff --git a/src/logic/scripting/lua/libs/lib__transform.cpp b/src/logic/scripting/lua/libs/lib__transform.cpp index 16662daaa..dbe2d8c75 100644 --- a/src/logic/scripting/lua/libs/lib__transform.cpp +++ b/src/logic/scripting/lua/libs/lib__transform.cpp @@ -61,9 +61,11 @@ static int l_get_rot(lua::State* L) { static int l_set_rot(lua::State* L) { if (auto entity = get_entity(L, 1)) { - auto matrix = lua::tomat4(L, 2); + glm::mat3 matrix(lua::tomat4(L, 2)); check_valid(matrix); - entity->getTransform().setRot(matrix); + auto rotation = glm::normalize(glm::quat_cast(matrix)); + entity->getTransform().setRot(glm::mat3_cast(rotation)); + entity->setInterpolatedRotation(rotation); } return 0; } diff --git a/src/objects/Entities.cpp b/src/objects/Entities.cpp index 27d4866a5..f1a058b14 100644 --- a/src/objects/Entities.cpp +++ b/src/objects/Entities.cpp @@ -403,6 +403,9 @@ void Entities::update(float delta) { if (skeleton.interpolation.isEnabled()) { skeleton.interpolation.updateTimer(delta); } + if (skeleton.rotationInterpolation.isEnabled()) { + skeleton.rotationInterpolation.updateTimer(delta); + } } } diff --git a/src/objects/Entity.cpp b/src/objects/Entity.cpp index 697c6618f..807e79bd4 100644 --- a/src/objects/Entity.cpp +++ b/src/objects/Entity.cpp @@ -20,13 +20,20 @@ void Entity::setInterpolatedPosition(const glm::vec3& position) { glm::vec3 Entity::getInterpolatedPosition() const { if (auto skeleton = getSkeleton()) { - if (skeleton->interpolation.isEnabled()) { - return skeleton->interpolation.getCurrent(); + const auto& interpolation = skeleton->interpolation; + if (interpolation.isEnabled() && interpolation.isRefreshed()) { + return interpolation.getCurrent(); } } return getTransform().pos; } +void Entity::setInterpolatedRotation(const glm::quat& rotation) { + if (auto skeleton = getSkeleton()) { + skeleton->rotationInterpolation.refresh(rotation); + } +} + void Entity::destroy() { if (isValid()) { entities.despawn(id); diff --git a/src/objects/Entity.hpp b/src/objects/Entity.hpp index 960bdfc9a..424c2e22b 100644 --- a/src/objects/Entity.hpp +++ b/src/objects/Entity.hpp @@ -6,6 +6,7 @@ #include #include #include +#include class Entities; struct EntityDef; @@ -72,6 +73,8 @@ public: glm::vec3 getInterpolatedPosition() const; + void setInterpolatedRotation(const glm::quat& rotation); + entt::entity getHandler() const { return entity; } diff --git a/src/objects/Transform.cpp b/src/objects/Transform.cpp index da876b81e..c398c48e7 100644 --- a/src/objects/Transform.cpp +++ b/src/objects/Transform.cpp @@ -5,6 +5,7 @@ #include "maths/util.hpp" #include +#include static debug::Logger logger("entity-transform"); diff --git a/src/util/Interpolation.hpp b/src/util/Interpolation.hpp index 757bb6f4d..e24c13c3a 100644 --- a/src/util/Interpolation.hpp +++ b/src/util/Interpolation.hpp @@ -1,7 +1,9 @@ #pragma once +#include #include #include +#include const float INTERPOLATION_DURATION = 0.15f; // seconds @@ -9,6 +11,7 @@ namespace util { template class VecInterpolation { bool enabled; + bool refreshed = false; glm::vec prevPos {std::numeric_limits::quiet_NaN()}; glm::vec nextPos {}; mutable glm::vec interpolationStep {}; @@ -37,6 +40,7 @@ namespace util { } else { interpolationStep = (nextPos - prevPos) / currentDuration.x; } + refreshed = true; timer = 0.0; } @@ -57,6 +61,50 @@ namespace util { return interpolated; } + bool isRefreshed() const { + return refreshed; + } + + bool isEnabled() const { + return enabled; + } + + void setEnabled(bool enabled) { + this->enabled = enabled; + } + }; + + class RotationInterpolation { + bool enabled; + bool refreshed = false; + glm::quat prevRot {1.0f, 0.0f, 0.0f, 0.0f}; + glm::quat nextRot {1.0f, 0.0f, 0.0f, 0.0f}; + float timer = 0.0f; + public: + RotationInterpolation(bool enabled) : enabled(enabled) {} + + void refresh(const glm::quat& rotation) { + prevRot = refreshed ? getCurrent() : rotation; + nextRot = rotation; + refreshed = true; + timer = 0.0f; + } + + void updateTimer(float delta) { + timer += delta; + } + + glm::quat getCurrent() const { + if (timer >= INTERPOLATION_DURATION) { + return nextRot; + } + return glm::slerp(prevRot, nextRot, timer / INTERPOLATION_DURATION); + } + + bool isRefreshed() const { + return refreshed; + } + bool isEnabled() const { return enabled; } From 59047f3c6aeb81905303b24b5647783dddc7bd5c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 26 Oct 2026 19:31:47 +0300 Subject: [PATCH 2/2] fix entity.transform size animation --- res/modules/internal/animation_codegen.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/res/modules/internal/animation_codegen.lua b/res/modules/internal/animation_codegen.lua index dd0094b48..35e535a0b 100644 --- a/res/modules/internal/animation_codegen.lua +++ b/res/modules/internal/animation_codegen.lua @@ -118,8 +118,8 @@ local env = { if target.set_rot then target:set_rot(rot) end - if target.set_scale then - target:set_scale(scale) + if target.set_size then + target:set_size(scale) end end end,