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; }