Friction not working for Vehicle in BulletPhysics - [SOLVED]

Post Reply
User avatar
manmohan29
Posts: 15
Joined: Wed Aug 20, 2014 9:26 pm
Location: Chandigarh, India

Friction not working for Vehicle in BulletPhysics - [SOLVED]

Post by manmohan29 »

I am creating a vehicle using bullet-physics engine (v 2.82).

I created a **ground ( btBoxShape )**, **a box** and **a vehicle** (following the demo). But friction between ground and vehicle wheels seems not working.

As soon as the vehicle is placed in 3d world, it starts moving forward.
START :
v.jpg
v.jpg (165.84 KiB) Viewed 4997 times
Steering works for the vehicle, but engineForce and brakingForce does not work (i.e. I cannot speed-up or stop the vehicle) :
3.png
3.png (35.16 KiB) Viewed 4997 times
I create physics world like this :

Code: Select all

void initPhysics()
{
    broadphase = new btDbvtBroadphase();
    collisionConfiguration = new btDefaultCollisionConfiguration();
    dispatcher = new btCollisionDispatcher(collisionConfiguration);
    solver = new btSequentialImpulseConstraintSolver();

    dynamicsWorld = new btDiscreteDynamicsWorld(dispatcher, broadphase, solver, collisionConfiguration);
    dynamicsWorld->setGravity(btVector3(0, -9.81, 0));
    
    // Debug Drawer
    bulletDebugugger.setDebugMode(btIDebugDraw::DBG_DrawWireframe);
    dynamicsWorld->setDebugDrawer(&bulletDebugugger);

    //groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 1);
    groundShape = new btBoxShape(btVector3(50, 3, 50));
    fallShape = new btBoxShape(btVector3(1, 1, 1));

    // Orientation and Position of Ground
    groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, -3, 0)));
    btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));
    groundRigidBody = new btRigidBody(groundRigidBodyCI);
    dynamicsWorld->addRigidBody(groundRigidBody);

    ///////////////////////////////////////////////////////////////////////
    //              Vehicle Setup
    ///////////////////////////////////////////////////////////////////////
    vehicleChassisShape = new btBoxShape(btVector3(1.f, 0.5f, 2.f));
    vehicleBody = new btCompoundShape();

    localTrans.setIdentity();
    localTrans.setOrigin(btVector3(0, 1, 0));
    vehicleBody->addChildShape(localTrans, vehicleChassisShape);
    
    localTrans.setOrigin(btVector3(3, 0.f, 0));
    vehicleMotionState = new btDefaultMotionState(localTrans);
    btVector3 vehicleInertia(0, 0, 0);
    vehicleBody->calculateLocalInertia(vehicleMass, vehicleInertia);
    btRigidBody::btRigidBodyConstructionInfo vehicleRigidBodyCI(vehicleMass, vehicleMotionState, vehicleBody, vehicleInertia);

    vehicleRigidBody = new btRigidBody(vehicleRigidBodyCI);
    dynamicsWorld->addRigidBody(vehicleRigidBody);

    gVehicleSteering = 0;
    vehicleRigidBody->setCenterOfMassTransform(btTransform::getIdentity());
    vehicleRigidBody->setLinearVelocity(btVector3(0, 0, 0));
    vehicleRigidBody->setAngularVelocity(btVector3(0, 0, 0));
    dynamicsWorld->getBroadphase()->getOverlappingPairCache()->cleanProxyFromPairs(vehicleRigidBody->getBroadphaseHandle(), dynamicsWorld->getDispatcher());

    wheelShape = new btCylinderShapeX(btVector3(wheelWidth, wheelRadius, wheelRadius));
    {
        vehicleRayCaster = new btDefaultVehicleRaycaster(dynamicsWorld);
        vehicle = new btRaycastVehicle(vehicleTuning, vehicleRigidBody, vehicleRayCaster);

        // never deactivate vehicle
        vehicleRigidBody->setActivationState(DISABLE_DEACTIVATION);
        dynamicsWorld->addVehicle(vehicle);

        float connectionHeight = 1.2f;
        bool isFrontWheel = true;

        vehicle->setCoordinateSystem(rightIndex, upIndex, forwardIndex); // 0, 1, 2

        // add wheels
        // front left
        btVector3 connectionPointCS0(CUBE_HALF_EXTENT-(0.3*wheelWidth), connectionHeight, 2*CUBE_HALF_EXTENT-wheelRadius);
        vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, vehicleTuning, isFrontWheel);
        // front right
        connectionPointCS0 = btVector3(-CUBE_HALF_EXTENT+(0.3*wheelWidth), connectionHeight, 2*CUBE_HALF_EXTENT-wheelRadius);
        vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, vehicleTuning, isFrontWheel);
        isFrontWheel = false;
        // rear right
        connectionPointCS0 = btVector3(-CUBE_HALF_EXTENT+(0.3*wheelWidth), connectionHeight, -2*CUBE_HALF_EXTENT+wheelRadius);
        vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, vehicleTuning, isFrontWheel);
        // rear left
        connectionPointCS0 = btVector3(CUBE_HALF_EXTENT-(0.3*wheelWidth), connectionHeight, -2*CUBE_HALF_EXTENT+wheelRadius);
        vehicle->addWheel(connectionPointCS0, wheelDirectionCS0, wheelAxleCS, suspensionRestLength, wheelRadius, vehicleTuning, isFrontWheel);
        
        for (int i = 0; i < vehicle->getNumWheels(); i++)
        {
            btWheelInfo& wheel = vehicle->getWheelInfo(i);
            wheel.m_suspensionStiffness = suspensionStiffness;
            wheel.m_wheelsDampingRelaxation = suspensionDamping;
            wheel.m_wheelsDampingCompression = suspensionCompression;
            wheel.m_frictionSlip = wheelFriction;
            wheel.m_rollInfluence = rollInfluence;
        }


    }

    ///////////////////////////////////////////////////////////////////////

    // Orientation and Position of Falling body
    fallMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(-1, 5, 0)));
    btScalar mass = 1;
    btVector3 fallInertia(0, 0, 0);
    fallShape->calculateLocalInertia(mass, fallInertia);
    btRigidBody::btRigidBodyConstructionInfo fallRigidBodyCI(mass, fallMotionState, fallShape, fallInertia);
    fallRigidBody = new btRigidBody(fallRigidBodyCI);
    dynamicsWorld->addRigidBody(fallRigidBody);

}
In my stepPhysics function I step like this :

