Make a lossless compression for btKart state saving

This commit is contained in:
Benau 2018-11-13 12:56:40 +08:00
parent b4df116661
commit 9de7376b0a
6 changed files with 49 additions and 43 deletions

View File

@ -2291,7 +2291,7 @@ void Kart::crashed(const Material *m, const Vec3 &normal)
const LinearWorld *lw = dynamic_cast<LinearWorld*>(World::getWorld()); const LinearWorld *lw = dynamic_cast<LinearWorld*>(World::getWorld());
if(m_kart_properties->getTerrainImpulseType() if(m_kart_properties->getTerrainImpulseType()
==KartProperties::IMPULSE_NORMAL && ==KartProperties::IMPULSE_NORMAL &&
m_vehicle->getCentralImpulseTime()<=0 ) m_vehicle->getCentralImpulseTicks()<=0 )
{ {
// Restrict impule to plane defined by gravity (i.e. X/Z plane). // Restrict impule to plane defined by gravity (i.e. X/Z plane).
// This avoids the problem that karts can be pushed up, e.g. above // This avoids the problem that karts can be pushed up, e.g. above
@ -2312,7 +2312,7 @@ void Kart::crashed(const Material *m, const Vec3 &normal)
m_bounce_back_ticks = 0; m_bounce_back_ticks = 0;
impulse = Vec3(0, 0, 0); impulse = Vec3(0, 0, 0);
//m_vehicle->setTimedCentralImpulse(0.1f, impulse); //m_vehicle->setTimedCentralImpulse(0.1f, impulse);
m_vehicle->setTimedCentralImpulse(0.0, impulse); m_vehicle->setTimedCentralImpulse(0, impulse);
} }
// If there is a quad graph, push the kart towards the previous // If there is a quad graph, push the kart towards the previous
// graph node center (we have to use the previous point since the // graph node center (we have to use the previous point since the
@ -2320,7 +2320,7 @@ void Kart::crashed(const Material *m, const Vec3 &normal)
// would be pushed forward). // would be pushed forward).
else if(m_kart_properties->getTerrainImpulseType() else if(m_kart_properties->getTerrainImpulseType()
==KartProperties::IMPULSE_TO_DRIVELINE && ==KartProperties::IMPULSE_TO_DRIVELINE &&
lw && m_vehicle->getCentralImpulseTime()<=0 && lw && m_vehicle->getCentralImpulseTicks()<=0 &&
Track::getCurrentTrack()->isPushBackEnabled()) Track::getCurrentTrack()->isPushBackEnabled())
{ {
int sector = lw->getSectorForKart(this); int sector = lw->getSectorForKart(this);
@ -2338,7 +2338,8 @@ void Kart::crashed(const Material *m, const Vec3 &normal)
impulse = Vec3(0, 0, -1); // Arbitrary impulse = Vec3(0, 0, -1); // Arbitrary
impulse *= m_kart_properties->getCollisionTerrainImpulse(); impulse *= m_kart_properties->getCollisionTerrainImpulse();
m_bounce_back_ticks = stk_config->time2Ticks(0.2f); m_bounce_back_ticks = stk_config->time2Ticks(0.2f);
m_vehicle->setTimedCentralImpulse(0.1f, impulse); m_vehicle->setTimedCentralImpulse(
(uint16_t)stk_config->time2Ticks(0.1f), impulse);
} }
} }

View File

