From d203239aa2afc4f50061d58f77a526e053fa5206 Mon Sep 17 00:00:00 2001 From: Benau Date: Tue, 31 Jul 2018 01:14:02 +0800 Subject: [PATCH] Improve kart kart collision in network --- src/karts/kart.cpp | 4 ++-- src/karts/kart.hpp | 2 +- src/karts/kart_rewinder.cpp | 18 ++++++++++++++---- src/physics/btKart.hpp | 8 ++++++-- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/karts/kart.cpp b/src/karts/kart.cpp index 977bb4c09..d94add348 100644 --- a/src/karts/kart.cpp +++ b/src/karts/kart.cpp @@ -2440,8 +2440,8 @@ void Kart::updatePhysics(int ticks) /*fade_out_time*/stk_config->time2Ticks(5.0f)); } } - - m_bounce_back_ticks-=ticks; + if (m_bounce_back_ticks > std::numeric_limits::min()) + m_bounce_back_ticks -= ticks; updateEnginePowerAndBrakes(ticks); diff --git a/src/karts/kart.hpp b/src/karts/kart.hpp index 1fb864f82..a163ee835 100644 --- a/src/karts/kart.hpp +++ b/src/karts/kart.hpp @@ -157,7 +157,7 @@ protected: /** A short time after a collision acceleration is disabled to allow * the karts to bounce back*/ - int m_bounce_back_ticks; + int16_t m_bounce_back_ticks; /** Time a kart is invulnerable. */ int16_t m_invulnerable_ticks; diff --git a/src/karts/kart_rewinder.cpp b/src/karts/kart_rewinder.cpp index dc83f39a6..e6a2fd84f 100644 --- a/src/karts/kart_rewinder.cpp +++ b/src/karts/kart_rewinder.cpp @@ -125,6 +125,11 @@ BareNetworkString* KartRewinder::saveState(std::vector* ru) buffer->add(m_vehicle->getTimedRotation()); buffer->addUInt8(m_vehicle->getCushioningDisableTime()); + // For collision rewind + buffer->addUInt16(m_bounce_back_ticks); + buffer->addFloat(m_vehicle->getCentralImpulseTime()); + buffer->add(m_vehicle->getAdditionalImpulse()); + // 2) Steering and other player controls // ------------------------------------- getControls().saveState(buffer); @@ -193,7 +198,14 @@ void KartRewinder::restoreState(BareNetworkString *buffer, int count) // Set timed rotation divides by time_rot m_vehicle->setTimedRotation(time_rot, time_rot*buffer->getVec3()); m_vehicle->setCushioningDisableTime(buffer->getUInt8()); - + + // Collision rewind + m_bounce_back_ticks = buffer->getUInt16(); + float central_impulse_time = buffer->getFloat(); + Vec3 additional_impulse = buffer->getVec3(); + m_vehicle->setTimedCentralImpulse(central_impulse_time, + additional_impulse, true/*rewind*/); + // For the raycast to determine the current material under the kart // the m_hardPointWS of the wheels is used. So after a rewind we // must restore the m_hardPointWS to the new values, otherwise they @@ -251,7 +263,6 @@ std::function KartRewinder::getLocalStateRestoreFunction() // Variable can be saved locally if its adjustment only depends on the kart // itself bool has_started = m_has_started; - int bounce_back_ticks = m_bounce_back_ticks; int brake_ticks = m_brake_ticks; int8_t min_nitro_ticks = m_min_nitro_ticks; @@ -277,12 +288,11 @@ std::function KartRewinder::getLocalStateRestoreFunction() // Skidding local state float remaining_jump_time = m_skidding->m_remaining_jump_time; - return [has_started, bounce_back_ticks, brake_ticks, min_nitro_ticks, + return [has_started, brake_ticks, min_nitro_ticks, initial_speed, steer_val_l, steer_val_r, current_fraction, max_speed_fraction, remaining_jump_time, this]() { m_has_started = has_started; - m_bounce_back_ticks = bounce_back_ticks; m_brake_ticks = brake_ticks; m_min_nitro_ticks = min_nitro_ticks; getAttachment()->setInitialSpeed(initial_speed); diff --git a/src/physics/btKart.hpp b/src/physics/btKart.hpp index a8758eb85..0dda75331 100644 --- a/src/physics/btKart.hpp +++ b/src/physics/btKart.hpp @@ -223,10 +223,11 @@ public: /** Sets an impulse that is applied for a certain amount of time. * \param t Time for the impulse to be active. * \param imp The impulse to apply. */ - void setTimedCentralImpulse(float t, const btVector3 &imp) + void setTimedCentralImpulse(float t, const btVector3 &imp, + bool rewind = false) { // Only add impulse if no other impulse is active. - if(m_time_additional_impulse>0) return; + if (m_time_additional_impulse > 0 && !rewind) return; m_additional_impulse = imp; m_time_additional_impulse = t; } // setTimedImpulse @@ -234,6 +235,9 @@ public: /** Returns the time an additional impulse is activated. */ float getCentralImpulseTime() const { return m_time_additional_impulse; } // ------------------------------------------------------------------------ + const btVector3& getAdditionalImpulse() const + { return m_additional_impulse; } + // ------------------------------------------------------------------------ /** Sets a rotation that is applied over a certain amount of time (to avoid * a too rapid changes in the kart). * \param t Time for the rotation to be applied.