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?):
-(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:
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
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];
[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