@ -154,13 +154,13 @@ BareNetworkString* KartRewinder::saveState(std::vector<std::string>* ru)
buffer->add(body->getLinearVelocity()); buffer->add(body->getLinearVelocity());
buffer->add(body->getAngularVelocity()); buffer->add(body->getAngularVelocity());
buffer->addFloat(m_vehicle->getTimedRotationTime()); buffer->addUInt16(m_vehicle->getTimedRotationTicks());
buffer->add(m_vehicle->getTimedRotation()); buffer->add(m_vehicle->getTimedRotation());
buffer->addUInt8(m_vehicle->getCushioningDisableTime()); buffer->addUInt8(m_vehicle->getCushioningDisableTime());
// For collision rewind // For collision rewind
buffer->addUInt16(m_bounce_back_ticks); buffer->addUInt16(m_bounce_back_ticks);
buffer->addFloat(m_vehicle->getCentralImpulseTime()); buffer->addUInt16(m_vehicle->getCentralImpulseTicks());
buffer->add(m_vehicle->getAdditionalImpulse()); buffer->add(m_vehicle->getAdditionalImpulse());
// 3) Steering and other player controls // 3) Steering and other player controls
@ -262,16 +262,17 @@ void KartRewinder::restoreState(BareNetworkString *buffer, int count)
setTrans(m_transfrom_from_network); setTrans(m_transfrom_from_network);
} }
float time_rot = buffer->getFloat(); uint16_t time_rot = buffer->getUInt16();
// Set timed rotation divides by time_rot // Set timed rotation divides by time_rot
m_vehicle->setTimedRotation(time_rot, time_rot*buffer->getVec3()); m_vehicle->setTimedRotation(time_rot,
stk_config->ticks2Time(time_rot) * buffer->getVec3());
m_vehicle->setCushioningDisableTime(buffer->getUInt8()); m_vehicle->setCushioningDisableTime(buffer->getUInt8());
// Collision rewind // Collision rewind
m_bounce_back_ticks = buffer->getUInt16(); m_bounce_back_ticks = buffer->getUInt16();
float central_impulse_time = buffer->getFloat(); uint16_t central_impulse_ticks = buffer->getUInt16();
Vec3 additional_impulse = buffer->getVec3(); Vec3 additional_impulse = buffer->getVec3();
m_vehicle->setTimedCentralImpulse(central_impulse_time, m_vehicle->setTimedCentralImpulse(central_impulse_ticks,
additional_impulse, true/*rewind*/); additional_impulse, true/*rewind*/);
// For the raycast to determine the current material under the kart // For the raycast to determine the current material under the kart

View File

