Code: Select all
//Car and wheel properties
#define CUBE_HALF_EXTENTS 1
int rightIndex = 0;
int upIndex = 1;
int forwardIndex = 2;
btVector3 wheelDirectionCS0(0,-1,0);
btVector3 wheelAxleCS(-1,0,0);
float gEngineForce = 0.f;
float gBreakingForce = 0.f;
float maxEngineForce = 1000.f;//this should be engine/velocity dependent
float maxBreakingForce = 100.f;
float gVehicleSteering = 0.f;
float steeringIncrement = 0.04f;
float steeringClamp = 0.3f;
float wheelRadius = 0.5f;
float wheelWidth = 0.4f;
float wheelFriction = 1000;//BT_LARGE_FLOAT;
float suspensionStiffness = 20.f;
float suspensionDamping = 2.3f;
float suspensionCompression = 4.4f;
float rollInfluence = 0.1f;//1.0f;
btScalar suspensionRestLength(0.6);
void BulletVehicle::initPhysics()
{
int i;
btCollisionShape* groundShape = new btBoxShape(btVector3(50,3,50));
m_collisionShapes.push_back(groundShape);
m_collisionConfiguration = new btDefaultCollisionConfiguration();
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
btVector3 worldMin(-1000,-1000,-1000);
btVector3 worldMax(1000,1000,1000);
m_overlappingPairCache = new btAxisSweep3(worldMin,worldMax);//add
m_constraintSolver = new btSequentialImpulseConstraintSolver();
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_constraintSolver,m_collisionConfiguration);
//m_dynamicsWorld->setGravity(btVector3(0,0,0));
btTransform tr;
tr.setIdentity();
//create ground collision shape
groundShape = new btStaticPlaneShape(btVector3(0,1,0),1);
m_collisionShapes.push_back(groundShape);
//create ground object
localCreateRigidBody(0,btTransform(btQuaternion(0,0,0,1),btVector3(0,-10,0)),groundShape);
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,0.5f,2.f));
m_collisionShapes.push_back(chassisShape);
btCompoundShape* compound = new btCompoundShape();
m_collisionShapes.push_back(compound);
btTransform localTrans;
localTrans.setIdentity();
//localTrans effectively shifts the center of mass with respect to the chassis
localTrans.setOrigin(btVector3(0,1,0));
compound->addChildShape(localTrans,chassisShape);
//Start making car : position is below
tr.setOrigin(btVector3(0,10.f,0));
m_carChassis = localCreateRigidBody(800,tr,compound);//chassisShape);
//m_carChassis->setDamping(0.2,0.2);
m_wheelShape = new btCylinderShapeX(btVector3(wheelWidth,wheelRadius,wheelRadius));
// create vehicle
{
m_vehicleRayCaster = new btDefaultVehicleRaycaster(m_dynamicsWorld);
m_vehicle = new btRaycastVehicle(m_tuning,m_carChassis,m_vehicleRayCaster);
//never deactivate the vehicle
m_carChassis->setActivationState(DISABLE_DEACTIVATION);
m_dynamicsWorld->addVehicle(m_vehicle);
//Now for the wheels : choose coordinate system
m_vehicle->setCoordinateSystem(rightIndex,upIndex,forwardIndex);
float connectionHeight = 1.2f;
//Front wheels : only affected by steering
bool isFrontWheel=true;
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),connectionHeight,2*CUBE_HALF_EXTENTS-wheelRadius);
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),connectionHeight,2*CUBE_HALF_EXTENTS-wheelRadius);
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
//Back wheels : only affected by engines
isFrontWheel = false;
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),connectionHeight,-2*CUBE_HALF_EXTENTS+wheelRadius);
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),connectionHeight,-2*CUBE_HALF_EXTENTS+wheelRadius);
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
//Wheel properties : adjust globals
for (int i=0;i<m_vehicle->getNumWheels();i++)
{
btWheelInfo& wheel = m_vehicle->getWheelInfo(i);
wheel.m_suspensionStiffness = suspensionStiffness;
wheel.m_wheelsDampingRelaxation = suspensionDamping;
wheel.m_wheelsDampingCompression = suspensionCompression;
wheel.m_frictionSlip = wheelFriction;
wheel.m_rollInfluence = rollInfluence;
}
}
//setCameraDistance(26.f);
}
Code: Select all
//draw the chassis
glColor3f(1, 1, 0);
m_vehicle->getChassisWorldTransform().getOpenGLMatrix(m);
glMultMatrixf(m);
glBegin(GL_QUADS); // Start Drawing A Textured Quad
glVertex3f(-0.5f, 0.25f, 1.0f); // Bottom Left
glVertex3f( 0.5f, 0.25f, 1.0f); // Bottom Right
glVertex3f( 0.5f, -0.25f, 1.0f); // Top Right
glVertex3f( -0.5f, -0.25f, 1.0f); // Top Left
glEnd();
glColor3f(1, 1, 1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture[0]);
for (i=0;i<m_vehicle->getNumWheels();i++){
//synchronize the wheels with the (interpolated) chassis worldtransform
m_vehicle->updateWheelTransform(i,true);
//draw wheels (cylinders)
m_vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(m);
//m_shapeDrawer->drawOpenGL(m,m_wheelShape,wheelColor,getDebugMode(),worldBoundsMin,worldBoundsMax);
//draw cylinder by pushing current matrix and mult m
glPushMatrix();
glMultMatrixf(m);
gluSphere(quadratic,1.0f,16,16);
glPopMatrix();
}

I have attached the code for the class BulletVehicle which initializes the physics world and uses opengl for rendering.