diff --git a/src/karts/kart.cpp b/src/karts/kart.cpp index ab4cf5788..ae049374e 100644 --- a/src/karts/kart.cpp +++ b/src/karts/kart.cpp @@ -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); diff --git a/src/modes/world.cpp b/src/modes/world.cpp index a69ff36de..c5d8a1242 100644 --- a/src/modes/world.cpp +++ b/src/modes/world.cpp @@ -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++) diff --git a/src/physics/btKart.cpp b/src/physics/btKart.cpp index 841915993..b2c0d50f9 100644 --- a/src/physics/btKart.cpp +++ b/src/physics/btKart.cpp @@ -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