RaycastVehicle upside down

Ipredlol
Posts: 8
Joined: Fri Mar 11, 2011 6:23 pm

RaycastVehicle upside down

Post by Ipredlol »

Hi folks,

I am using bullet's RaycastVehicle och directx to simulate a car. I am using default values multiplied by 40 from VehicleDemo but the vehicle seems to be upside down(Check out the first pic) and the body of the car except the wheels goes through the surface(every collisionshape's margin has been set to 0.1, and by give them higher values it made no difference). Then I tried to multiply the connectionHeight by -1, the wheels were in the right places but the car sinks down completely after bouncing a couple of times with the central circle above the surface(the second pic). What have I done wrong here? Which is the correct way to flip a vehicle?
raycast.jpg
raycast2.jpg
Below is the code just in case anyone feels like reading:)

Code: Select all

	int rightIndex = 0;
	int upIndex = 1;
	int forwardIndex = 2;
	btVector3 wheelDirectionCS0(0,-1,0);
	btVector3 wheelAxleCS(-1,0,0);



const int maxProxies = 32766;
const int maxOverlap = 65535;

float	gEngineForce = 0.f;
float	gBreakingForce = 0.f;

float	maxEngineForce = 160000.f;//this should be engine/velocity dependent 80000
float	maxBreakingForce = 16000.f;

float	gVehicleSteering = 0.f;
float	steeringIncrement = 0.04f;
float	steeringClamp = 0.3f;
float	wheelRadius = 40.f;
float	wheelWidth = 32.f;
float	wheelFriction = 500;//BT_LARGE_FLOAT;
float	suspensionStiffness = 20.f;
float	suspensionDamping = 1.f;
float	suspensionCompression = 4.4f;
float	rollInfluence = 0.1f;//1.0f;

list<btVector3> vertList;
int count;
btTriangleMesh* trimesh;

btScalar suspensionRestLength(48);

#define CUBE_HALF_EXTENTS 80

Ship::Ship()
:
ship(0),
m_indexVertexArrays(0),
m_vertices(0)
{
	m_vehicle = 0;
	m_wheelShape = 0;
	forward = 'W';
	back = 'S';
	right = 'D';
	left = 'A';
}

Ship::~Ship()
{}

void Ship::initPhysics()
{

	btCollisionShape* groundShape;
	m_collisionShapes.push_back(groundShape);
	m_collisionConfiguration = new btDefaultCollisionConfiguration();
	m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
	btVector3 worldMin(-1000,-1000,-1000);
	btVector3 worldMax(1000,1000,1000);
	m_overlappingPairCache = new btAxisSweep3(worldMin,worldMax);
	m_constraintSolver = new btSequentialImpulseConstraintSolver();
	m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_constraintSolver,m_collisionConfiguration);
	

	btTransform tr;
	tr.setIdentity();

	//LPCTSTR meshName = L"Media\\SpaceShip2.obj";
	//const char* convexShapeName = "Media\\SpaceShip2.obj";
	//LPCTSTR meshName1 = L"Media\\Track2.obj";
	//const char* concaveShape1Name = "Media\\Track2.obj";

	LPCTSTR meshName = L"Media\\Box.obj";
	const char* convexShapeName = "Media\\Box.obj";
	LPCTSTR meshName1 = L"Media\\box33.obj";
	const char* concaveShape1Name = "Media\\box33.obj";

	groundShape = MakeConcave(0,0,0 ,concaveShape1Name);
	//groundShape->setMargin(20.f);
	
	//groundShape = new btStaticPlaneShape(btVector3(0,-1,0),1);
	//groundShape = new btBoxShape(btVector3(50,3,50));
	groundShape->setMargin(0.1f);
	m_collisionShapes.push_back(groundShape);

	btTransform	startTransform;
	startTransform.setIdentity();
	startTransform.setOrigin(btVector3(0,0,0));  
	btDefaultMotionState* motionState = new btDefaultMotionState(startTransform);
	
   	btVector3 fallInertia(0,0,0);
		btScalar fixed = 0;
	btScalar canMove = 10000;
	btRigidBody* bana = new btRigidBody(fixed,motionState, groundShape ,fallInertia);
	//bana1->setRestitution(0.01);
	bana->setFriction(5.f);
	m_dynamicsWorld->addRigidBody(bana);


	btCollisionShape* shipShape = new btBoxShape(btVector3(80.f,40.f,160.f));
	shipShape->setMargin(0.1f);
	m_collisionShapes.push_back(shipShape);
	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,80,0));
	compound->addChildShape(localTrans,shipShape);
	fallInertia = btVector3(0,0,0);

	compound->calculateLocalInertia(canMove, fallInertia);
	//startTransform.setOrigin(btVector3(-6216,-100,-1356)); 
	compound->setMargin(0.1f);
	startTransform.setOrigin(btVector3(0,7000,0)); 
	m_dynamicsWorld->setGravity(btVector3(0,-10,0));

	btDefaultMotionState* motionState1 = new btDefaultMotionState(startTransform);
	
	ship = new btRigidBody(canMove,motionState1,compound,fallInertia);
	//ship->setRestitution(0.0);
	//ship->setFriction(5.f);
	m_dynamicsWorld->addRigidBody(ship);

	m_wheelShape = new btCylinderShapeX(btVector3(wheelWidth,wheelRadius,wheelRadius));
	m_wheelShape->setMargin(0.1f);
	//clientResetScene();

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

		m_dynamicsWorld->addVehicle(m_vehicle);


		float connectionHeight = 96.f; //Jag har ändrat värdet,48
		bool isFrontWheel=true;

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

		btVector3 connectionPointCS0(CUBE_HALF_EXTENTS-(0.3*wheelWidth),connectionHeight,2*CUBE_HALF_EXTENTS-wheelRadius);

		m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);

		connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),connectionHeight,2*CUBE_HALF_EXTENTS-wheelRadius);

		m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);

		connectionPointCS0 = btVector3(-CUBE_HALF_EXTENTS+(0.3*wheelWidth),connectionHeight,-2*CUBE_HALF_EXTENTS+wheelRadius);

		isFrontWheel = false;
		m_vehicle->addWheel(connectionPointCS0,wheelDirectionCS0,wheelAxleCS,suspensionRestLength,wheelRadius,m_tuning,isFrontWheel);

		connectionPointCS0 = btVector3(CUBE_HALF_EXTENTS-(0.3*wheelWidth),connectionHeight,-2*CUBE_HALF_EXTENTS+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;
		}
	}
}


