From 336ce3a6b881d9ea23e7e826c5d5c551283c9c53 Mon Sep 17 00:00:00 2001 From: Martins Mozeiko Date: Mon, 28 Oct 2019 11:17:44 -0700 Subject: [PATCH] Revert "Fix lane change for lanes with different numbers of waypoints" This reverts commit 2d0bafb75351a82f9c1e2676a4343d17f500cf5d. --- Assets/Scripts/Controllers/NPCController.cs | 11 +++++----- Assets/Scripts/Managers/MapManager.cs | 23 --------------------- Assets/Scripts/Map/MapLaneSection.cs | 19 +++++++---------- 3 files changed, 13 insertions(+), 40 deletions(-) diff --git a/Assets/Scripts/Controllers/NPCController.cs b/Assets/Scripts/Controllers/NPCController.cs index 768d8ab2b..7664866a0 100644 --- a/Assets/Scripts/Controllers/NPCController.cs +++ b/Assets/Scripts/Controllers/NPCController.cs @@ -574,7 +574,7 @@ private void ResetData() isRightTurn = false; isWaitingToDodge = false; isDodge = false; - laneChange = true; + laneChange = false; isStopLight = false; isStopSign = false; hasReachedStopSign = false; @@ -887,7 +887,6 @@ public void SetLaneData(List data) private void SetChangeLaneData(List data) { laneData = new List(data); - currentIndex = SimulatorManager.Instance.MapManager.GetLaneNextIndex(transform.position, currentMapLane); currentTarget = laneData[currentIndex]; isDodge = false; // ??? } @@ -975,7 +974,6 @@ private void EvaluateTarget() { currentIndex++; currentTarget = laneData[currentIndex]; - Coroutines[(int)CoroutineID.DelayChangeLane] = FixedUpdateManager.StartCoroutine(DelayChangeLane()); } else { @@ -995,6 +993,7 @@ private void GetNextLane() normalSpeed = RandomGenerator.NextFloat(laneSpeedLimit - 3 + aggression, laneSpeedLimit + 1 + aggression); SetLaneData(currentMapLane.mapWorldPositions); SetTurnSignal(); + Coroutines[(int)CoroutineID.DelayChangeLane] = FixedUpdateManager.StartCoroutine(DelayChangeLane()); } else // issue getting new waypoints so despawn { @@ -1007,8 +1006,8 @@ private IEnumerator DelayChangeLane() { if (Control == ControlType.Waypoints) yield break; if (!currentMapLane.isTrafficLane) yield break; - if (RandomGenerator.Next(5) == 1) yield break; - if (!laneChange) yield break; + if (RandomGenerator.Next(3) == 1) yield break; + if (!(laneChange)) yield break; if (currentMapLane.leftLaneForward != null) { @@ -1023,7 +1022,7 @@ private IEnumerator DelayChangeLane() SetNPCTurnSignal(); } - yield return FixedUpdateManager.WaitForFixedSeconds(RandomGenerator.NextFloat(1f, 3f)); + yield return FixedUpdateManager.WaitForFixedSeconds(RandomGenerator.NextFloat(0f, 2f)); if (currentIndex >= laneData.Count - 2) { diff --git a/Assets/Scripts/Managers/MapManager.cs b/Assets/Scripts/Managers/MapManager.cs index 4c7fcadfe..38bdf4852 100644 --- a/Assets/Scripts/Managers/MapManager.cs +++ b/Assets/Scripts/Managers/MapManager.cs @@ -72,29 +72,6 @@ public MapLane GetClosestLane(Vector3 position) return result; } - public int GetLaneNextIndex(Vector3 position, MapLane lane) - { - float minDist = float.PositiveInfinity; - int index = -1; - - for (int i = 0; i < lane.mapWorldPositions.Count - 1; i++) - { - var p0 = lane.mapWorldPositions[i]; - var p1 = lane.mapWorldPositions[i + 1]; - - var p = Utility.ClosetPointOnSegment(p0, p1, position); - - float d = Vector3.SqrMagnitude(position - p); - if (d < minDist) - { - minDist = d; - index = i + 1; - } - } - - return index; - } - // api public void GetPointOnLane(Vector3 point, out Vector3 position, out Quaternion rotation) { diff --git a/Assets/Scripts/Map/MapLaneSection.cs b/Assets/Scripts/Map/MapLaneSection.cs index 2c1daf55e..733eeb655 100644 --- a/Assets/Scripts/Map/MapLaneSection.cs +++ b/Assets/Scripts/Map/MapLaneSection.cs @@ -43,7 +43,7 @@ public void SetLaneData() lane.leftBoundType = LaneBoundaryType.DOTTED_WHITE; lane.rightBoundType = LaneBoundaryType.DOTTED_WHITE; - var laneIdx = lane.mapWorldPositions.Count - 1; // index to compute vector from lane to otherLane and distance between those two lanes + var idx = 1; // index to compute vector from lane to otherLane and distance between those two lanes var laneDir = (lane.mapWorldPositions[1] - lane.mapWorldPositions[0]).normalized; var minDistLeft = 50f; var minDistRight = 50f; @@ -58,25 +58,20 @@ public void SetLaneData() var otherLane = lanes[j]; if (lane == otherLane) continue; - var otherIdx = otherLane.mapWorldPositions.Count - 1; // Check if these two lanes have same directions by check the dist between 1st pos in lane and (the 1st and last pos in otherLane). var isSameDirection = true; - var laneDirection = (lane.mapWorldPositions[laneIdx] - lane.mapWorldPositions[0]).normalized; - var otherLaneDirection = (otherLane.mapWorldPositions[otherIdx] - otherLane.mapWorldPositions[0]).normalized; + var laneDirection = (lane.mapWorldPositions[lane.mapWorldPositions.Count-1] - lane.mapWorldPositions[0]).normalized; + var otherLaneDirection = (otherLane.mapWorldPositions[otherLane.mapWorldPositions.Count-1] - otherLane.mapWorldPositions[0]).normalized; if (Vector3.Dot(laneDirection, otherLaneDirection) < 0) { isSameDirection = false; } - var cross = Vector3.Cross(laneDir, (otherLane.mapWorldPositions[otherIdx] - lane.mapWorldPositions[laneIdx]).normalized).y; - var dist = Mathf.RoundToInt(Vector3.Distance(lane.mapWorldPositions[laneIdx], otherLane.mapWorldPositions[otherIdx])); - if (dist <= 1) - { - dist = Mathf.RoundToInt(Vector3.Distance(lane.mapWorldPositions[0], otherLane.mapWorldPositions[0])); - } - if (isSameDirection) // same direction { + var cross = Vector3.Cross(laneDir, (otherLane.mapWorldPositions[idx] - lane.mapWorldPositions[idx]).normalized).y; + var dist = Mathf.RoundToInt(Vector3.Distance(lane.mapWorldPositions[idx], otherLane.mapWorldPositions[idx])); + if (cross < 0) // otherLane is left of lane { if (dist < minDistLeft) // closest lane left of lane is otherLane @@ -102,6 +97,8 @@ public void SetLaneData() else // opposite direction { isOneWay = false; + var cross = Vector3.Cross(laneDir, (otherLane.mapWorldPositions[otherLane.mapWorldPositions.Count - 1 - idx] - lane.mapWorldPositions[idx]).normalized).y; + var dist = Mathf.RoundToInt(Vector3.Distance(lane.mapWorldPositions[idx], otherLane.mapWorldPositions[otherLane.mapWorldPositions.Count - 1 - idx])); if (cross < 0) // otherLane is left of lane {