From f48491c2790ab94cbe11616c58c69669621a1739 Mon Sep 17 00:00:00 2001 From: Benau Date: Mon, 20 Aug 2018 15:40:29 +0800 Subject: [PATCH] Make sure all karts has the same position before go phase --- src/karts/abstract_kart.cpp | 34 ++++++++++++++++++++++++---------- src/karts/abstract_kart.hpp | 5 ++++- src/karts/ghost_kart.hpp | 4 ++-- src/modes/world.cpp | 2 ++ 4 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/karts/abstract_kart.cpp b/src/karts/abstract_kart.cpp index f5ed4e05b..d6fda8d50 100644 --- a/src/karts/abstract_kart.cpp +++ b/src/karts/abstract_kart.cpp @@ -143,16 +143,6 @@ void AbstractKart::setKartAnimation(AbstractKartAnimation *ka) m_kart_animation = ka; } // setKartAnimation -// ---------------------------------------------------------------------------- -/** Moves the current physical transform into this kart's position. - */ -void AbstractKart::kartIsInRestNow() -{ - // Update the kart transforms with the newly computed position - // after all karts are reset - setTrans(getBody()->getWorldTransform()); -} // kartIsInRest - // ---------------------------------------------------------------------------- /** Returns the time at which the kart was at a given distance. * Returns -1.0f if none */ @@ -160,3 +150,27 @@ float AbstractKart::getTimeForDistance(float distance) { return -1.0f; } // getTimeForDistance + +// ---------------------------------------------------------------------------- +/** Moves the current physical transform into this kart's position. + */ +void AbstractKart::kartIsInRestNow() +{ + // Update the kart transforms with the newly computed position + // after all karts are reset + m_starting_transform = getBody()->getWorldTransform(); + setTrans(m_starting_transform); +} // kartIsInRest + +// ------------------------------------------------------------------------ +/** Called before go phase to make sure all karts start at the same + * position in case there is a slope. */ +void AbstractKart::makeKartRest() +{ + btRigidBody *body = getBody(); + body->clearForces(); + body->setLinearVelocity(Vec3(0.0f)); + body->setAngularVelocity(Vec3(0.0f)); + body->proceedToTransform(m_starting_transform); + setTrans(m_starting_transform); +} // makeKartRest diff --git a/src/karts/abstract_kart.hpp b/src/karts/abstract_kart.hpp index be51191b1..95c479a01 100644 --- a/src/karts/abstract_kart.hpp +++ b/src/karts/abstract_kart.hpp @@ -74,7 +74,7 @@ private: /** Index of kart in world. */ unsigned int m_world_kart_id; - + btTransform m_starting_transform; protected: /** The kart properties. */ std::unique_ptr m_kart_properties; @@ -509,6 +509,9 @@ public: virtual void playSound(SFXBuffer* buffer) = 0; // ------------------------------------------------------------------------ virtual bool isVisible() = 0; + // ------------------------------------------------------------------------ + virtual void makeKartRest(); + }; // AbstractKart diff --git a/src/karts/ghost_kart.hpp b/src/karts/ghost_kart.hpp index 47fe0b98e..f91d79287 100644 --- a/src/karts/ghost_kart.hpp +++ b/src/karts/ghost_kart.hpp @@ -98,9 +98,9 @@ public: float getTimeForEggs(int egg_number); // ------------------------------------------------------------------------ - virtual void kartIsInRestNow() OVERRIDE {}; + virtual void kartIsInRestNow() OVERRIDE {} // ------------------------------------------------------------------------ - + virtual void makeKartRest() OVERRIDE {} }; // GhostKart #endif diff --git a/src/modes/world.cpp b/src/modes/world.cpp index 06a1ad67e..c5a8c53bc 100644 --- a/src/modes/world.cpp +++ b/src/modes/world.cpp @@ -1064,6 +1064,8 @@ void World::update(int ticks) // Update all karts that are not eliminated if(!m_karts[i]->isEliminated() || (sta && sta->isMoving())) m_karts[i]->update(ticks); + if (isStartPhase()) + m_karts[i]->makeKartRest(); } PROFILER_POP_CPU_MARKER(); if(race_manager->isRecordingRace()) ReplayRecorder::get()->update(ticks);