Setting up a Vehicle

Started by
8 comments, last by Medo Mex 11 years ago

I'm having trouble in setting up a vehicle, I'm trying to make a simple vehicle (1 box and 4 wheels) in Bullet Physics, but couldn't get it to work.

The vehicle never move forward and sometimes it get up side down.

The following code is taken from Bullet sample, I made some modifications:


#include "Vehicle.h"

#ifdef FORCE_ZAXIS_UP
int rightIndex = 0; 
int upIndex = 2; 
int forwardIndex = 1;
btVector3 wheelDirectionCS0(0,0,-1);
btVector3 wheelAxleCS(1,0,0);
#else
int rightIndex = 0;
int upIndex = 1;
int forwardIndex = 2;
btVector3 wheelDirectionCS0(0,0,-1); // 0,-1,0
btVector3 wheelAxleCS(1,0,0);
#endif


const int maxProxies = 32766;
const int maxOverlap = 65535;


///btRaycastVehicle is the interface for the constraint that implements the raycast vehicle
///notice that for higher-quality slow-moving vehicles, another approach might be better
///implementing explicit hinged-wheel constraints with cylinder collision, rather then raycasts
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 = 60.5f;
float wheelWidth = 100.4f;
float wheelFriction = 1000;//BT_LARGE_FLOAT;
float suspensionStiffness = 20.f;
float suspensionDamping = 2.3f;
float suspensionCompression = 400.4f;
float rollInfluence = 0.1f;//1.0f;


btScalar suspensionRestLength(0.6);


#define CUBE_HALF_EXTENTS 1


D3DXMATRIX ConvertMatrix( btTransform &trn ) 
{
btVector3 R = trn.getBasis().getColumn(0);
btVector3 U = trn.getBasis().getColumn(1);
btVector3 L = trn.getBasis().getColumn(2);
btVector3 P = trn.getOrigin();


D3DXVECTOR3 vR, vU, vL, vP;
vR.x = R.x();vR.y = R.y();vR.z = R.z();
vU.x = U.x();vU.y = U.y();vU.z = U.z();
vL.x = L.x();vL.y = L.y();vL.z = L.z();
vP.x = P.x();vP.y = P.y();vP.z = P.z();


D3DXMATRIX matOutput;
matOutput._11 = vR.x;matOutput._12 = vR.y;matOutput._13 = vR.z;matOutput._14 = 0.f;
matOutput._21 = vU.x;matOutput._22 = vU.y;matOutput._23 = vU.z;matOutput._24 = 0.f;
matOutput._31 = vL.x;matOutput._32 = vL.y;matOutput._33 = vL.z;matOutput._34 = 0.f;
matOutput._41 = vP.x;matOutput._42 = vP.y;matOutput._43 = vP.z;matOutput._44 = 1.f;

return matOutput;
}


Vehicle::Vehicle(LPDIRECT3DDEVICE9 lpD3DDev, btDiscreteDynamicsWorld *world) : d3ddev(lpD3DDev), m_dynamicsWorld(world)
{
D3DXCreateBox(d3ddev, 50.0f, 30.0f, 30.0f, &meshChassis, NULL);
D3DXCreateCylinder(d3ddev, 5.0f, 5.0f, 5.0f, 20, 20, &meshWheels, NULL);
}


void Vehicle::DetectInput()
{
if (GetAsyncKeyState(VK_UP) != 0)
{
gEngineForce = maxEngineForce;
gBreakingForce = 0.f;
}


if (GetAsyncKeyState(VK_DOWN) != 0)
{
gBreakingForce = maxBreakingForce; 
gEngineForce = 0.f;
}


if (GetAsyncKeyState(VK_RIGHT) != 0)
{
gVehicleSteering -= steeringIncrement;
if ( gVehicleSteering < -steeringClamp)
gVehicleSteering = -steeringClamp;
}


if (GetAsyncKeyState(VK_LEFT) != 0)
{
gVehicleSteering += steeringIncrement;
if ( gVehicleSteering > steeringClamp)
gVehicleSteering = steeringClamp;
}
}


