Bullet only:
1) some minor restructuring as preparation for converting the missiles to bullet. 2) Added support for later bullet versions (using btBroadphaseInterface instead of btOverlappingPairCache in ctor of btDiscreteDynamicsWorld). Default is still the old interface, use -DNEWBULLET for physics.cpp if a newer bullet version is available git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/trunk/supertuxkart@1270 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
parent
d06c13fb36
commit
638970bd46
70
src/kart.cpp
70
src/kart.cpp
@ -184,7 +184,6 @@ void Kart::createPhysics(ssgEntity *obj)
|
||||
btBoxShape *shape = new btBoxShape(btVector3(0.5*kart_width,
|
||||
0.5*kart_length,
|
||||
0.5*m_kart_height));
|
||||
m_kart_chassis = new btCompoundShape();
|
||||
btTransform shiftCenterOfGravity;
|
||||
shiftCenterOfGravity.setIdentity();
|
||||
// Shift center of gravity downwards, so that the kart
|
||||
@ -192,6 +191,9 @@ void Kart::createPhysics(ssgEntity *obj)
|
||||
// (it's in units of kart_height)
|
||||
const float CENTER_SHIFT = getGravityCenterShift();
|
||||
shiftCenterOfGravity.setOrigin(btVector3(0.0f,0.0f,CENTER_SHIFT*m_kart_height));
|
||||
|
||||
// kart chassis is actually managed in moveable as shape
|
||||
m_kart_chassis = new btCompoundShape();
|
||||
m_kart_chassis->addChildShape(shiftCenterOfGravity, shape);
|
||||
|
||||
// Set mass and inertia
|
||||
@ -204,28 +206,24 @@ void Kart::createPhysics(ssgEntity *obj)
|
||||
// --------------------
|
||||
btTransform trans;
|
||||
trans.setIdentity();
|
||||
m_motion_state = new btDefaultMotionState(trans);
|
||||
createBody(mass, trans, m_kart_chassis, inertia);
|
||||
|
||||
// Then create a rigid body
|
||||
// ------------------------
|
||||
m_kart_body = new btRigidBody(mass, m_motion_state,
|
||||
m_kart_chassis, inertia);
|
||||
m_kart_body->setDamping(m_kart_properties->getChassisLinearDamping(),
|
||||
m_kart_properties->getChassisAngularDamping() );
|
||||
m_body->setDamping(m_kart_properties->getChassisLinearDamping(),
|
||||
m_kart_properties->getChassisAngularDamping() );
|
||||
|
||||
// Reset velocities
|
||||
// ----------------
|
||||
m_kart_body->setLinearVelocity (btVector3(0.0f,0.0f,0.0f));
|
||||
m_kart_body->setAngularVelocity(btVector3(0.0f,0.0f,0.0f));
|
||||
m_body->setLinearVelocity (btVector3(0.0f,0.0f,0.0f));
|
||||
m_body->setAngularVelocity(btVector3(0.0f,0.0f,0.0f));
|
||||
|
||||
// Create the actual vehicle
|
||||
// -------------------------
|
||||
m_vehicle_raycaster =
|
||||
new btDefaultVehicleRaycaster(world->getPhysics()->getPhysicsWorld());
|
||||
m_tuning = new btRaycastVehicle::btVehicleTuning();
|
||||
m_vehicle = new btRaycastVehicle(*m_tuning, m_kart_body, m_vehicle_raycaster);
|
||||
m_vehicle = new btRaycastVehicle(*m_tuning, m_body, m_vehicle_raycaster);
|
||||
// never deactivate the vehicle
|
||||
m_kart_body->setActivationState(DISABLE_DEACTIVATION);
|
||||
m_body->setActivationState(DISABLE_DEACTIVATION);
|
||||
m_vehicle->setCoordinateSystem(/*right: */ 0, /*up: */ 2, /*forward: */ 1);
|
||||
|
||||
// Add wheels
|
||||
@ -311,9 +309,7 @@ Kart::~Kart()
|
||||
{
|
||||
delete m_kart_chassis->getChildShape(i);
|
||||
}
|
||||
delete m_kart_chassis;
|
||||
delete m_kart_body;
|
||||
delete m_motion_state;
|
||||
// the actual shape gets deleted in ~moveable
|
||||
#endif
|
||||
} // ~Kart
|
||||
|
||||
@ -325,7 +321,7 @@ Kart::~Kart()
|
||||
#ifdef BULLET
|
||||
bool Kart::isInRest()
|
||||
{
|
||||
return fabs(m_kart_body->getLinearVelocity ().z())<0.2;
|
||||
return fabs(m_body->getLinearVelocity ().z())<0.2;
|
||||
} // isInRest
|
||||
#endif
|
||||
//-----------------------------------------------------------------------------
|
||||
@ -335,12 +331,12 @@ bool Kart::isInRest()
|
||||
void Kart::adjustSpeedWeight(float f)
|
||||
{
|
||||
#ifdef BULLET
|
||||
m_kart_body->setLinearVelocity(m_kart_body->getLinearVelocity()*f);
|
||||
m_body->setLinearVelocity(m_body->getLinearVelocity()*f);
|
||||
// getMass returns the mass increased by the attachment
|
||||
btVector3 inertia;
|
||||
float m=getMass();
|
||||
m_kart_chassis->calculateLocalInertia(m, inertia);
|
||||
m_kart_body->setMassProps(m, inertia);
|
||||
m_body->setMassProps(m, inertia);
|
||||
#else
|
||||
getVelocity()->xyz[1] *= f;
|
||||
#endif
|
||||
@ -405,9 +401,9 @@ void Kart::reset()
|
||||
m_reset_pos.xyz[2]+0.5*m_kart_height));
|
||||
m_vehicle->applyEngineForce (0.0f, 2);
|
||||
m_vehicle->applyEngineForce (0.0f, 3);
|
||||
m_kart_body->setCenterOfMassTransform(*trans);
|
||||
m_kart_body->setLinearVelocity (btVector3(0.0f,0.0f,0.0f));
|
||||
m_kart_body->setAngularVelocity(btVector3(0.0f,0.0f,0.0f));
|
||||
m_body->setCenterOfMassTransform(*trans);
|
||||
m_body->setLinearVelocity (btVector3(0.0f,0.0f,0.0f));
|
||||
m_body->setAngularVelocity(btVector3(0.0f,0.0f,0.0f));
|
||||
for(int j=0; j<m_vehicle->getNumWheels(); j++)
|
||||
{
|
||||
m_vehicle->updateWheelTransform(j, true);
|
||||
@ -594,7 +590,7 @@ void Kart::handleExplosion(const sgVec3& pos, bool direct_hit)
|
||||
{
|
||||
#ifdef BULLET
|
||||
if(direct_hit) {
|
||||
btVector3 velocity = m_kart_body->getLinearVelocity();
|
||||
btVector3 velocity = m_body->getLinearVelocity();
|
||||
|
||||
velocity.setX( 0.0f );
|
||||
velocity.setY( 0.0f );
|
||||
@ -631,7 +627,7 @@ void Kart::forceCrash ()
|
||||
{
|
||||
m_wheelie_angle = CRASH_PITCH ;
|
||||
#ifdef BULLET
|
||||
btVector3 velocity = m_kart_body->getLinearVelocity();
|
||||
btVector3 velocity = m_body->getLinearVelocity();
|
||||
|
||||
velocity.setY( 0.0f );
|
||||
velocity.setX( 0.0f );
|
||||
@ -715,14 +711,14 @@ void Kart::update (float dt)
|
||||
#ifdef BULLET
|
||||
m_curr_pos.xyz[2] += rescue_height*dt/rescue_time;
|
||||
|
||||
btTransform pos=m_kart_body->getCenterOfMassTransform();
|
||||
btTransform pos=m_body->getCenterOfMassTransform();
|
||||
pos.setOrigin(btVector3(m_curr_pos.xyz[0],m_curr_pos.xyz[1],m_curr_pos.xyz[2]));
|
||||
btQuaternion q_roll (btVector3(0., 1., 0.),
|
||||
-m_rescue_roll*dt/rescue_time*M_PI/180.0);
|
||||
btQuaternion q_pitch(btVector3(1., 0., 0.),
|
||||
-m_rescue_pitch*dt/rescue_time*M_PI/180.0);
|
||||
pos.setRotation(pos.getRotation()*q_roll*q_pitch);
|
||||
m_kart_body->setCenterOfMassTransform(pos);
|
||||
m_body->setCenterOfMassTransform(pos);
|
||||
m_motion_state->setWorldTransform(pos);
|
||||
//printf("Set %f %f %f\n",pos.getOrigin().x(),pos.getOrigin().y(),pos.getOrigin().z());
|
||||
#else
|
||||
@ -856,7 +852,7 @@ void Kart::draw()
|
||||
t.getOpenGLMatrix(m);
|
||||
|
||||
btVector3 wire_color(0.5f, 0.5f, 0.5f);
|
||||
world->getPhysics()->debugDraw(m, m_kart_body->getCollisionShape(),
|
||||
world->getPhysics()->debugDraw(m, m_body->getCollisionShape(),
|
||||
wire_color);
|
||||
btCylinderShapeX *wheelShape =
|
||||
new btCylinderShapeX( btVector3(0.3,
|
||||
@ -895,7 +891,7 @@ float Kart::handleWheelie(float dt)
|
||||
}
|
||||
if(m_wheelie_angle <=0.0f) return 0.0f;
|
||||
|
||||
const btTransform& chassisTrans = m_kart_body->getCenterOfMassTransform();
|
||||
const btTransform& chassisTrans = m_body->getCenterOfMassTransform();
|
||||
btVector3 targetUp(0.0f, 0.0f, 1.0f);
|
||||
btVector3 forwardW (chassisTrans.getBasis()[0][1],
|
||||
chassisTrans.getBasis()[1][1],
|
||||
@ -908,14 +904,14 @@ float Kart::handleWheelie(float dt)
|
||||
const float balance_recovery= m_kart_properties->getWheelieBalanceRecovery();
|
||||
float alpha = (targetUp.dot(forwardW));
|
||||
float deltaalpha = m_wheelie_angle*M_PI/180.0f - alpha;
|
||||
btVector3 angvel = m_kart_body->getAngularVelocity();
|
||||
btVector3 angvel = m_body->getAngularVelocity();
|
||||
float projvel = angvel.dot(crossProd);
|
||||
float deltavel = -projvel * gLeanRecovery / step
|
||||
-deltaalpha * balance_recovery / step;
|
||||
btVector3 deltaangvel = deltavel * crossProd;
|
||||
|
||||
angvel += deltaangvel;
|
||||
m_kart_body->setAngularVelocity(angvel);
|
||||
m_body->setAngularVelocity(angvel);
|
||||
return m_kart_properties->getWheeliePowerBoost() * getMaxPower()
|
||||
* m_wheelie_angle/getWheelieMaxPitch();
|
||||
} // handleWheelie
|
||||
@ -968,7 +964,7 @@ void Kart::updatePhysics (float dt)
|
||||
{
|
||||
//Vector3 impulse(0.0f, 0.0f, 10.0f);
|
||||
// getVehicle()->getRigidBody()->applyCentralImpulse(impulse);
|
||||
btVector3 velocity = m_kart_body->getLinearVelocity();
|
||||
btVector3 velocity = m_body->getLinearVelocity();
|
||||
velocity.setZ( 3.0f );
|
||||
|
||||
getVehicle()->getRigidBody()->setLinearVelocity( velocity );
|
||||
@ -997,7 +993,7 @@ void Kart::updatePhysics (float dt)
|
||||
{
|
||||
const float velocity_ratio = max_speed/m_speed;
|
||||
m_speed = max_speed;
|
||||
btVector3 velocity = m_kart_body->getLinearVelocity();
|
||||
btVector3 velocity = m_body->getLinearVelocity();
|
||||
|
||||
velocity.setY( velocity.getY() * velocity_ratio );
|
||||
velocity.setX( velocity.getX() * velocity_ratio );
|
||||
@ -1267,17 +1263,17 @@ void Kart::endRescue()
|
||||
m_rescue = false ;
|
||||
#ifdef BULLET
|
||||
world->getPhysics()->addKart(this, m_vehicle);
|
||||
m_kart_body->setLinearVelocity (btVector3(0.0f,0.0f,0.0f));
|
||||
m_kart_body->setAngularVelocity(btVector3(0.0f,0.0f,0.0f));
|
||||
m_body->setLinearVelocity (btVector3(0.0f,0.0f,0.0f));
|
||||
m_body->setAngularVelocity(btVector3(0.0f,0.0f,0.0f));
|
||||
// FIXME: This code positions the kart correctly back on the track
|
||||
// (nearest waypoint) - but if the kart is simply upside down,
|
||||
// it feels better if the kart is left where it was. Perhaps
|
||||
// this code should only be used if a rescue was not triggered
|
||||
// by the kart being upside down??
|
||||
btTransform pos=m_kart_body->getCenterOfMassTransform();
|
||||
btTransform pos=m_body->getCenterOfMassTransform();
|
||||
pos.setOrigin(btVector3(m_curr_pos.xyz[0],m_curr_pos.xyz[1],
|
||||
m_curr_pos.xyz[2]+0.5*m_kart_height));
|
||||
m_kart_body->setCenterOfMassTransform(pos);
|
||||
m_body->setCenterOfMassTransform(pos);
|
||||
m_motion_state->setWorldTransform(pos);
|
||||
|
||||
#endif
|
||||
@ -1522,7 +1518,7 @@ void Kart::placeModel ()
|
||||
btTransform t;
|
||||
if(m_rescue)
|
||||
{
|
||||
t=m_kart_body->getCenterOfMassTransform();
|
||||
t=m_body->getCenterOfMassTransform();
|
||||
// m_motion_state->getWorldTransform(t);
|
||||
}
|
||||
else
|
||||
@ -1535,7 +1531,7 @@ void Kart::placeModel ()
|
||||
//printf(" is %f %f %f\n",t.getOrigin().x(),t.getOrigin().y(),t.getOrigin().z());
|
||||
// Transfer the new position and hpr to m_curr_pos
|
||||
sgSetCoord(&m_curr_pos, m);
|
||||
const btVector3 &v=m_kart_body->getLinearVelocity();
|
||||
const btVector3 &v=m_body->getLinearVelocity();
|
||||
sgSetVec3(m_velocity.xyz, v.x(), v.y(), v.z());
|
||||
}
|
||||
sgCoord c ;
|
||||
|
@ -101,11 +101,9 @@ protected:
|
||||
// physics parameters, storing it saves time
|
||||
#ifdef BULLET
|
||||
btRaycastVehicle::btVehicleTuning *m_tuning;
|
||||
btCompoundShape *m_kart_chassis;
|
||||
btVehicleRaycaster *m_vehicle_raycaster;
|
||||
btRaycastVehicle *m_vehicle;
|
||||
btCompoundShape *m_kart_chassis;
|
||||
btRigidBody *m_kart_body;
|
||||
btDefaultMotionState *m_motion_state;
|
||||
float m_kart_height;
|
||||
float m_time_since_stuck;
|
||||
#endif
|
||||
@ -234,7 +232,6 @@ public:
|
||||
void createPhysics (ssgEntity *obj);
|
||||
#ifdef BULLET
|
||||
btRaycastVehicle *getVehicle () const {return m_vehicle; }
|
||||
btRigidBody *getKartBody () const {return m_kart_body; }
|
||||
void updateBulletPhysics(float dt);
|
||||
void draw ();
|
||||
bool isInRest ();
|
||||
|
@ -27,11 +27,15 @@
|
||||
|
||||
Moveable::Moveable (bool bHasHistory)
|
||||
{
|
||||
m_shadow = 0;
|
||||
m_first_time = true ;
|
||||
m_model = new ssgTransform();
|
||||
#ifdef BULLET
|
||||
m_body = 0;
|
||||
m_motion_state = 0;
|
||||
#endif
|
||||
m_shadow = 0;
|
||||
m_first_time = true ;
|
||||
m_model = new ssgTransform();
|
||||
|
||||
m_model ->ref();
|
||||
m_model->ref();
|
||||
|
||||
sgZeroVec3 ( m_reset_pos.xyz ) ; sgZeroVec3 ( m_reset_pos.hpr ) ;
|
||||
|
||||
@ -51,6 +55,10 @@ Moveable::Moveable (bool bHasHistory)
|
||||
//-----------------------------------------------------------------------------
|
||||
Moveable::~Moveable()
|
||||
{
|
||||
#ifdef BULLET
|
||||
if(m_body ) delete m_body;
|
||||
if(m_motion_state) delete m_motion_state;
|
||||
#endif
|
||||
if(m_history_velocity)
|
||||
{
|
||||
delete [] m_history_velocity;
|
||||
@ -75,6 +83,19 @@ void Moveable::reset ()
|
||||
|
||||
} // reset
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
#ifdef BULLET
|
||||
void Moveable::createBody(float mass, btTransform& position,
|
||||
btCollisionShape *shape, btVector3 inertia) {
|
||||
|
||||
m_motion_state = new btDefaultMotionState(position);
|
||||
|
||||
// Then create a rigid body
|
||||
// ------------------------
|
||||
m_body = new btRigidBody(mass, m_motion_state,
|
||||
shape, inertia);
|
||||
} // createBody
|
||||
#endif
|
||||
//-----------------------------------------------------------------------------
|
||||
void Moveable::update (float dt)
|
||||
{
|
||||
|
@ -23,6 +23,9 @@
|
||||
|
||||
#include <plib/ssg.h>
|
||||
#include "material.hpp"
|
||||
#ifdef BULLET
|
||||
#include "btBulletDynamicsCommon.h"
|
||||
#endif
|
||||
|
||||
/* Limits of Kart performance */
|
||||
#define CRASH_PITCH -45.0f
|
||||
@ -55,6 +58,10 @@ protected:
|
||||
float collectIsectData ( sgVec3 start, sgVec3 end ) ;
|
||||
sgCoord* m_history_velocity;
|
||||
sgCoord* m_history_position;
|
||||
#ifdef BULLET
|
||||
btRigidBody* m_body;
|
||||
btDefaultMotionState* m_motion_state;
|
||||
#endif
|
||||
|
||||
public:
|
||||
|
||||
@ -86,6 +93,11 @@ public:
|
||||
float getIsectData (sgVec3 start, sgVec3 end );
|
||||
void WriteHistory (char* s, int kartNumber, int indx);
|
||||
void ReadHistory (char* s, int kartNumber, int indx);
|
||||
#ifdef BULLET
|
||||
btRigidBody* getBody () const {return m_body; }
|
||||
void createBody(float mass, btTransform& position,
|
||||
btCollisionShape *shape, btVector3 inertia);
|
||||
#endif
|
||||
}
|
||||
; // class Moveable
|
||||
|
||||
|
@ -34,8 +34,16 @@ Physics::Physics(float gravity)
|
||||
btCollisionDispatcher *dispatcher = new btCollisionDispatcher();
|
||||
btVector3 worldMin(-1000, -1000, -1000);
|
||||
btVector3 worldMax( 1000, 1000, 1000);
|
||||
|
||||
// The bullet interface has changed recently, define NEWBULLET
|
||||
// for the new interface, default is the old interface
|
||||
#ifdef NEWBULLET
|
||||
btBroadphaseInterface *pairCache = new btAxisSweep3(worldMin, worldMax);
|
||||
btConstraintSolver *constraintSolver = new btSequentialImpulseConstraintSolver();
|
||||
#else
|
||||
btOverlappingPairCache *pairCache = new btAxisSweep3(worldMin, worldMax);
|
||||
btConstraintSolver *constraintSolver = new btSequentialImpulseConstraintSolver();
|
||||
#endif
|
||||
m_dynamics_world = new btDiscreteDynamicsWorld(dispatcher, pairCache,
|
||||
constraintSolver);
|
||||
m_dynamics_world->setGravity(btVector3(0.0f, 0.0f, -gravity));
|
||||
@ -133,7 +141,7 @@ void Physics::convertTrack(ssgEntity *track, sgMat4 m)
|
||||
//* Adds a kart to the physics engine
|
||||
void Physics::addKart(const Kart *kart, btRaycastVehicle *vehicle)
|
||||
{
|
||||
m_dynamics_world->addRigidBody(kart->getKartBody());
|
||||
m_dynamics_world->addRigidBody(kart->getBody());
|
||||
m_dynamics_world->addVehicle(vehicle);
|
||||
|
||||
} // addKart
|
||||
@ -144,7 +152,7 @@ void Physics::addKart(const Kart *kart, btRaycastVehicle *vehicle)
|
||||
*/
|
||||
void Physics::removeKart(const Kart *kart, btRaycastVehicle *vehicle)
|
||||
{
|
||||
m_dynamics_world->removeRigidBody(kart->getKartBody());
|
||||
m_dynamics_world->removeRigidBody(kart->getBody());
|
||||
m_dynamics_world->removeVehicle(vehicle);
|
||||
} // removeKart
|
||||
|
||||
|
@ -75,6 +75,7 @@ void ProjectileManager::cleanup()
|
||||
m_active_explosions.clear();
|
||||
} // cleanup
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
/** General projectile update call. */
|
||||
void ProjectileManager::update(float dt)
|
||||
{
|
||||
@ -122,6 +123,7 @@ void ProjectileManager::update(float dt)
|
||||
|
||||
} // update
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
/** See if there is an old, unused projectile object available. If so,
|
||||
* reuse this object, otherwise create a new one. */
|
||||
Projectile *ProjectileManager::newProjectile(Kart *kart, int type)
|
||||
@ -142,6 +144,7 @@ Projectile *ProjectileManager::newProjectile(Kart *kart, int type)
|
||||
|
||||
} // newProjectile
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
/** See if there is an old, unused explosion object available. If so,
|
||||
* reuse this object, otherwise create a new one. */
|
||||
Explosion* ProjectileManager::newExplosion(sgCoord* coord)
|
||||
|
Loading…
x
Reference in New Issue
Block a user