fix(gameplay): make entity interpolation frame-rate independent

This commit is contained in:
2026-08-06 11:06:29 +08:00
parent d6051486a8
commit 5f2644b52d
2 changed files with 10 additions and 9 deletions

View File

@@ -53,12 +53,12 @@ private:
std::unordered_map<std::string_view, CreateFunc> m_factories; std::unordered_map<std::string_view, CreateFunc> m_factories;
tbb::concurrent_queue<TaskPair> m_tasks; tbb::concurrent_queue<TaskPair> m_tasks;
void handle_task(); void handle_task(float dt);
void handle_entity_destory(EntityID id); void handle_entity_destory(EntityID id);
// not thread safe // not thread safe
void handle_entity_create(EntityID id, std::string_view name, void handle_entity_create(EntityID id, std::string_view name,
const glm::vec3& pos); const glm::vec3& pos);
void handle_entity_update(UpdateInfo& info); void handle_entity_update(UpdateInfo& info, float dt);
template <typename... Args> template <typename... Args>
void create_entity_in_registry(EntityID id, Args&&... args) { void create_entity_in_registry(EntityID id, Args&&... args) {
{ {

View File

@@ -14,7 +14,7 @@ using namespace google::protobuf;
namespace Cubed { namespace Cubed {
ClientEntityManager::ClientEntityManager(ClientWorld& world) : m_world(world) {} ClientEntityManager::ClientEntityManager(ClientWorld& world) : m_world(world) {}
void ClientEntityManager::update(float dt) { void ClientEntityManager::update(float dt) {
handle_task(); handle_task(dt);
auto view = m_registry.view<BaseClientCreature>(); auto view = m_registry.view<BaseClientCreature>();
for (auto e : view) { for (auto e : view) {
@@ -51,7 +51,7 @@ void ClientEntityManager::handle_entity_create(EntityID id,
r->position.value = pos; r->position.value = pos;
} }
void ClientEntityManager::handle_entity_update(UpdateInfo& info) { void ClientEntityManager::handle_entity_update(UpdateInfo& info, float dt) {
entt::entity e; entt::entity e;
{ {
cacc a; cacc a;
@@ -68,10 +68,11 @@ void ClientEntityManager::handle_entity_update(UpdateInfo& info) {
creature->pose.gait = info.gait; creature->pose.gait = info.gait;
auto r = m_registry.try_get<RenderTransform>(e); auto r = m_registry.try_get<RenderTransform>(e);
ASSERT(r); ASSERT(r);
r->direction.value = float alpha = 1 - std::exp(-dt / 0.1);
glm::mix(r->direction.value, creature->transform.direction.value, 0.15); r->direction.value = glm::mix(r->direction.value,
creature->transform.direction.value, alpha);
r->position.value = 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) { 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)); client->send(make_packet(msg));
} }
void ClientEntityManager::handle_task() { void ClientEntityManager::handle_task(float dt) {
TaskPair pair; TaskPair pair;
while (m_tasks.try_pop(pair)) { while (m_tasks.try_pop(pair)) {
switch (pair.first) { switch (pair.first) {
@@ -131,7 +132,7 @@ void ClientEntityManager::handle_task() {
case Command::UPDATE: { case Command::UPDATE: {
auto* p = std::get_if<UpdateInfo>(&pair.second); auto* p = std::get_if<UpdateInfo>(&pair.second);
ASSERT(p); ASSERT(p);
handle_entity_update(*p); handle_entity_update(*p, dt);
} }
} }
} }