SuperTuxKart
compress_network_body.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2018 SuperTuxKart-Team
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_COMPRESS_NETWORK_BODY_HPP
20 #define HEADER_COMPRESS_NETWORK_BODY_HPP
21 
23 #include "mini_glm.hpp"
24 
25 #include "LinearMath/btMotionState.h"
26 #include "btBulletDynamicsCommon.h"
27 
28 namespace CompressNetworkBody
29 {
30  using namespace MiniGLM;
31  // ------------------------------------------------------------------------
33  inline void setCompressedValues(float x, float y, float z,
34  uint32_t compressed_q,
35  short lvx, short lvy, short lvz,
36  short avx, short avy, short avz,
37  btRigidBody* body, btMotionState* ms)
38  {
39  btTransform trans;
40  trans.setOrigin(btVector3(x,y,z));
41  trans.setRotation(decompressbtQuaternion(compressed_q));
42  btVector3 lv(toFloat32(lvx), toFloat32(lvy), toFloat32(lvz));
43  btVector3 av(toFloat32(avx), toFloat32(avy), toFloat32(avz));
44 
45  body->setWorldTransform(trans);
46  ms->setWorldTransform(trans);
47  body->setInterpolationWorldTransform(trans);
48  body->setLinearVelocity(lv);
49  body->setAngularVelocity(av);
50  body->setInterpolationLinearVelocity(lv);
51  body->setInterpolationAngularVelocity(av);
52  body->updateInertiaTensor();
53  } // setCompressedValues
54  // ------------------------------------------------------------------------
62  inline void compress(btRigidBody* body, btMotionState* ms,
63  BareNetworkString* bns = NULL)
64  {
65  float x = body->getWorldTransform().getOrigin().x();
66  float y = body->getWorldTransform().getOrigin().y();
67  float z = body->getWorldTransform().getOrigin().z();
68  uint32_t compressed_q =
69  compressQuaternion(body->getWorldTransform().getRotation());
70  short lvx = toFloat16(body->getLinearVelocity().x());
71  short lvy = toFloat16(body->getLinearVelocity().y());
72  short lvz = toFloat16(body->getLinearVelocity().z());
73  short avx = toFloat16(body->getAngularVelocity().x());
74  short avy = toFloat16(body->getAngularVelocity().y());
75  short avz = toFloat16(body->getAngularVelocity().z());
76  setCompressedValues(x, y, z, compressed_q, lvx, lvy, lvz, avx, avy,
77  avz, body, ms);
78  // if bns is null, it's locally compress (for rounding values)
79  if (!bns)
80  return;
81 
82  bns->addFloat(x).addFloat(y).addFloat(z).addUInt32(compressed_q);
83  bns->addUInt16(lvx).addUInt16(lvy).addUInt16(lvz)
84  .addUInt16(avx).addUInt16(avy).addUInt16(avz);
85  } // compress
86  // ------------------------------------------------------------------------
87  /* Called during rewind when restoring data from game state. */
88  inline void decompress(const BareNetworkString* bns,
89  btRigidBody* body, btMotionState* ms)
90  {
91  float x = bns->getFloat();
92  float y = bns->getFloat();
93  float z = bns->getFloat();
94  uint32_t compressed_q = bns->getUInt32();
95  short lvx = bns->getUInt16();
96  short lvy = bns->getUInt16();
97  short lvz = bns->getUInt16();
98  short avx = bns->getUInt16();
99  short avy = bns->getUInt16();
100  short avz = bns->getUInt16();
101  setCompressedValues(x, y, z, compressed_q, lvx, lvy, lvz, avx, avy,
102  avz, body, ms);
103  } // decompress
104 };
105 
106 #endif // HEADER_COMPRESS_NETWORK_BODY_HPP
Describes a chain of 8-bit unsigned integers.
Definition: network_string.hpp:53
float getFloat() const
Gets a 4 byte floating point value.
Definition: network_string.hpp:358
uint32_t getUInt32() const
Returns a unsigned 32 bit integer.
Definition: network_string.hpp:324
uint16_t getUInt16() const
Returns an unsigned 16 bit integer.
Definition: network_string.hpp:340
Defines functions to easily manipulate 8-bit network destinated strings.