@ -536,7 +536,8 @@ void Skidding::update(int ticks, bool is_on_ground,
t = std::min(t, kp->getSkidRevertVisualTime()); t = std::min(t, kp->getSkidRevertVisualTime());
btVector3 rot(0, m_visual_rotation * kp->getSkidPostSkidRotateFactor(), 0); btVector3 rot(0, m_visual_rotation * kp->getSkidPostSkidRotateFactor(), 0);
m_kart->getVehicle()->setTimedRotation(t, rot); m_kart->getVehicle()->setTimedRotation(
(uint16_t)stk_config->time2Ticks(t), rot);
// skid_time is used to count backwards for the GFX // skid_time is used to count backwards for the GFX
m_skid_time = stk_config->time2Ticks(t); m_skid_time = stk_config->time2Ticks(t);
if(bonus_time>0) if(bonus_time>0)

View File

@ -124,9 +124,9 @@ void btKart::reset()
m_allow_sliding = false; m_allow_sliding = false;
m_num_wheels_on_ground = 0; m_num_wheels_on_ground = 0;
m_additional_impulse = btVector3(0,0,0); m_additional_impulse = btVector3(0,0,0);
m_time_additional_impulse = 0; m_ticks_additional_impulse = 0;
m_additional_rotation = btVector3(0,0,0); m_additional_rotation = btVector3(0,0,0);
m_time_additional_rotation = 0; m_ticks_additional_rotation = 0;
m_max_speed = -1.0f; m_max_speed = -1.0f;
m_min_speed = 0.0f; m_min_speed = 0.0f;
m_cushioning_disable_time = 0; m_cushioning_disable_time = 0;
@ -598,23 +598,21 @@ void btKart::updateVehicle( btScalar step )
// Apply additional impulse set by supertuxkart // Apply additional impulse set by supertuxkart
// -------------------------------------------- // --------------------------------------------
if(m_time_additional_impulse>0) if(m_ticks_additional_impulse>0)
{ {
float dt = step > m_time_additional_impulse // We have fixed timestep
? m_time_additional_impulse float dt = stk_config->ticks2Time(1);
: step;
m_chassisBody->applyCentralImpulse(m_additional_impulse*dt); m_chassisBody->applyCentralImpulse(m_additional_impulse*dt);
m_time_additional_impulse -= dt; m_ticks_additional_impulse--;
} }
// Apply additional rotation set by supertuxkart // Apply additional rotation set by supertuxkart
// --------------------------------------------- // ---------------------------------------------
if(m_time_additional_rotation>0) if(m_ticks_additional_rotation>0)
{ {
btTransform &t = m_chassisBody->getWorldTransform(); btTransform &t = m_chassisBody->getWorldTransform();
float dt = step > m_time_additional_rotation // We have fixed timestep
? m_time_additional_rotation float dt = stk_config->ticks2Time(1);
: step;
btQuaternion add_rot(m_additional_rotation.getY()*dt, btQuaternion add_rot(m_additional_rotation.getY()*dt,
m_additional_rotation.getX()*dt, m_additional_rotation.getX()*dt,
m_additional_rotation.getZ()*dt); m_additional_rotation.getZ()*dt);
@ -628,7 +626,7 @@ void btKart::updateVehicle( btScalar step )
// kart, or a strongly 'visual jolt' of the kart // kart, or a strongly 'visual jolt' of the kart
btTransform &iwt=m_chassisBody->getInterpolationWorldTransform(); btTransform &iwt=m_chassisBody->getInterpolationWorldTransform();
iwt.setRotation(iwt.getRotation()*add_rot); iwt.setRotation(iwt.getRotation()*add_rot);
m_time_additional_rotation -= dt; m_ticks_additional_rotation--;
} }
adjustSpeed(m_min_speed, m_max_speed); adjustSpeed(m_min_speed, m_max_speed);
} // updateVehicle } // updateVehicle
@ -883,7 +881,7 @@ void btKart::updateFriction(btScalar timeStep)
m_forwardImpulse[wheel] = rollingFriction; m_forwardImpulse[wheel] = rollingFriction;
if(m_time_additional_impulse>0) if(m_ticks_additional_impulse>0)
{ {
sliding = true; sliding = true;
m_wheelInfo[wheel].m_skidInfo = 0.0f; m_wheelInfo[wheel].m_skidInfo = 0.0f;
@ -913,7 +911,7 @@ void btKart::updateFriction(btScalar timeStep)
// Note: don't reset zipper speed, or the kart rewinder will // Note: don't reset zipper speed, or the kart rewinder will
// get incorrect zipper information. // get incorrect zipper information.
if (sliding && (m_allow_sliding || m_time_additional_impulse>0) ) if (sliding && (m_allow_sliding || m_ticks_additional_impulse>0) )
{ {
for (int wheel = 0; wheel < getNumWheels(); wheel++) for (int wheel = 0; wheel < getNumWheels(); wheel++)
{ {

View File

@ -19,6 +19,8 @@ class btDynamicsWorld;
#include "BulletDynamics/Vehicle/btWheelInfo.h" #include "BulletDynamics/Vehicle/btWheelInfo.h"
#include "BulletDynamics/Dynamics/btActionInterface.h" #include "BulletDynamics/Dynamics/btActionInterface.h"
#include "config/stk_config.hpp"
class btVehicleTuning; class btVehicleTuning;
class Kart; class Kart;
struct btWheelContactPoint; struct btWheelContactPoint;
@ -79,13 +81,13 @@ private:
btVector3 m_additional_impulse; btVector3 m_additional_impulse;
/** The time the additional impulse should be applied. */ /** The time the additional impulse should be applied. */
float m_time_additional_impulse; uint16_t m_ticks_additional_impulse;
/** Additional rotation that is applied over a certain amount of time. */ /** Additional rotation that is applied over a certain amount of time. */
btVector3 m_additional_rotation; btVector3 m_additional_rotation;
/** Duration over which the additional rotation is applied. */ /** Duration over which the additional rotation is applied. */
float m_time_additional_rotation; uint16_t m_ticks_additional_rotation;
/** The rigid body that is the chassis of the kart. */ /** The rigid body that is the chassis of the kart. */
btRigidBody *m_chassisBody; btRigidBody *m_chassisBody;
@ -221,36 +223,37 @@ public:
unsigned int getNumWheelsOnGround() const {return m_num_wheels_on_ground;} unsigned int getNumWheelsOnGround() const {return m_num_wheels_on_ground;}
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Sets an impulse that is applied for a certain amount of time. /** Sets an impulse that is applied for a certain amount of time.
* \param t Time for the impulse to be active. * \param t Ticks for the impulse to be active.
* \param imp The impulse to apply. */ * \param imp The impulse to apply. */
void setTimedCentralImpulse(float t, const btVector3 &imp, void setTimedCentralImpulse(uint16_t t, const btVector3 &imp,
bool rewind = false) bool rewind = false)
{ {
// Only add impulse if no other impulse is active. // Only add impulse if no other impulse is active.
if (m_time_additional_impulse > 0 && !rewind) return; if (m_ticks_additional_impulse > 0 && !rewind) return;
m_additional_impulse = imp; m_additional_impulse = imp;
m_time_additional_impulse = t; m_ticks_additional_impulse = t;
} // setTimedImpulse } // setTimedImpulse
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns the time an additional impulse is activated. */ /** Returns the time an additional impulse is activated. */
float getCentralImpulseTime() const { return m_time_additional_impulse; } uint16_t getCentralImpulseTicks() const
{ return m_ticks_additional_impulse; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
const btVector3& getAdditionalImpulse() const const btVector3& getAdditionalImpulse() const
{ return m_additional_impulse; } { return m_additional_impulse; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Sets a rotation that is applied over a certain amount of time (to avoid /** Sets a rotation that is applied over a certain amount of time (to avoid
* a too rapid changes in the kart). * a too rapid changes in the kart).
* \param t Time for the rotation to be applied. * \param t Ticks for the rotation to be applied.
* \param torque The rotation to apply. */ * \param torque The rotation to apply. */
void setTimedRotation(float t, const btVector3 &rot) void setTimedRotation(uint16_t t, const btVector3 &rot)
{ {
if(t>0) m_additional_rotation = rot/t; if(t>0) m_additional_rotation = rot / (stk_config->ticks2Time(t));
m_time_additional_rotation = t; m_ticks_additional_rotation = t;
} // setTimedTorque } // setTimedTorque
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
const btVector3& getTimedRotation() const { return m_additional_rotation; } const btVector3& getTimedRotation() const { return m_additional_rotation; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
float getTimedRotationTime() const { return m_time_additional_rotation; } uint16_t getTimedRotationTicks() const { return m_ticks_additional_rotation; }
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
/** Returns the time cushioning is disabled. Used for networking state /** Returns the time cushioning is disabled. Used for networking state
* saving. */ * saving. */

View File

@ -496,27 +496,29 @@ void Physics::KartKartCollision(AbstractKart *kart_a,
// First push one kart to the left (if there is not already // First push one kart to the left (if there is not already
// an impulse happening - one collision might cause more // an impulse happening - one collision might cause more
// than one impulse otherwise) // than one impulse otherwise)
if(right_kart->getVehicle()->getCentralImpulseTime()<=0) if(right_kart->getVehicle()->getCentralImpulseTicks()<=0)
{ {
const KartProperties *kp = left_kart->getKartProperties(); const KartProperties *kp = left_kart->getKartProperties();
Vec3 impulse(kp->getCollisionImpulse()*f_right, 0, 0); Vec3 impulse(kp->getCollisionImpulse()*f_right, 0, 0);
impulse = right_kart->getTrans().getBasis() * impulse; impulse = right_kart->getTrans().getBasis() * impulse;
right_kart->getVehicle() right_kart->getVehicle()
->setTimedCentralImpulse(kp->getCollisionImpulseTime(), ->setTimedCentralImpulse(
impulse); (uint16_t)stk_config->time2Ticks(kp->getCollisionImpulseTime()),
impulse);
right_kart ->getBody()->setAngularVelocity(btVector3(0,0,0)); right_kart ->getBody()->setAngularVelocity(btVector3(0,0,0));
} }
// Then push the other kart to the right (if there is no // Then push the other kart to the right (if there is no
// impulse happening atm). // impulse happening atm).
if(left_kart->getVehicle()->getCentralImpulseTime()<=0) if(left_kart->getVehicle()->getCentralImpulseTicks()<=0)
{ {
const KartProperties *kp = right_kart->getKartProperties(); const KartProperties *kp = right_kart->getKartProperties();
Vec3 impulse = Vec3(-kp->getCollisionImpulse()*f_left, 0, 0); Vec3 impulse = Vec3(-kp->getCollisionImpulse()*f_left, 0, 0);
impulse = left_kart->getTrans().getBasis() * impulse; impulse = left_kart->getTrans().getBasis() * impulse;
left_kart->getVehicle() left_kart->getVehicle()
->setTimedCentralImpulse(kp->getCollisionImpulseTime(), ->setTimedCentralImpulse(
impulse); (uint16_t)stk_config->time2Ticks(kp->getCollisionImpulseTime()),
impulse);
left_kart->getBody()->setAngularVelocity(btVector3(0,0,0)); left_kart->getBody()->setAngularVelocity(btVector3(0,0,0));
} }