Avoid setting gravity in btKart

This commit is contained in:
Benau 2016-09-19 11:18:16 +08:00
parent 8a0a9b2617
commit 36eca8bc55
3 changed files with 31 additions and 14 deletions

View File

@ -1370,6 +1370,13 @@ void Kart::update(float dt)
const Material* material=m_terrain_info->getMaterial();
if (!material) // kart falling off the track
{
if (!m_flying)
{
float g = World::getWorld()->getTrack()->getGravity();
Vec3 gravity(0, -g, 0);
btRigidBody *body = getVehicle()->getRigidBody();
body->setGravity(gravity);
}
// let kart fall a bit before rescuing
const Vec3 *min, *max;
World::getWorld()->getTrack()->getAABB(&min, &max);
@ -1380,6 +1387,19 @@ void Kart::update(float dt)
}
else
{
if (!m_flying)
{
float g = World::getWorld()->getTrack()->getGravity();
Vec3 gravity(0.0f, -g, 0.0f);
btRigidBody *body = getVehicle()->getRigidBody();
// If the material should overwrite the gravity,
if (material->hasGravity())
{
Vec3 normal = m_terrain_info->getNormal();
gravity = normal * -g;
}
body->setGravity(gravity);
} // if !flying
handleMaterialSFX(material);
if (material->isDriveReset() && isOnGround())
new RescueAnimation(this);

View File

@ -27,6 +27,7 @@
#include "config/user_config.hpp"
#include "graphics/camera.hpp"
#include "graphics/irr_driver.hpp"
#include "graphics/material.hpp"
#include "graphics/render_info.hpp"
#include "io/file_manager.hpp"
#include "input/device_manager.hpp"
@ -695,6 +696,13 @@ void World::resetAllKarts()
// Do a longer initial simulation, which should be long enough for all
// karts to be firmly on ground.
float g = World::getWorld()->getTrack()->getGravity();
for (KartList::iterator i = m_karts.begin(); i != m_karts.end(); i++)
{
if ((*i)->isGhostKart()) continue;
(*i)->getBody()->setGravity((*i)->getMaterial()->hasGravity() ?
(*i)->getNormal() * -g : Vec3(0, -g, 0));
}
for(int i=0; i<60; i++) m_physics->update(1.f/60.f);
for ( KartList::iterator i=m_karts.begin(); i!=m_karts.end(); i++)

View File

@ -25,7 +25,6 @@
#include "graphics/material.hpp"
#include "karts/kart.hpp"
#include "modes/world.hpp"
#include "physics/physics.hpp"
#include "physics/triangle_mesh.hpp"
#include "tracks/terrain_info.hpp"
#include "tracks/track.hpp"
@ -376,18 +375,6 @@ void btKart::updateAllWheelPositions()
// ----------------------------------------------------------------------------
void btKart::updateVehicle( btScalar step )
{
bool new_g = m_kart->getMaterial() && m_kart->getMaterial()->hasGravity();
if (new_g && !m_kart->isFlying())
{
getRigidBody()->setGravity(m_kart->getNormal() * World::getWorld()
->getPhysics()->getPhysicsWorld()->getGravity().y());
}
else if (!m_kart->isFlying())
{
getRigidBody()->setGravity(World::getWorld()
->getPhysics()->getPhysicsWorld()->getGravity());
}
updateAllWheelPositions();
const btTransform& chassisTrans = getChassisWorldTransform();
@ -459,7 +446,9 @@ void btKart::updateVehicle( btScalar step )
if(m_num_wheels_on_ground==0)
{
btVector3 kart_up = getChassisWorldTransform().getBasis().getColumn(1);
btVector3 terrain_up = new_g ? m_kart->getNormal() : Vec3(0, 1, 0);
btVector3 terrain_up =
m_kart->getMaterial() && m_kart->getMaterial()->hasGravity() ?
m_kart->getNormal() : Vec3(0, 1, 0);
// Length of axis depends on the angle - i.e. the further awat
// the kart is from being upright, the larger the applied impulse
// will be, resulting in fast changes when the kart is on its