]> git.localhorst.tv Git - blank.git/commitdiff
higher precision for quats over the net
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 5 Nov 2015 10:27:28 +0000 (11:27 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 5 Nov 2015 10:27:28 +0000 (11:27 +0100)
doc/protocol
src/net/net.cpp
tst/net/PacketTest.cpp

index 025a13d1b09e784d291ea0398e1503be9ffc312e..8e2694970ce596b049b2facb4aa313634214b085 100644 (file)
@@ -30,7 +30,8 @@ packu          2    16bit unsigned  int representing a float value normalized to
                        it can be unpacked by dividing by 65535
 vec3n          6    3x packn
 vec3u          6    3x packu
-quat           8    4x packn float
+quat           8    2bit index of largest component, a 2bit padding, then 3x 20bit small components in xyzw
+                       order mapped from [-0.7072,0.7072] to [0,1048574] (with largest omitted)
 entity state  42    [ 0] vec3i chunk pos (there's a variation where this is a vec3b)
                     [12] vec3u block pos by 16,
                     [18] vec3 velocity,
index 5d062343b8976f28425b3c45fd7b704dc00e0e2b..2e202e0da8037d20fc8b668e4324af42ac314c06 100644 (file)
@@ -431,18 +431,67 @@ void Packet::Payload::ReadString(string &dst, size_t off, size_t maxlen) const n
 }
 
 void Packet::Payload::Write(const glm::quat &val, size_t off) noexcept {
-       WritePackN(val.w, off);
-       WritePackN(val.x, off + 2);
-       WritePackN(val.y, off + 4);
-       WritePackN(val.z, off + 6);
+       // find the largest component
+       float largest = 0.0f;
+       int largest_index = -1;
+       for (int i = 0; i < 4; ++i) {
+               float iabs = abs(val[i]);
+               if (iabs > largest) {
+                       largest = iabs;
+                       largest_index = i;
+               }
+       }
+       // make sure it's positive
+       const glm::quat q(val[largest_index] < 0.0f ? -val : val);
+       // move index to the two most significant bits
+       uint64_t packed = uint64_t(largest_index) << 62;
+       // we have to map from [-0.7072,0.7072] to [-524287,524287] and move into positive range
+       constexpr float conv = 524287.0f / 0.7072f;
+       // if largest is 1, the other three are 0
+       // precision of comparison is the interval of our mapping
+       if (abs(1.0 - largest) < (1.0f / conv)) {
+               packed |= 0x7FFFF7FFFF7FFFF;
+       } else {
+               // pack the three smaller components into 20bit ints each
+               int shift = 40;
+               for (int i = 0; i < 4; ++i) {
+                       if (i != largest_index) {
+                               packed |= uint64_t(int(q[i] * conv) + 524287) << shift;
+                               shift -= 20;
+                       }
+               }
+       }
+       // and write it out
+       Write(packed, off);
 }
 
 void Packet::Payload::Read(glm::quat &val, size_t off) const noexcept {
-       ReadPackN(val.w, off);
-       ReadPackN(val.x, off + 2);
-       ReadPackN(val.y, off + 4);
-       ReadPackN(val.z, off + 6);
-       val = normalize(val);
+       // extract the 8 byte packed value
+       uint64_t packed = 0;
+       Read(packed, off);
+       // two most significant bits are the index of the largest (omitted) component
+       int largest_index = packed >> 62;
+       // if all other three are 0, largest is 1 and we can omit the conversion
+       if ((packed & 0xFFFFFFFFFFFFFFF) == 0x7FFFF7FFFF7FFFF) {
+               val = { 0.0f, 0.0f, 0.0f, 0.0f };
+               val[largest_index] = 1.0f;
+               return;
+       }
+       // we have to map from [-524287,524287] to [-0.7072,0.7072]
+       constexpr float conv = 0.7072f / 524287.0f;
+       int shift = 40;
+       for (int i = 0; i < 4; ++i) {
+               if (i != largest_index) {
+                       val[i] = float(int((packed >> shift) & 0xFFFFF) - 524287) * conv;
+                       shift -= 20;
+               } else {
+                       // set to zero so the length of the other three can be determined
+                       val[i] = 0.0f;
+               }
+       }
+       // omitted component squared is 1 - length squared of others
+       val[largest_index] = sqrt(1.0f - dot(val, val));
+       // and already normalized
 }
 
 void Packet::Payload::Write(const EntityState &state, size_t off) noexcept {
index 86517ca32b7c4d3abf3543e4b74c289bb0fa418a..880d6a54af47aa6c907e3b5846e1e2203bc583ff 100644 (file)
@@ -116,7 +116,7 @@ void PacketTest::testJoin() {
        EntityState write_state;
        write_state.pos = { { 7, 2, -3 }, { 1.5f, 0.9f, 12.0f } };
        write_state.velocity = { 0.025f, 0.001f, 0.0f };
-       write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f };
+       write_state.orient = normalize(glm::quat(0.863f, 0.0f, 0.505f, 0.0f));
        write_state.pitch = 0.3f;
        write_state.yaw = -2.3f;
        write_entity.SetState(write_state);
@@ -165,7 +165,7 @@ void PacketTest::testPlayerUpdate() {
        EntityState write_state;
        write_state.pos = { { 7, 2, -3 }, { 1.5f, 0.9f, 12.0f } };
        write_state.velocity = { 0.025f, 0.001f, 0.0f };
-       write_state.orient = { 1.0f, 0.0f, 0.0f, 0.0f };
+       write_state.orient = { 0.0f, 0.0f, 1.0f, 0.0f };
        glm::vec3 write_movement(0.5f, -1.0f, 1.0f);
        uint8_t write_actions = 0x05;
        uint8_t write_slot = 3;