Top down car - killing sideways velocity - attempt 1

I am using Box2D with Cocos2D for iPhone.

I have read not to set lateral velocity directly (is this true), but this is the only method I can find to remove the sideways movement of the wheels.

I decided to try an approach which is to approximate the force necessary to remove the necessary velocity vector. i.e. I calculate the correct linear velocity along the line of the wheel rotation, and then work out what the velocity vector is from the linear velocity and this corrected velocity.

I then assume that I need to remove this "difference velocity vector" over one timestep (approximately), so turn it into a deceleration problem. I know the mass, I know the velocity vector (the difference vector from real velocity to required velocity), and so use f=ma to apply a force (an impulse doesn't seem to work?).

It seems to work, although I feel I am not using dt correctly, and am not sure if this will affect collisions etc.

I thought I would post it for some comments...I ain't no physics head, so am sure this cant be right!

I would add, I seem to get a better efffect if I affect the wheels and the car body (all samples I have seen just correct the velocity of the wheels?):


Code:

-(void) applyOrthoganalForceForTarget:(b2Body *)targetBody dt:(ccTime) dt {
   
   b2Vec2 localPoint;
   
   localPoint.Set(0,0);
   
   if (dt < b2_epsilon || dt == 0.0f) return;
   
   
   // Get the linear velocity of the the centre of the body
   b2Vec2 bodyVelocity = targetBody->GetLinearVelocityFromLocalPoint(localPoint);
   

      
   // Get the roation vector
   
   b2Vec2 bodyRotation = targetBody->GetTransform().R.col2;
      
   // get a corrected velocity vector by projecting the linear velocity onto the
   // rotation normal vector
   bodyRotation *= b2Dot(bodyVelocity,bodyRotation);
   b2Vec2 correctedVelocity = bodyRotation;
   
   
   // work out the difference vector from the original lateral velocity  to the new corrected velocity in
   // the direction of the wheel rotation
   b2Vec2 velocityDifference;
   
   velocityDifference.x = correctedVelocity.x - bodyVelocity.x;
   velocityDifference.y = correctedVelocity.y - bodyVelocity.y;
   
   // velocityDifference is effectively the velocity we need to kill by applying a force
   
   // To work out the force needed, we can assume that we want to take the
   // velocityDifference vector back to zero.
   
   // We will assume that all time steps dt are similar so we are using the change of the velocityDifference vector
   // back to zero over the time dt...i.e. a deceleration
   
   // we can use f=ma to then work out the force needed
   
   b2Vec2 oppositeForce;
   oppositeForce.x = (targetBody->GetMass() * (velocityDifference.x))/dt;
   
   oppositeForce.y = (targetBody->GetMass() * (velocityDifference.y))/dt;
   
   // apply the necessary opposing force to remove the velocityDifference
   
   if (oppositeForce.x != NAN  && oppositeForce.y != NAN) {
      targetBody->ApplyForce(oppositeForce, targetBody->GetPosition());
   }
   
}



I am calling it with the wheels AND the body:

Code:
   [self applyOrthoganalForceForTarget:leftWheel dt:dt];
   [self applyOrthoganalForceForTarget:rightWheel dt:dt];
   [self applyOrthoganalForceForTarget:leftRearWheel dt:dt];
   [self applyOrthoganalForceForTarget:rightRearWheel dt:dt];
   [self applyOrthoganalForceForTarget:body dt:dt];



I am guessing to build in some kind of skidding I can scale the oppositeForce.x and oppositeForce.y values down a bit.

Regards

i
Not sure if its too late, but I think what you want to be using here is the b2TensorDampingController from the contributions. By setting the right values in the Tensor matrix you can get a pretty good simulation of wheels from top down.
Hi,

When I looked into the code for this, it just seemed to correct linear velocity as all the other solutions seem to. I was trying to use forces rather than setting linear velocity directly. I believe setting linear velocity directly on dynamic bodies is not advised. Hence my solution simply works out which force vector to apply to reduce the sideways acceleration down to zero over one timestep.


This is the controller you mention, which ultimately seems to just adjust LV??

Code:


void b2TensorDampingController::Step(const b2TimeStep& step)
{
   float32 timestep = step.dt;
   if(timestep<=B2_FLT_EPSILON)
      return;
   if(timestep>maxTimestep && maxTimestep>0)
      timestep = maxTimestep;
   for(b2ControllerEdge *i=m_bodyList;i;i=i->nextBody){
      b2Body* body = i->body;
      if(body->IsSleeping())
         continue;
      b2Vec2 damping = body->GetWorldVector(
                     b2Mul(T,
                        body->GetLocalVector(
                           body->GetLinearVelocity()
                        )
                     )
                  );
      body->SetLinearVelocity(body->GetLinearVelocity() + timestep * damping);
   }
}

Original thread: https://box2d.org/forum/viewtopic.php?t=5362