Code: Select all
createVehicle()
{
//used to update the vehicle, vehicle controller
//float gEngineForce = 0.f;
//float gBreakingForce = 0.f;
//float gVehicleSteering = 0.f;
float maxEngineForce = 1000.f;//this should be engine/velocity dependent
float maxBreakingForce = 100.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;
float vehicleMass = 800.f;
btScalar suspensionRestLength(0.6);
btVector3 wheelDirectionCS0(0,-1,0);
btVector3 wheelAxleCS(-1,0,0);
btVector3 localInertia(0,0,0);
int rightIndex = 0;
int upIndex = 1;
int forwardIndex = 2;
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,0.5f,2.f));
collisionShapes.push_back(chassisShape);
btCompoundShape* compound = new btCompoundShape();
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);
//----localcreateRidgidBody
btTransform tr;
tr.setIdentity();
tr.setOrigin(btVector3(0,1.f,0));
btDefaultMotionState* myMotionState = new btDefaultMotionState(tr);
btRigidBody::btRigidBodyConstructionInfo rbInfo(vehicleMass,myMotionState,compound,localInertia);
compound->calculateLocalInertia(vehicleMass,localInertia);
m_carChassis = new btRigidBody(rbInfo);//chassisShape);
//m_carChassis->setDamping(0.2,0.2);
dynamicsWorld->addRigidBody(m_carChassis);
//----end localcreateRidgidBody
//wheelShape
m_wheelShape = new btCylinderShapeX(btVector3(wheelWidth,wheelRadius,wheelRadius));
/// create vehicle
m_vehicleRayCaster = new btDefaultVehicleRaycaster(dynamicsWorld);
m_vehicle = new btRaycastVehicle(m_tuning,m_carChassis,m_vehicleRayCaster);
///never deactivate the vehicle
m_carChassis->setActivationState(DISABLE_DEACTIVATION);
dynamicsWorld->addVehicle(m_vehicle);
float connectionHeight = 1.2f;//1.2
bool isFrontWheel=true;
//choose coordinate system
m_vehicle->setCoordinateSystem(rightIndex,upIndex,forwardIndex);
btVector3 connectionPointCS0(1-(0.3*wheelWidth),connectionHeight,2*1-wheelRadius);
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
connectionPointCS0 = btVector3(-1+(0.3*wheelWidth),connectionHeight,2*1-wheelRadius);
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
connectionPointCS0 = btVector3(-1+(0.3*wheelWidth),connectionHeight,-2*1+wheelRadius);
isFrontWheel = false;
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
connectionPointCS0 = btVector3(1-(0.3*wheelWidth),connectionHeight,-2*1+wheelRadius);
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
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;
}
}
Code: Select all
void PhysicsWorld::updateVehicle()
{
m_vehicle->resetSuspension();
m_vehicle->updateWheelTransform(0,true);
m_vehicle->updateWheelTransform(1,true);
m_vehicle->updateWheelTransform(2,true);
m_vehicle->updateWheelTransform(3,true);
int wheelIndex = 2;
m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
m_vehicle->setBrake(gBreakingForce,wheelIndex);
wheelIndex = 3;
m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
m_vehicle->setBrake(gBreakingForce,wheelIndex);
wheelIndex = 0;
m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
wheelIndex = 1;
m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
}