void Vehicle::Render(float timeElapsed)
{
btTransform trn;
trn.setIdentity();


m_vehicle->getRigidBody()->getMotionState()->getWorldTransform( trn );


D3DXMATRIX matTransformation; //= ConvertMatrix(trn);


D3DXMatrixIdentity(&matTransformation);
matTransformation = ConvertMatrix(trn);
//D3DXMatrixTranslation(&matTransformation, 1200.0f, -400.0f, 0.0f);


d3ddev->SetTransform(D3DTS_WORLD, &matTransformation);
meshChassis->DrawSubset(0);


int i;
for (i=0;i<m_vehicle->getNumWheels();i++)
{
//synchronize the wheels with the (interpolated) chassis worldtransform
m_vehicle->updateWheelTransform(i,true);


btTransform wheelTrans;
wheelTrans.setIdentity();
btVector3 wheelPosition;
//m_vehicle->getWheelInfo(i).m_worldTransform(wheelPosition); //.m_worldTransform(wheelTrans);


wheelTrans = m_vehicle->getWheelInfo(i).m_worldTransform;


D3DXMATRIX matWheelTrans = ConvertMatrix(wheelTrans);
//D3DXMatrixIdentity(&matWheelTrans);
//D3DXMatrixTranslation(&matWheelTrans, wheelPosition.getX(), wheelPosition.getY(), wheelPosition.getZ());


d3ddev->SetTransform(D3DTS_WORLD, &matWheelTrans);
D3DMATERIAL9 material;
material.Diffuse = D3DXCOLOR(255, 255, 255, 255);
material.Ambient = material.Diffuse;
d3ddev->SetMaterial(&material);
meshWheels->DrawSubset(0);


//int wheelId = 16+i;


//draw wheels (cylinders)
//m_vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(m);
//m_shapeDrawer->drawOpenGL(m,m_wheelShape,wheelColor,getDebugMode(),worldBoundsMin,worldBoundsMax);
}
}


btRigidBody* Vehicle::localCreateRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape)
{
btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));


//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);


btVector3 localInertia(0,0,0);
if (isDynamic)
shape->calculateLocalInertia(mass,localInertia);


//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects


#define USE_MOTIONSTATE 1
#ifdef USE_MOTIONSTATE
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);


btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);


btRigidBody* body = new btRigidBody(cInfo);
body->setContactProcessingThreshold(0.0f); // m_defaultContactProcessingThreshold


#else
btRigidBody* body = new btRigidBody(mass,0,shape,localInertia); 
body->setWorldTransform(startTransform);
#endif//


m_dynamicsWorld->addRigidBody(body);


return body;
}


void Vehicle::Create(D3DXVECTOR3 position)
{
btTransform tr;
tr.setIdentity();
btAlignedObjectArray<btCollisionShape*> m_collisionShapes;


#ifdef FORCE_ZAXIS_UP
//   indexRightAxis = 0; 
//   indexUpAxis = 2; 
//   indexForwardAxis = 1; 
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,2.f, 0.5f));
btCompoundShape* compound = new btCompoundShape();
btTransform localTrans;
localTrans.setIdentity();
//localTrans effectively shifts the center of mass with respect to the chassis
localTrans.setOrigin(btVector3(0,0,1));
#else
btCollisionShape* chassisShape = new btBoxShape(btVector3(25.0f, 15.0f, 15.0f));
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));
#endif

compound->addChildShape(localTrans,chassisShape);
tr.setOrigin(btVector3(0,0.f,0));


m_carChassis = localCreateRigidBody(800.0f,tr,compound);//chassisShape); 800.0f
//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);
float connectionHeight = -18.2f;
bool isFrontWheel=true;


//choose coordinate system
m_vehicle->setCoordinateSystem(rightIndex,upIndex,forwardIndex);

#ifdef FORCE_ZAXIS_UP
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight);
#else
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(25.3),connectionHeight,10*CUBE_HALF_EXTENTS);
#endif


m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(25.3),connectionHeight,10*CUBE_HALF_EXTENTS);
#endif


m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(25.3),connectionHeight,-10*CUBE_HALF_EXTENTS);
#endif //FORCE_ZAXIS_UP
isFrontWheel = false;
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(25.3),connectionHeight,-10*CUBE_HALF_EXTENTS);
#endif
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;
}
}
}

Advertisement

I'm having trouble in setting up a vehicle, I'm trying to make a simple vehicle (1 box and 4 wheels) in Bullet Physics, but couldn't get it to work.

The vehicle never move forward and sometimes it get up side down.

The following code is taken from Bullet sample, I made some modifications:


#include "Vehicle.h"

#ifdef FORCE_ZAXIS_UP
int rightIndex = 0; 
int upIndex = 2; 
int forwardIndex = 1;
btVector3 wheelDirectionCS0(0,0,-1);
btVector3 wheelAxleCS(1,0,0);
#else
int rightIndex = 0;
int upIndex = 1;
int forwardIndex = 2;
btVector3 wheelDirectionCS0(0,0,-1); // 0,-1,0
btVector3 wheelAxleCS(1,0,0);
#endif


const int maxProxies = 32766;
const int maxOverlap = 65535;


///btRaycastVehicle is the interface for the constraint that implements the raycast vehicle
///notice that for higher-quality slow-moving vehicles, another approach might be better
///implementing explicit hinged-wheel constraints with cylinder collision, rather then raycasts
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 = 60.5f;
float wheelWidth = 100.4f;
float wheelFriction = 1000;//BT_LARGE_FLOAT;
float suspensionStiffness = 20.f;
float suspensionDamping = 2.3f;
float suspensionCompression = 400.4f;
float rollInfluence = 0.1f;//1.0f;


btScalar suspensionRestLength(0.6);


