Added convenience functions that encode and decode Vec3s and Quats.

This commit is contained in:
hiker 2016-01-29 08:14:13 +11:00
parent 6039f63c0b
commit e81935df99
2 changed files with 42 additions and 17 deletions

View File

@ -24,6 +24,9 @@
#define NETWORK_STRING_HPP
#include "utils/types.hpp"
#include "utils/vec3.hpp"
#include "LinearMath/btQuaternion.h"
#include "irrString.h"
@ -256,6 +259,19 @@ public:
return *this;
} // operator+=
// ------------------------------------------------------------------------
/** Adds the xyz components of a Vec3 to the string. */
NetworkString& add(const Vec3 &xyz)
{
return addFloat(xyz.getX()).addFloat(xyz.getY()).addFloat(xyz.getZ());
} // add
// ------------------------------------------------------------------------
/** Adds the four components of a quaternion. */
NetworkString& add(const btQuaternion &quat)
{
return addFloat(quat.getX()).addFloat(quat.getY())
.addFloat(quat.getZ()).addFloat(quat.getW());
} // add
// ------------------------------------------------------------------------
/** Returns the content of the network string as a std::string. */
const std::string std_string() const
@ -500,6 +516,25 @@ public:
*dst = getAndRemoveUChar(0);
return *this;
} // guc
// ------------------------------------------------------------------------
/** Reads the three components of a Vec3 from the given position. */
NetworkString& get(Vec3 *xyz, int pos)
{
xyz->setX(getFloat(pos ));
xyz->setY(getFloat(pos+4));
xyz->setZ(getFloat(pos+8));
return *this;
} // addVec3
// ------------------------------------------------------------------------
/** Reads the four components of a quaternion from the given position. */
NetworkString& get(btQuaternion *quat, int pos)
{
quat->setX(getFloat(pos ));
quat->setY(getFloat(pos+ 4));
quat->setZ(getFloat(pos+ 8));
quat->setW(getFloat(pos+12));
return *this;
} // addVec3
}; // class NetworkString

View File

@ -42,18 +42,12 @@ bool KartUpdateProtocol::notifyEventAsynchronous(Event* event)
{
uint8_t kart_id = ns.getUInt8(0);
float a,b,c;
a = ns.getFloat(1);
b = ns.getFloat(5);
c = ns.getFloat(9);
float d,e,f,g;
d = ns.getFloat(13);
e = ns.getFloat(17);
f = ns.getFloat(21);
g = ns.getFloat(25);
Vec3 xyz;
btQuaternion quat;
ns.get(&xyz, 1).get(&quat, 13);
pthread_mutex_trylock(&m_positions_updates_mutex);
m_next_positions.push_back(Vec3(a,b,c));
m_next_quaternions.push_back(btQuaternion(d,e,f,g));
m_next_positions.push_back(xyz);
m_next_quaternions.push_back(quat);
m_karts_ids.push_back(kart_id);
pthread_mutex_unlock(&m_positions_updates_mutex);
ns.removeFront(29);
@ -85,10 +79,8 @@ void KartUpdateProtocol::update()
{
AbstractKart* kart = world->getKart(i);
Vec3 xyz = kart->getXYZ();
btQuaternion quat = kart->getRotation();
ns.addUInt8( kart->getWorldKartId());
ns.af(xyz[0]).af(xyz[1]).af(xyz[2]); // add position
ns.af(quat.x()).af(quat.y()).af(quat.z()).af(quat.w()); // add rotation
ns.add(xyz).add(kart->getRotation());
Log::verbose("KartUpdateProtocol",
"Sending %d's positions %f %f %f",
kart->getWorldKartId(), xyz[0], xyz[1], xyz[2]);
@ -103,10 +95,8 @@ void KartUpdateProtocol::update()
{
AbstractKart *kart = World::getWorld()->getLocalPlayerKart(i);
const Vec3 &xyz = kart->getXYZ();
btQuaternion quat = kart->getRotation();
ns.addUInt8(kart->getWorldKartId());
ns.af(xyz[0]).af(xyz[1]).af(xyz[2]); // add position
ns.af(quat.x()).af(quat.y()).af(quat.z()).af(quat.w()); // add rotation
ns.add(xyz).add(kart->getRotation());
Log::verbose("KartUpdateProtocol",
"Sending %d's positions %f %f %f",
kart->getWorldKartId(), xyz[0], xyz[1], xyz[2]);