function state = update_road_lane(state, P)
% checks for the possibility of entering another road at an intersection
% if our relative heading is > 45%, then it's likely we intend to turn
% so we check if we have transitioned to the new road
% if we're already inside the new road, then we update our state to reflect that

road = P.roads(state.road_id);

if abs(mod(state.yaw,pi)) > pi/4
  other_road = P.roads(road.intersection.other_road_id);
  local_frame = other_road.frame_inv*vehicle_frame(state, P);
  ey = local_frame(2,3);
  
  if inside_road(ey, other_road)
    disp(['id=' num2str(state.id) ' snapping to new road'])
    [min_dey, min_id] = min(abs([other_road.lanes.ey] - ey));
    state.x = local_frame(1,3);
    state.ey = ey;
    state.yaw = atan2(local_frame(2,1), local_frame(1,1));
    state.road_id = road.intersection.other_road_id;
    state.lane_id = min_id;    
    state
    return
  end
end

[min_dey, min_id] = min(abs([P.roads(state.road_id).lanes.ey] - state.ey));
state.lane_id = min_id;  