#define CUBE_HALF_EXTENTS 1


D3DXMATRIX ConvertMatrix( btTransform &trn ) 
{
btVector3 R = trn.getBasis().getColumn(0);
btVector3 U = trn.getBasis().getColumn(1);
btVector3 L = trn.getBasis().getColumn(2);
btVector3 P = trn.getOrigin();


D3DXVECTOR3 vR, vU, vL, vP;
vR.x = R.x();vR.y = R.y();vR.z = R.z();
vU.x = U.x();vU.y = U.y();vU.z = U.z();
vL.x = L.x();vL.y = L.y();vL.z = L.z();
vP.x = P.x();vP.y = P.y();vP.z = P.z();


D3DXMATRIX matOutput;
matOutput._11 = vR.x;matOutput._12 = vR.y;matOutput._13 = vR.z;matOutput._14 = 0.f;
matOutput._21 = vU.x;matOutput._22 = vU.y;matOutput._23 = vU.z;matOutput._24 = 0.f;
matOutput._31 = vL.x;matOutput._32 = vL.y;matOutput._33 = vL.z;matOutput._34 = 0.f;
matOutput._41 = vP.x;matOutput._42 = vP.y;matOutput._43 = vP.z;matOutput._44 = 1.f;

return matOutput;
}


Vehicle::Vehicle(LPDIRECT3DDEVICE9 lpD3DDev, btDiscreteDynamicsWorld *world) : d3ddev(lpD3DDev), m_dynamicsWorld(world)
{
D3DXCreateBox(d3ddev, 50.0f, 30.0f, 30.0f, &meshChassis, NULL);
D3DXCreateCylinder(d3ddev, 5.0f, 5.0f, 5.0f, 20, 20, &meshWheels, NULL);
}


void Vehicle::DetectInput()
{
if (GetAsyncKeyState(VK_UP) != 0)
{
gEngineForce = maxEngineForce;
gBreakingForce = 0.f;
}


if (GetAsyncKeyState(VK_DOWN) != 0)
{
gBreakingForce = maxBreakingForce; 
gEngineForce = 0.f;
}


if (GetAsyncKeyState(VK_RIGHT) != 0)
{
gVehicleSteering -= steeringIncrement;
if ( gVehicleSteering < -steeringClamp)
gVehicleSteering = -steeringClamp;
}


if (GetAsyncKeyState(VK_LEFT) != 0)
{
gVehicleSteering += steeringIncrement;
if ( gVehicleSteering > steeringClamp)
gVehicleSteering = steeringClamp;
}
}


void Vehicle::Render(float timeElapsed)
{
btTransform trn;
trn.setIdentity();


m_vehicle->getRigidBody()->getMotionState()->getWorldTransform( trn );


D3DXMATRIX matTransformation; //= ConvertMatrix(trn);


D3DXMatrixIdentity(&matTransformation);
matTransformation = ConvertMatrix(trn);
//D3DXMatrixTranslation(&matTransformation, 1200.0f, -400.0f, 0.0f);


d3ddev->SetTransform(D3DTS_WORLD, &matTransformation);
meshChassis->DrawSubset(0);


int i;
for (i=0;i<m_vehicle->getNumWheels();i++)
{
//synchronize the wheels with the (interpolated) chassis worldtransform
m_vehicle->updateWheelTransform(i,true);


btTransform wheelTrans;
wheelTrans.setIdentity();
btVector3 wheelPosition;
//m_vehicle->getWheelInfo(i).m_worldTransform(wheelPosition); //.m_worldTransform(wheelTrans);


wheelTrans = m_vehicle->getWheelInfo(i).m_worldTransform;


D3DXMATRIX matWheelTrans = ConvertMatrix(wheelTrans);
//D3DXMatrixIdentity(&matWheelTrans);
//D3DXMatrixTranslation(&matWheelTrans, wheelPosition.getX(), wheelPosition.getY(), wheelPosition.getZ());


d3ddev->SetTransform(D3DTS_WORLD, &matWheelTrans);
D3DMATERIAL9 material;
material.Diffuse = D3DXCOLOR(255, 255, 255, 255);
material.Ambient = material.Diffuse;
d3ddev->SetMaterial(&material);
meshWheels->DrawSubset(0);


//int wheelId = 16+i;


//draw wheels (cylinders)
//m_vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(m);
//m_shapeDrawer->drawOpenGL(m,m_wheelShape,wheelColor,getDebugMode(),worldBoundsMin,worldBoundsMax);
}
}


btRigidBody* Vehicle::localCreateRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape)
{
btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));


//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);


btVector3 localInertia(0,0,0);
if (isDynamic)
shape->calculateLocalInertia(mass,localInertia);


//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects


#define USE_MOTIONSTATE 1
#ifdef USE_MOTIONSTATE
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);


btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);