Code: Select all

// does not work
    vehicle->applyEngineForce(maxEngineForce, WHEEL_REARLEFT);
    vehicle->applyEngineForce(maxEngineForce, WHEEL_REARRIGHT);
    // these also do not work
    vehicle->setBrake(gBreakingForce, WHEEL_REARLEFT);
    vehicle->setBrake(gBreakingForce, WHEEL_REARRIGHT);
    // this works
    vehicle->setSteeringValue(gVehicleSteering, WHEEL_FRONTLEFT);
    vehicle->setSteeringValue(gVehicleSteering, WHEEL_FRONTRIGHT);
    
    dynamicsWorld->stepSimulation(1 / 60.0f, 10);
However If I apply brakingForce to all 4 wheels (i.e. including WHEEL_FRONTLEFT and WHEEL_FRONTRIGHT), then my vehicle stops, but keeps sliding/moving forward very very slowly.

Where am I doing it wrong ?

IDE : Visual Studio 2010 SP1
BulletPhysics version : 2.82
OS : Windows 7 64bit
Last edited by manmohan29 on Tue Aug 26, 2014 10:38 am, edited 1 time in total.
User avatar
manmohan29
Posts: 15
Joined: Wed Aug 20, 2014 9:26 pm
Location: Chandigarh, India

Re: Friction not working for Vehicle in BulletPhysics

Post by manmohan29 »

In VehicleDemo I see something called btHeightfieldTerrainShape is being used for groundShape.
Is that necessary ?

In my application I am using just 3 bodies : ground (btBoxShape), box and a vehicle.
User avatar
manmohan29
Posts: 15
Joined: Wed Aug 20, 2014 9:26 pm
Location: Chandigarh, India

Re: Friction not working for Vehicle in BulletPhysics

Post by manmohan29 »

Ground has shape of btBoxShape and vehicle starts moving as soon as it is placed on the ground.
Applying brake/acceleration does not work at all. However i can steer the vehicle.

When I hit the box with my vehicle then it slides for some distance then stops. That means friction is working propely between ground and box.
Then what is causing such a behavior of my vehicle ?

Any ideas what is wrong with my code ?
User avatar
manmohan29
Posts: 15
Joined: Wed Aug 20, 2014 9:26 pm
Location: Chandigarh, India

Re: Friction not working for Vehicle in BulletPhysics - [SOL

Post by manmohan29 »

I found what was wrong in my code. I was applying maxEngineForce to my wheels in my stepPhysics function.

Code: Select all

vehicle->applyEngineForce(maxEngineForce, WHEEL_REARLEFT);
vehicle->applyEngineForce(maxEngineForce, WHEEL_REARRIGHT);
changing above lines to these :

Code: Select all

vehicle->applyEngineForce(gEngineForce, WHEEL_REARLEFT);
vehicle->applyEngineForce(gEngineForce, WHEEL_REARRIGHT);
fixed everything.
Once again silly mistake kept me stuck for 2 days.
Post Reply