I'm working on a 2D, top-down driving game with some basic AI vehicles. The AI Vehicles move along a bidirected graph of positions. Each vehicle has a current position (from the graph) and a target position, but instead of linearly interpolating directly between the positions, the vehicles each have a maximum steering angle, and so must steer their way to the next point.

The vehicle is defined geometrically as follows:

enter image description here

  • The vehicle itself is a rectangle, comprising a center point (O), a width and a height.
  • The vehicle has both a front (F) and rear (R) 'axis' from which the wheels are extruded.
  • The vehicle is steered from the front axis, by an angle defined as (a). The vehicle's
  • The current heading is (A).
  • The current steering heading is (B).
  • The desired target is (T).

The vehicle's heading is only changed when both the steering angle is set and there is some forward motion (i.e. the vehicle cannot turn when standing still).

As long as the target T is sufficiently far enough away, over time the vehicles can be correctly steered towards the targets.

The problem is, when the target is defined too close to the front axis, the vehicle can never reach the target, as the turning arc is too large (they drive around the target indefinitely).

I think the solution is to first detect if the target is 'too close', and if so, the vehicle needs to reverse a little first, and then begin driving forwards again. I think this would be achivable by defining a circle adjacent to the vehicle to detect if the target point can be reached based on the maximum turning angle and current position - but I don't know how to create such a circle. Something like:

enter image description here

How can I create a circle (the origin and radius) from the current vehicle position and max turn angle? Is there a different, potentially better solution?

Thanks a lot

edit The following is how I update the position of the vehicle. In a nutshell, the positions of the front and rear wheels are first calculated, and their positions are independently updated. Then the new position of the car is worked out from there. I cut a little for brevity, but it should be enough to see how it works:

private void updateVehicle(BaseVehicle pCar) {

    final float lAccelerationAmt = 30;
    final float lWheelBase = 32;

    pCar.speed += lAccelerationAmt ;

    // Extrude front and rear axels
    pCar.frontWheels.x = pCar.x + lWheelBase / 2 * cos(pCar.rotation);
    pCar.frontWheels.y = pCar.y + lWheelBase / 2 * sin(pCar.rotation);

    pCar.rearWheels.x = pCar.x - lWheelBase / 2 * cos(pCar.rotation);
    pCar.rearWheels.y = pCar.y - lWheelBase / 2 * sin(pCar.rotation);

    // Move the vehicle
    pCar.rearWheels.x += pCar.speed * delta * cos(pCar.rotation);
    pCar.rearWheels.y += pCar.speed * delta * sin(pCar.rotation);

    pCar.frontWheels.x += pCar.speed * delta * cos(pCar.rotation + pCar.steerAngle);
    pCar.frontWheels.y += pCar.speed * delta * sin(pCar.rotation + pCar.steerAngle);

    // Extrapolate the new car location
    pCar.x = (pCar.frontWheels.x + pCar.rearWheels.x) / 2;
    pCar.y = (pCar.frontWheels.y + pCar.rearWheels.y) / 2;

    // update the vehicle heading
    pCar.rotation = atan2(pCar.frontWheels.y - pCar.rearWheels.y, pCar.frontWheels.x - pCar.rearWheels.x);

  • \$\begingroup\$ turn angle doesn't really make sense for me. A vehicle as a turn radius. How do you define your turn angle? How far does your vehicle have to move to achieve this angle? With these values you should be able to get the radius. \$\endgroup\$ – Linaith Aug 17 '18 at 12:24
  • \$\begingroup\$ maybe that's what is confusing me. The turn angle I have defined is literally the angle offset from the current heading. E.g. vehicle is heading at 0°, the turn angle is -/+25 degrees from that. I'm going to google about turn radius' instead, maybe that makes more sense .. \$\endgroup\$ – LintfordPickle Aug 17 '18 at 12:47
  • \$\begingroup\$ How did you define your turn angle? can you turn while you are not moving? how far do you have to move that you achieve this angle? \$\endgroup\$ – Linaith Aug 17 '18 at 12:50
  • \$\begingroup\$ the angle was chosen arbitrarily. The vehicle does have to move forward in order for the vehicle heading to be updated. The angle is achieved in the next frame (i.e. instantly) \$\endgroup\$ – LintfordPickle Aug 17 '18 at 13:33
  • \$\begingroup\$ You might find this earlier Q&A on path planning with limited turning radii useful too. \$\endgroup\$ – DMGregory Aug 17 '18 at 15:05

Based on my comment here an Answer:

you have to move to turn your vehicle. Otherwise you would not have the Problem.
If you know the distance, you can calculate the radius:

enter image description here

more formulas: circular sector - Wikipedia

the origin is a little bit more complicated. You have to know around which point your vehicle turns. From this point you have a line (vector) with the length r and in an 90 angle to your car.


Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.