What's the appropriate value for step?

Started by
7 comments, last by Ravnock 11 years, 4 months ago
mRaycastVehicle->updateVehicle(???) // What should I put in this parameter?
Advertisement
You shouldn't call it at all. You add the vehicle to the world, and then this method will be called by bullet with the timestep value that bullet is configured to use.
When I don't call it, the wheels are penetrating the terrain and the vehicle never move, when I call it the vehicle work and move normally, the wheels are perfectly adjusted, problem is I don't know what is the value I should use for 'step' parameter.
You still should not call it.
Did you add the vehicle to the world? ( mDynamicsWorld->addVehicle(mRaycastVehicle); )

If you don't believe me, take a look at the VehicleDemo in the bullet examples. Works fine without any explicit call to updateVehicle.
Yes, I added the vehicle to the dynamics world, I see that the wheels can rotate to the right and to the left, but the vehicle never move and half of the wheels are penetrating the terrain.
Ok. There is something else wrong then.

A call to updateVehicle is not a solution, even though it might look like that in this particular case.
What that will do is that all suspension forces, engine forces and such are added twice. once in the real bullet update, and another time by you.
Depending on what you send in, it might also be scaled weirdly.

So its normal that you get different behaviour if you DO call it, but it can never be correct behaviour, and you still should NOT call it yourself.

There is probably something wrong with the tuning of your vehicle, or some other setup.
Are you updating the world?
All the RigidBodies are working perfectly, I'm only having the problem with the vehicle, here is the vehicle class that I'm using:
#include "Vehicle.h"
#include "d3d9.h"
#include "d3dx9.h"
#include "physics.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)
{
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 = 4.4f;
mWheelRadius = 20.5f;
#endif
mSize = 4.7f;
}
Vehicle::~Vehicle(void)
{
Destroy();
}
void Vehicle::Destroy()
{
mDynamicsWorld->removeVehicle( mRaycastVehicle );
SAFE_DELETE( mCompoundShape );
SAFE_DELETE( mChassisShape );
SAFE_DELETE( mVehicleRaycaster );
SAFE_DELETE( mRaycastVehicle );
//SAFE_RELEASE( mWheelDebugMesh );
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 = GetChassisMesh(); // Prototype: LPD3DXMESH GetChassisMesh();
mWheelMesh = GetWheelsMesh(); // Prototype: LPD3DXMESH GetWheelsMesh();
#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 = 4.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 = 18.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(btVector3(35.76, -2.0, 20.37),wheelDirectionCS0,wheelAxleCS,suspensionRestLength,mWheelRadius,mVehicleTuning,isFrontWheel);
connectionPointCS0 = btVector3(-mSize+(.3f*mWheelWidth),connectionHeight,2.f*mSize-mWheelRadius);
mRaycastVehicle->addWheel(btVector3(-35.76, -2.0, 20.37),wheelDirectionCS0,wheelAxleCS,suspensionRestLength,mWheelRadius,mVehicleTuning,isFrontWheel);
connectionPointCS0 = btVector3(-mSize+(.3f*mWheelWidth),connectionHeight,-2.f*mSize+mWheelRadius);
isFrontWheel = false;
mRaycastVehicle->addWheel(btVector3(35.76, -2.0, -20.37),wheelDirectionCS0,wheelAxleCS,suspensionRestLength,mWheelRadius,mVehicleTuning,isFrontWheel);
connectionPointCS0 = btVector3(mSize-(.3f*mWheelWidth),connectionHeight,-2.f*mSize+mWheelRadius);
mRaycastVehicle->addWheel(btVector3(-35.76, -2.0, -20.37),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;
D3DXMatrixRotationZ( &matRot, D3DXToRadian(-90.0f));
D3DXMatrixMultiply(&matWorld, &matRot, &matWorld);
mDevice->SetTransform( D3DTS_WORLD, &matWorld );
//mWheelDebugMesh->DrawSubset( 0 );
mWheelMesh->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
}
}

Yes, I added the vehicle to the dynamics world, I see that the wheels can rotate to the right and to the left, but the vehicle never move and half of the wheels are penetrating the terrain.


Have you tried to initialize the vehicle some meters over the floor? Maybe is stuck inside the floor...

This topic is closed to new replies.

Advertisement