Skip to content

Commit

Permalink
Merge branch 'gravity_from_normal'
Browse files Browse the repository at this point in the history
  • Loading branch information
hiker committed Jan 30, 2014
2 parents 8156b8a + fe04a73 commit 9ef4885
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 22 deletions.
6 changes: 6 additions & 0 deletions src/graphics/material.cpp
Expand Up @@ -146,6 +146,7 @@ Material::Material(const XMLNode *node, int index, bool deprecated)

node->get("water-splash", &m_water_splash );
node->get("jump", &m_is_jump_texture );
node->get("has-gravity", &m_has_gravity );

if (m_collision_reaction != NORMAL)
{
Expand Down Expand Up @@ -344,6 +345,10 @@ Material::Material(const XMLNode *node, int index, bool deprecated)
}

} // for i <node->getNumNodes()

if(m_has_gravity)
m_high_tire_adhesion = true;

install(/*is_full_path*/false);
} // Material

Expand Down Expand Up @@ -410,6 +415,7 @@ void Material::init(unsigned int index)
m_is_heightmap = false;
m_water_splash = false;
m_is_jump_texture = false;
m_has_gravity = false;

for (int n=0; n<EMIT_KINDS_COUNT; n++)
{
Expand Down
10 changes: 10 additions & 0 deletions src/graphics/material.hpp
Expand Up @@ -111,6 +111,11 @@ class Material : public NoCopy
* leaving it and being in the air. */
bool m_is_jump_texture;

/** True if driving on this texture should adjust the gravity of the kart
* to be along the normal of the triangle. This allows karts to drive e.g
* upside down. */
bool m_has_gravity;

/** Speed of the 'main' wave in the water shader. Only used if
m_graphical_effect == WATER_SHADER */
float m_water_shader_speed_1;
Expand Down Expand Up @@ -312,6 +317,11 @@ class Material : public NoCopy
* jump animation. */
bool isJumpTexture() const { return m_is_jump_texture; }
// ------------------------------------------------------------------------
/** Returns true if this texture adjusts the gravity vector of the kart
* to be parallel to the normal of the triangle - which allows karts to
* e.g. drive upside down. */
bool hasGravity() const { return m_has_gravity; }
// ------------------------------------------------------------------------
/** Returns the zipper parametersfor the current material. */
void getZipperParameter(float *zipper_max_speed_increase,
float *zipper_duration,
Expand Down
45 changes: 34 additions & 11 deletions src/karts/kart.cpp
Expand Up @@ -409,7 +409,7 @@ void Kart::reset()
}


m_terrain_info->update(getXYZ());
m_terrain_info->update(getTrans());

// Reset is also called when the kart is created, at which time
// m_controller is not yet defined, so this has to be tested here.
Expand Down Expand Up @@ -1102,16 +1102,19 @@ void Kart::update(float dt)

if (!m_flying)
{
// When really on air, free fly, when near ground, try to glide / adjust for landing
// If zipped, be stable, so ramp+zipper can allow nice jumps without scripting the fly
if(!isNearGround() &&
m_max_speed->getSpeedIncreaseTimeLeft(MaxSpeed::MS_INCREASE_ZIPPER)<=0.0f )
// When really on air, free fly, when near ground, try to glide /
// adjust for landing. If zipped, be stable, so ramp+zipper can
// allow nice jumps without scripting the fly
// Also disable he upright constraint when gravity is changed by
// the terrain
if( (!isNearGround() &&
m_max_speed->getSpeedIncreaseTimeLeft(MaxSpeed::MS_INCREASE_ZIPPER)<=0.0f ) ||
(getMaterial() && getMaterial()->hasGravity()) )
m_uprightConstraint->setLimit(M_PI);
else
m_uprightConstraint->setLimit(m_kart_properties->getUprightTolerance());
}


// TODO: hiker said this probably will be moved to btKart or so when updating bullet engine.
// Neutralize any yaw change if the kart leaves the ground, so the kart falls more or less
// straight after jumping, but still allowing some "boat shake" (roll and pitch).
Expand Down Expand Up @@ -1177,22 +1180,27 @@ void Kart::update(float dt)
m_skid_sound->position ( getXYZ() );
m_boing_sound->position ( getXYZ() );

// Check if a kart is (nearly) upside down and not moving much --> automatic rescue
if(World::getWorld()->getTrack()->isAutoRescueEnabled() &&
// Check if a kart is (nearly) upside down and not moving much -->
// automatic rescue
// But only do this if auto-rescue is enabled (i.e. it will be disabled in
// battle mode), and the material the kart is driving on does not have
// gravity (which can
if(World::getWorld()->getTrack()->isAutoRescueEnabled() &&
(!m_terrain_info->getMaterial() ||
!m_terrain_info->getMaterial()->hasGravity()) &&
!getKartAnimation() && fabs(getRoll())>60*DEGREE_TO_RAD &&
fabs(getSpeed())<3.0f )
{
new RescueAnimation(this, /*is_auto_rescue*/true);
}

btTransform trans=getTrans();
// Add a certain epsilon (0.3) to the height of the kart. This avoids
// problems of the ray being cast from under the track (which happened
// e.g. on tux tollway when jumping down from the ramp, when the chassis
// partly tunnels through the track). While tunneling should not be
// happening (since Z velocity is clamped), the epsilon is left in place
// just to be on the safe side (it will not hit the chassis itself).
Vec3 pos_plus_epsilon = trans.getOrigin()+btVector3(0,0.3f,0);
Vec3 epsilon(0,0.3f,0);

// Make sure that the ray doesn't hit the kart. This is done by
// resetting the collision filter group, so that this collision
Expand All @@ -1203,7 +1211,8 @@ void Kart::update(float dt)
old_group = m_body->getBroadphaseHandle()->m_collisionFilterGroup;
m_body->getBroadphaseHandle()->m_collisionFilterGroup = 0;
}
m_terrain_info->update(pos_plus_epsilon);

m_terrain_info->update(getTrans(), epsilon);
if(m_body->getBroadphaseHandle())
{
m_body->getBroadphaseHandle()->m_collisionFilterGroup = old_group;
Expand All @@ -1212,6 +1221,10 @@ void Kart::update(float dt)
const Material* material=m_terrain_info->getMaterial();
if (!material) // kart falling off the track
{
Vec3 gravity(0, -9.8f, 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);
Expand All @@ -1221,6 +1234,16 @@ void Kart::update(float dt)
}
else
{
Vec3 gravity(0.0f, -9.8f, 0.0f);
btRigidBody *body = getVehicle()->getRigidBody();
// If the material should overwrite the gravity,
if(material->hasGravity())
{
Vec3 normal = m_terrain_info->getNormal();
gravity = normal * -9.8f;
}
body->setGravity(gravity);

handleMaterialSFX(material);
if (material->isDriveReset() && isOnGround())
new RescueAnimation(this);
Expand Down
12 changes: 6 additions & 6 deletions src/physics/physics.cpp
Expand Up @@ -454,16 +454,16 @@ btScalar Physics::solveGroup(btCollisionObject** bodies, int numBodies,
btPersistentManifold* contact_manifold =
m_dynamics_world->getDispatcher()->getManifoldByIndexInternal(i);

btCollisionObject* objA =
static_cast<btCollisionObject*>(contact_manifold->getBody0());
btCollisionObject* objB =
static_cast<btCollisionObject*>(contact_manifold->getBody1());
const btCollisionObject* objA =
static_cast<const btCollisionObject*>(contact_manifold->getBody0());
const btCollisionObject* objB =
static_cast<const btCollisionObject*>(contact_manifold->getBody1());

unsigned int num_contacts = contact_manifold->getNumContacts();
if(!num_contacts) continue; // no real collision

UserPointer *upA = (UserPointer*)(objA->getUserPointer());
UserPointer *upB = (UserPointer*)(objB->getUserPointer());
const UserPointer *upA = (UserPointer*)(objA->getUserPointer());
const UserPointer *upB = (UserPointer*)(objB->getUserPointer());

if(!upA || !upB) continue;

Expand Down
23 changes: 19 additions & 4 deletions src/tracks/terrain_info.cpp
Expand Up @@ -45,18 +45,33 @@ TerrainInfo::TerrainInfo(const Vec3 &pos)
m_material = NULL;
update(pos);
} // TerrainInfo

//-----------------------------------------------------------------------------
/** Update the terrain information based on the latest position.
* \param Position from which to start the rayast from.
*/
void TerrainInfo::update(const Vec3 &from)
{
m_last_material = m_material;
btVector3 to(from);
to.setY(-10000.0f);

const TriangleMesh &tm = World::getWorld()->getTrack()->getTriangleMesh();
tm.castRay(from, to, &m_hit_point, &m_material, &m_normal);
} // update
//-----------------------------------------------------------------------------
/** Update the terrain information based on the latest position.
* \param Position from which to start the rayast from.
*/
void TerrainInfo::update(const Vec3& pos)
void TerrainInfo::update(const btTransform &trans, const Vec3 &offset)
{
m_last_material = m_material;
btVector3 to(pos);
to.setY(-100000.f);
btVector3 from = trans(offset);
btVector3 to(0, -10000.0f, 0);
to = trans(to);

const TriangleMesh &tm = World::getWorld()->getTrack()->getTriangleMesh();
tm.castRay(pos, to, &m_hit_point, &m_material, &m_normal);
tm.castRay(from, to, &m_hit_point, &m_material, &m_normal);
} // update

// -----------------------------------------------------------------------------
Expand Down
10 changes: 9 additions & 1 deletion src/tracks/terrain_info.hpp
Expand Up @@ -21,6 +21,7 @@

#include "utils/vec3.hpp"

class btTransform;
class Material;

/** This class stores information about the triangle that's under an object, i.e.:
Expand All @@ -44,10 +45,17 @@ class TerrainInfo
TerrainInfo(const Vec3 &pos);
virtual ~TerrainInfo() {};

virtual void update(const Vec3 &pos);
bool getSurfaceInfo(const Vec3 &from, Vec3 *position,
const Material **m);
virtual void update(const btTransform &trans, const Vec3 &offset);
virtual void update(const Vec3 &from);

// ------------------------------------------------------------------------
/** Simple wrapper with no offset. */
virtual void update(const btTransform &trans)
{
update(trans, Vec3(0,0,0));
}
// ------------------------------------------------------------------------
/** Returns the height of the terrain. we're currently above */
float getHoT() const {return m_hit_point.getY(); }
Expand Down

0 comments on commit 9ef4885

Please sign in to comment.