0
votes

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.

2

2 Answers

1
votes

If there is a way to check current direction the wheel is rotating, then you should check if it's maximum speed only if you are trying to speed up. As you've said, now you check for maximum speed both if you are speeding up and slowing down.

0
votes

You can adjust accelerateRatio based on current speed and the desired speed.

accelerateRatio = k * (float) Math.abs((
                getDesiredAngularVelocity() - wheel.getAngularVelocity());

That way you get larger deceleration if desired velocity is along the opposite direction, and it helps you to stop faster.