From 3142c701e3c68553094c783e0d2b77ee4e3fc37c Mon Sep 17 00:00:00 2001 From: stiffly Date: Tue, 19 Jan 2016 11:42:30 +0100 Subject: [PATCH] WIP interpolating scale and orientation --- include/Game/InterpolationSystem.h | 13 +++++++++++-- src/Game/InterpolationSystem.cpp | 30 +++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/include/Game/InterpolationSystem.h b/include/Game/InterpolationSystem.h index c8e88834..73329094 100644 --- a/include/Game/InterpolationSystem.h +++ b/include/Game/InterpolationSystem.h @@ -5,6 +5,7 @@ #include #include #include +#include #include "Common.h" #include "Core/System.h" @@ -12,6 +13,7 @@ #include "Network/EInterpolate.h" +#define SNAPSHOTINTERVAL 0.05f class InterpolationSystem : public PureSystem { @@ -19,7 +21,7 @@ class InterpolationSystem : public PureSystem { glm::vec3 Position; glm::vec3 Scale; - glm::vec3 Orientation; + glm::quat Orientation; double interpolationTime; }; public: @@ -35,7 +37,14 @@ private: //std::unordered_map> m_InterpolationPoints; std::unordered_map m_InterpolationPoints; - glm::vec3 vectorInterpolation(glm::vec3 prev, glm::vec3 next, double currentTime); + //glm::vec3 vectorInterpolation(glm::vec3 prev, glm::vec3 next, double currentTime); + template + T vectorInterpolation(T prev, T next, double currentTime) + { + T difference = next - prev; + T vector = (difference / SNAPSHOTINTERVAL) * static_cast(currentTime); + return vector; + } EventRelay m_EInterpolate; bool InterpolationSystem::OnInterpolate(const Events::Interpolate& e); diff --git a/src/Game/InterpolationSystem.cpp b/src/Game/InterpolationSystem.cpp index 2d8de336..d3592132 100644 --- a/src/Game/InterpolationSystem.cpp +++ b/src/Game/InterpolationSystem.cpp @@ -24,17 +24,27 @@ void InterpolationSystem::UpdateComponent(World * world, ComponentWrapper & tran { Transform& sTransform = m_InterpolationPoints[transform.EntityID]; sTransform.interpolationTime += dt; + // Position glm::vec3 nextPosition = sTransform.Position; glm::vec3 currentPosition = static_cast(transform["Position"]); - (glm::vec3&)transform["Position"] += vectorInterpolation(currentPosition, nextPosition, sTransform.interpolationTime); + (glm::vec3&)transform["Position"] += vectorInterpolation(currentPosition, nextPosition, sTransform.interpolationTime); + // Orientation + glm::quat nextOrientation = sTransform.Orientation; + glm::quat currentOrientation = glm::quat(static_cast(transform["Orientation"])); + (glm::vec3&)transform["Orientation"] = glm::eulerAngles(glm::slerp(currentOrientation, nextOrientation, sTransform.interpolationTime / SNAPSHOTINTERVAL)); + // Scale + glm::vec3 nextScale = sTransform.Scale; + glm::vec3 currentScale = static_cast(transform["Scale"]); + //glm::vec3 resize = vectorInterpolation(currentScale, nextScale, sTransform.interpolationTime); + (glm::vec3&)transform["Scale"] += vectorInterpolation(currentScale, nextScale, sTransform.interpolationTime); } -glm::vec3 InterpolationSystem::vectorInterpolation(glm::vec3 prev, glm::vec3 next, double currentTime) -{ - glm::vec3 difference = next - prev; - glm::vec3 position = difference / 0.05f * static_cast(currentTime); - return position; -} +//glm::vec3 InterpolationSystem::vectorInterpolation(glm::vec3 prev, glm::vec3 next, double currentTime) +//{ +// glm::vec3 difference = next - prev; +// glm::vec3 position = difference / SNAPSHOTINTERVAL * static_cast(currentTime); +// return position; +//} bool InterpolationSystem::OnInterpolate(const Events::Interpolate & e) { @@ -43,9 +53,11 @@ bool InterpolationSystem::OnInterpolate(const Events::Interpolate & e) // Read the data memcpy(&transform.Position, e.DataArray.get() + offset, sizeof(glm::vec3)); offset += sizeof(glm::vec3); - memcpy(&transform.Orientation, e.DataArray.get() + offset, sizeof(glm::vec3)); + glm::vec3 tempOrientation; + memcpy(&tempOrientation, e.DataArray.get() + offset, sizeof(glm::vec3)); + transform.Orientation = glm::quat(tempOrientation); offset += sizeof(glm::vec3); - memcpy(&transform.Scale, e.DataArray.get() + offset, sizeof(glm::vec3)); + memcpy(&transform.Scale, e.DataArray.get() + offset, sizeof(glm::vec3)); transform.interpolationTime = 0.0f; m_InterpolationPoints[e.Entity] = transform; // Check if queue already exists