Smoothing the speed of a kart (necessary to reduce camera stutter)
results in difficult to reproduce physics. So maintain two speed values: smoothed (for camera), and 'normal' (for everything else).
This commit is contained in:
@@ -81,7 +81,7 @@ void CameraNormal::smoothMoveCamera(float dt)
|
||||
const KartProperties *kp = m_kart->getKartProperties();
|
||||
float max_increase_with_zipper = kp->getZipperMaxSpeedIncrease();
|
||||
float max_speed_without_zipper = kp->getEngineMaxSpeed();
|
||||
float current_speed = m_kart->getSpeed();
|
||||
float current_speed = m_kart->getSmoothedSpeed();
|
||||
|
||||
const Skidding *ks = m_kart->getSkidding();
|
||||
float skid_factor = ks->getVisualSkidRotation();
|
||||
@@ -107,10 +107,10 @@ void CameraNormal::smoothMoveCamera(float dt)
|
||||
core::vector3df wanted_position = m_kart_camera_position_with_offset.toIrrVector();
|
||||
|
||||
float f = 5.0f;
|
||||
if ((m_kart->getSpeed() > 5 ) || (m_kart->getSpeed() < 0 ))
|
||||
if ((current_speed > 5 ) || (current_speed < 0 ))
|
||||
{
|
||||
f = m_kart->getSpeed()>0 ? m_kart->getSpeed()/3 + 1.0f
|
||||
: -1.5f * m_kart->getSpeed() + 2.0f;
|
||||
f = current_speed >0 ? current_speed/3 + 1.0f
|
||||
: -1.5f * current_speed + 2.0f;
|
||||
}
|
||||
current_position += (wanted_position - current_position) * (dt *f);
|
||||
|
||||
|
||||
@@ -274,6 +274,10 @@ public:
|
||||
* like Ghost. */
|
||||
virtual float getSpeed() const = 0;
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the exponentially smoothened speed of the kart in
|
||||
* which is removes shaking from camera. */
|
||||
virtual float getSmoothedSpeed() const = 0;
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the current maximum speed for this kart, this includes all
|
||||
* bonus and maluses that are currently applied. */
|
||||
virtual float getCurrentMaxSpeed() const = 0;
|
||||
|
||||
@@ -156,6 +156,7 @@ Kart::Kart (const std::string& ident, unsigned int world_kart_id,
|
||||
// Set position and heading:
|
||||
m_reset_transform = init_transform;
|
||||
m_speed = 0.0f;
|
||||
m_smoothed_speed = 0.0f;
|
||||
|
||||
m_kart_model->setKart(this);
|
||||
|
||||
@@ -352,6 +353,7 @@ void Kart::reset()
|
||||
m_brake_time = 0.0f;
|
||||
m_time_last_crash = 0.0f;
|
||||
m_speed = 0.0f;
|
||||
m_smoothed_speed = 0.0f;
|
||||
m_current_lean = 0.0f;
|
||||
m_view_blocked_by_plunger = 0.0f;
|
||||
m_bubblegum_time = 0.0f;
|
||||
@@ -1518,10 +1520,9 @@ void Kart::updateSpeed()
|
||||
// in the distance between kart and camera to jitter as well (typically
|
||||
// only in the order of centimetres though). Smoothing the speed value
|
||||
// gets rid of this jitter, and also r
|
||||
float old_speed = m_speed;
|
||||
m_speed = getVehicle()->getRigidBody()->getLinearVelocity().length();
|
||||
float f = 0.3f;
|
||||
m_speed = f*m_speed + (1.0f - f)*old_speed;
|
||||
m_smoothed_speed = f*m_speed + (1.0f - f)*m_smoothed_speed;
|
||||
|
||||
// calculate direction of m_speed
|
||||
const btTransform& chassisTrans = getVehicle()->getChassisWorldTransform();
|
||||
@@ -1531,16 +1532,18 @@ void Kart::updateSpeed()
|
||||
chassisTrans.getBasis()[2][2]);
|
||||
|
||||
if (forwardW.dot(getVehicle()->getRigidBody()->getLinearVelocity()) < btScalar(0.))
|
||||
m_speed *= -1.f;
|
||||
{
|
||||
m_speed = -m_speed;
|
||||
}
|
||||
|
||||
// At low velocity, forces on kart push it back and forth so we ignore this
|
||||
if (fabsf(m_speed) < 0.2f) // quick'n'dirty workaround for bug 1776883
|
||||
m_speed = 0;
|
||||
|
||||
if (dynamic_cast<RescueAnimation*>(getKartAnimation()) ||
|
||||
dynamic_cast<ExplosionAnimation*>(getKartAnimation()))
|
||||
// - quick'n'dirty workaround for bug 1776883
|
||||
if (fabsf(m_speed) < 0.2f ||
|
||||
dynamic_cast<RescueAnimation*> ( getKartAnimation() ) ||
|
||||
dynamic_cast<ExplosionAnimation*>( getKartAnimation() ) )
|
||||
{
|
||||
m_speed = 0;
|
||||
m_speed = 0;
|
||||
m_smoothed_speed = 0;
|
||||
}
|
||||
} // updateSpeed
|
||||
|
||||
|
||||
@@ -197,7 +197,11 @@ protected:
|
||||
/** When a kart has its view blocked by the plunger, this variable will be
|
||||
* > 0 the number it contains is the time left before removing plunger. */
|
||||
float m_view_blocked_by_plunger;
|
||||
/** The current speed (i.e. length of velocity vector) of this kart. */
|
||||
float m_speed;
|
||||
/** For camera handling an exponentially smoothened value is used, which
|
||||
* reduces stuttering of the camera. */
|
||||
float m_smoothed_speed;
|
||||
|
||||
std::vector<SFXBase*> m_custom_sounds;
|
||||
SFXBase *m_beep_sound;
|
||||
@@ -364,6 +368,9 @@ public:
|
||||
/** Returns the speed of the kart in meters/second. */
|
||||
virtual float getSpeed() const {return m_speed; }
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the speed of the kart in meters/second. */
|
||||
virtual float getSmoothedSpeed() const { return m_smoothed_speed; }
|
||||
// ------------------------------------------------------------------------
|
||||
/** This is used on the client side only to set the speed of the kart
|
||||
* from the server information. */
|
||||
virtual void setSpeed(float s) {m_speed = s; }
|
||||
|
||||
Reference in New Issue
Block a user