Skip to content

Fix neighbour_vehicles not detecting vehicles on connected lanes#667

Open
Lidang-Jiang wants to merge 1 commit intoFarama-Foundation:masterfrom
Lidang-Jiang:fix/neighbour-vehicles-connected-lanes
Open

Fix neighbour_vehicles not detecting vehicles on connected lanes#667
Lidang-Jiang wants to merge 1 commit intoFarama-Foundation:masterfrom
Lidang-Jiang:fix/neighbour-vehicles-connected-lanes

Conversation

@Lidang-Jiang
Copy link
Copy Markdown
Contributor

Summary

Road.neighbour_vehicles() only searched the current lane segment for front/rear neighbours. Vehicles that had crossed into a connected next or previous lane segment were invisible, causing incorrect behaviour in multi-segment environments like Racetrack, Roundabout, and Intersection.

This PR extends the search to connected lanes:

  • Downstream (next segments): found via graph[_to] using the current lane's endpoint as the new start node, with longitudinal offset += current_lane.length
  • Upstream (previous segments): found by scanning for graph edges ending at the current lane's start node, with longitudinal offset -= prev_lane.length

All 61 existing tests pass unchanged. 14 new tests added covering same-segment regression, connected-lane detection, curve segments, multi-lane filtering, and edge cases.

Fixes #626

Before (bug reproduction)
BUG CONFIRMED: Front vehicle on connected lane b->c was NOT detected!

Road layout: a->b (50m) connected to b->c (50m)
Ego position: lane a->b, longitudinal=48 (near end)
Front vehicle: lane b->c, longitudinal=5 (on next segment)

neighbour_vehicles result:
  front = None  (WRONG - should detect the vehicle on b->c)
  rear = None
After (bug fixed)
FIXED: Front vehicle detected at [55.  0.]

Road layout: a->b (50m) connected to b->c (50m)
Ego position: lane a->b, longitudinal=48 (near end)
Front vehicle: lane b->c, longitudinal=5 (on next segment)

neighbour_vehicles result:
  front = Vehicle at [55.  0.]  (CORRECT)
  rear = None

pytest results: 75 passed, 1 skipped in 1.82s (was 61 passed, 1 skipped)
Full test output (after)
============================= test session starts ==============================
platform linux -- Python 3.12.12, pytest-9.0.2, pluggy-1.6.0

tests/road/test_neighbour_vehicles.py::TestSameSegmentNeighbours::test_front_and_rear_on_same_lane PASSED
tests/road/test_neighbour_vehicles.py::TestSameSegmentNeighbours::test_no_neighbours PASSED
tests/road/test_neighbour_vehicles.py::TestSameSegmentNeighbours::test_only_front_vehicle PASSED
tests/road/test_neighbour_vehicles.py::TestSameSegmentNeighbours::test_only_rear_vehicle PASSED
tests/road/test_neighbour_vehicles.py::TestConnectedLaneNeighbours::test_front_on_next_segment PASSED
tests/road/test_neighbour_vehicles.py::TestConnectedLaneNeighbours::test_rear_on_previous_segment PASSED
tests/road/test_neighbour_vehicles.py::TestConnectedLaneNeighbours::test_front_on_curve_segment PASSED
tests/road/test_neighbour_vehicles.py::TestConnectedLaneNeighbours::test_closer_same_segment_preferred PASSED
tests/road/test_neighbour_vehicles.py::TestConnectedLaneNeighbours::test_both_connected_front_and_rear PASSED
tests/road/test_neighbour_vehicles.py::TestConnectedLaneNeighbours::test_different_lane_id_not_matched PASSED
tests/road/test_neighbour_vehicles.py::TestEdgeCases::test_no_downstream_connection PASSED
tests/road/test_neighbour_vehicles.py::TestEdgeCases::test_no_upstream_connection PASSED
tests/road/test_neighbour_vehicles.py::TestEdgeCases::test_far_vehicle_still_detected PASSED
tests/road/test_neighbour_vehicles.py::TestEdgeCases::test_lane_index_none PASSED

======================== 75 passed, 1 skipped in 1.82s ========================

Test plan

  • 4 same-segment regression tests (existing behaviour preserved)
  • 6 connected-lane tests (next segment, previous segment, curve, preference, multi-segment, lane filtering)
  • 4 edge case tests (no downstream/upstream, far vehicle, None lane_index)
  • All 61 original tests pass unchanged

Road.neighbour_vehicles() only searched the current lane segment for
front/rear neighbours. Vehicles that had crossed into a connected
next/previous segment were invisible, causing incorrect behaviour in
multi-segment environments (Racetrack, Roundabout, Intersection).

Extend the search to downstream and upstream connected lanes via the
road network graph, mapping longitudinal coordinates with appropriate
offsets. All 61 existing tests pass unchanged.

Fixes Farama-Foundation#626

Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Method neighbour_vehicles doesn't consider connected lanes

1 participant