btRigidBody* body = new btRigidBody(cInfo);
body->setContactProcessingThreshold(0.0f); // m_defaultContactProcessingThreshold


#else
btRigidBody* body = new btRigidBody(mass,0,shape,localInertia); 
body->setWorldTransform(startTransform);
#endif//


m_dynamicsWorld->addRigidBody(body);


return body;
}


void Vehicle::Create(D3DXVECTOR3 position)
{
btTransform tr;
tr.setIdentity();
btAlignedObjectArray<btCollisionShape*> m_collisionShapes;


#ifdef FORCE_ZAXIS_UP
//   indexRightAxis = 0; 
//   indexUpAxis = 2; 
//   indexForwardAxis = 1; 
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,2.f, 0.5f));
btCompoundShape* compound = new btCompoundShape();
btTransform localTrans;
localTrans.setIdentity();
//localTrans effectively shifts the center of mass with respect to the chassis
localTrans.setOrigin(btVector3(0,0,1));
#else
btCollisionShape* chassisShape = new btBoxShape(btVector3(25.0f, 15.0f, 15.0f));
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));
#endif

compound->addChildShape(localTrans,chassisShape);
tr.setOrigin(btVector3(0,0.f,0));


m_carChassis = localCreateRigidBody(800.0f,tr,compound);//chassisShape); 800.0f
//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);
float connectionHeight = -18.2f;
bool isFrontWheel=true;


//choose coordinate system
m_vehicle->setCoordinateSystem(rightIndex,upIndex,forwardIndex);

#ifdef FORCE_ZAXIS_UP
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight);
#else
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(25.3),connectionHeight,10*CUBE_HALF_EXTENTS);
#endif


m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(25.3),connectionHeight,10*CUBE_HALF_EXTENTS);
#endif


m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(25.3),connectionHeight,-10*CUBE_HALF_EXTENTS);
#endif //FORCE_ZAXIS_UP
isFrontWheel = false;
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(25.3),connectionHeight,-10*CUBE_HALF_EXTENTS);
#endif
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;
}
}
}

engineForce is never used, am I right?

I think you should override the updateVehicle virtual function

@Ravnock:
Here is how I'm using engine force:

