Skip to content

Commit

Permalink
Revert "Fix lane change for lanes with different numbers of waypoints"
Browse files Browse the repository at this point in the history
This reverts commit 2d0bafb.
  • Loading branch information
martins-mozeiko committed Oct 28, 2019
1 parent bcdcab6 commit 336ce3a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 40 deletions.
11 changes: 5 additions & 6 deletions Assets/Scripts/Controllers/NPCController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ private void ResetData()
isRightTurn = false;
isWaitingToDodge = false;
isDodge = false;
laneChange = true;
laneChange = false;
isStopLight = false;
isStopSign = false;
hasReachedStopSign = false;
Expand Down Expand Up @@ -887,7 +887,6 @@ public void SetLaneData(List<Vector3> data)
private void SetChangeLaneData(List<Vector3> data)
{
laneData = new List<Vector3>(data);
currentIndex = SimulatorManager.Instance.MapManager.GetLaneNextIndex(transform.position, currentMapLane);
currentTarget = laneData[currentIndex];
isDodge = false; // ???
}
Expand Down Expand Up @@ -975,7 +974,6 @@ private void EvaluateTarget()
{
currentIndex++;
currentTarget = laneData[currentIndex];
Coroutines[(int)CoroutineID.DelayChangeLane] = FixedUpdateManager.StartCoroutine(DelayChangeLane());
}
else
{
Expand All @@ -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
{
Expand All @@ -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)
{
Expand All @@ -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)
{
Expand Down
23 changes: 0 additions & 23 deletions Assets/Scripts/Managers/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
19 changes: 8 additions & 11 deletions Assets/Scripts/Map/MapLaneSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
{
Expand Down

0 comments on commit 336ce3a

Please sign in to comment.