From 4a0444987a9e0828d9c5781bcb6f3634683e6c94 Mon Sep 17 00:00:00 2001 From: hiker Date: Fri, 20 Apr 2018 17:31:31 +1000 Subject: [PATCH] Fixed skidding in network mode. Only the jump duration is part of the state, the current height of the kart in case of a jump is then computed (without keeping track of speed etc). Jumping is now only done during updateGraphics(). --- src/graphics/skid_marks.cpp | 2 +- src/karts/kart.cpp | 12 ++++---- src/karts/kart_gfx.cpp | 2 +- src/karts/skidding.cpp | 61 ++++++++++++++++++++++++------------- src/karts/skidding.hpp | 18 ++--------- 5 files changed, 50 insertions(+), 45 deletions(-) diff --git a/src/graphics/skid_marks.cpp b/src/graphics/skid_marks.cpp index 3933eb0f1..77c06d38e 100644 --- a/src/graphics/skid_marks.cpp +++ b/src/graphics/skid_marks.cpp @@ -124,7 +124,7 @@ void SkidMarks::update(float dt, bool force_skid_marks, ( force_skid_marks || ( (skid->getSkidState()==Skidding::SKID_ACCUMULATE_LEFT|| skid->getSkidState()==Skidding::SKID_ACCUMULATE_RIGHT ) - && skid->getGraphicalJumpOffset()<=0 + && !skid->isJumping() && delta.length2()>=0.0001f ) ); if(m_skid_marking) diff --git a/src/karts/kart.cpp b/src/karts/kart.cpp index 7233e3cd7..98ea441ad 100644 --- a/src/karts/kart.cpp +++ b/src/karts/kart.cpp @@ -2428,9 +2428,9 @@ void Kart::updatePhysics(int ticks) m_skidding->update(ticks, isOnGround(), m_controls.getSteer(), m_controls.getSkidControl()); m_vehicle->setVisualRotation(m_skidding->getVisualSkidRotation()); - if(( m_skidding->getSkidState() == Skidding::SKID_ACCUMULATE_LEFT || - m_skidding->getSkidState() == Skidding::SKID_ACCUMULATE_RIGHT ) && - m_skidding->getGraphicalJumpOffset()==0) + if( ( m_skidding->getSkidState() == Skidding::SKID_ACCUMULATE_LEFT || + m_skidding->getSkidState() == Skidding::SKID_ACCUMULATE_RIGHT ) && + !m_skidding->isJumping() ) { if(m_skid_sound->getStatus()!=SFXBase::SFX_PLAYING && !isWheeless()) m_skid_sound->play(getXYZ()); @@ -3011,9 +3011,9 @@ void Kart::updateGraphics(float dt) Vec3 center_shift(0, 0, 0); - center_shift.setY(m_skidding->getGraphicalJumpOffset() - + fabsf(lean_height) - +m_graphical_y_offset); + // Update the skidding jump height: + float jump_height = m_skidding->updateGraphics(dt); + center_shift.setY(jump_height + fabsf(lean_height) + m_graphical_y_offset); center_shift = getTrans().getBasis() * center_shift; float heading = m_skidding->getVisualSkidRotation(); diff --git a/src/karts/kart_gfx.cpp b/src/karts/kart_gfx.cpp index 697e606dc..d344d49e3 100644 --- a/src/karts/kart_gfx.cpp +++ b/src/karts/kart_gfx.cpp @@ -370,7 +370,7 @@ void KartGFX::updateTerrain(const ParticleKind *pk) const float skidding = m_kart->getSkidding()->getSkidFactor(); // Only create particles when the kart is actually on ground bool on_ground = m_kart->isOnGround() && - m_kart->getSkidding()->getGraphicalJumpOffset()==0; + !m_kart->getSkidding()->isJumping(); if (skidding > 1.0f && on_ground) rate = fabsf(m_kart->getControls().getSteer()) > 0.8 ? skidding - 1 : 0; else if (speed >= 0.5f && on_ground) diff --git a/src/karts/skidding.cpp b/src/karts/skidding.cpp index 9754b7d53..becf2fdaa 100644 --- a/src/karts/skidding.cpp +++ b/src/karts/skidding.cpp @@ -72,9 +72,7 @@ void Skidding::reset() m_real_steering = 0.0f; m_visual_rotation = 0.0f; m_skid_bonus_ready = false; - m_gfx_jump_offset = 0.0f; m_remaining_jump_time = 0.0f; - m_jump_speed = 0.0f; m_kart->getKartGFX()->setCreationRateAbsolute(KartGFX::KGFX_SKIDL, 0); m_kart->getKartGFX()->setCreationRateAbsolute(KartGFX::KGFX_SKIDR, 0); m_kart->getKartGFX()->updateSkidLight(0); @@ -88,10 +86,11 @@ void Skidding::reset() // ---------------------------------------------------------------------------- /** Save the skidding state of a kart. It only saves the important physics - * values, not visual only values like m_visual_rotation, m_gfx_jump_offset, - * m_remaining_jump_time and m_jump_speed. Similarly m_real_steering is output - * of updateRewind() and will be recomputed every frame when update() is called, - * and similart m_skid_bonus_ready + * values including m_remaining_jump_time (while this is mostly a graphical + * effect, ou ycan't skid while still doing a jump, so it does affect the + * staet), but not visual only values like m_visual_rotation. Similarly + * m_real_steering is output of updateRewind() and will be recomputed every + * frame when update() is called, and similar for m_skid_bonus_ready * \param buffer Buffer for the state information. */ void Skidding::saveState(BareNetworkString *buffer) @@ -224,7 +223,38 @@ float Skidding::getSteeringWhenSkidding(float steering) const } // getSteeringWhenSkidding // ---------------------------------------------------------------------------- -/** Updates skidding status. +/** Called once per rendered frame to potentially update the graphical jump + * height. The jump at the start of a skid is graphical only. + * \param dt Time step size. + * \return Current height of the jump. + */ +float Skidding::updateGraphics(float dt) +{ + if (m_remaining_jump_time <= 0) return 0; + + if (m_remaining_jump_time < 0) + { + m_remaining_jump_time = 0.0f; + return 0.0f; + } + + const KartProperties *kp = m_kart->getKartProperties(); + // At the beginning of the jump the speed is: + // v0 = 0.5 * gravity * full_jump_time + // and it is reduced by gravity. So: + // v(t) = v0 - t*gravity + // Therefore the height 't' seconds after start of the jump is: + // h(t) = v0*t - 0.5 * gravity *t^2 + float gravity = Track::getCurrentTrack()->getGravity(); + float v0 = 0.5f * gravity * kp->getSkidGraphicalJumpTime(); + float jump_time = kp->getSkidGraphicalJumpTime() + - m_remaining_jump_time; + return v0 * jump_time - 0.5f * gravity * jump_time * jump_time; + +} // updateGraphics + + // ---------------------------------------------------------------------------- + /** Updates skidding status. * \param ticks Number of physics time steps - should be 1. * \param is_on_ground True if the kart is on ground. * \param steering Raw steering of the kart [-1,1], i.e. not adjusted by @@ -236,6 +266,8 @@ void Skidding::update(int ticks, bool is_on_ground, { float dt = stk_config->ticks2Time(ticks); + m_remaining_jump_time -= dt; + const KartProperties *kp = m_kart->getKartProperties(); // If a kart animation is shown, stop all skidding bonuses. @@ -285,19 +317,6 @@ void Skidding::update(int ticks, bool is_on_ground, else if (m_skid_factor < 1.0f) m_skid_factor = 1.0f; - // If skidding was started and a graphical jump should still - // be displayed, update the data - if(m_remaining_jump_time>0) - { - m_jump_speed -= Track::getCurrentTrack()->getGravity()*dt; - m_gfx_jump_offset += m_jump_speed * dt; - m_remaining_jump_time -= dt; - if(m_remaining_jump_time<0) - { - m_remaining_jump_time = 0.0f; - m_gfx_jump_offset = 0.0f; - } - } // This is only reached if the new skidding is enabled // --------------------------------------------------- @@ -347,8 +366,6 @@ void Skidding::update(int ticks, bool is_on_ground, m_kart->getVehicle()->getRigidBody()->applyCentralImpulse(imp); // Some karts might use a graphical-only jump. Set it up: - m_jump_speed = Track::getCurrentTrack()->getGravity() - * 0.5f * kp->getSkidGraphicalJumpTime(); m_remaining_jump_time = kp->getSkidGraphicalJumpTime(); #ifdef SKID_DEBUG diff --git a/src/karts/skidding.hpp b/src/karts/skidding.hpp index 1e37cf68b..1f86bd3d3 100644 --- a/src/karts/skidding.hpp +++ b/src/karts/skidding.hpp @@ -65,14 +65,6 @@ private: /** Set to >0 when a graphical jump is to be done. */ float m_remaining_jump_time; - /** A vertical offset used to make the kart do a graphical 'jump' when - * skidding is started. */ - float m_gfx_jump_offset; - - /** Keeps track of a graphical jump speed (which simulates the physics, - * i.e. gravity is used to reduce the jump speed. */ - float m_jump_speed; - public: /** SKID_NONE: Kart is currently not skidding. * SKID_ACCUMULATE_LEFT: Kart is skidding to the left and accumulating @@ -104,6 +96,7 @@ public: Skidding(Kart *kart); ~Skidding(); void reset(); + float updateGraphics(float dt); void update(int dt, bool is_on_ground, float steer, KartControl::SkidControl skidding); void saveState(BareNetworkString *buffer); @@ -124,12 +117,6 @@ public: * a fraction of the maximum steering angle ( so in [-1, 1]). */ float getSteeringFraction() { return m_real_steering; } // ------------------------------------------------------------------------ - /** Returns an additional height offset that is used to show a graphical - * jump when a skid starts. So when this is >0 the wheels appear not to - * touch the ground (though in reality the physical kart does, but also - * see physical jumping implemented in this object). */ - float getGraphicalJumpOffset() const { return m_gfx_jump_offset; } - // ------------------------------------------------------------------------ /** Returns the skidding state. */ SkidState getSkidState() const { return m_skid_state; } // ------------------------------------------------------------------------ @@ -139,7 +126,8 @@ public: * stopped skidding now. This function returns false if the kart is * actually using the skid bonus. */ bool getSkidBonusReady() const { return m_skid_bonus_ready; } - + // ------------------------------------------------------------------------ + bool isJumping() const { return m_remaining_jump_time > 0; } }; // Skidding