Bullet vehicle wheels direction problem [Code Attached]

Started by
3 comments, last by Medo Mex 11 years, 4 months ago
I'm trying to setup a vehicle in Bullet physics.

The problem I'm having now is that the wheels are not looking at the correct direction and therefore not rotating at the valid direction.

See the screenshot
[attachment=12414:wheel.jpg]
Advertisement
Is there a problem with simply modifying the wheel mesh by rotating it 90 degrees in an external editor? Can't really tell you much if you don't post code...

"Only idiots quote themselves" - MisterFuzzy

As you can see, the wheels are not setup correctly, the wheels should be looking at the same direction the vehicle is looking at, but as you see the wheels are looking at the sides of the vehicle.

And I don't have any problem with external editor.
I have been having a problem with Bullet Vehicle, the wheels are looking at the sides of the vehicle instead of looking at the same direction the chassis is looking at.

Here is the code:
#include "Vehicle.h"
#include "d3d9.h"
#include "d3dx9.h"
#include "BulletCollision/CollisionShapes/btShapeHull.h"
//#define DEFAULT_VEHICLE_SETUP
//#define DEBUG_VEHICLE_WHEEL
#define SAFE_DELETE(p) { if (p) { delete (p); (p)=NULL; } }
#define SAFE_RELEASE(p) { if (p) { (p)->Release(); (p)=NULL; } }
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;
}
btBvhTriangleMeshShape *CreateBvhTriangleMeshShape(LPD3DXMESH pMesh, TriMeshData *pData)
{
DWORD numVertices = pMesh->GetNumVertices();
DWORD numFaces = pMesh->GetNumFaces();
Vertex *v = 0;
pMesh->LockVertexBuffer(0, (void**)&v);
// Extract vertices
pData->vertices = new btScalar[numVertices * 3];
for(DWORD i = 0; i < numVertices; i++)
{
pData->vertices[i*3+0] = v.position.x;
pData->vertices[i*3+1] = v.position.y;
pData->vertices[i*3+2] = v.position.z;
}
pMesh->UnlockVertexBuffer();
// Extract indices
pData->indices = new int[numFaces * 3];
WORD* ind = 0;
pMesh->LockIndexBuffer(0,(void**)&ind);

//memcpy( &indices, &ind, sizeof(ind));
for(DWORD i = 0; i < numFaces; i++)
{
pData->indices[i*3+0] = ind[i*3+0];
pData->indices[i*3+1] = ind[i*3+1];
pData->indices[i*3+2] = ind[i*3+2];
}
pMesh->UnlockIndexBuffer();
int indexStride = 3 * sizeof(int);
int vertStride = sizeof(btVector3);
pData->indexVertexArrays = new btTriangleIndexVertexArray(numFaces, pData->indices, indexStride,
numVertices, (btScalar*) &pData->vertices[0], sizeof(btScalar) * 3);
bool useQuantizedAabbCompression = true;
btBvhTriangleMeshShape *shape = new btBvhTriangleMeshShape(pData->indexVertexArrays, true);

// The indices, vertices, and array needs to be manually deleted else Bullet will get a access violation
//delete indices;
//delete vertices;
//delete indexVertexArrays;
return shape;
}
btConvexHullShape *CreateConvexHullShape( LPD3DXMESH pMesh )
{
TriMeshData trimeshshape;
btBvhTriangleMeshShape *triangleMeshShape = CreateBvhTriangleMeshShape( pMesh, &trimeshshape );
btTriangleMesh* triangleMesh = new btTriangleMesh();
for(DWORD i = 0; i < pMesh->GetNumFaces(); i++)
{
int index0 = trimeshshape.indices[i*3];
int index1 = trimeshshape.indices[i*3+1];
int index2 = trimeshshape.indices[i*3+2];
btVector3 vertex0(trimeshshape.vertices[index0*3], trimeshshape.vertices[index0*3+1],trimeshshape.vertices[index0*3+2]);
btVector3 vertex1(trimeshshape.vertices[index1*3], trimeshshape.vertices[index1*3+1],trimeshshape.vertices[index1*3+2]);
btVector3 vertex2(trimeshshape.vertices[index2*3], trimeshshape.vertices[index2*3+1],trimeshshape.vertices[index2*3+2]);
triangleMesh->addTriangle(vertex0,vertex1,vertex2);
}
btConvexShape* tmpConvexShape = new btConvexTriangleMeshShape(triangleMesh);
// Create a hull approximation
btShapeHull* hull = new btShapeHull(tmpConvexShape);
btScalar margin = tmpConvexShape->getMargin();
hull->buildHull(margin);

btConvexHullShape* simplifiedConvexShape = new btConvexHullShape();
for(int i= 0; i < hull->numVertices(); i++ )
{
simplifiedConvexShape->addPoint(hull->getVertexPointer());
}
delete triangleMeshShape;
delete triangleMesh;
delete tmpConvexShape;
delete hull;
return simplifiedConvexShape;
}
btRigidBody* CreateRigidBody(float mass, const btTransform& startTransform, btCollisionShape* shape)
{
//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
btDefaultMotionState* myMotionState = new btDefaultMotionState( startTransform );
btRigidBody::btRigidBodyConstructionInfo cInfo(mass,myMotionState,shape,localInertia);
btRigidBody* body = new btRigidBody(cInfo);
return body;
}
Vehicle::Vehicle(void)
{
//LOG_DEBUG( "Vehicle constructor called." );
mChassisShape = NULL;
mCompoundShape = NULL;
mVehicleRaycaster = NULL;
mRaycastVehicle = NULL;
mWheelDebugMesh = NULL;
mChassisMesh = NULL;
mWheelMesh = NULL;
mSteeringValue = 0.f;
mMaxEngineForce = 3000.f; // This should be engine/velocity dependent
mMaxBreakingForce = 100.f;
mSteeringIncrement = 0.04f;
mSteeringClamp = 0.4f;
mEngineForce = 0.f;
mBreakingForce = 0.f;
mSuspensionStiffness = 20.f;
mSuspensionDamping = 2.3f;
mSuspensionCompression = 4.4f;
mWheelFriction = 1000;//1e30f;
mRollInfluence = 0.1f;//1.0f;
mChassisMesh = NULL;
mWheelMesh = NULL;
mWheelDebugMesh = NULL;
#ifdef DEFAULT_VEHICLE_SETUP
mWheelWidth = 0.4f;
mWheelRadius = 0.5f;
#else
mWheelWidth = 0.4f;
mWheelRadius = 1.f;
#endif
mSize = 1.0f;
}
Vehicle::~Vehicle(void)
{
//LOG_DEBUG( "Vehicle destructor Called." );
Destroy();
}
void Vehicle::Destroy()
{
mDynamicsWorld->removeVehicle( mRaycastVehicle );
SAFE_DELETE( mCompoundShape );
SAFE_DELETE( mChassisShape );
SAFE_DELETE( mVehicleRaycaster );
SAFE_DELETE( mRaycastVehicle );
SAFE_DELETE( mChassisMesh );
SAFE_DELETE( mWheelMesh );
SAFE_DELETE( mSimplifiedConvexShape );
}
bool Vehicle::Create( LPDIRECT3DDEVICE9 device, btDynamicsWorld *dynamicsWorld, btVector3 &position, btScalar mass )
{
mDevice = device;
mDynamicsWorld = dynamicsWorld;
mChassisMesh = new Mesh();
mChassisMesh->Create( mDevice, "Media//chassis.x" );
mWheelMesh = new Mesh();
mWheelMesh->Create( mDevice, "Media//wheel.x" );

#ifdef DEFAULT_VEHICLE_SETUP
// create a chassis shape that is proportfion to the size
mChassisShape = new btBoxShape(btVector3(1.f * mSize, 0.5f, 2.0f * mSize));
#else
mSize = 1.7f;
mChassisShape = new btConvexHullShape();
// Extract vertices from vehicle mesh
Vertex *v = 0;
mChassisMesh->LockVertexBuffer(0, (void**)&v);
for( DWORD i = 0; i < mChassisMesh->GetNumVertices(); i++ )
{
((btConvexHullShape*)mChassisShape)->addPoint( btVector3( v.position.x, v.position.y, v.position.z ) );
}
mChassisMesh->UnlockVertexBuffer();
#endif
mSimplifiedConvexShape = CreateConvexHullShape( mChassisMesh );
mCompoundShape = new btCompoundShape();
//mWheelWidth = 0.4f * mSize;
//mWheelRadius = 0.5f * mSize;
btTransform localTrans;
localTrans.setIdentity();
localTrans.setOrigin( btVector3(0.0f, 1.0f, 0.0f ) );
#ifdef DEFAULT_VEHICLE_SETUP
mCompoundShape->addChildShape(localTrans, mChassisShape);
#else
mCompoundShape->addChildShape(localTrans, mSimplifiedConvexShape);
#endif
localTrans.setOrigin( position );
mChassisRigidBody = CreateRigidBody( mass, localTrans, mCompoundShape );
mChassisRigidBody->setCenterOfMassTransform( localTrans );
mChassisRigidBody->setLinearVelocity(btVector3(0,0,0));
mChassisRigidBody->setAngularVelocity(btVector3(0,0,0));
mDynamicsWorld->addRigidBody(mChassisRigidBody );
mDynamicsWorld->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(mChassisRigidBody->getBroadphaseHandle(), mDynamicsWorld->getDispatcher());
int rightIndex = 0;
int upIndex = 1;
int forwardIndex = 2;
btVector3 wheelDirectionCS0(0,-1,0);
btVector3 wheelAxleCS(-1,0,0);
btScalar suspensionRestLength(0.6);
mVehicleRaycaster = new btDefaultVehicleRaycaster(mDynamicsWorld);
mRaycastVehicle = new btRaycastVehicle(mVehicleTuning, mChassisRigidBody, mVehicleRaycaster);
// Never deactivate the vehicle
mChassisRigidBody->setActivationState(DISABLE_DEACTIVATION);
mDynamicsWorld->addVehicle(mRaycastVehicle);
#ifdef DEFAULT_VEHICLE_SETUP
float connectionHeight = 1.2f;
#else
float connectionHeight = 1.9f;
#endif
bool isFrontWheel = true;
//choose coordinate system
mRaycastVehicle->setCoordinateSystem(rightIndex, upIndex, forwardIndex);
btVector3 connectionPointCS0(mSize-(.3f*mWheelWidth),connectionHeight,2.f*mSize-mWheelRadius);
mRaycastVehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,mWheelRadius,mVehicleTuning,isFrontWheel);
connectionPointCS0 = btVector3(-mSize+(.3f*mWheelWidth),connectionHeight,2.f*mSize-mWheelRadius);
mRaycastVehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,mWheelRadius,mVehicleTuning,isFrontWheel);
connectionPointCS0 = btVector3(-mSize+(.3f*mWheelWidth),connectionHeight,-2.f*mSize+mWheelRadius);
isFrontWheel = false;
mRaycastVehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,mWheelRadius,mVehicleTuning,isFrontWheel);
connectionPointCS0 = btVector3(mSize-(.3f*mWheelWidth),connectionHeight,-2.f*mSize+mWheelRadius);
mRaycastVehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,mWheelRadius,mVehicleTuning,isFrontWheel);
for (int i=0;i<mRaycastVehicle->getNumWheels();i++)
{
btWheelInfo& wheel = mRaycastVehicle->getWheelInfo(i);
wheel.m_suspensionStiffness = mSuspensionStiffness;
wheel.m_wheelsDampingRelaxation = mSuspensionDamping;
wheel.m_wheelsDampingCompression = mSuspensionCompression;
wheel.m_frictionSlip = mWheelFriction;
wheel.m_rollInfluence = mRollInfluence;
}
// Create a wheel shape
#ifdef DEBUG_VEHICLE_WHEEL
btCylinderShapeX wheelShape(btVector3(mWheelWidth,mWheelRadius,mWheelRadius));
int upAxis = ((btCylinderShapeX)wheelShape).getUpAxis();
float radius = ((btCylinderShapeX)wheelShape).getRadius();
float halfHeight = ((btCylinderShapeX)wheelShape).getHalfExtentsWithMargin()[upAxis];
D3DXCreateCylinder( mDevice, radius, radius, halfHeight* 2.0f, 15, 10, &mWheelDebugMesh, NULL );
#endif
return true;
}
void Vehicle::Update( float elapsedTime )
{
if(GetKeyState( VK_UP ) & 0x8000 )
{
mEngineForce = mMaxEngineForce;
mBreakingForce = 0.f;
}
else
{
mEngineForce = 0.f;
}
if(GetKeyState( VK_DOWN ) & 0x8000 )
{
mEngineForce = -mMaxEngineForce;
mBreakingForce = 0.f;
}
else
{
mBreakingForce = 0.f;
}
if(GetKeyState( VK_LEFT ) & 0x8000 )
{
mSteeringValue -= mSteeringIncrement;
if ( mSteeringValue < -mSteeringClamp)
mSteeringValue = -mSteeringClamp;
}
else
{
// Return wheel to original position
if ( mSteeringValue <= 0.0f )
mSteeringValue += mSteeringIncrement;
}
if(GetKeyState( VK_RIGHT ) & 0x8000 )
{
mSteeringValue += mSteeringIncrement;
if(mSteeringValue > mSteeringClamp)
mSteeringValue = mSteeringClamp;
}
else
{
// Return wheel to original position
if( mSteeringValue >= 0.0f )
mSteeringValue -= mSteeringIncrement;
}
int wheelIndex = 2;
mRaycastVehicle->applyEngineForce(mEngineForce,wheelIndex);
mRaycastVehicle->setBrake(mBreakingForce,wheelIndex);
wheelIndex = 3;
mRaycastVehicle->applyEngineForce(mEngineForce,wheelIndex);
mRaycastVehicle->setBrake(mBreakingForce,wheelIndex);
wheelIndex = 0;
mRaycastVehicle->setSteeringValue(mSteeringValue,wheelIndex);
wheelIndex = 1;
mRaycastVehicle->setSteeringValue(mSteeringValue,wheelIndex);
// Update vehicle direction
btWheelInfo& wheel = mRaycastVehicle->getWheelInfo( 0 );
btVector3 up = -wheel.m_raycastInfo.m_wheelDirectionWS;
const btVector3& right = wheel.m_raycastInfo.m_wheelAxleWS;
mForward = up.cross(right);
mForward.normalize();
}
void Vehicle::Render()
{
// Draw wheels (cylinders)
for (int i=0;i<mRaycastVehicle->getNumWheels();i++)
{
//synchronize the wheels with the (interpolated) chassis worldtransform
mRaycastVehicle->updateWheelTransform(i, true );
D3DXMATRIX matWorld = ConvertMatrix( mRaycastVehicle->getWheelInfo(i).m_worldTransform );
btVector3 pos = mRaycastVehicle->getWheelInfo(i).m_worldTransform.getOrigin();
#ifndef DEFAULT_VEHICLE_SETUP
mDevice->SetTransform( D3DTS_WORLD, &matWorld );
if( mWheelMesh )
{
mWheelMesh->DrawSubset( 0 );
}
#endif
#ifdef DEBUG_VEHICLE_WHEEL
D3DXMATRIX matRot;
D3DXMatrixRotationY( &matRot, D3DXToRadian(-90.0f));
D3DXMatrixMultiply(&matWorld, &matRot, &matWorld);
mDevice->SetTransform( D3DTS_WORLD, &matWorld );
mWheelDebugMesh->DrawSubset( 0 ) ;
#endif
}
// Draw chassis
{
btTransform trn;
mRaycastVehicle->getRigidBody()->getMotionState()->getWorldTransform( trn );
#ifndef DEFAULT_VEHICLE_SETUP
if( mChassisMesh )
{
D3DXMATRIX matWorld = ConvertMatrix( trn );
D3DXMATRIX matRot, matTrans, matScale;
// offset 1.0 for the wheel
D3DXMatrixTranslation( &matTrans, 0.0f, 1.0f, 0.0f );
D3DXMatrixMultiply(&matWorld, &matTrans, &matWorld);
mDevice->SetTransform( D3DTS_WORLD, &matWorld );
mChassisMesh->DrawSubset( 0 );
}
#else
if( shapeDrawer )
{
shapeDrawer->Render( mRaycastVehicle->getRigidBody()->getCollisionShape(), trn );
}
#endif
}
}


Any help would be greatly appreciated.
Any updates? have been working on this for the last few days and it's not working right.

This topic is closed to new replies.

Advertisement