// remove any vehicles that have gone past the end of this lane segment
if (sinkLaneSegment != null) {
int count = vehicles.size();
// remove any vehicles that have gone past the end of this road segment
while (count > 0) {
final Vehicle vehicle = vehicles.get(0);
if (vehicle.getRearPosition() < roadLength) {
break;
}
// if the vehicle is past the end of this road segment then move it onto the
// sink lane for its lane
// TODO - check previous lane correct (used for drawing vehicle when changing lanes)
// final int prevLaneOnNewRoadSegment = lane;
// final int prevLaneOnNewRoadSegment = sinkLane[vehicle.previousLane()];
final double rearPositionOnNewRoadSegment = vehicle.getRearPosition() - roadLength;
double exitEndPos = Vehicle.EXIT_POSITION_NOT_SET;
if (sinkLaneSegment.type() == Lanes.Type.TRAFFIC) {
final int exitRoadSegmentId = vehicle.exitRoadSegmentId();
if (exitRoadSegmentId == sinkLaneSegment.roadSegment.id()) {
// vehicle is on exit exit road segment, so exit end pos is end of this
// road segment
exitEndPos = sinkLaneSegment.roadLength();
} else {
// check if next segment is exit segment
final RoadSegment sinkSinkRoad = sinkLaneSegment.roadSegment();
if (sinkSinkRoad != null && sinkSinkRoad.id() == exitRoadSegmentId) {
// next road segment is exit road segment
exitEndPos = sinkLaneSegment.roadLength() + sinkSinkRoad.roadLength();
}
}
}
final int laneOnNewRoadSegment = sinkLaneSegment.lane();
vehicle.moveToNewRoadSegment(sinkLaneSegment.roadSegment(), laneOnNewRoadSegment,
rearPositionOnNewRoadSegment, exitEndPos);
// remove vehicle from this road segment
vehicles.remove(0);
--count;
++removedVehicleCount;