//to be implemented by the demo
void Ship::renderme()
{m_vehicle->getRigidBody()->activate(true);
	for (int i=0;i<m_vehicle->getNumWheels();i++)
	{
		//synchronize the wheels with the (interpolated) chassis worldtransform
		m_vehicle->updateWheelTransform(i,true);
	}
}

void Ship::clientMoveAndDisplay()
{
	m_vehicle->getRigidBody()->activate(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);
		m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
		m_vehicle->setBrake(gBreakingForce,wheelIndex);
		wheelIndex = 1;
		m_vehicle->setSteeringValue(gVehicleSteering,wheelIndex);
		m_vehicle->applyEngineForce(gEngineForce,wheelIndex);
		m_vehicle->setBrake(gBreakingForce,wheelIndex);


		cout << "IS IN CONTACT? " << m_vehicle->getWheelInfo(0).m_raycastInfo.m_isInContact << endl;
		cout << "IS IN CONTACT? " << m_vehicle->getWheelInfo(1).m_raycastInfo.m_isInContact << endl;
		cout << "IS IN CONTACT? " << m_vehicle->getWheelInfo(2).m_raycastInfo.m_isInContact << endl;
		cout << "IS IN CONTACT? " << m_vehicle->getWheelInfo(3).m_raycastInfo.m_isInContact << endl;
	}

	 m_dynamicsWorld->stepSimulation(1/6.f,10);
	renderme(); 
}


void Ship::clientResetScene()
{
	gVehicleSteering = 0.f;
	ship->setCenterOfMassTransform(btTransform::getIdentity());
	ship->setLinearVelocity(btVector3(0,0,0));
	ship->setAngularVelocity(btVector3(0,0,0));
	m_dynamicsWorld->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(ship->getBroadphaseHandle(),m_dynamicsWorld->getDispatcher());
	if (m_vehicle)
	{
		m_vehicle->resetSuspension();
		for (int i=0;i<m_vehicle->getNumWheels();i++)
		{
			//synchronize the wheels with the (interpolated) chassis worldtransform
			m_vehicle->updateWheelTransform(i,true);
		}
	}

}
void Ship::displayCallback(){
}
void Ship::specialKeyboard(int key, int x, int y){
	if ( GetAsyncKeyState(right) ) {
		cout<< "left"<< endl;
			gVehicleSteering += steeringIncrement;
			if (	gVehicleSteering > steeringClamp)
					gVehicleSteering = steeringClamp;
	} else if ( GetAsyncKeyState(left) ) {
		cout<< "right"<< endl;
			gVehicleSteering -= steeringIncrement;
			if (	gVehicleSteering < -steeringClamp)
					gVehicleSteering = -steeringClamp;
	} 
	if ( GetAsyncKeyState(forward) ) {
		//ship->applyCentralForce(btVector3(100.f,2000.f,100.f));
		cout<< "forward"<< endl;
			gEngineForce = maxEngineForce;
			gBreakingForce = 0.f;
	} else if ( GetAsyncKeyState(back) ) {
		cout<< "back"<< endl;
			gBreakingForce = maxBreakingForce; 
			gEngineForce = 0.f;
	}
}
You do not have the required permissions to view the files attached to this post.
Bubba1869
Posts: 5
Joined: Mon Mar 21, 2011 3:50 pm

Re: RaycastVehicle upside down

Post by Bubba1869 »

Have you tried lowering you connection height? In my project using the ray-cast vehicle the value is around -0.3 to lower it past the rigid body slightly. At 96.0f your connection height might be way too high causing the wheels to rest on your plane but the rigid body to be 'hanging' underneath.

Hope this helps