[SOLVED] Using (setting up) btRaycastVehicle - help needed

User avatar
ainurakne
Posts: 4
Joined: Sat Apr 30, 2011 3:48 pm
Location: Estonia

[SOLVED] Using (setting up) btRaycastVehicle - help needed

Post by ainurakne »

Hello everyone.
I have recently started to acquaint myself with Bullet Physics, but I have stumbled upon a problem that I haven't managed to solve yet - I can not get the chassis of a vehicle to move, not even to fall down. (Though I can get the chassis to fall down if I add it into the world as a rigid body)

This is what I have bunched together from various tutorials and from the vehicle demo so far:

Code: Select all

// The world itself
btBroadphaseInterface* broadphase = new btDbvtBroadphase();
btDefaultCollisionConfiguration* collisionConfiguration = new btDefaultCollisionConfiguration();
btCollisionDispatcher* dispatcher = new btCollisionDispatcher(collisionConfiguration);
btSequentialImpulseConstraintSolver* solver = new btSequentialImpulseConstraintSolver;

btDiscreteDynamicsWorld* dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
dynamicsWorld->setGravity(btVector3(0.0f, -9.81f, 0.0f));


// The vehicle
btScalar chassisMass(1.0f);
btVector3 chassisInertia(0.0f, 0.0f, 0.0f);
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.0f, 0.5f, 2.0f));
btDefaultMotionState* chassisMotionState = new btDefaultMotionState(btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f), btVector3(0.0f, 5.0f, 0.0f)));
chassisShape->calculateLocalInertia(chassisMass, chassisInertia);
btRigidBody::btRigidBodyConstructionInfo chassisRigidBodyCI(chassisMass, chassisMotionState, chassisShape, chassisInertia);
btRigidBody* chassisRigidBody = new btRigidBody(chassisRigidBodyCI);
chassisRigidBody->setActivationState(DISABLE_DEACTIVATION);

btRaycastVehicle::btVehicleTuning tuning;
btVehicleRaycaster* raycaster = new btDefaultVehicleRaycaster(dynamicsWorld);
btRaycastVehicle* vehicle = new btRaycastVehicle(tuning, chassisRigidBody, raycaster);
vehicle->setCoordinateSystem(0, 1, 2);

btVector3 wheelDirection(0.0f, -1.0f, 0.0f);
btVector3 wheelAxis(-1.0f, 0.0f, 0.0f);
btScalar suspensionRestLength(0.6f);
btScalar wheelRadius(0.5f);
vehicle->addWheel(btVector3(-0.5f, 1.2f, 1.0f), wheelDirection, wheelAxis, suspensionRestLength, wheelRadius, tuning, true);
vehicle->addWheel(btVector3(0.5f, 1.2f, 1.0f), wheelDirection, wheelAxis, suspensionRestLength, wheelRadius, tuning, true);
vehicle->addWheel(btVector3(-0.5f, 1.2f, -1.0f), wheelDirection, wheelAxis, suspensionRestLength, wheelRadius, tuning, false);
vehicle->addWheel(btVector3(0.5f, 1.2f, -1.0f), wheelDirection, wheelAxis, suspensionRestLength, wheelRadius, tuning, false);

dynamicsWorld->addAction(vehicle);


// This is done every frame to update physics
dynamicsWorld->stepSimulation(1.0f / 60.0f, 10, 1.0f / 60.0f);


// And that's how I get the transformation of the vehicle chassis
GLfloat matrix[16];
vehicle->getChassisWorldTransform().getOpenGLMatrix(&matrix[0]);
Obviously there must be something important that I have missed, but I can not figure out what it is.

Any help would be greatly appreciated.


EDIT: If I do both: add the vehicle into the world as an action and add the chassis of the vehicle into the world as a rigid body - then the graphical representation of my vehicles descends slowly until it reaches the ground plane. Just as the rigid body physics tries to pull it down, but the vehicle physics tries to hold it in one place.

(BTW: I am using Bullet 2.78, compiled on MinGW-w64, if it matters)
Last edited by ainurakne on Mon May 09, 2011 9:47 pm, edited 1 time in total.
User avatar
ainurakne
Posts: 4
Joined: Sat Apr 30, 2011 3:48 pm
Location: Estonia

Re: Using btRaycastVehicle - help needed

Post by ainurakne »

Anyone?
Any ideas?

Thanks,
ainurakne
User avatar
ainurakne
Posts: 4
Joined: Sat Apr 30, 2011 3:48 pm
Location: Estonia

Setting up btRaycastVehicle - problem solved

Post by ainurakne »

I dissected the vehicle demo and finally found the cause of my problem - well, actually two problems:

1. I didn't add my vehicle's chassis into the dynamics world as a rigid body (although I tried it couple of times, but since it made no difference, I discarded it before). It turns out that one must do both - add its btRaycastVehicle into the dynamics world via addAction() or addVehicle() and add the chassis of the vehicle via addRigidBody().

2. I had attached the wheels of my vehicle too high. It turns out that if one connects the wheels above the upper bounds of the rigid body that represents the chassis of the vehicle, so that the rays cast from the connection points hit the chassis of the vehicle - the car begins to float. It may descend slowly, hang in one place in the air or even ascend into the sky, depending on the spring parameters and the distance between the hull of the vehicle and the connection points of the wheels. That's because when the rays hit the chassis, then the wheels think it's ground and try to lift the vehicle higher.



So here's the fixed vehicle creation part of the code for everyone who may encouter similar problems:

Code: Select all

// The vehicle
btScalar chassisMass(1.0f);
btVector3 chassisInertia(0.0f, 0.0f, 0.0f);
btCollisionShape* chassisShape = new btBoxShape(btVector3(1.0f, 0.5f, 2.0f));
btDefaultMotionState* chassisMotionState = new btDefaultMotionState(btTransform(btQuaternion(0.0f, 0.0f, 0.0f, 1.0f), btVector3(0.0f, 5.0f, 0.0f)));
chassisShape->calculateLocalInertia(chassisMass, chassisInertia);
btRigidBody::btRigidBodyConstructionInfo chassisRigidBodyCI(chassisMass, chassisMotionState, chassisShape, chassisInertia);
btRigidBody* chassisRigidBody = new btRigidBody(chassisRigidBodyCI);
chassisRigidBody->setActivationState(DISABLE_DEACTIVATION);
// Be sure to add the chassis of the vehicle into the world as a rigid body
dynamicsWorld->addRigidBody(chassisRigidBody);

btRaycastVehicle::btVehicleTuning tuning;
btVehicleRaycaster* raycaster = new btDefaultVehicleRaycaster(dynamicsWorld);
btRaycastVehicle* vehicle = new btRaycastVehicle(tuning, chassisRigidBody, raycaster);
vehicle->setCoordinateSystem(0, 1, 2);

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

dynamicsWorld->addAction(vehicle);


// This is done every frame to update physics
dynamicsWorld->stepSimulation(1.0f / 60.0f, 10, 1.0f / 60.0f);


// And that's how I get the transformation of the vehicle chassis
GLfloat matrix[16];
vehicle->getChassisWorldTransform().getOpenGLMatrix(&matrix[0]);
paranoidray
Posts: 1
Joined: Fri Dec 13, 2013 2:49 pm

Re: [SOLVED] Using (setting up) btRaycastVehicle - help need

Post by paranoidray »

Thanks, I was trying to use a vehicle without a compound shape (to understand the basics) and my problem also was the incorrectly positioned wheels.
This post has helped me tremendously !

Thanks again for taking the time to post the detailed explanation Ainurakne.