void Vehicle::DetectInput()
{
if (GetAsyncKeyState(VK_UP) != 0)
{
    gEngineForce = maxEngineForce;
    gBreakingForce = 0.f;
}
....

Not working.

If I should override updateVehicle() I'm not sure what should I add in that function.

@Ravnock:
Here is how I'm using engine force:

void Vehicle::DetectInput()
{
if (GetAsyncKeyState(VK_UP) != 0)
{
    gEngineForce = maxEngineForce;
    gBreakingForce = 0.f;
}
....

Not working.

If I should override updateVehicle() I'm not sure what should I add in that function.

Is your Vehicle a child class of btRaycastVehicle? Could you post the cpp and header file?

Sure!

The class is not well completed, but I'm trying to get the vehicle to work so I can make the appropriate changes after that.

Vehicle.h:


#include "d3d9.h"
#include "d3dx9.h"
#include "PhysicsEngine.h"


class Vehicle
{
public:
Vehicle(LPDIRECT3DDEVICE9 lpD3DDev, btDiscreteDynamicsWorld *dynamicsWorld);
~Vehicle();


void Create(D3DXVECTOR3 position);


void DetectInput();
void Render(float elapsedTime);
private:
// Device pointer
LPDIRECT3DDEVICE9 device;
// World pointer
btDiscreteDynamicsWorld* m_dynamicsWorld;


btRigidBody* m_carChassis;
btRaycastVehicle::btVehicleTuning m_tuning;
btVehicleRaycaster* m_vehicleRayCaster;
btRaycastVehicle* m_vehicle;
btCollisionShape* m_wheelShape;

// For rendering
LPD3DXMESH meshChassis;
LPD3DXMESH meshWheels;

btRigidBody* localCreateRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape);

};
Vehicle.cpp:

#include "Vehicle.h"
#include "physics.h"


#ifdef FORCE_ZAXIS_UP
int rightIndex = 0; 
int upIndex = 2; 
int forwardIndex = 1;
btVector3 wheelDirectionCS0(0,0,-1);
btVector3 wheelAxleCS(1,0,0);
#else
int rightIndex = 0;
int upIndex = 1;
int forwardIndex = 2;
btVector3 wheelDirectionCS0(0,0,-1); // 0,-1,0
btVector3 wheelAxleCS(1,0,0);
#endif


const int maxProxies = 32766;
const int maxOverlap = 65535;


///btRaycastVehicle is the interface for the constraint that implements the raycast vehicle
///notice that for higher-quality slow-moving vehicles, another approach might be better
///implementing explicit hinged-wheel constraints with cylinder collision, rather then raycasts
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 = 60.5f;
float wheelWidth = 100.4f;
float wheelFriction = 1000;//BT_LARGE_FLOAT;
float suspensionStiffness = 20.f;
float suspensionDamping = 2.3f;
float suspensionCompression = 400.4f;
float rollInfluence = 0.1f;//1.0f;




btScalar suspensionRestLength(0.6);


#define CUBE_HALF_EXTENTS 1




D3DXMATRIX ConvertMatrix( btTransform &trn ) 
{
btVector3 R = trn.getBasis().getColumn(0);
btVector3 U = trn.getBasis().getColumn(1);
btVector3 L = trn.getBasis().getColumn(2);
btVector3 P = trn.getOrigin();


D3DXVECTOR3 vR, vU, vL, vP;
vR.x = R.x();vR.y = R.y();vR.z = R.z();
vU.x = U.x();vU.y = U.y();vU.z = U.z();
vL.x = L.x();vL.y = L.y();vL.z = L.z();
vP.x = P.x();vP.y = P.y();vP.z = P.z();


D3DXMATRIX matOutput;
matOutput._11 = vR.x;matOutput._12 = vR.y;matOutput._13 = vR.z;matOutput._14 = 0.f;
matOutput._21 = vU.x;matOutput._22 = vU.y;matOutput._23 = vU.z;matOutput._24 = 0.f;
matOutput._31 = vL.x;matOutput._32 = vL.y;matOutput._33 = vL.z;matOutput._34 = 0.f;
matOutput._41 = vP.x;matOutput._42 = vP.y;matOutput._43 = vP.z;matOutput._44 = 1.f;


return matOutput;
}






Vehicle::Vehicle(LPDIRECT3DDEVICE9 lpD3DDev, btDiscreteDynamicsWorld *dynamicsWorld) : d3ddev(lpD3DDev), m_dynamicsWorld(dynamicsWorld)
{
D3DXCreateBox(d3ddev, 50.0f, 30.0f, 30.0f, &meshChassis, NULL);
D3DXCreateCylinder(d3ddev, 5.0f, 5.0f, 5.0f, 20, 20, &meshWheels, NULL);
}


void Vehicle::DetectInput()
{
if (GetAsyncKeyState(VK_UP) != 0)
{
gEngineForce = maxEngineForce;
gBreakingForce = 0.f;
}


if (GetAsyncKeyState(VK_DOWN) != 0)
{
gBreakingForce = maxBreakingForce; 
gEngineForce = 0.f;
}


if (GetAsyncKeyState(VK_RIGHT) != 0)
{
gVehicleSteering -= steeringIncrement;
if ( gVehicleSteering < -steeringClamp)
gVehicleSteering = -steeringClamp;
}


if (GetAsyncKeyState(VK_LEFT) != 0)
{
gVehicleSteering += steeringIncrement;
if ( gVehicleSteering > steeringClamp)
gVehicleSteering = steeringClamp;
}
}


void Vehicle::Render(float timeElapsed)
{
btTransform trn;
trn.setIdentity();


m_vehicle->getRigidBody()->getMotionState()->getWorldTransform( trn );


D3DXMATRIX matTransformation; //= ConvertMatrix(trn);


D3DXMatrixIdentity(&matTransformation);
matTransformation = ConvertMatrix(trn);
//D3DXMatrixTranslation(&matTransformation, 1200.0f, -400.0f, 0.0f);


d3ddev->SetTransform(D3DTS_WORLD, &matTransformation);
meshChassis->DrawSubset(0);


int i;
for (i=0;i<m_vehicle->getNumWheels();i++)
{
//synchronize the wheels with the (interpolated) chassis worldtransform
m_vehicle->updateWheelTransform(i,true);


btTransform wheelTrans;
wheelTrans.setIdentity();
btVector3 wheelPosition;
//m_vehicle->getWheelInfo(i).m_worldTransform(wheelPosition); //.m_worldTransform(wheelTrans);


wheelTrans = m_vehicle->getWheelInfo(i).m_worldTransform;




D3DXMATRIX matWheelTrans = ConvertMatrix(wheelTrans);
//D3DXMatrixIdentity(&matWheelTrans);
//D3DXMatrixTranslation(&matWheelTrans, wheelPosition.getX(), wheelPosition.getY(), wheelPosition.getZ());


d3ddev->SetTransform(D3DTS_WORLD, &matWheelTrans);
D3DMATERIAL9 material;
material.Diffuse = D3DXCOLOR(255, 255, 255, 255);
material.Ambient = material.Diffuse;
d3ddev->SetMaterial(&material);
meshWheels->DrawSubset(0);


//int wheelId = 16+i;


//draw wheels (cylinders)
//m_vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(m);
//m_shapeDrawer->drawOpenGL(m,m_wheelShape,wheelColor,getDebugMode(),worldBoundsMin,worldBoundsMax);
}
}


btRigidBody* Vehicle::localCreateRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape)
{
btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));


//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);


btVector3 localInertia(0,0,0);
if (isDynamic)
shape->calculateLocalInertia(mass,localInertia);


//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects


