Merge branch 'speed-cap-to-physics'

This commit is contained in:
hiker 2018-02-09 09:38:34 +11:00
commit b3243a3589
8 changed files with 107 additions and 78 deletions

View File

@ -39,7 +39,7 @@ public:
virtual void updateAction( btCollisionWorld* collisionWorld, btScalar deltaTimeStep)=0;
virtual void debugDraw(btIDebugDraw* debugDrawer) = 0;
virtual void resetMaxSpeed() = 0;
};
#endif //_BT_ACTION_INTERFACE_H

View File

@ -297,6 +297,12 @@ int btDiscreteDynamicsWorld::stepSimulation( btScalar timeStep,int maxSubSteps,
}
clearForces();
// Reset the max speeds of all karts, so that supertuxkart can
// set any new max_speed.
for (int i = 0; i<m_actions.size(); i++)
{
m_actions[i]->resetMaxSpeed();
}
#ifndef BT_NO_PROFILE
CProfileManager::Increment_Frame_Counter();

View File

@ -1209,7 +1209,7 @@ void Kart::eliminate()
void Kart::update(float dt)
{
// Reset any instand speed increase in the bullet kart
m_vehicle->resetInstantSpeed();
m_vehicle->setMinSpeed(0);
// update star effect (call will do nothing if stars are not activated)
m_stars_effect->update(dt);

View File

@ -74,7 +74,7 @@ BareNetworkString* KartRewinder::saveState() const
buffer->add(body->getLinearVelocity());
buffer->add(body->getAngularVelocity());
buffer->addUInt8(m_has_started); // necessary for startup speed boost
buffer->addFloat(m_vehicle->getInstantSpeedIncrease());
buffer->addFloat(m_vehicle->getMinSpeed());
// 2) Steering and other player controls
// -------------------------------------
@ -120,7 +120,7 @@ void KartRewinder::rewindToState(BareNetworkString *buffer)
// before Moveable::update() is called (which updates the transform)
setTrans(t);
m_has_started = buffer->getUInt8()!=0; // necessary for startup speed boost
m_vehicle->instantSpeedIncreaseTo(buffer->getFloat());
m_vehicle->setMinSpeed(buffer->getFloat());
// 2) Steering and other controls
// ------------------------------

View File

@ -133,7 +133,7 @@ void MaxSpeed::instantSpeedIncrease(unsigned int category,
// the speed might be too low for certain jumps).
if(speed < m_min_speed) speed = m_min_speed;
m_kart->getVehicle()->instantSpeedIncreaseTo(speed);
m_kart->getVehicle()->setMinSpeed(speed);
} // instantSpeedIncrease
@ -318,10 +318,15 @@ void MaxSpeed::update(float dt)
// --------------------------------------
if(m_min_speed > 0 && m_kart->getSpeed() < m_min_speed)
{
m_kart->getVehicle()->instantSpeedIncreaseTo(m_min_speed);
m_kart->getVehicle()->setMinSpeed(m_min_speed);
}
else if ( m_kart->getSpeed()>m_current_max_speed && m_kart->isOnGround() )
m_kart->getVehicle()->capSpeed(m_current_max_speed);
else
m_kart->getVehicle()->setMinSpeed(0); // no additional acceleration
if (m_kart->isOnGround())
m_kart->getVehicle()->setMaxSpeed(m_current_max_speed);
else
m_kart->getVehicle()->setMaxSpeed(9999.9f);
} // update

View File

@ -278,7 +278,7 @@ void OverWorld::onMouseClick(int x, int y)
// be the location of the challenge bubble.
AbstractKart* kart = getKart(0);
kart->setXYZ(challenge->m_position);
kart->getVehicle()->capSpeed(0);
kart->getVehicle()->setMaxSpeed(0);
unsigned int index = getRescuePositionIndex(kart);
btTransform s = getRescueTransform(index);

View File

