Can't figure out how btRaycastVehicle works.

Post Reply
Midnightas
Posts: 8
Joined: Fri Aug 24, 2018 9:03 pm

Can't figure out how btRaycastVehicle works.

Post by Midnightas »

Here is the code for making the vehicle:

Code: Select all

	{
		btCompoundShape* compo = new btCompoundShape();
		
		btTransform t;
		t.setIdentity();
		t.setOrigin({0, -4, 0});
		compo->addChildShape(t, shape);
		shape = compo;
	}
	
	btTransform transform;
	transform.setIdentity();
	transform.setOrigin({0, 10, 0});
	
	float mass = 20;
	
	btVector3 localInertia{0, 0, 0};
	shape->calculateLocalInertia(mass, localInertia);
	
	btDefaultMotionState* motionState = new btDefaultMotionState(transform);
	btRigidBody::btRigidBodyConstructionInfo info{mass, motionState, shape, localInertia};
	body = new btRigidBody(info);
	
	btRaycastVehicle::btVehicleTuning tuning;
	vehicle = new btRaycastVehicle(tuning, body, game->vehicleRaycaster);
	vehicle->addWheel({+0.8, -4.5, -1.8}, {0, -1, 0}, {-1, 0, 0}, 0.7, 0.4, tuning, true);
	vehicle->addWheel({-0.8, -4.5, -1.8}, {0, -1, 0}, {-1, 0, 0}, 0.7, 0.4, tuning, true);
	vehicle->addWheel({+0.8, -4.5, +1.9}, {0, -1, 0}, {-1, 0, 0}, 0.7, 0.4, tuning, false);
	vehicle->addWheel({-0.8, -4.5, +1.9}, {0, -1, 0}, {-1, 0, 0}, 0.7, 0.4, tuning, false);
The body and vehicle are added later so that's not the problem. The model is a gimpact triangle mesh I loaded earlier. But I can't at all figure out what the wheel height and origin (in the compound shape) need to be. Anything I tried results in either the vehicle flying and spinning, or not moving at all (but just before hitting the ground, if I applied engine force then it flies off again). The magic values are from the model, except for the Y value, which I kept guessing.

I know I definitely forgot something.
Midnightas
Posts: 8
Joined: Fri Aug 24, 2018 9:03 pm

Re: Can't figure out how btRaycastVehicle works.

Post by Midnightas »

As a temporary solution I put the wheels outside the car model, and it kinda works. But, there is a problem where steering makes you slow down extremely.
I apply engine force to the back wheels and steer with the front ones, and I did calculate the inertia (which seems to have fixed the issue for someone), so I have no idea.

EDIT: Also steering in general just looks wrong, it's as if the entire car just rotates on it's center.
Here's a video, I move it forward and try steering, then try reversing and then going back again. You can see how unnatural it looks: https://www.youtube.com/watch?v=TAmrj5nIOPU.
PcChip
Posts: 33
Joined: Sun May 20, 2018 3:09 pm

Re: Can't figure out how btRaycastVehicle works.

Post by PcChip »

I pulled this out of my old "OpenGL Playground" code

the project doesn't compile anymore so I can't test it, but this used to work for me

Code: Select all

	// The vehicle
	btScalar chassisMass(1000.0f);
	btVector3 chassisInertia(0.0f, 0.0f, 0.0f);
	btCollisionShape* chassisShape = borderBuggy_Chasis_Split->meshEntries[0]->convexHullShape;  //new btBoxShape(btVector3(0.5f, 0.25f, 1.0f));
	btDefaultMotionState* chassisMotionState = new btDefaultMotionState(btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f), btVector3(8.0f, 7.0f, 6.0f)));
	chassisShape->calculateLocalInertia(chassisMass, chassisInertia);
	btRigidBody::btRigidBodyConstructionInfo chassisRigidBodyCI(chassisMass, chassisMotionState, chassisShape, chassisInertia);
	btRigidBody* chassisRigidBody = new btRigidBody(chassisRigidBodyCI);
	chassisRigidBody->setActivationState(DISABLE_DEACTIVATION);
	chassisRigidBody->setUserPointer((void*)100666);
	dynamicsWorld->addRigidBody(chassisRigidBody);

	btRaycastVehicle::btVehicleTuning tuning;
	btVehicleRaycaster* raycaster = new btDefaultVehicleRaycaster(dynamicsWorld);

	tuning.m_suspensionStiffness = 50;
	tuning.m_maxSuspensionTravelCm = 50;
	tuning.m_frictionSlip = 3;


	vehicle = new btRaycastVehicle(tuning, chassisRigidBody, raycaster);
	vehicle->setCoordinateSystem(0, 1, 2);
	//vehicle->getWheelInfo(
	//tuning.m_frictionSlip = 0;


	btVector3 wheelDirection(0.0f, -1.0f, 0.0f);
	btVector3 wheelAxis(-1.0f, 0.0f, 0.0f);
	btScalar suspensionRestLength(0.1f);
	btScalar wheelRadius(0.118f);
	// Be sure to attach the wheels not higher than the upper bounds of the hull of the vehicle chassis
	vehicle->addWheel(btVector3(-0.23f, 0.117f, -0.461f), wheelDirection, wheelAxis, suspensionRestLength, wheelRadius, tuning, true);
	vehicle->addWheel(btVector3( 0.23f, 0.117f, -0.461f), wheelDirection, wheelAxis, suspensionRestLength, wheelRadius, tuning, true);
	vehicle->addWheel(btVector3(-0.23f, 0.117f,   0.39f), wheelDirection, wheelAxis, suspensionRestLength, wheelRadius, tuning, false);
	vehicle->addWheel(btVector3( 0.23f, 0.117f,   0.39f), wheelDirection, wheelAxis, suspensionRestLength, wheelRadius, tuning, false);

	dynamicsWorld->addAction(vehicle);
plus read my last post here to see how to render it: viewtopic.php?f=9&t=9573&p=40971#p40971
Midnightas
Posts: 8
Joined: Fri Aug 24, 2018 9:03 pm

Re: Can't figure out how btRaycastVehicle works.

Post by Midnightas »

That's basically what I'm doing :(. No matter what numbers I put in the vehicle just stops when I apply any steering.
PcChip
Posts: 33
Joined: Sun May 20, 2018 3:09 pm

Re: Can't figure out how btRaycastVehicle works.

Post by PcChip »

I don't think anyone can help you until we see the debugdraw
Midnightas
Posts: 8
Joined: Fri Aug 24, 2018 9:03 pm

Re: Can't figure out how btRaycastVehicle works.

Post by Midnightas »

PcChip wrote: Sat Aug 24, 2019 11:59 pm I don't think anyone can help you until we see the debugdraw
Aight, I hoped it wouldn't come down to this, debug draw is a PITA when with shaders :P.
Post Reply