• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Medo Mex

Setting up a Vehicle

9 posts in this topic

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;
}
}
}

 

Edited by Medo3337
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
@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.

0

Share this post


Link to post
Share on other sites

@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?

0

Share this post


Link to post
Share on other sites

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;
}
}
}
0

Share this post


Link to post
Share on other sites

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 }
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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);

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0