#define USE_MOTIONSTATE 1
#ifdef USE_MOTIONSTATE
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);


btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);


btRigidBody* body = new btRigidBody(cInfo);
body->setContactProcessingThreshold(0.0f); // m_defaultContactProcessingThreshold


#else
btRigidBody* body = new btRigidBody(mass,0,shape,localInertia); 
body->setWorldTransform(startTransform);
#endif//


m_dynamicsWorld->addRigidBody(body);


return body;
}


void Vehicle::Create(D3DXVECTOR3 position)
{
btTransform tr;
tr.setIdentity();
btAlignedObjectArray<btCollisionShape*> m_collisionShapes;


#ifdef FORCE_ZAXIS_UP
//   indexRightAxis = 0; 
//   indexUpAxis = 2; 
//   indexForwardAxis = 1; 
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,2.f, 0.5f));
btCompoundShape* compound = new btCompoundShape();
btTransform localTrans;
localTrans.setIdentity();
//localTrans effectively shifts the center of mass with respect to the chassis
localTrans.setOrigin(btVector3(0,0,1));
#else
btCollisionShape* chassisShape = new btBoxShape(btVector3(25.0f, 15.0f, 15.0f));
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));
#endif


compound->addChildShape(localTrans,chassisShape);


tr.setOrigin(btVector3(0,0.f,0));


m_carChassis = localCreateRigidBody(800.0f,tr,compound);//chassisShape); 800.0f
//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);


float connectionHeight = -18.2f;




bool isFrontWheel=true;


//choose coordinate system
m_vehicle->setCoordinateSystem(rightIndex,upIndex,forwardIndex);


#ifdef FORCE_ZAXIS_UP
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight);
#else
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(25.3),connectionHeight,10*CUBE_HALF_EXTENTS);
#endif


m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(25.3),connectionHeight,10*CUBE_HALF_EXTENTS);
#endif


m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(25.3),connectionHeight,-10*CUBE_HALF_EXTENTS);
#endif //FORCE_ZAXIS_UP
isFrontWheel = false;
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(25.3),connectionHeight,-10*CUBE_HALF_EXTENTS);
#endif
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;
}
}
}

Sure!

The class is not well completed, but I'm trying to get the vehicle to work so I can make the appropriate changes after that.

Vehicle.h:


#include "d3d9.h"
#include "d3dx9.h"
#include "PhysicsEngine.h"


class Vehicle
{
public:
Vehicle(LPDIRECT3DDEVICE9 lpD3DDev, btDiscreteDynamicsWorld *dynamicsWorld);
~Vehicle();


void Create(D3DXVECTOR3 position);


void DetectInput();
void Render(float elapsedTime);
private:
// Device pointer
LPDIRECT3DDEVICE9 device;
// World pointer
btDiscreteDynamicsWorld* m_dynamicsWorld;


btRigidBody* m_carChassis;
btRaycastVehicle::btVehicleTuning m_tuning;
btVehicleRaycaster* m_vehicleRayCaster;
btRaycastVehicle* m_vehicle;
btCollisionShape* m_wheelShape;

// For rendering
LPD3DXMESH meshChassis;
LPD3DXMESH meshWheels;

btRigidBody* localCreateRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape);

};
Vehicle.cpp:

#include "Vehicle.h"
#include "physics.h"


#ifdef FORCE_ZAXIS_UP
int rightIndex = 0; 
int upIndex = 2; 
int forwardIndex = 1;
btVector3 wheelDirectionCS0(0,0,-1);
btVector3 wheelAxleCS(1,0,0);
#else
int rightIndex = 0;
int upIndex = 1;
int forwardIndex = 2;
btVector3 wheelDirectionCS0(0,0,-1); // 0,-1,0
btVector3 wheelAxleCS(1,0,0);
#endif


const int maxProxies = 32766;
const int maxOverlap = 65535;


///btRaycastVehicle is the interface for the constraint that implements the raycast vehicle
///notice that for higher-quality slow-moving vehicles, another approach might be better
///implementing explicit hinged-wheel constraints with cylinder collision, rather then raycasts
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 = 60.5f;
float wheelWidth = 100.4f;
float wheelFriction = 1000;//BT_LARGE_FLOAT;
float suspensionStiffness = 20.f;
float suspensionDamping = 2.3f;
float suspensionCompression = 400.4f;
float rollInfluence = 0.1f;//1.0f;




btScalar suspensionRestLength(0.6);


#define CUBE_HALF_EXTENTS 1




