Add document for compressed network values

This commit is contained in:
Benau 2019-03-26 02:44:52 +08:00
parent 194d7d6419
commit d8edde34da
5 changed files with 27 additions and 12 deletions

View File

@ -88,7 +88,7 @@ private:
/** Time left till attachment expires. */ /** Time left till attachment expires. */
int16_t m_ticks_left; 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; int16_t m_initial_speed;
/** For zoom-in animation */ /** For zoom-in animation */

View File

@ -444,7 +444,7 @@ bool Flyable::updateAndDelete(int ticks)
if(m_has_hit_something) return true; 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()) if (NetworkConfig::get()->roundValuesNow())
CompressNetworkBody::compress(m_body.get(), m_motion_state.get()); CompressNetworkBody::compress(m_body.get(), m_motion_state.get());
// Save the compressed values if done in client // Save the compressed values if done in client

View File

@ -29,11 +29,12 @@ namespace CompressNetworkBody
{ {
using namespace MiniGLM; using namespace MiniGLM;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
inline void setRoundedDownValues(float x, float y, float z, /** Set body and motion state of bullet object with compressed values. */
uint32_t compressed_q, inline void setCompressedValues(float x, float y, float z,
short lvx, short lvy, short lvz, uint32_t compressed_q,
short avx, short avy, short avz, short lvx, short lvy, short lvz,
btRigidBody* body, btMotionState* ms) short avx, short avy, short avz,
btRigidBody* body, btMotionState* ms)
{ {
btTransform trans; btTransform trans;
trans.setOrigin(btVector3(x,y,z)); trans.setOrigin(btVector3(x,y,z));
@ -49,8 +50,15 @@ namespace CompressNetworkBody
body->setInterpolationLinearVelocity(lv); body->setInterpolationLinearVelocity(lv);
body->setInterpolationAngularVelocity(av); body->setInterpolationAngularVelocity(av);
body->updateInertiaTensor(); 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, inline void compress(btRigidBody* body, btMotionState* ms,
BareNetworkString* bns = NULL) BareNetworkString* bns = NULL)
{ {
@ -65,9 +73,9 @@ namespace CompressNetworkBody
short avx = toFloat16(body->getAngularVelocity().x()); short avx = toFloat16(body->getAngularVelocity().x());
short avy = toFloat16(body->getAngularVelocity().y()); short avy = toFloat16(body->getAngularVelocity().y());
short avz = toFloat16(body->getAngularVelocity().z()); 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); 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) if (!bns)
return; return;
@ -76,6 +84,7 @@ namespace CompressNetworkBody
.addUInt16(avx).addUInt16(avy).addUInt16(avz); .addUInt16(avx).addUInt16(avy).addUInt16(avz);
} // compress } // compress
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/* Called during rewind when restoring data from game state. */
inline void decompress(const BareNetworkString* bns, inline void decompress(const BareNetworkString* bns,
btRigidBody* body, btMotionState* ms) btRigidBody* body, btMotionState* ms)
{ {
@ -89,7 +98,7 @@ namespace CompressNetworkBody
short avx = bns->getUInt16(); short avx = bns->getUInt16();
short avy = bns->getUInt16(); short avy = bns->getUInt16();
short avz = 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); avz, body, ms);
} // decompress } // decompress
}; };

View File

@ -633,7 +633,7 @@ void PhysicalObject::update(float dt)
{ {
if (!m_is_dynamic) return; if (!m_is_dynamic) return;
// Round down values in network for better synchronization // Round values in network for better synchronization
if (NetworkConfig::get()->roundValuesNow()) if (NetworkConfig::get()->roundValuesNow())
CompressNetworkBody::compress(m_body, m_motion_state); CompressNetworkBody::compress(m_body, m_motion_state);

View File

@ -568,6 +568,12 @@ namespace MiniGLM
return compressVector3(tangent) | 1 << 30; return compressVector3(tangent) | 1 << 30;
} // quickTangent } // 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, inline void compressbtTransform(btTransform& cur_t,
int* compressed_data = NULL) int* compressed_data = NULL)
{ {