   2d car physics help
  hi there, I'm trying to get a 2d car physics simulation working but I've ran into a problem. basically I have a rigid body class that the user can apply forces to at certain positions, this rigid body has certain properties such as mass, moment of inertia (angular mass) and can be used to calculate the velocity of certain points on the rigid body. The car is a rigid body that has four wheels attached, these wheels then apply forces to the car in order to make it move.At each wheel I calculate friction forces for both the forward and side parts of the wheel based on the current velocity (direction part) of the wheel. The problem is that I don't know how I can cap the friction forces to stop the car spinning in the other direction.... when the velocity of a tyre gets below a certain value the frictional forces will cause the car to spin in the opposite direction meaning that in the next frame the velocity will be in the opposite direction which means the frictional force gets applied in that direction, then in the next frame it will have switched again and so on. this turns into an infinite loop where at small velocities the car starts to jitter and slowly rotate about its center.I've tried to calculate the amount of force required to reduce the velocity to 0 but it still doesn't work. i.e.F = maa = (vf - vi)/timestepa = (0 - vi)/timestepa = -vi/timestepm = carMass/4The code for the wheel looks a bit like this:void Wheel::Update(float timeStep){// get the world position of the wheelVector2 worldPos = car->RelativeToWorld( this->position );// velocity (in world space)velocity = car->GetVelocity( worldPos );// need to rotate velocity into car spacevelocity = car->RotateWorldToRelative( velocity );// calculate the traction force the tyre applies// to the car// torque = force * radius, therefore: force = torque / radiusfloat tractionForce = netTorque / radius;// friction coefficientfloat forwardCoefficient = rollingCoefficient;if(braking){forwardCoefficient = brakingCoefficient;}// calculate the friction force not considering the shape// of the tyre or the coefficients (let friction coefficient = 1)// F = uN = 1N = N = mg = weight of tyreVector2 frictionForce = -velocity.Normalized() * (car->GetMass() / 4.0);// project the friction force onto the side and forward directionssideFrictionForce = sideDir.Project(frictionForce) * sideCoefficient;forwardFrictionForce = forwardDir.Project(frictionForce) * forwardCoefficient;// calculate the amount of friction force required to reduce the velocity to 0// in the next frame// F = ma// a = (vf - vi)/t// a = (0 - vi)/t// a = -vi/tVector2 sideProjectedVel = sideDir.Project(velocity);Vector2 forwardProjectedVel = forwardDir.Project(velocity);Vector2 sideCapForce = (-sideProjectedVel/timeStep) * ((car->GetMass() / 4.0) * 9.8);Vector2 forwardCapForce = (-forwardProjectedVel/timeStep) * (car->GetMass() / 4.0);if(sideFrictionForce.Magnitude() >= sideCapForce.Magnitude()){sideFrictionForce = sideCapForce;}if(forwardFrictionForce.Magnitude() >= forwardCapForce.Magnitude()){forwardFrictionForce = forwardCapForce;}// add all forces (net force) to the carcar->ApplyForce(car->RotateRelativeToWorld(sideFrictionForce + forwardFrictionForce), worldPos);car->ApplyForce(car->RotateRelativeToWorld( forwardDir * tractionForce ), worldPos);netTorque = 0.0;}I'm quite tempted to just project the velocity onto the sideDir and forwardDir vector and multiply them by some negative coefficients to give me the frictional forces as this solves most of the problems, the reason I haven't done this is because (I think?) frictional forces should be independant of velocity.   
  Car physics is pretty easy when you keep track of the angular as well as the linear velocity of the objects.  So the side friction force can just be proportional to the angular velocity.In my physics engine I found the following calculations work well for turning a vehicle on the ground.Vector3D vel = crossProd(body->localAngVel, wheelPos) + body->localVelocity;float speed = vel.magnitude();if ( speed > 0.0001f ){    vel *= Vector3D( 1.0f, 100.0f, 1.0f ); //y is side force, x is forward,  so sideways drag is 100 greater then forard    Vector3D dragForce = ( -vel ) * 12.0f;    body->addForce( dragForce );    body->addTorque( dragForce*20.0f, wheelPos );}So the drag is only calculated when the local velocity of the wheel is above a certain threshold.  I got the hard-coded numbers through trial and error, but of course you will want to make them variables.All coordinates are in local body space, not in world space.   ;  It's pretty typical to take the magic number penalty constraint method. It works fairly well but a more sophisticated force curve may give better (or maybe worse results).No matter what though, if you're integrating forces you're going to lend yourself to drift. Park the car on a hill for instance (may not be an issue in a 2d sim). The car will just slide down the hill. You can predict the acceleration or add world springs to combat this problem pretty efficiently but it's still going to be there.My new vehicle model doesnt use forces at all. It's purely kinematic. You can disable the dynamic sim and the car will drive around like it's on rails. Then you can enable dynamics anywhere you want from zero to one so that the visual appeal of the dynamics does not influence the actual behavior of the car.   