Use btTransform and normal compression if possible

This commit is contained in:
Benau 2019-02-27 11:04:49 +08:00
parent 733794bb3e
commit cb1769acea
10 changed files with 129 additions and 52 deletions

View File

@ -29,6 +29,7 @@
#include "physics/physics.hpp" #include "physics/physics.hpp"
#include "physics/triangle_mesh.hpp" #include "physics/triangle_mesh.hpp"
#include "tracks/track.hpp" #include "tracks/track.hpp"
#include "utils/mini_glm.hpp"
#include <limits> #include <limits>
@ -72,6 +73,8 @@ AbstractKartAnimation::AbstractKartAnimation(AbstractKart* kart,
kart->setSquash(0.0f, 0.0f); kart->setSquash(0.0f, 0.0f);
} }
} }
MiniGLM::compressbtTransform(m_created_transform,
m_created_transform_compressed);
} // AbstractKartAnimation } // AbstractKartAnimation
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -179,9 +182,10 @@ float AbstractKartAnimation::getMaximumHeight(const Vec3& up_vector,
void AbstractKartAnimation::saveState(BareNetworkString* buffer) void AbstractKartAnimation::saveState(BareNetworkString* buffer)
{ {
buffer->addUInt32(m_created_ticks); buffer->addUInt32(m_created_ticks);
buffer->add(m_created_transform.getOrigin()); buffer->addInt24(m_created_transform_compressed[0])
btQuaternion q = m_created_transform.getRotation(); .addInt24(m_created_transform_compressed[1])
buffer->add(q); .addInt24(m_created_transform_compressed[2])
.addUInt32(m_created_transform_compressed[3]);
} // saveState } // saveState
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -190,6 +194,10 @@ void AbstractKartAnimation::saveState(BareNetworkString* buffer)
void AbstractKartAnimation::restoreBasicState(BareNetworkString* buffer) void AbstractKartAnimation::restoreBasicState(BareNetworkString* buffer)
{ {
m_created_ticks = buffer->getUInt32(); m_created_ticks = buffer->getUInt32();
m_created_transform.setOrigin(buffer->getVec3()); m_created_transform_compressed[0] = buffer->getInt24();
m_created_transform.setRotation(buffer->getQuat()); m_created_transform_compressed[1] = buffer->getInt24();
m_created_transform_compressed[2] = buffer->getInt24();
m_created_transform_compressed[3] = buffer->getUInt32();
m_created_transform =
MiniGLM::decompressbtTransform(m_created_transform_compressed);
} // restoreBasicState } // restoreBasicState

View File

@ -67,6 +67,9 @@ protected:
* to recreate the animation with the same one. */ * to recreate the animation with the same one. */
btTransform m_created_transform; btTransform m_created_transform;
/* Compressed values for server to send to avoid compressing everytime. */
int m_created_transform_compressed[4];
void resetPowerUp(); void resetPowerUp();
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void restoreBasicState(BareNetworkString* buffer); void restoreBasicState(BareNetworkString* buffer);

View File

@ -29,6 +29,7 @@
#include "network/network_string.hpp" #include "network/network_string.hpp"
#include "tracks/check_cannon.hpp" #include "tracks/check_cannon.hpp"
#include "tracks/check_manager.hpp" #include "tracks/check_manager.hpp"
#include "utils/mini_glm.hpp"
#include "LinearMath/btTransform.h" #include "LinearMath/btTransform.h"
@ -75,6 +76,8 @@ CannonAnimation::CannonAnimation(Flyable* flyable, CheckCannon* cc)
{ {
m_flyable = flyable; m_flyable = flyable;
m_created_transform = m_flyable->getTrans(); m_created_transform = m_flyable->getTrans();
MiniGLM::compressbtTransform(m_created_transform,
m_created_transform_compressed);
m_check_cannon = cc; m_check_cannon = cc;
init(cc->getIpo()->clone(), cc->getLeftPoint(), cc->getRightPoint(), init(cc->getIpo()->clone(), cc->getLeftPoint(), cc->getRightPoint(),
cc->getTargetLeft(), cc->getTargetRight(), /*skid_rot*/0); cc->getTargetLeft(), cc->getTargetRight(), /*skid_rot*/0);

View File

@ -27,6 +27,9 @@
#include "network/network_string.hpp" #include "network/network_string.hpp"
#include "race/race_manager.hpp" #include "race/race_manager.hpp"
#include "tracks/track.hpp" #include "tracks/track.hpp"
#include "utils/mini_glm.hpp"
#include <cstring>
/** A static create function that does only create an explosion if /** A static create function that does only create an explosion if
* the explosion happens to be close enough to affect the kart. * the explosion happens to be close enough to affect the kart.
@ -82,6 +85,8 @@ ExplosionAnimation *ExplosionAnimation::create(AbstractKart *kart)
ExplosionAnimation::ExplosionAnimation(AbstractKart* kart, bool direct_hit) ExplosionAnimation::ExplosionAnimation(AbstractKart* kart, bool direct_hit)
: AbstractKartAnimation(kart, "ExplosionAnimation") : AbstractKartAnimation(kart, "ExplosionAnimation")
{ {
memset(m_reset_trans_compressed, 0, 16);
Vec3 normal = m_created_transform.getBasis().getColumn(1).normalized();
// Put the kart back to its own flag base like rescue if direct hit in CTF // Put the kart back to its own flag base like rescue if direct hit in CTF
bool reset = race_manager->getMinorMode() == bool reset = race_manager->getMinorMode() ==
RaceManager::MINOR_MODE_CAPTURE_THE_FLAG && direct_hit; RaceManager::MINOR_MODE_CAPTURE_THE_FLAG && direct_hit;
@ -92,11 +97,13 @@ ExplosionAnimation::ExplosionAnimation(AbstractKart* kart, bool direct_hit)
btTransform reset_trans = m_kart->getTrans(); btTransform reset_trans = m_kart->getTrans();
m_kart->getBody()->setCenterOfMassTransform(prev_trans); m_kart->getBody()->setCenterOfMassTransform(prev_trans);
m_kart->setTrans(prev_trans); m_kart->setTrans(prev_trans);
init(direct_hit, m_kart->getNormal(), reset_trans); MiniGLM::compressbtTransform(reset_trans,
m_reset_trans_compressed);
init(direct_hit, normal, reset_trans);
} }
else else
{ {
init(direct_hit, m_kart->getNormal(), init(direct_hit, normal,
btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f))); btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f)));
} }
@ -121,15 +128,19 @@ ExplosionAnimation::ExplosionAnimation(AbstractKart* kart, BareNetworkString* b)
void ExplosionAnimation::restoreData(BareNetworkString* b) void ExplosionAnimation::restoreData(BareNetworkString* b)
{ {
bool direct_hit = b->getUInt8() == 1; bool direct_hit = b->getUInt8() == 1;
Vec3 normal = b->getVec3(); Vec3 normal = m_created_transform.getBasis().getColumn(1).normalized();
btTransform reset_transform = btTransform reset_transform =
btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f)); btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f));
if (race_manager->getMinorMode() == if (race_manager->getMinorMode() ==
RaceManager::MINOR_MODE_CAPTURE_THE_FLAG && direct_hit) RaceManager::MINOR_MODE_CAPTURE_THE_FLAG && direct_hit)
{ {
reset_transform.setOrigin(b->getVec3()); m_reset_trans_compressed[0] = b->getInt24();
reset_transform.setRotation(b->getQuat()); m_reset_trans_compressed[1] = b->getInt24();
m_reset_trans_compressed[2] = b->getInt24();
m_reset_trans_compressed[3] = b->getUInt32();
reset_transform =
MiniGLM::decompressbtTransform(m_reset_trans_compressed);
} }
init(direct_hit, normal, reset_transform); init(direct_hit, normal, reset_transform);
} // restoreData } // restoreData
@ -288,13 +299,14 @@ bool ExplosionAnimation::hasResetAlready() const
void ExplosionAnimation::saveState(BareNetworkString* buffer) void ExplosionAnimation::saveState(BareNetworkString* buffer)
{ {
AbstractKartAnimation::saveState(buffer); AbstractKartAnimation::saveState(buffer);
buffer->addUInt8(m_direct_hit ? 1 : 0).add(m_normal); buffer->addUInt8(m_direct_hit ? 1 : 0);
if (race_manager->getMinorMode() == if (race_manager->getMinorMode() ==
RaceManager::MINOR_MODE_CAPTURE_THE_FLAG && m_direct_hit) RaceManager::MINOR_MODE_CAPTURE_THE_FLAG && m_direct_hit)
{ {
buffer->add(m_reset_trans.getOrigin()); buffer->addInt24(m_reset_trans_compressed[0])
btQuaternion q = m_reset_trans.getRotation(); .addInt24(m_reset_trans_compressed[1])
buffer->add(q); .addInt24(m_reset_trans_compressed[2])
.addUInt32(m_reset_trans_compressed[3]);
} }
} // saveState } // saveState

View File

@ -58,6 +58,9 @@ friend class KartRewinder;
/** Used for reset kart back to flag base in CTF. */ /** Used for reset kart back to flag base in CTF. */
btTransform m_reset_trans; btTransform m_reset_trans;
/* Compressed values for server to send to avoid compressing everytime. */
int m_reset_trans_compressed[4];
bool m_direct_hit; bool m_direct_hit;
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -26,6 +26,7 @@
#include "modes/follow_the_leader.hpp" #include "modes/follow_the_leader.hpp"
#include "modes/three_strikes_battle.hpp" #include "modes/three_strikes_battle.hpp"
#include "network/network_string.hpp" #include "network/network_string.hpp"
#include "utils/mini_glm.hpp"
#include "ISceneNode.h" #include "ISceneNode.h"
@ -46,9 +47,10 @@ RescueAnimation::RescueAnimation(AbstractKart* kart, bool is_auto_rescue)
World::getWorld()->moveKartAfterRescue(kart); World::getWorld()->moveKartAfterRescue(kart);
btTransform rescue_transform = kart->getTrans(); btTransform rescue_transform = kart->getTrans();
MiniGLM::compressbtTransform(rescue_transform,
m_rescue_transform_compressed);
kart->getBody()->setCenterOfMassTransform(prev_trans); kart->getBody()->setCenterOfMassTransform(prev_trans);
kart->setTrans(prev_trans); kart->setTrans(prev_trans);
m_created_transform = prev_trans;
// Determine maximum rescue height with up-raycast // Determine maximum rescue height with up-raycast
float max_height = m_kart->getKartProperties()->getRescueHeight(); float max_height = m_kart->getKartProperties()->getRescueHeight();
@ -59,7 +61,7 @@ RescueAnimation::RescueAnimation(AbstractKart* kart, bool is_auto_rescue)
float timer = m_kart->getKartProperties()->getRescueDuration(); float timer = m_kart->getKartProperties()->getRescueDuration();
float velocity = max_height / timer; float velocity = max_height / timer;
init(rescue_transform, velocity, up_vector); init(rescue_transform, velocity);
m_kart->getAttachment()->clear(); m_kart->getAttachment()->clear();
// Add a hit unless it was auto-rescue // Add a hit unless it was auto-rescue
@ -103,12 +105,14 @@ RescueAnimation::RescueAnimation(AbstractKart* kart, BareNetworkString* b)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void RescueAnimation::restoreData(BareNetworkString* b) void RescueAnimation::restoreData(BareNetworkString* b)
{ {
btTransform rescue_transform; m_rescue_transform_compressed[0] = b->getInt24();
rescue_transform.setOrigin(b->getVec3()); m_rescue_transform_compressed[1] = b->getInt24();
rescue_transform.setRotation(b->getQuat()); m_rescue_transform_compressed[2] = b->getInt24();
m_rescue_transform_compressed[3] = b->getUInt32();
btTransform rescue_transform =
MiniGLM::decompressbtTransform(m_rescue_transform_compressed);
float velocity = b->getFloat(); float velocity = b->getFloat();
Vec3 up_vector = b->getVec3(); init(rescue_transform, velocity);
init(rescue_transform, velocity, up_vector);
} // restoreData } // restoreData
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -117,14 +121,13 @@ void RescueAnimation::restoreData(BareNetworkString* b)
* time. * time.
*/ */
void RescueAnimation::init(const btTransform& rescue_transform, void RescueAnimation::init(const btTransform& rescue_transform,
float velocity, const Vec3& up_vector) float velocity)
{ {
m_rescue_transform = rescue_transform; m_rescue_transform = rescue_transform;
float timer = m_kart->getKartProperties()->getRescueDuration(); float timer = m_kart->getKartProperties()->getRescueDuration();
m_end_ticks = m_created_ticks + stk_config->time2Ticks(timer); m_end_ticks = m_created_ticks + stk_config->time2Ticks(timer);
m_rescue_moment = m_created_ticks + stk_config->time2Ticks(timer * 0.4f); m_rescue_moment = m_created_ticks + stk_config->time2Ticks(timer * 0.4f);
m_velocity = velocity; m_velocity = velocity;
m_up_vector = up_vector;
} // init } // init
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
@ -152,7 +155,7 @@ void RescueAnimation::update(int ticks)
float dur = stk_config->ticks2Time(m_end_ticks - m_rescue_moment - float dur = stk_config->ticks2Time(m_end_ticks - m_rescue_moment -
(World::getWorld()->getTicksSinceStart() - m_rescue_moment)); (World::getWorld()->getTicksSinceStart() - m_rescue_moment));
Vec3 xyz = m_rescue_transform.getOrigin() + Vec3 xyz = m_rescue_transform.getOrigin() +
dur * m_velocity * m_up_vector; dur * m_velocity * m_rescue_transform.getBasis().getColumn(1);
m_kart->setXYZ(xyz); m_kart->setXYZ(xyz);
m_kart->setRotation(m_rescue_transform.getRotation()); m_kart->setRotation(m_rescue_transform.getRotation());
} }
@ -161,7 +164,7 @@ void RescueAnimation::update(int ticks)
float dur = stk_config->ticks2Time( float dur = stk_config->ticks2Time(
World::getWorld()->getTicksSinceStart() - m_created_ticks); World::getWorld()->getTicksSinceStart() - m_created_ticks);
Vec3 xyz = m_created_transform.getOrigin() + Vec3 xyz = m_created_transform.getOrigin() +
dur * m_velocity * m_up_vector; dur * m_velocity * m_created_transform.getBasis().getColumn(1);
m_kart->setXYZ(xyz); m_kart->setXYZ(xyz);
m_kart->setRotation(m_created_transform.getRotation()); m_kart->setRotation(m_created_transform.getRotation());
} }
@ -191,10 +194,11 @@ void RescueAnimation::updateGraphics(float dt)
void RescueAnimation::saveState(BareNetworkString* buffer) void RescueAnimation::saveState(BareNetworkString* buffer)
{ {
AbstractKartAnimation::saveState(buffer); AbstractKartAnimation::saveState(buffer);
buffer->add(m_rescue_transform.getOrigin()); buffer->addInt24(m_rescue_transform_compressed[0])
btQuaternion q = m_rescue_transform.getRotation(); .addInt24(m_rescue_transform_compressed[1])
buffer->add(q); .addInt24(m_rescue_transform_compressed[2])
buffer->addFloat(m_velocity).add(m_up_vector); .addUInt32(m_rescue_transform_compressed[3]);
buffer->addFloat(m_velocity);
} // saveState } // saveState
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -32,9 +32,6 @@ class RescueAnimation: public AbstractKartAnimation
{ {
protected: protected:
friend class KartRewinder; friend class KartRewinder;
/** Column 1 of btTransform of kart. */
Vec3 m_up_vector;
/** The velocity with which the kart is moved. */ /** The velocity with which the kart is moved. */
float m_velocity; float m_velocity;
@ -48,13 +45,15 @@ friend class KartRewinder;
/* Final transformation to place kart. */ /* Final transformation to place kart. */
btTransform m_rescue_transform; btTransform m_rescue_transform;
/* Compressed values for server to send to avoid compressing everytime. */
int m_rescue_transform_compressed[4];
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
RescueAnimation(AbstractKart* kart, BareNetworkString* b); RescueAnimation(AbstractKart* kart, BareNetworkString* b);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void restoreData(BareNetworkString* b); void restoreData(BareNetworkString* b);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void init(const btTransform& rescue_transform, float velocity, void init(const btTransform& rescue_transform, float velocity);
const Vec3& up_vector);
public: public:
RescueAnimation(AbstractKart* kart, RescueAnimation(AbstractKart* kart,
bool is_auto_rescue = false); bool is_auto_rescue = false);

View File

@ -30,17 +30,15 @@ const Vec3 g_kart_flag_offset(0.0, 0.2f, -0.5f);
// ============================================================================ // ============================================================================
BareNetworkString* CTFFlag::saveState(std::vector<std::string>* ru) BareNetworkString* CTFFlag::saveState(std::vector<std::string>* ru)
{ {
using namespace MiniGLM;
ru->push_back(getUniqueIdentity()); ru->push_back(getUniqueIdentity());
BareNetworkString* buffer = new BareNetworkString(); BareNetworkString* buffer = new BareNetworkString();
buffer->addUInt8(m_flag_status); buffer->addUInt8(m_flag_status);
if (m_flag_status == OFF_BASE) if (m_flag_status == OFF_BASE)
{ {
Vec3 normal = quatRotate(m_flag_trans.getRotation(), buffer->addInt24(m_off_base_compressed[0])
Vec3(0.0f, 1.0f, 0.0f)); .addInt24(m_off_base_compressed[1])
buffer->add(m_flag_trans.getOrigin()); .addInt24(m_off_base_compressed[2])
buffer->addUInt32( .addUInt32(m_off_base_compressed[3]);
compressVector3(Vec3(normal.normalize()).toIrrVector()));
buffer->addUInt16(m_ticks_since_off_base); buffer->addUInt16(m_ticks_since_off_base);
} }
return buffer; return buffer;
@ -53,12 +51,11 @@ void CTFFlag::restoreState(BareNetworkString* buffer, int count)
m_flag_status = buffer->getUInt8(); m_flag_status = buffer->getUInt8();
if (m_flag_status == OFF_BASE) if (m_flag_status == OFF_BASE)
{ {
Vec3 origin = buffer->getVec3(); m_off_base_compressed[0] = buffer->getInt24();
uint32_t normal_packed = buffer->getUInt32(); m_off_base_compressed[1] = buffer->getInt24();
Vec3 normal = decompressVector3(normal_packed); m_off_base_compressed[2] = buffer->getInt24();
m_flag_trans.setOrigin(origin); m_off_base_compressed[3] = buffer->getUInt32();
m_flag_trans.setRotation( m_flag_trans = decompressbtTransform(m_off_base_compressed);
shortestArcQuat(Vec3(0.0f, 1.0f, 0.0f), normal));
m_ticks_since_off_base = buffer->getUInt16(); m_ticks_since_off_base = buffer->getUInt16();
} }
updateFlagTrans(m_flag_trans); updateFlagTrans(m_flag_trans);
@ -120,3 +117,13 @@ void CTFFlag::updateFlagGraphics(irr::scene::IAnimatedMeshSceneNode* flag_node)
flag_node->setAnimationSpeed(25.0f); flag_node->setAnimationSpeed(25.0f);
} }
} // updateFlagPosition } // updateFlagPosition
// ----------------------------------------------------------------------------
void CTFFlag::dropFlagAt(const btTransform& t)
{
m_flag_status = OFF_BASE;
m_ticks_since_off_base = 0;
m_flag_trans = t;
using namespace MiniGLM;
compressbtTransform(m_flag_trans, m_off_base_compressed);
} // updateFlagPosition

View File

@ -23,6 +23,7 @@
#include "utils/vec3.hpp" #include "utils/vec3.hpp"
#include "LinearMath/btTransform.h" #include "LinearMath/btTransform.h"
#include <cstring>
enum FlagColor : unsigned int enum FlagColor : unsigned int
{ {
@ -58,6 +59,8 @@ private:
FlagColor m_flag_color; FlagColor m_flag_color;
/* Set by dropFlagAt to pre-compressed the dropped flag transformation. */
int m_off_base_compressed[4];
public: public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
CTFFlag(FlagColor fc, const btTransform& base_trans) CTFFlag(FlagColor fc, const btTransform& base_trans)
@ -70,6 +73,7 @@ public:
m_flag_trans.setRotation(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f)); m_flag_trans.setRotation(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f));
m_flag_color = fc; m_flag_color = fc;
m_ticks_since_off_base = 0; m_ticks_since_off_base = 0;
memset(m_off_base_compressed, 0, 16);
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
virtual void saveTransform() {} virtual void saveTransform() {}
@ -116,12 +120,7 @@ public:
updateFlagTrans(); updateFlagTrans();
} }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void dropFlagAt(const btTransform& t) void dropFlagAt(const btTransform& t);
{
m_flag_status = OFF_BASE;
m_ticks_since_off_base = 0;
m_flag_trans = t;
}
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
bool isInBase() const { return m_flag_status == IN_BASE; } bool isInBase() const { return m_flag_status == IN_BASE; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -19,6 +19,7 @@
#define HEADER_MINI_GLM_HPP #define HEADER_MINI_GLM_HPP
#include "LinearMath/btQuaternion.h" #include "LinearMath/btQuaternion.h"
#include "LinearMath/btTransform.h"
#include "utils/vec3.hpp" #include "utils/vec3.hpp"
#include <algorithm> #include <algorithm>
@ -29,6 +30,8 @@
#include <quaternion.h> #include <quaternion.h>
#include <vector3d.h> #include <vector3d.h>
#include "irrMath.h"
using namespace irr; using namespace irr;
// GLM without template // GLM without template
@ -565,6 +568,42 @@ namespace MiniGLM
return compressVector3(tangent) | 1 << 30; return compressVector3(tangent) | 1 << 30;
} // quickTangent } // quickTangent
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
inline void compressbtTransform(btTransform& cur_t,
int* compressed_data = NULL)
{
int x = (int)(cur_t.getOrigin().x() * 1000.0f);
int y = (int)(cur_t.getOrigin().y() * 1000.0f);
int z = (int)(cur_t.getOrigin().z() * 1000.0f);
x = core::clamp(x, -8388608, 8388607);
y = core::clamp(y, -8388608, 8388607);
z = core::clamp(z, -8388608, 8388607);
uint32_t compressed_q = compressQuaternion(cur_t.getRotation());
cur_t.setOrigin(btVector3(
(float)x / 1000.0f,
(float)y / 1000.0f,
(float)z / 1000.0f));
cur_t.setRotation(decompressbtQuaternion(compressed_q));
if (compressed_data)
{
compressed_data[0] = x;
compressed_data[1] = y;
compressed_data[2] = z;
compressed_data[3] = (int)compressed_q;
}
} // compressbtTransform
// ------------------------------------------------------------------------
inline btTransform decompressbtTransform(int* compressed_data)
{
btTransform trans;
trans.setOrigin(btVector3(
(float)compressed_data[0] / 1000.0f,
(float)compressed_data[1] / 1000.0f,
(float)compressed_data[2] / 1000.0f));
trans.setRotation(decompressbtQuaternion(
(uint32_t)compressed_data[3]));
return trans;
} // decompressbtTransform
// ------------------------------------------------------------------------
void unitTesting(); void unitTesting();
} }