@ -118,7 +118,6 @@ void btKart::reset()
updateWheelTransform(i, true);
}
m_visual_wheels_touch_ground = false;
m_zipper_speed = btScalar(0);
m_skid_angular_velocity = 0;
m_is_skidding = false;
m_allow_sliding = false;
@ -128,6 +127,8 @@ void btKart::reset()
m_additional_rotation = btVector3(0,0,0);
m_time_additional_rotation = 0;
m_visual_rotation = 0;
m_max_speed = -1.0f;
m_min_speed = 0.0f;
// Set the brakes so that karts don't slide downhill
setAllBrakes(5.0f);
@ -551,6 +552,7 @@ void btKart::updateVehicle( btScalar step )
iwt.setRotation(iwt.getRotation()*add_rot);
m_time_additional_rotation -= dt;
}
adjustSpeed(m_min_speed, m_max_speed);
} // updateVehicle
// ----------------------------------------------------------------------------
@ -770,51 +772,35 @@ void btKart::updateFriction(btScalar timeStep)
(btRigidBody*) wheelInfo.m_raycastInfo.m_groundObject;
if(!groundObject) continue;
if(m_zipper_speed > 0)
{
if (wheel==2 || wheel==3)
{
// The zipper velocity is the speed that should be
// reached. So compute the impulse to accelerate the
// kart up to that speed:
m_forwardImpulse[wheel] =
0.5f*(m_zipper_speed -
getRigidBody()->getLinearVelocity().length())
/ m_chassisBody->getInvMass();
}
btScalar rollingFriction = 0.f;
if (wheelInfo.m_engineForce != 0.f)
{
rollingFriction = wheelInfo.m_engineForce* timeStep;
}
else
{
btScalar rollingFriction = 0.f;
if (wheelInfo.m_engineForce != 0.f)
{
rollingFriction = wheelInfo.m_engineForce* timeStep;
}
else
{
btScalar defaultRollingFrictionImpulse = 0.f;
btScalar maxImpulse = wheelInfo.m_brake
? wheelInfo.m_brake
: defaultRollingFrictionImpulse;
btWheelContactPoint contactPt(m_chassisBody, groundObject,
wheelInfo.m_raycastInfo.m_contactPointWS,
m_forwardWS[wheel],maxImpulse);
rollingFriction = calcRollingFriction(contactPt);
// This is a work around for the problem that a kart shakes
// if it is braking: we get a minor impulse forward, which
// bullet then tries to offset by applying a backward
// impulse - which is a bit too big, causing a impulse
// backwards, ... till the kart is shaking backwards and
// forwards. By only applying half of the impulse in case
// of low friction this goes away.
if(wheelInfo.m_brake && fabsf(rollingFriction)<10)
rollingFriction*=0.5f;
}
m_forwardImpulse[wheel] = rollingFriction;
btScalar defaultRollingFrictionImpulse = 0.f;
btScalar maxImpulse = wheelInfo.m_brake
? wheelInfo.m_brake
: defaultRollingFrictionImpulse;
btWheelContactPoint contactPt(m_chassisBody, groundObject,
wheelInfo.m_raycastInfo.m_contactPointWS,
m_forwardWS[wheel], maxImpulse);
rollingFriction = calcRollingFriction(contactPt);
// This is a work around for the problem that a kart shakes
// if it is braking: we get a minor impulse forward, which
// bullet then tries to offset by applying a backward
// impulse - which is a bit too big, causing a impulse
// backwards, ... till the kart is shaking backwards and
// forwards. By only applying half of the impulse in case
// of low friction this goes away.
if (wheelInfo.m_brake && fabsf(rollingFriction) < 10)
rollingFriction *= 0.5f;
}
m_forwardImpulse[wheel] = rollingFriction;
if(m_time_additional_impulse>0)
{
sliding = true;
@ -1015,33 +1001,40 @@ void btKart::setSliding(bool active)
} // setSliding
// ----------------------------------------------------------------------------
/** Activates an additional speedup for the kart so that it reaches the
* specified speed.
* \param speed The speed to reach.
/** Adjusts the velocity of this kart to be at least the specified minimum,
* and less than or equal to the maximum. If necessary the kart will
* instantaneously change its speed.
* \param min_speed Minimum speed, 0 means no effect.
* \param max_speed Maximum speed the kart is allowed to have.
*/
void btKart::instantSpeedIncreaseTo(float speed)
{
// Avoid that a speed 'increase' might cause a slowdown
if (m_chassisBody->getLinearVelocity().length2() > speed*speed)
{
return;
}
m_zipper_speed = speed;
} // activateZipper
// ----------------------------------------------------------------------------
/** Caps the speed at a given value. If necessary the kart will
* instantaneously change its speed. */
void btKart::capSpeed(float max_speed)
void btKart::adjustSpeed(btScalar min_speed, btScalar max_speed)
{
const btVector3 &velocity = m_chassisBody->getLinearVelocity();
float speed = velocity.length();
if(speed!=0)
if (speed < min_speed && min_speed > 0)
{
if (speed > 0)
{
// The speedup is only for the direction of the normal.
const btVector3 &normal = m_kart->getNormal();
btVector3 upright_component = normal * normal.dot(velocity);
// Subtract the upright velocity component,
btVector3 v = velocity - upright_component;
const float velocity_ratio = min_speed / v.length();
// Scale the velocity in the plane, then add the upright component
// of the velocity back in.
m_chassisBody->setLinearVelocity( v*velocity_ratio
+ upright_component );
}
}
else if (speed >0 && max_speed >= 0 && speed > max_speed)
{
const float velocity_ratio = max_speed / speed;
m_chassisBody->setLinearVelocity(velocity * velocity_ratio);
}
} // capSpeed
} // adjustSpeed
// ----------------------------------------------------------------------------
//Shorter version of above raycast function. This is used when projecting