D3DXMATRIX ConvertMatrix( btTransform &trn ) 
{
btVector3 R = trn.getBasis().getColumn(0);
btVector3 U = trn.getBasis().getColumn(1);
btVector3 L = trn.getBasis().getColumn(2);
btVector3 P = trn.getOrigin();


D3DXVECTOR3 vR, vU, vL, vP;
vR.x = R.x();vR.y = R.y();vR.z = R.z();
vU.x = U.x();vU.y = U.y();vU.z = U.z();
vL.x = L.x();vL.y = L.y();vL.z = L.z();
vP.x = P.x();vP.y = P.y();vP.z = P.z();


D3DXMATRIX matOutput;
matOutput._11 = vR.x;matOutput._12 = vR.y;matOutput._13 = vR.z;matOutput._14 = 0.f;
matOutput._21 = vU.x;matOutput._22 = vU.y;matOutput._23 = vU.z;matOutput._24 = 0.f;
matOutput._31 = vL.x;matOutput._32 = vL.y;matOutput._33 = vL.z;matOutput._34 = 0.f;
matOutput._41 = vP.x;matOutput._42 = vP.y;matOutput._43 = vP.z;matOutput._44 = 1.f;


return matOutput;
}






Vehicle::Vehicle(LPDIRECT3DDEVICE9 lpD3DDev, btDiscreteDynamicsWorld *dynamicsWorld) : d3ddev(lpD3DDev), m_dynamicsWorld(dynamicsWorld)
{
D3DXCreateBox(d3ddev, 50.0f, 30.0f, 30.0f, &meshChassis, NULL);
D3DXCreateCylinder(d3ddev, 5.0f, 5.0f, 5.0f, 20, 20, &meshWheels, NULL);
}


void Vehicle::DetectInput()
{
if (GetAsyncKeyState(VK_UP) != 0)
{
gEngineForce = maxEngineForce;
gBreakingForce = 0.f;
}


if (GetAsyncKeyState(VK_DOWN) != 0)
{
gBreakingForce = maxBreakingForce; 
gEngineForce = 0.f;
}


if (GetAsyncKeyState(VK_RIGHT) != 0)
{
gVehicleSteering -= steeringIncrement;
if ( gVehicleSteering < -steeringClamp)
gVehicleSteering = -steeringClamp;
}


if (GetAsyncKeyState(VK_LEFT) != 0)
{
gVehicleSteering += steeringIncrement;
if ( gVehicleSteering > steeringClamp)
gVehicleSteering = steeringClamp;
}
}


void Vehicle::Render(float timeElapsed)
{
btTransform trn;
trn.setIdentity();


m_vehicle->getRigidBody()->getMotionState()->getWorldTransform( trn );


D3DXMATRIX matTransformation; //= ConvertMatrix(trn);


D3DXMatrixIdentity(&matTransformation);
matTransformation = ConvertMatrix(trn);
//D3DXMatrixTranslation(&matTransformation, 1200.0f, -400.0f, 0.0f);


d3ddev->SetTransform(D3DTS_WORLD, &matTransformation);
meshChassis->DrawSubset(0);


int i;
for (i=0;i<m_vehicle->getNumWheels();i++)
{
//synchronize the wheels with the (interpolated) chassis worldtransform
m_vehicle->updateWheelTransform(i,true);


btTransform wheelTrans;
wheelTrans.setIdentity();
btVector3 wheelPosition;
//m_vehicle->getWheelInfo(i).m_worldTransform(wheelPosition); //.m_worldTransform(wheelTrans);


wheelTrans = m_vehicle->getWheelInfo(i).m_worldTransform;




D3DXMATRIX matWheelTrans = ConvertMatrix(wheelTrans);
//D3DXMatrixIdentity(&matWheelTrans);
//D3DXMatrixTranslation(&matWheelTrans, wheelPosition.getX(), wheelPosition.getY(), wheelPosition.getZ());


d3ddev->SetTransform(D3DTS_WORLD, &matWheelTrans);
D3DMATERIAL9 material;
material.Diffuse = D3DXCOLOR(255, 255, 255, 255);
material.Ambient = material.Diffuse;
d3ddev->SetMaterial(&material);
meshWheels->DrawSubset(0);


//int wheelId = 16+i;


//draw wheels (cylinders)
//m_vehicle->getWheelInfo(i).m_worldTransform.getOpenGLMatrix(m);
//m_shapeDrawer->drawOpenGL(m,m_wheelShape,wheelColor,getDebugMode(),worldBoundsMin,worldBoundsMax);
}
}


btRigidBody* Vehicle::localCreateRigidBody(float mass, const btTransform& startTransform,btCollisionShape* shape)
{
btAssert((!shape || shape->getShapeType() != INVALID_SHAPE_PROXYTYPE));


//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);


btVector3 localInertia(0,0,0);
if (isDynamic)
shape->calculateLocalInertia(mass,localInertia);


//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects


#define USE_MOTIONSTATE 1
#ifdef USE_MOTIONSTATE
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);


btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);


btRigidBody* body = new btRigidBody(cInfo);
body->setContactProcessingThreshold(0.0f); // m_defaultContactProcessingThreshold


#else
btRigidBody* body = new btRigidBody(mass,0,shape,localInertia); 
body->setWorldTransform(startTransform);
#endif//


