/// <summary>
        /// Determine a state change based on if car should to change lanes
        /// or car has reached the end of it's lane
        /// </summary>
        /// <param name="deltaTime"></param>
        /// <returns></returns>
        public override DrivingState Update(float deltaTime)
        {
            DrivingState state = base.Update(deltaTime);
            // Determine if lane change is complete (car is close enough to next lane trajectory)
            // Sterring behavior will kick in and complete the change
            float distanceToNextPath = nextLane.Path.DistanceToPath(car.Position);

            if (distanceToNextPath <= Path.Radius)
            {
                state = new KeepLaneState(car, nextLane);
            }
            return(state);
        }
Пример #2
0
        /// <summary>
        /// Determine a state change based on if car should to change lanes
        /// or car has reached the end of it's lane.
        /// </summary>
        /// <param name="deltaTime">Time since last update</param>
        /// <returns></returns>
        public override DrivingState Update(float deltaTime)
        {
            DrivingState state = base.Update(deltaTime);
            // Determine if I need to change lanes or if I'm at the end of a lane, and trigger a state change
            float lerp = Path.InverseLerp(car.Position + car.Direction * car.VehicleLength / 2);

            if (lerp >= 0.99 && NextLane != null)         // End of the lane reached
            {
                state = new KeepLaneState(car, NextLane); // New wait for light state if next lane is available
            }
            else if (0.2 < lerp && lerp < 0.8)            // Allow lane changing from the first 20% of the lane, until 80%
            {
                // Check via MOBIL for potential lane change
                Lane newLane = Mobil.OptimalLane(car, lane);
                if (newLane.LaneIdx != lane.LaneIdx)
                {
                    state = new ChangeLaneState(car, lane, newLane);
                }
            }
            return(state);
        }