the symptom, in your words

"A self-crossing track breaks nearest-waypoint lap position; drive it from a monotone cursor"

✓ verified on 26.07.22
lane Writing gameplayposted

▸ SYMPTOM

You build a self-crossing race track, a figure-eight or any lap that revisits the same ground, and each car's lap position jumps by about half a lap right at the crossing. The lookup that finds a car's place on the ring runs a fresh nearest-waypoint search every frame. At the crossing that search snaps to the closest waypoint, which now sits on the other branch, an arclength away from where the car actually is.

▸ CAUSE

Two points that sit physically close on a self-crossing ring can be arclength-distant. A nearest-waypoint lookup measures straight-line distance, so at the crossing it picks the nearer point on the wrong branch and the reported lap position leaps.

The same crossing flips the "outward" direction. The two branches pass through the crossing on different headings, so a lateral-offset calculation done in a fixed world-outer-normal frame mirrors the offset's sign on one side.

▸ FIX

Three changes are required together. Any one alone leaves a hole.

  1. Drive track position from a monotone cursor that advances along the waypoint ring, not a fresh nearest-waypoint search each frame. The cursor only moves forward along the branch it is already committed to, so it cannot jump to the geometrically nearer but lap-distant crossing point.
  2. Do lateral and offset math in a left-of-travel frame, not a fixed world-outer-normal frame. The "outward" direction reverses sign at the crossing, so a frame that is relative to travel direction keeps the offset correct on both branches.
  3. Keep the branch-discrimination cone tighter than the branches' angular separation. When two lobes meet at a sharp intersection, set the angular acceptance cone to about two-thirds of the branch separation. A cone of 60 degrees worked for a 90 degree crossing. A wider cone lets a position near the crossing get claimed by the wrong branch.

Clamp each branch's distance query to its own segment extent. A branch that points away from a wedge then clamps to its own endpoint and reports a much larger distance, so the wedge is classified against the branch that genuinely bounds it.

▸ WHY IT WORKS

A crossing creates four wedges, and the failure is always the same: a position near the crossing has two candidate branches whose straight-line distances are almost equal but whose lap positions differ by half a lap. The monotone cursor removes the choice, because it never re-searches; it advances along the branch the car is already on. The left-of-travel frame removes the sign flip, because the offset is measured against the car's own heading. The tight cone and the per-branch segment clamp resolve the remaining wedges, because a branch aimed away from a wedge reports a large clamped distance and loses the claim.

The segment clamp is load-bearing, not decoration. It is worth marking as such before someone simplifies it away, because without it a branch reports an unbounded distance past its own end and the four wedges stop classifying cleanly.

Verified on engine 26.07.22: seen in a real project.
s&box moves fast; an undated fix is a liability. Spot a stale detail?
changelog
  • Published

Want to know when new guides or fixes drop? Join the community to help build this out. Report gotchas, flag outdated fixes, or just lurk.

Join the Discord