From 5f2644b52d69a4ce470b738f61cee36f733407b4 Mon Sep 17 00:00:00 2001 From: zhenyan121 <3367366583@qq.com> Date: Thu, 6 Aug 2026 11:06:29 +0800 Subject: [PATCH] fix(gameplay): make entity interpolation frame-rate independent --- include/Cubed/gameplay/client_entity_manager.hpp | 4 ++-- src/gameplay/client_entity_manager.cpp | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/include/Cubed/gameplay/client_entity_manager.hpp b/include/Cubed/gameplay/client_entity_manager.hpp index 0489291..6330f05 100644 --- a/include/Cubed/gameplay/client_entity_manager.hpp +++ b/include/Cubed/gameplay/client_entity_manager.hpp @@ -53,12 +53,12 @@ private: std::unordered_map m_factories; tbb::concurrent_queue m_tasks; - void handle_task(); + void handle_task(float dt); void handle_entity_destory(EntityID id); // not thread safe void handle_entity_create(EntityID id, std::string_view name, const glm::vec3& pos); - void handle_entity_update(UpdateInfo& info); + void handle_entity_update(UpdateInfo& info, float dt); template void create_entity_in_registry(EntityID id, Args&&... args) { { diff --git a/src/gameplay/client_entity_manager.cpp b/src/gameplay/client_entity_manager.cpp index 5543ff4..e1abd2b 100644 --- a/src/gameplay/client_entity_manager.cpp +++ b/src/gameplay/client_entity_manager.cpp @@ -14,7 +14,7 @@ using namespace google::protobuf; namespace Cubed { ClientEntityManager::ClientEntityManager(ClientWorld& world) : m_world(world) {} void ClientEntityManager::update(float dt) { - handle_task(); + handle_task(dt); auto view = m_registry.view(); for (auto e : view) { @@ -51,7 +51,7 @@ void ClientEntityManager::handle_entity_create(EntityID id, r->position.value = pos; } -void ClientEntityManager::handle_entity_update(UpdateInfo& info) { +void ClientEntityManager::handle_entity_update(UpdateInfo& info, float dt) { entt::entity e; { cacc a; @@ -68,10 +68,11 @@ void ClientEntityManager::handle_entity_update(UpdateInfo& info) { creature->pose.gait = info.gait; auto r = m_registry.try_get(e); ASSERT(r); - r->direction.value = - glm::mix(r->direction.value, creature->transform.direction.value, 0.15); + float alpha = 1 - std::exp(-dt / 0.1); + r->direction.value = glm::mix(r->direction.value, + creature->transform.direction.value, alpha); r->position.value = - glm::mix(r->position.value, creature->transform.position.value, 0.15); + glm::mix(r->position.value, creature->transform.position.value, alpha); } void ClientEntityManager::receive_entity_create(S2CEntityCreate& s2c) { @@ -113,7 +114,7 @@ void ClientEntityManager::create(std::string_view name, const glm::vec3& pos) { client->send(make_packet(msg)); } -void ClientEntityManager::handle_task() { +void ClientEntityManager::handle_task(float dt) { TaskPair pair; while (m_tasks.try_pop(pair)) { switch (pair.first) { @@ -131,7 +132,7 @@ void ClientEntityManager::handle_task() { case Command::UPDATE: { auto* p = std::get_if(&pair.second); ASSERT(p); - handle_entity_update(*p); + handle_entity_update(*p, dt); } } }