Problems with raycast vehicle

benb7760
Posts: 7
Joined: Sun Apr 11, 2010 12:12 pm

Problems with raycast vehicle

Post by benb7760 »

This is my first time using Bullet for vehicles, and I am having trouble with them. I have made a class to create and destroy them, however after adding all wheels and calling init(), nothing seems to be happening. I have a breakpoint set on the setWorldTransform of the motion state for the vehicle being created, however it appears that function is never called.

Can anybody spot the problem(s) in my code? I am using bullet 2.76

Code: Select all

class pBtVehicleObjectInstance : public pVehicleObjectInstance {
public:
	pBtVehicleObjectInstance(unsigned int objid, unsigned int objinstid, evtTbl * evts, btDiscreteDynamicsWorld * dy, btCollisionShape * cShape, btCollisionShape * wShape){
		objID = objid;
		instID = objinstid;
		lEvts = evts;
		dynam = dy;
		Chassis_mass = 800;
		ChassisShape = new btBoxShape(btVector3(1.f,0.5f,2.f));//cShape;
		WheelShape = wShape;
	}
	void addWheel(unsigned int eid, unsigned int einst, float * cPoint, bool front){
		int tmp=0;
		while(wheelBool[tmp]==true){
			tmp++;
		}
		wheelBool[tmp]=true;
		wheelID[tmp]=eid;
		wheelInst[tmp]=einst;
		wheelPos[tmp]=cPoint;
		wheelFront[tmp]=front;
	}
	void init(){
		btVector3 localInertia(0,0,0);
		ChassisState = new btVehicleBodyMotionState(objID,instID,lEvts,btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
		ChassisRigidBody = new btRigidBody(Chassis_mass,ChassisState,ChassisShape,localInertia);

	ChassisRigidBody->setCenterOfMassTransform(btTransform::getIdentity());
	ChassisRigidBody->setLinearVelocity(btVector3(0,0,0));
	ChassisRigidBody->setAngularVelocity(btVector3(0,0,0));
	dynam->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(ChassisRigidBody->getBroadphaseHandle(),dynam->getDispatcher());

		mVehicleRayCaster = new btDefaultVehicleRaycaster(dynam);
		mVehicle = new btRaycastVehicle(mTuning,ChassisRigidBody,mVehicleRayCaster);
		//ChassisRigidBody->setActivationState(DISABLE_DEACTIVATION);
		mVehicle->setCoordinateSystem(0,1,2);
		// Other Wheel Stuff
		dynam->addVehicle(mVehicle);
		int tmp=0;
		while(wheelBool[tmp]==true){
			btScalar suspensionRestLength(0.6);
			btVector3 connectionPointCS0 = btVector3(wheelPos[tmp][0],wheelPos[tmp][1],wheelPos[tmp][2]);
			btVector3 wheelDirectionCS0(0,-1,0);
			btVector3 wheelAxleCS(-1,0,0);
			btWheelInfo& wheel = mVehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,0.25,mTuning,wheelFront[tmp]);
			// Set Properties
				wheel.m_suspensionStiffness = 20.f;
				wheel.m_wheelsDampingRelaxation = 2.3f;
				wheel.m_wheelsDampingCompression = 4.4f;
				wheel.m_frictionSlip = 1000;
				wheel.m_rollInfluence = 0.1f;
			tmp++;
		}
		for(int i=0;i<mVehicle->getNumWheels();i++){
			btWheelInfo& wheel = mVehicle->getWheelInfo(i);
			if(wheel.m_bIsFrontWheel == true){
				mVehicle->applyEngineForce(1000.f, i);
			}
		}
	}
	void setPosRot(float xP, float yP, float zP, float wR, float xR, float yR, float zR){
		;
	}
	std::map<unsigned int, unsigned int> wheelID;
	std::map<unsigned int, unsigned int> wheelInst;
	std::map<unsigned int, float *> wheelPos;
	std::map<unsigned int, bool> wheelFront;
	std::map<unsigned int, bool> wheelBool;
private:
	float Chassis_mass;
	unsigned int objID;
	unsigned int instID;
	evtTbl * lEvts;
	btDiscreteDynamicsWorld * dynam;
	// Vehicle stuff
	btCollisionShape * ChassisShape;
	btVehicleBodyMotionState * ChassisState;
	btRigidBody * ChassisRigidBody;
	btCollisionShape*	WheelShape;
	btRaycastVehicle::btVehicleTuning	mTuning;
	btVehicleRaycaster*	mVehicleRayCaster;
	btRaycastVehicle*	mVehicle;
};
benb7760
Posts: 7
Joined: Sun Apr 11, 2010 12:12 pm

Re: Problems with raycast vehicle

Post by benb7760 »

I discovered that the problem was I had not added the chassis' rigid body to the dynamics world. I am however still having trouble, when the vehicle interacts with a static plane it seems the rigidbody is not rotating when being steered.. and when it interacts with a heightfield, the vehicle does not tilt at all to stay level, and seems to get caught on the terrain, probably as the wheels seem to be going underground, and the chassis collides with the terrain?

Can anybody help me with this?
benb7760
Posts: 7
Joined: Sun Apr 11, 2010 12:12 pm

Re: Problems with raycast vehicle

Post by benb7760 »

I took a video today to see if maybe somebody can recognise what is going wrong here because I cannot.
http://74.53.67.2/~iycp1/ben/stupid.avi
When the video starts, the front wheels are facing away, however to make the vehicle move forward, I have to apply a negative force. Not sure why this is, as almost all of the code i use for vehicles is currently lifted directly from the vehicle demo.

Currently the wheels are visually rotated on the wrong axis, but this is not a problem which bothers me right now, the mesh was exported incorrectly.

Does anybody have any clues? I haven't made any progress in weeks and that isn't a very motivating fact :(
User avatar
Erwin Coumans
Site Admin
Posts: 4221
Joined: Sun Jun 26, 2005 6:43 pm
Location: California, USA

Re: Problems with raycast vehicle

Post by Erwin Coumans »

One obvious problem is that you don't calculate the local inertia tensor, but leave it to zero. Please use chassisShape->calculateLocalInertia(mass,localInertia).
btVector3 localInertia(0,0,0);
ChassisState = new btVehicleBodyMotionState(objID,instID,lEvts,btTransform(btQuaternion(0,0,0,1),btVector3(0,50,0)));
ChassisRigidBody = new btRigidBody(Chassis_mass,ChassisState,ChassisShape,localInertia);
For other issues, I suggest you use Bullet/Demos/VehicleDemo or Bullet/Demos/ForkLiftDemo as a starting point. Both are working vehicle examples.

Thanks,
Erwin
benb7760
Posts: 7
Joined: Sun Apr 11, 2010 12:12 pm

Re: Problems with raycast vehicle

Post by benb7760 »

Can't believe I made two simple mistakes like this! I didn't look closely at the localCreateRigidBody function, so I guess that must have thrown me off.

Thanks Erwin!