Vehicle tips

proof

15-02-2011 00:44:39

After a long day of trying to figure out on how to make my car stable, I finally figured out what was the problem.
The car just tends to roll over on any turn, which is seriously annoying. I figured out that by lowering its COM (Center of Mass), it stays stable. So when you create the body, you can do:

body->setCMassOffsetLocalPosition(0, 0, 0);

The point (0, 0, 0) works for me the best, but that's probably because the chassis is a little up from the ground, so if this doesn't work for you, try lowering the Y component to -0.5f. Note that if you go too far down, it starts rolling over in the opposite side.
With this COM and maximum steering angle of 30 degrees, back torque of 1000 and a mass of 1350 the vehicle acts fine with default settings for mLateralTireFunction and mLongitudinalTireFunction. I was playing around with those too and found out how to make the car drift a little:

mLateralTireFunction.mAsymptoteSlip = 0.04f;
mLateralTireFunction.mAsymptoteValue = 0.6f;
mLateralTireFunction.mExtremumSlip = 0.01f;
mLateralTireFunction.mExtremumValue = 1f;
mLateralTireFunction.mStiffnessFactor = 4000f;

mLongitudalTireFunction.mAsymptoteSlip = 0.04f;
mLongitudalTireFunction.mAsymptoteValue = 0.6f;
mLongitudalTireFunction.mExtremumSlip = 0.01f;
mLongitudalTireFunction.mExtremumValue = 1f;
mLongitudalTireFunction.mStiffnessFactor = 6000f;


I haven't managed to make suspensions work fine. If I increase mSuspension.mSpring, it either does nothing visible, or starts making my car float around and bounce a lot :(

When I finish gears, I will upload some classes I made for loading a car from a configuration file that looks something like:

CHA1 2.1 0.8 5.0 -0.005 0.653 0.005
CHA2 1.6 0.5 2.3 0.009 1.160 -0.473
MASS 1350
WHEEL_RADIUS 0.39
F_TORQUE 0
B_TORQUE 1000
MAX_STEER 30
FL_WHEEL 0.889 0.358 1.620
FR_WHEEL -0.889 0.358 1.620
BL_WHEEL 0.889 0.358 -1.440
BR_WHEEL -0.889 0.358 -1.440
LATTF 0.04 0.6 0.01 1 4000
LOGTF 0.04 0.6 0.01 1 6000
SUSPENSION 20 300 0 1


Recompiling takes time :(

And, here's how my car looks:

betajaen

15-02-2011 08:09:31

Nice.

Lowering the COM is typically the solution I have found in PhysX. It is likely to apply to other physics engine, as in reality the same is true. Although I'm pretty sure that forward/backward COM is taken into account to help with over or under steer.

I'd like to see those classes though. ;)

proof

15-02-2011 13:28:59

Well I don't really know the principle behind gear shifting and RPM, so I'm going to look through the physx sample code and hijack some of it :mrgreen:

betajaen

15-02-2011 13:37:58

That would be my first action as well, the vehicle code sample is pretty good, (and don't tell anyone I said this; so is the Havok vehicle demo too). ;)

proof

15-02-2011 22:35:10

There.

Still needs some editing though, but there are some things that I really have no idea how to do yet :D

Things to be done:
- I have no idea on how to limit the car speed. Currently, if you try to speed up, you can get to the max gear and cap on the max rpm, but you will still speed up because of the torque. It's much more notable if you're going in reverse
- Discover which wheels are touching the ground. If the car is totally in the air (or even partially), disable gear shifting (also forgot to make a gears->setLocked(bool) method)
- Figure out more values for the car lateral and longitudinal tire functions

I've uploaded only the classes and a test model and its config file. I was just bored enough to setup another project, as it is boring.
Usage:

Vehicle* camaro = Vehicle::createVehicle(VehId_Camaro, critter);

//there's an enum in Vehicle.h
//enum VehicleIds
//{
// VehId_Camaro = 1
//};


Maybe it was a bad decision to map cars like that. std::string Vehicle::getVehicleName(VehicleIds vehicleId); in Vehicle.cpp is being responsible for recognizing the Id and returning its name for file loading. Carrying on:

// input example
if (input->isKeyDown(OIS::KC_W))
camaro->accelerate();

if (input->isKeyDown(OIS::KC_S))
camaro->deaccelerate();

if (input->isKeyDown(OIS::KC_A))
camaro->steerLeft();

if (input->isKeyDown(OIS::KC_D))
camaro->steerRight();

// call the update before physics execution
camaro->update(time);

// advance physics
pWorld->advance(time);

// updates the car wheel nodes
camaro->postPhysics(time);


Note that car->update(float diff) is a virtual method, allowing you to extend the vehicle class in order to make AI cars (that's the first thing that ran through my head).

The system will look for 2 meshes: carname.mesh and carname_wheel.mesh . It will also look for a .car configuration file, and I'm sorry that the path is already hardcoded ^^ (to change it, Vehicle.cpp:63).

Possible .car configuration values:

Wheel positions:
FL_WHEEL x y z
FR_WHEEL x y z
BL_WHEEL x y z
BR_WHEEL x y z


Wheel properties: (note: wheel_width behaves unexpectedly)
WHEEL_RADIUS radius
WHEEL_WIDTH width
IWM inverseWheelMass


Vehicle properties:
MASS mass
COFMASS centerOfMass
F_TORQUE boolFrontWheelTorque
B_TORQUE boolBackWheelTorque
TORQUE_CURVE rpm torque
MAX_STEER maxWheelSteerAngle
MAX_VELOCITY unused...yet
SUSPENSION damper spring targetValue travel


Chassis (constructs two boxes for the chassis):
CHA1 width height depth x y z
CHA2 width height depth x y z


Tire functions (LATTF = Lateral Tire Function, LOGTF = Longitudinal Tire Function):
LATTF asymptoteSlip asymptoteValue extremumSlip extremumValue stiffnessFactor
LOGTF asymptoteSlip asymptoteValue extremumSlip extremumValue stiffnessFactor
LATTF_F, LATTF_B, LOGTF_F, LOGTF_B
Suffixes _F and _B stand for front wheels and back wheels


Gear ratios:
GEARS numberOfGears g1 g2 g3 ... gn reverseGear
RPM minRpmToGearDown maxRpmToGearUp minRpm maxRpm


:shock: :mrgreen:

pizzazhang

16-02-2011 10:11:36

Your car is pretty good. I just implemented my basic vehicle functions (move front, move back, turn left and turn right) just using simple code and my vehicle has no effects like accelerate or drift. I also didn't considerate the mass condition, I just froze the X and Y rotation. What you did is very helpful to me. thank you for sharing your ideas!

deshan

05-03-2011 15:07:27

Your car is great. Thanks for sharing.

scott

11-06-2011 08:34:23

hi... i am a beginner
and i want to know that is there any way to run the code given above.... because i want to know how things work for vehicle
i think the link given to download the code is broken.... but i had the copy of it so hear is another link
http://cid-8c456e026f391c84.office.live ... px/.Public

Crhonos

20-08-2011 14:46:19

There is a cpp file with "main" missing.
I tried to make a project using the files you povided, but there is no cpp with main, so, no project.

dudeabot

02-10-2011 17:02:19

since this topic is very old , I started a new thread viewtopic.php?f=6&t=14858&p=82856#p82856 with my findings

there you can find a working visual studio 2008 solution as well!