From d8edde34da5b872a37edbcea468c48bb452cb09b Mon Sep 17 00:00:00 2001 From: Benau Date: Tue, 26 Mar 2019 02:44:52 +0800 Subject: [PATCH] Add document for compressed network values --- src/items/attachment.hpp | 2 +- src/items/flyable.cpp | 2 +- src/network/compress_network_body.hpp | 27 ++++++++++++++++++--------- src/physics/physical_object.cpp | 2 +- src/utils/mini_glm.hpp | 6 ++++++ 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/src/items/attachment.hpp b/src/items/attachment.hpp index 154f1134a..c03c6e2ba 100644 --- a/src/items/attachment.hpp +++ b/src/items/attachment.hpp @@ -88,7 +88,7 @@ private: /** Time left till attachment expires. */ int16_t m_ticks_left; - /** For parachutes only, rounded down to 2 digits for network. */ + /** For parachutes only, stored in cm/s for networking. */ int16_t m_initial_speed; /** For zoom-in animation */ diff --git a/src/items/flyable.cpp b/src/items/flyable.cpp index a9b8fc4db..0c8ef09e0 100644 --- a/src/items/flyable.cpp +++ b/src/items/flyable.cpp @@ -444,7 +444,7 @@ bool Flyable::updateAndDelete(int ticks) if(m_has_hit_something) return true; - // Round down values in network for better synchronization + // Round values in network for better synchronization if (NetworkConfig::get()->roundValuesNow()) CompressNetworkBody::compress(m_body.get(), m_motion_state.get()); // Save the compressed values if done in client diff --git a/src/network/compress_network_body.hpp b/src/network/compress_network_body.hpp index 10b2cc2ae..8750979cc 100644 --- a/src/network/compress_network_body.hpp +++ b/src/network/compress_network_body.hpp @@ -29,11 +29,12 @@ namespace CompressNetworkBody { using namespace MiniGLM; // ------------------------------------------------------------------------ - inline void setRoundedDownValues(float x, float y, float z, - uint32_t compressed_q, - short lvx, short lvy, short lvz, - short avx, short avy, short avz, - btRigidBody* body, btMotionState* ms) + /** Set body and motion state of bullet object with compressed values. */ + inline void setCompressedValues(float x, float y, float z, + uint32_t compressed_q, + short lvx, short lvy, short lvz, + short avx, short avy, short avz, + btRigidBody* body, btMotionState* ms) { btTransform trans; trans.setOrigin(btVector3(x,y,z)); @@ -49,8 +50,15 @@ namespace CompressNetworkBody body->setInterpolationLinearVelocity(lv); body->setInterpolationAngularVelocity(av); body->updateInertiaTensor(); - } // setRoundedDownValues + } // setCompressedValues // ------------------------------------------------------------------------ + /** Compress transformation and velocities of bullet object, it will + * call MiniGLM::compressQuaternion for compress quaternion of + * transformation and convert linear and angular velocities to half floats + * it can be used by client to locally round values to make sure client + * and server have similar state when saving state if you don't provoide + * bns. + */ inline void compress(btRigidBody* body, btMotionState* ms, BareNetworkString* bns = NULL) { @@ -65,9 +73,9 @@ namespace CompressNetworkBody short avx = toFloat16(body->getAngularVelocity().x()); short avy = toFloat16(body->getAngularVelocity().y()); short avz = toFloat16(body->getAngularVelocity().z()); - setRoundedDownValues(x, y, z, compressed_q, lvx, lvy, lvz, avx, avy, + setCompressedValues(x, y, z, compressed_q, lvx, lvy, lvz, avx, avy, avz, body, ms); - // if bns is null, it's locally compress (for rounding down values) + // if bns is null, it's locally compress (for rounding values) if (!bns) return; @@ -76,6 +84,7 @@ namespace CompressNetworkBody .addUInt16(avx).addUInt16(avy).addUInt16(avz); } // compress // ------------------------------------------------------------------------ + /* Called during rewind when restoring data from game state. */ inline void decompress(const BareNetworkString* bns, btRigidBody* body, btMotionState* ms) { @@ -89,7 +98,7 @@ namespace CompressNetworkBody short avx = bns->getUInt16(); short avy = bns->getUInt16(); short avz = bns->getUInt16(); - setRoundedDownValues(x, y, z, compressed_q, lvx, lvy, lvz, avx, avy, + setCompressedValues(x, y, z, compressed_q, lvx, lvy, lvz, avx, avy, avz, body, ms); } // decompress }; diff --git a/src/physics/physical_object.cpp b/src/physics/physical_object.cpp index 68e6a191e..a510dbf73 100644 --- a/src/physics/physical_object.cpp +++ b/src/physics/physical_object.cpp @@ -633,7 +633,7 @@ void PhysicalObject::update(float dt) { if (!m_is_dynamic) return; - // Round down values in network for better synchronization + // Round values in network for better synchronization if (NetworkConfig::get()->roundValuesNow()) CompressNetworkBody::compress(m_body, m_motion_state); diff --git a/src/utils/mini_glm.hpp b/src/utils/mini_glm.hpp index a6215df41..e466c283b 100644 --- a/src/utils/mini_glm.hpp +++ b/src/utils/mini_glm.hpp @@ -568,6 +568,12 @@ namespace MiniGLM return compressVector3(tangent) | 1 << 30; } // quickTangent // ------------------------------------------------------------------------ + /** Round and save compressed values (optionally) btTransform. + * It will round with 2 digits with min / max +/- 2^23 / 100 for origin in + * btTransform and call compressQuaternion above to compress the rotation + * part, if compressed_data is provided, 3 24 bits and 1 32 bits of + * compressed data will be written in an int[4] array. + */ inline void compressbtTransform(btTransform& cur_t, int* compressed_data = NULL) {