I am making android game, using box2d for physics, I have vehicles, with wheels attached to the main vehicle body using WheelJoints. Now I am looking for a proper way to accelerate those vehicles, and also limit their speed to certain value, currently I am doing it with this way:
public void accelerate(int direction)
{
if (Math.abs(wheel1.getAngularVelocity()) < maxSpeed)
{
wheel1.applyAngularImpulse(accelerateRatio * direction);
wheel1.applyAngularImpulse(accelerateRatio * direction);
}
}
Where:
- wheel1 and wheel2 are my wheels bodies.
- int direction is a direction we want to accelerate (1 right, -1 left)
- accelerateRatio - ratio of acceleration, like 10 for example.
- maxSpeed - max speed of the vehicle, like 12 etc.
I do not think its a perfect solution, especially because it has annoying bug, while lets say accelerating right, and than accelerating left, vehicle has to firstly slow down, because there is check of max speed.