m_dynamicsWorld->addRigidBody(body);


return body;
}


void Vehicle::Create(D3DXVECTOR3 position)
{
btTransform tr;
tr.setIdentity();
btAlignedObjectArray<btCollisionShape*> m_collisionShapes;


#ifdef FORCE_ZAXIS_UP
//   indexRightAxis = 0; 
//   indexUpAxis = 2; 
//   indexForwardAxis = 1; 
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,2.f, 0.5f));
btCompoundShape* compound = new btCompoundShape();
btTransform localTrans;
localTrans.setIdentity();
//localTrans effectively shifts the center of mass with respect to the chassis
localTrans.setOrigin(btVector3(0,0,1));
#else
btCollisionShape* chassisShape = new btBoxShape(btVector3(25.0f, 15.0f, 15.0f));
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));
#endif


compound->addChildShape(localTrans,chassisShape);


tr.setOrigin(btVector3(0,0.f,0));


m_carChassis = localCreateRigidBody(800.0f,tr,compound);//chassisShape); 800.0f
//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);


float connectionHeight = -18.2f;




bool isFrontWheel=true;


//choose coordinate system
m_vehicle->setCoordinateSystem(rightIndex,upIndex,forwardIndex);


#ifdef FORCE_ZAXIS_UP
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight);
#else
btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(25.3),connectionHeight,10*CUBE_HALF_EXTENTS);
#endif


m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),2*CUBE_HALF_EXTENTS-wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(25.3),connectionHeight,10*CUBE_HALF_EXTENTS);
#endif


m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(25.3),connectionHeight,-10*CUBE_HALF_EXTENTS);
#endif //FORCE_ZAXIS_UP
isFrontWheel = false;
m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);
#ifdef FORCE_ZAXIS_UP
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),-2*CUBE_HALF_EXTENTS+wheelRadius, connectionHeight);
#else
connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(25.3),connectionHeight,-10*CUBE_HALF_EXTENTS);
#endif
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;
}
}
}

You are updating the gEngineForce var in your code, this is right, but this var is never used elsewhere. I was checking the original btRaycastVehicle code and I think you should do something similar with that gEngineForce: apply it to the wheels:

void btRaycastVehicle::applyEngineForce(btScalar force, int wheel)

383 {
384 btAssert(wheel>=0 && wheel < getNumWheels());
385 btWheelInfo& wheelInfo = getWheelInfo(wheel);
386 wheelInfo.m_engineForce = force;
387 }

I added the following lines:


void update()
{
       int wheelIndex = 1;
       m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
       m_vehicle->setBrake(gBreakingForce,wheelIndex);
       wheelIndex = 2;
       m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
       m_vehicle->setBrake(gBreakingForce,wheelIndex);

       wheelIndex = 1;
       m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
       wheelIndex = 2;
       m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
}

Now I can notice that the vehicle is steering but not moving forward or backward.

I added the following lines:


void update()
{
       int wheelIndex = 1;
       m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
       m_vehicle->setBrake(gBreakingForce,wheelIndex);
       wheelIndex = 2;
       m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
       m_vehicle->setBrake(gBreakingForce,wheelIndex);

       wheelIndex = 1;
       m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
       wheelIndex = 2;
       m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
}

Now I can notice that the vehicle is steering but not moving forward or backward.

Are you breaking and accelerating at the same time?

m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
m_vehicle->setBrake(gBreakingForce,wheelIndex);

Here is the full update() code:


if (GetAsyncKeyState(VK_UP) != 0)
{
    gEngineForce = maxEngineForce;
    gBreakingForce = 0.f;
}

if (GetAsyncKeyState(VK_DOWN) != 0)
{
    gBreakingForce = maxBreakingForce; 
    gEngineForce = 0.f;
}

if (GetAsyncKeyState(VK_RIGHT) != 0)
{
    gVehicleSteering -= steeringIncrement;
    if ( gVehicleSteering < -steeringClamp)
         gVehicleSteering = -steeringClamp;
}

if (GetAsyncKeyState(VK_LEFT) != 0)
{
    gVehicleSteering += steeringIncrement;
    if ( gVehicleSteering > steeringClamp)
         gVehicleSteering = steeringClamp;
}


{
int wheelIndex = 1;
m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
//m_vehicle->setBrake(gBreakingForce,wheelIndex);
wheelIndex = 2;
m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
//m_vehicle->setBrake(gBreakingForce,wheelIndex);
wheelIndex = 0;
m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
//m_vehicle->setBrake(gBreakingForce,wheelIndex);
wheelIndex = 3;
m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
//m_vehicle->setBrake(gBreakingForce,wheelIndex);

wheelIndex = 1;
m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
wheelIndex = 2;
m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
}
 

As you can see, I tried commenting m_vehicle->setBrake() lines, now the vehicle is getting upside down instead of moving forward.

Still having the same problem...

This topic is closed to new replies.

Advertisement