Steering makes Vehicle not work correctly

dwells254lb43
Posts: 10
Joined: Wed Jan 21, 2009 12:27 am

Steering makes Vehicle not work correctly

Post by dwells254lb43 »

I have used all the code from the vehicle demo and you can double check me to make sure I am using it all correctly. When I create a vehicle it works correctly when going straight but as soon as i try to make it turn it starts to strafe and move to a crawl. Anyone have any idea on why it is doing this?

Code: Select all

createVehicle()
{
	//used to update the vehicle, vehicle controller
	//float	gEngineForce = 0.f;
	//float	gBreakingForce = 0.f;
	//float	gVehicleSteering = 0.f;

	float	maxEngineForce = 1000.f;//this should be engine/velocity dependent
	float	maxBreakingForce = 100.f;

	
	float	steeringIncrement = 0.04f;
	float	steeringClamp = 0.3f;
	float	wheelRadius = 0.5f;
	float	wheelWidth = 0.4f;
	float	wheelFriction = 1000;//BT_LARGE_FLOAT;
	float	suspensionStiffness = 20.f;
	float	suspensionDamping = 2.3f;
	float	suspensionCompression = 4.4f;
	float	rollInfluence = 0.1f;//1.0f;
	float	vehicleMass = 800.f;
	btScalar suspensionRestLength(0.6);

	btVector3 wheelDirectionCS0(0,-1,0);
	btVector3 wheelAxleCS(-1,0,0);
	
	btVector3 localInertia(0,0,0);

	int rightIndex = 0;
	int upIndex = 1;
	int forwardIndex = 2;


	btCollisionShape* chassisShape = new btBoxShape(btVector3(1.f,0.5f,2.f));
	collisionShapes.push_back(chassisShape);

	btCompoundShape* compound = new btCompoundShape();
	collisionShapes.push_back(compound);
	btTransform localTrans;
	localTrans.setIdentity();
	//localTrans effectively shifts the center of mass with respect to the chassis
	localTrans.setOrigin(btVector3(0,1,0));

	compound->addChildShape(localTrans,chassisShape);

	//----localcreateRidgidBody
	btTransform tr;
	tr.setIdentity();
	tr.setOrigin(btVector3(0,1.f,0));
	btDefaultMotionState* myMotionState = new btDefaultMotionState(tr);
	btRigidBody::btRigidBodyConstructionInfo rbInfo(vehicleMass,myMotionState,compound,localInertia);

	compound->calculateLocalInertia(vehicleMass,localInertia);

	m_carChassis = new btRigidBody(rbInfo);//chassisShape);
	//m_carChassis->setDamping(0.2,0.2);
	dynamicsWorld->addRigidBody(m_carChassis);
	//----end localcreateRidgidBody 
	
	//wheelShape
	m_wheelShape = new btCylinderShapeX(btVector3(wheelWidth,wheelRadius,wheelRadius));

	/// create vehicle
	m_vehicleRayCaster = new btDefaultVehicleRaycaster(dynamicsWorld);
	m_vehicle = new btRaycastVehicle(m_tuning,m_carChassis,m_vehicleRayCaster);
	
	///never deactivate the vehicle
	m_carChassis->setActivationState(DISABLE_DEACTIVATION);

	dynamicsWorld->addVehicle(m_vehicle);

	float connectionHeight = 1.2f;//1.2

	bool isFrontWheel=true;

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

	btVector3 connectionPointCS0(1-(0.3*wheelWidth),connectionHeight,2*1-wheelRadius);
	m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);

	connectionPointCS0 = btVector3(-1+(0.3*wheelWidth),connectionHeight,2*1-wheelRadius);
	m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);

	connectionPointCS0 = btVector3(-1+(0.3*wheelWidth),connectionHeight,-2*1+wheelRadius);
	isFrontWheel = false;
	m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);

	connectionPointCS0 = btVector3(1-(0.3*wheelWidth),connectionHeight,-2*1+wheelRadius);
	m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);

	for (int i=0;i<m_vehicle->getNumWheels();i++)
		{
			btWheelInfo& wheel = m_vehicle->getWheelInfo(i);
			wheel.m_suspensionStiffness = suspensionStiffness;
			wheel.m_wheelsDampingRelaxation = suspensionDamping;
			wheel.m_wheelsDampingCompression = suspensionCompression;
			wheel.m_frictionSlip = wheelFriction;
			wheel.m_rollInfluence = rollInfluence;
		}

}
then for the update I have

Code: Select all

void PhysicsWorld::updateVehicle()
{
	m_vehicle->resetSuspension();
	m_vehicle->updateWheelTransform(0,true);
	m_vehicle->updateWheelTransform(1,true);
	m_vehicle->updateWheelTransform(2,true);
	m_vehicle->updateWheelTransform(3,true);

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

	wheelIndex = 0;
	m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
	wheelIndex = 1;
	m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
}
DannyW
Posts: 1
Joined: Wed Jul 22, 2009 11:17 am

Re: Steering makes Vehicle not work correctly

Post by DannyW »

Hello

I just started out with bullet and had this problem too.
I forgot to calculate the inertia of the car chassis. You didn't forget, but you did it in the wrong order.

Just swap these two lines:

Code: Select all

   btRigidBody::btRigidBodyConstructionInfo rbInfo(vehicleMass,myMotionState,compound,localInertia);

   compound->calculateLocalInertia(vehicleMass,localInertia);

Hope this fixes it!
Danny