diff --git a/src/network/protocols/kart_update_protocol.cpp b/src/network/protocols/kart_update_protocol.cpp index c6bcb2c20..dce04265e 100644 --- a/src/network/protocols/kart_update_protocol.cpp +++ b/src/network/protocols/kart_update_protocol.cpp @@ -10,14 +10,6 @@ KartUpdateProtocol::KartUpdateProtocol() : Protocol(PROTOCOL_KART_UPDATE) { - // Allocate arrays to store one position and rotation for each kart - // (which is the update information from the server to the client). - m_next_positions.resize(World::getWorld()->getNumKarts()); - m_next_quaternions.resize(World::getWorld()->getNumKarts()); - - // This flag keeps track if valid data for an update is in - // the arrays - m_was_updated = false; } // KartUpdateProtocol // ---------------------------------------------------------------------------- @@ -28,6 +20,16 @@ KartUpdateProtocol::~KartUpdateProtocol() // ---------------------------------------------------------------------------- void KartUpdateProtocol::setup() { + // Allocate arrays to store one position and rotation for each kart + // (which is the update information from the server to the client). + m_next_positions.resize(World::getWorld()->getNumKarts()); + m_next_quaternions.resize(World::getWorld()->getNumKarts()); + + // This flag keeps track if valid data for an update is in + // the arrays + m_was_updated = false; + + m_previous_time = 0; } // setup // ---------------------------------------------------------------------------- @@ -73,11 +75,11 @@ void KartUpdateProtocol::update(float dt) { if (!World::getWorld()) return; - static double time = 0; + double current_time = StkTime::getRealTime(); - if (current_time > time + 0.1) // 10 updates per second + if (current_time > m_previous_time + 0.1) // 10 updates per second { - time = current_time; + m_previous_time = current_time; if (NetworkConfig::get()->isServer()) { World *world = World::getWorld(); diff --git a/src/network/protocols/kart_update_protocol.hpp b/src/network/protocols/kart_update_protocol.hpp index be5e144f4..04d83f488 100644 --- a/src/network/protocols/kart_update_protocol.hpp +++ b/src/network/protocols/kart_update_protocol.hpp @@ -25,6 +25,10 @@ private: /** True if a new update for the kart positions was received. */ bool m_was_updated; + /** Time the last kart update was sent. Used to send updates with + * a fixed frequency. */ + double m_previous_time; + public: KartUpdateProtocol(); virtual ~KartUpdateProtocol();