Files
Towards/modules/client/src/network/EntityPositionInterpolator.cpp
T

51 lines
1.8 KiB
C++
Raw Normal View History

2026-07-18 14:31:15 +02:00
#include "EntityPositionInterpolator.hpp"
#include "world/Transform.hpp"
#include "InterpolatedProperty.hpp"
#include "EntityInterpolation.hpp"
#include <spdlog/spdlog.h>
namespace tw::net {
EntityPositionInterpolator::EntityPositionInterpolator(
2026-08-03 23:30:18 +02:00
entt::registry* registry, size_t bufferingIntervalInMillis
2026-07-18 14:31:15 +02:00
) : m_registry(registry),
m_bufferingIntervalInMillis(bufferingIntervalInMillis)
2026-08-03 23:30:18 +02:00
{ }
2026-07-18 14:31:15 +02:00
void EntityPositionInterpolator::register_entity(entt::entity entity) {
m_registry->emplace<EntityPositionInterpolation>(entity, glm::vec3());
}
2026-08-03 23:30:18 +02:00
void EntityPositionInterpolator::set_position(Clock::time_point time_point, entt::entity entity, glm::vec3 position) {
2026-07-18 14:31:15 +02:00
auto* interpolation = m_registry->try_get<EntityPositionInterpolation>(entity);
if(interpolation == nullptr) {
spdlog::warn("Attempt to add position for non-registered entity {}", (uint32_t)entity);
return;
}
2026-08-05 15:20:56 +02:00
interpolation->push(time_point, position);
2026-07-18 14:31:15 +02:00
}
2026-08-03 23:30:18 +02:00
glm::vec3 EntityPositionInterpolator::get_position(Clock::time_point time_point, entt::entity entity) {
2026-07-18 14:31:15 +02:00
auto* interpolation = m_registry->try_get<EntityPositionInterpolation>(entity);
if(interpolation == nullptr) {
spdlog::warn("Attempt to get position for non-registered entity {}", (uint32_t)entity);
return glm::vec3();
}
2026-08-05 15:20:56 +02:00
auto [from, to, value] = interpolation->get_values_around(time_point);
2026-07-18 14:31:15 +02:00
return glm::mix(from, to, value);
}
2026-08-03 23:30:18 +02:00
void EntityPositionInterpolator::interpolate_smoothed_entities(Clock::time_point time_point) {
2026-07-18 14:31:15 +02:00
m_registry->view<EntityPositionInterpolation, Transform>()
.each([&](const auto entity, const EntityPositionInterpolation& interpolation, Transform& ts) {
2026-08-03 23:30:18 +02:00
auto [from, to, value] = interpolation.get_values_around(time_point);
2026-07-18 14:31:15 +02:00
ts.set_position(glm::mix(from, to, value));
});
}
}