View File

@ -69,10 +69,6 @@ private:
btScalar m_damping;
btVehicleRaycaster *m_vehicleRaycaster;
/** The zipper speed (i.e. the velocity the kart should reach in
* the first frame that the zipper is active). */
btScalar m_zipper_speed;
/** The angular velocity to be applied when the kart skids.
* 0 means no skidding. */
btScalar m_skid_angular_velocity;
@ -124,6 +120,15 @@ private:
* for skid marks. */
float m_visual_rotation;
/** Minimum speed for the kart. Used e.g. for zippers. Setting this value
* will potentially instantaneously accelerate the kart to the minimum
* speed requested (in the next physics step). */
btScalar m_min_speed;
/** Maximum speed for the kart. It is reset to -1 at the end of each
* physics steps, so need to be set again by the application. */
btScalar m_max_speed;
/** True if the visual wheels touch the ground. */
bool m_visual_wheels_touch_ground;
@ -176,8 +181,8 @@ public:
virtual void updateFriction(btScalar timeStep);
public:
void setSliding(bool active);
void instantSpeedIncreaseTo(float speed);
void capSpeed(float max_speed);
void instantSpeedIncreaseTo(btScalar speed);
void adjustSpeed(btScalar min_speed, btScalar max_speed);
void updateAllWheelPositions();
// ------------------------------------------------------------------------
/** Returns true if both rear visual wheels touch the ground. */
@ -273,10 +278,30 @@ public:
m_time_additional_rotation = t;
} // setTimedTorque
// ------------------------------------------------------------------------
/** Returns the current zipper speed. */
float getInstantSpeedIncrease() const { return m_zipper_speed; }
/** Sets the maximum speed for this kart. */
void setMaxSpeed(float new_max_speed)
{
// Only change m_max_speed if it has not been set (<0), or
// the new value is smaller than the current maximum. For example,
// overworld will set the max_speed to 0 in case of teleporting to
// a bubble, but set it again later (based on zipper etc activated).
// We need to make sure that the 0 is maintained.
if(m_max_speed <0 || m_max_speed > new_max_speed)
m_max_speed = new_max_speed;
} // setMaxSpeed
// ------------------------------------------------------------------------
void resetInstantSpeed() { m_zipper_speed = 0; }
/** Resets the maximum so any new maximum value from the application will
* be accepted. */
virtual void resetMaxSpeed() { m_max_speed = -1.0f; m_min_speed = 0.0f; }
// ------------------------------------------------------------------------
/** Sets the minimum speed for this kart. */
void setMinSpeed(float s)
{
if(s > m_min_speed) m_min_speed = s;
}
// ------------------------------------------------------------------------
/** Returns the minimum speed for this kart. */
btScalar getMinSpeed() const { return m_min_speed; }
}; // class btKart
#endif //BT_KART_HPP