/// <summary> /// When on a node, go to a randomly allowed direction except backwards /// </summary> /// <param name="node"></param> private void OnNodeTriggerRandom(IntersectionNode node) { var dirs = node.AllowedDirections; if (dirs.Contains(_moveQueue.CurrentDirection.Opposite()) && dirs.Count > 1) { dirs.Remove(_moveQueue.CurrentDirection.Opposite()); } transform.position = node.Position; _moveQueue.CurrentDirection = dirs.ElementAt((int)(Random.value * dirs.Count)); }
/// <summary> /// What happens when the player reaches a <see cref="IntersectionNode"/> /// </summary> /// <param name="node"></param> public void OnNodeTrigger(IntersectionNode node) { // Check if next direction is allowed if (_moveQueue.NextDirection != Utility.EDirection4.None && node.IsAllowed(_moveQueue.NextDirection)) { // Set correct position and update movement transform.position = node.Position; _moveQueue.CurrentDirection = _moveQueue.NextDirection; //if (_nodeMovement.CurrentDirection != Utility.EDirection.None) // _collidingNode = null; } // Check if current direction is allowed else if (!node.IsAllowed(_moveQueue.CurrentDirection)) { //GetComponent<Rigidbody2D>().position = node.Position; transform.position = node.Position; _moveQueue.CurrentDirection = Utility.EDirection4.None; _collidingNode = node; } }
public MapData LocateWaypointWithLineOfSight(WaypointDetectionMethod method, List <GPSLocation> history, int waypointIndex = 0, int viewArc = 0, double distance = 1.0) { MapData waypoint = null; var location = history.Last(); var sortedMAPPoints = GLOSAHelper.SortMAPDataByDistanceFromCurrentLocation(_mapDataCache, location.Latitude, location.Longitude); if (sortedMAPPoints != null && sortedMAPPoints.Count > 0) { switch (method) { case WaypointDetectionMethod.ShortestDistance: waypoint = sortedMAPPoints.First(); break; case WaypointDetectionMethod.GPSHistoryDirection: GPSLocation to = history[history.Count - 1]; double? vehicleHeading = LocationHelper.HeadingFromGPSTrail(history); List <IntersectionNode> listOfMAPDataWaypoints = IntersectionNode.IntersectionNodesFromMAPData(_mapDataCache).ToList(); var filtered = IntersectionNode.Filter(listOfMAPDataWaypoints, vehicleHeading, history); var sortedByBearingAndDistance = IntersectionNode.SortIntersectionNodes(filtered, to, vehicleHeading, viewArc, distance).ToList(); if (sortedByBearingAndDistance.Count() > waypointIndex) { var tn = sortedByBearingAndDistance.ToList()[waypointIndex]; waypoint = tn.MapData; } break; default: throw new Exception("Waypoint Detection Method not implemented."); break; } } return(waypoint); }
/// <summary> /// Behaviour when on <see cref="IntersectionNode"/> /// </summary> /// <param name="node"></param> public void OnNodeTrigger(IntersectionNode node) { switch (Behavior) { case EBehavior.Random: OnNodeTriggerRandom(node); break; case EBehavior.StayInFront: OnNodeTriggerInFront(node); break; case EBehavior.StayBehind: OnNodeTriggerBehind(node); break; case EBehavior.KeepDistance: OnNodeTriggerDistance(node); break; default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// Update is called once per frame /// </summary> void Update() { if (Input.GetButtonDown("Up") && (_collidingNode == null || _collidingNode.AllowUp)) { _moveQueue.NextDirection = Utility.EDirection4.Up; } else if (Input.GetButtonDown("Right") && (_collidingNode == null || _collidingNode.AllowRight)) { _moveQueue.NextDirection = Utility.EDirection4.Right; } else if (Input.GetButtonDown("Down") && (_collidingNode == null || _collidingNode.AllowDown)) { _moveQueue.NextDirection = Utility.EDirection4.Down; } else if (Input.GetButtonDown("Left") && (_collidingNode == null || _collidingNode.AllowLeft)) { _moveQueue.NextDirection = Utility.EDirection4.Left; } if (_moveQueue.CurrentDirection != Utility.EDirection4.None) { _collidingNode = null; } }
private void correctIntersection(IntersectionNode intersection) { int roadCount = intersection.pointList.Count; List<int> highwayIndexes = new List<int>(); float waySize = 0; for(int i = 0 ; i < roadCount ; i++) { int index = getHighwayIndex(intersection.wayIds[i]); highwayIndexes.Add(index); waySize += highwayList[index].waySize; } //Shared waySize at the intersection waySize = waySize / (float)roadCount; List<Vector3> forwardVectors = new List<Vector3>(); List<Vector3> leftVectors = new List<Vector3>(); List<Vector3> rightVectors = new List<Vector3>(); List<LineSegment> lineSegments = new List<LineSegment>(); Vector3 up = Vector3.up; for(int i = 0 ; i < roadCount ; i++) { Vector3 v = intersection.pointList[i] - intersection.node.meterPosition; forwardVectors.Add(v); Vector3 right = Vector3.Cross(up, v).normalized; rightVectors.Add(right); leftVectors.Add(-right); LineSegment ls = new LineSegment(); ls.Left1 = new Vector2(intersection.node.meterPosition.x, intersection.node.meterPosition.z) + new Vector2(-right.x, -right.z) * (waySize / 2.0f); ls.Left2 = new Vector2(intersection.pointList[i].x, intersection.pointList[i].z) + new Vector2(-right.x, -right.z) * (waySize/2.0f); ls.Right1 = new Vector2(intersection.node.meterPosition.x, intersection.node.meterPosition.z) + new Vector2(right.x, right.z) * (waySize / 2.0f); ls.Right2 = new Vector2(intersection.pointList[i].x, intersection.pointList[i].z) + new Vector2(right.x, right.z) * (waySize / 2.0f); lineSegments.Add(ls); } List<IntersectionAngle> angles = new List<IntersectionAngle>(); for (int i = 0; i < roadCount - 1; i++) { for (int j = i+1; j < roadCount; j++) { float dot = Vector3.Dot(forwardVectors[i].normalized, forwardVectors[j].normalized); double angle = Math.Acos(dot) * 180.0 / Math.PI; IntersectionAngle ia = new IntersectionAngle(); ia.way1No = i; ia.way2No = j; ia.angle = angle; angles.Add(ia); } } List<IntersectionAngle> sortedAngles = angles.OrderBy(o => o.angle).ToList(); intersectValidator[] truthtable = new intersectValidator[roadCount]; for(int k = 0 ; k < truthtable.Length ; k++) { truthtable[k].left = false; truthtable[k].right = false; } List<intersectionInfo> intersectList = new List<intersectionInfo>(); int iterationCount = 0; while (sortedAngles.Count != 0) { if (iterationCount == 10) { throw new System.InvalidOperationException("Sorted Angles entered to a loop"); //Debug.Log("ERRROR IN THE WHILE LOOP !!!"); //break; } else iterationCount++; //intersect these two int way1No = sortedAngles[0].way1No; int way2No = sortedAngles[0].way2No; intersectionside iside; //Normal intersect if ((truthtable[way1No].left == false && truthtable[way1No].right == false) || (truthtable[way2No].left == false && truthtable[way2No].right == false)) { iside = intersectWay(way1No, way2No, intersection, lineSegments[way1No], lineSegments[way2No], forwardVectors[way1No], forwardVectors[way2No],sortedAngles[0].angle); } else //Forced intersect { if (truthtable[way1No].left || truthtable[way2No].right) iside = forcedIntersectWay(way1No, way2No, intersection, lineSegments[way1No], lineSegments[way2No], forwardVectors[way1No], forwardVectors[way2No], false); else if (truthtable[way1No].right || truthtable[way2No].left) iside = forcedIntersectWay(way1No, way2No, intersection, lineSegments[way1No], lineSegments[way2No], forwardVectors[way1No], forwardVectors[way2No], true); else { Debug.Log("SOMETHING SHOULD BE REALLY WRONG ABOUT HIGHWAYS :( !!!"); throw new System.InvalidOperationException("Forced Intersection somehow does not work!"); //iside = new intersectionside(); } } if (iside.success) { intersectionInfo ii = new intersectionInfo(); ii.vertice = iside.vertex; ii.wayNo1 = way1No; ii.wayNo2 = way2No; ii.isleftright = iside.leftright; intersectList.Add(ii); if (iside.leftright == true) { truthtable[way1No].left = true; truthtable[way2No].right = true; } else { truthtable[way1No].right = true; truthtable[way2No].left = true; } //Update AngleList sortedAngles.RemoveAt(0); if (truthtable[way1No].left && truthtable[way1No].right) sortedAngles.RemoveAll(item => (item.way1No == way1No || item.way2No == way1No)); if (truthtable[way2No].left && truthtable[way2No].right) sortedAngles.RemoveAll(item => (item.way1No == way2No || item.way2No == way2No)); } else { IntersectionAngle ang = sortedAngles[0]; ang.angle = 360.0 - ang.angle; sortedAngles.RemoveAt(0); sortedAngles.Add(ang); } } fillIntersectionHole(intersectList, isTrafficLight(intersection.node), intersection.node.id); }
//Helper for correctIntersectionPoint() private void correct2wayIntersection(IntersectionNode intersection) { Vector3 up = new Vector3(0, 1, 0); int index1 = getHighwayIndex(intersection.wayIds[0]); Vector3 forward1 = intersection.node.meterPosition - intersection.pointList[0]; //**************************************************************************** Vector3 right1 = Vector3.Cross(up, forward1); right1 = right1.normalized; Vector3 left1 = -right1; float waySize1 = highwayList[index1].waySize; int index2 = getHighwayIndex(intersection.wayIds[1]); Vector3 forward2 = intersection.pointList[1] - intersection.node.meterPosition; //***************************************************************************** Vector3 right2 = Vector3.Cross(up, forward2); right2 = right2.normalized; Vector3 left2 = -right2; float waySize2 = highwayList[index2].waySize; int intersectionType = -1; if (intersection.intersectionIndex[0] == 0 && intersection.intersectionIndex[1] == 0) intersectionType = 0; else if (intersection.intersectionIndex[0] == 0 && intersection.intersectionIndex[1] != 0) intersectionType = 1; else if (intersection.intersectionIndex[0] != 0 && intersection.intersectionIndex[1] == 0) intersectionType = 2; else if (intersection.intersectionIndex[0] != 0 && intersection.intersectionIndex[1] != 0) intersectionType = 3; float waySize = (waySize1 + waySize2) / 2.0f; //Left Side Vector2 p0 = new Vector2(intersection.pointList[0].x, intersection.pointList[0].z) + new Vector2(left1.x, left1.z) * (waySize / 2.0f); Vector2 p1 = new Vector2(intersection.node.meterPosition.x, intersection.node.meterPosition.z) + new Vector2(left1.x, left1.z) * (waySize / 2.0f); Vector2 p2 = new Vector2(intersection.node.meterPosition.x, intersection.node.meterPosition.z) + new Vector2(left2.x, left2.z) * (waySize / 2.0f); Vector2 p3 = new Vector2(intersection.pointList[1].x, intersection.pointList[1].z) + new Vector2(left2.x, left2.z) * (waySize / 2.0f); Vector3 newVertexLeft, newVertexRight; //INTERSECTION LEFT Vector2 iL = new Vector2(); if (!Geometry.getInfiniteLineIntersection(ref iL, p0, p1, p2, p3)) newVertexLeft = new Vector3(p1.x, terrain.getTerrainHeight2(p1.y + terrain.terrainInfo.shiftZ, p1.x + terrain.terrainInfo.shiftX), p1.y); else newVertexLeft = new Vector3(iL.x, terrain.getTerrainHeight2(iL.y + terrain.terrainInfo.shiftZ, iL.x + terrain.terrainInfo.shiftX), iL.y); //Right Side p0 = new Vector2(intersection.pointList[0].x, intersection.pointList[0].z) + new Vector2(right1.x, right1.z) * (waySize / 2.0f); p1 = new Vector2(intersection.node.meterPosition.x, intersection.node.meterPosition.z) + new Vector2(right1.x, right1.z) * (waySize / 2.0f); p2 = new Vector2(intersection.node.meterPosition.x, intersection.node.meterPosition.z) + new Vector2(right2.x, right2.z) * (waySize / 2.0f); p3 = new Vector2(intersection.pointList[1].x, intersection.pointList[1].z) + new Vector2(right2.x, right2.z) * (waySize / 2.0f); //INTERSECTION RIGHT Vector2 iR = new Vector2(); if (!Geometry.getInfiniteLineIntersection(ref iR, p0, p1, p2, p3)) newVertexRight = new Vector3(p1.x, terrain.getTerrainHeight2(p1.y + terrain.terrainInfo.shiftZ, p1.x + terrain.terrainInfo.shiftX), p1.y); else newVertexRight = new Vector3(iR.x, terrain.getTerrainHeight2(iR.y + terrain.terrainInfo.shiftZ, iR.x + terrain.terrainInfo.shiftX), iR.y); if(isTrafficLight(intersection.node)) { int intersectionIndex = intersections.FindIndex(item => item.node.id == intersection.node.id); if (intersections[intersectionIndex].trafficLights != null) { for (int ind = 0; ind < intersections[intersectionIndex].trafficLights.Count; ind++) GameObject.Destroy(intersections[intersectionIndex].trafficLights[ind].object3D); } IntersectionNode node = intersections[intersectionIndex]; node.trafficLights = new List<Object3D>(); node.trafficLights.Add(DefaultObject3DHandler.drawTrafficSign(newVertexLeft)); node.trafficLights.Add(DefaultObject3DHandler.drawTrafficSign(newVertexRight)); intersections[intersectionIndex] = node; } if (intersectionType == 0) //start - start { highwayList[index1].rightSideVertexes[intersection.intersectionIndex[0]] = newVertexLeft; highwayList[index2].leftSideVertexes[intersection.intersectionIndex[1]] = newVertexLeft; highwayList[index1].leftSideVertexes[intersection.intersectionIndex[0]] = newVertexRight; highwayList[index2].rightSideVertexes[intersection.intersectionIndex[1]] = newVertexRight; } else if (intersectionType == 1) //start - end { highwayList[index1].rightSideVertexes[intersection.intersectionIndex[0]] = newVertexLeft; highwayList[index2].rightSideVertexes[intersection.intersectionIndex[1]] = newVertexLeft; highwayList[index1].leftSideVertexes[intersection.intersectionIndex[0]] = newVertexRight; highwayList[index2].leftSideVertexes[intersection.intersectionIndex[1]] = newVertexRight; } else if (intersectionType == 2) // end - start { highwayList[index1].leftSideVertexes[intersection.intersectionIndex[0]] = newVertexLeft; highwayList[index2].rightSideVertexes[intersection.intersectionIndex[1]] = newVertexRight; highwayList[index1].rightSideVertexes[intersection.intersectionIndex[0]] = newVertexRight; highwayList[index2].leftSideVertexes[intersection.intersectionIndex[1]] = newVertexLeft; } else if (intersectionType == 3) // end - end { highwayList[index1].leftSideVertexes[intersection.intersectionIndex[0]] = newVertexLeft; highwayList[index2].rightSideVertexes[intersection.intersectionIndex[1]] = newVertexLeft; highwayList[index1].rightSideVertexes[intersection.intersectionIndex[0]] = newVertexRight; highwayList[index2].leftSideVertexes[intersection.intersectionIndex[1]] = newVertexRight; } }
/// <summary> /// Updates the highway nodes of way1 and way2 for the detected newVertex /// </summary> private void applyIntersection(int way1No, int way2No,int highwayIndex1,int highwayIndex2, Vector3 newVertex, IntersectionNode intersection, bool leftRight) { if(leftRight) { //Update Way1 if (intersection.intersectionTypes[way1No] == IntersectionType.front) highwayList[highwayIndex1].leftSideVertexes[0] = newVertex; else highwayList[highwayIndex1].rightSideVertexes[highwayList[highwayIndex1].rightSideVertexes.Count - 1] = newVertex; //Update Way2 if (intersection.intersectionTypes[way2No] == IntersectionType.front) highwayList[highwayIndex2].rightSideVertexes[0] = newVertex; else highwayList[highwayIndex2].leftSideVertexes[highwayList[highwayIndex2].leftSideVertexes.Count - 1] = newVertex; } else { //Update Way1 if (intersection.intersectionTypes[way1No] == IntersectionType.front) highwayList[highwayIndex1].rightSideVertexes[0] = newVertex; else highwayList[highwayIndex1].leftSideVertexes[highwayList[highwayIndex1].leftSideVertexes.Count - 1] = newVertex; //Update Way2 if (intersection.intersectionTypes[way2No] == IntersectionType.front) highwayList[highwayIndex2].leftSideVertexes[0] = newVertex; else highwayList[highwayIndex2].rightSideVertexes[highwayList[highwayIndex2].rightSideVertexes.Count - 1] = newVertex; } }
private void resetIntersectionNode(IntersectionNode node) { for(int i = 0 ; i < node.wayIds.Count ; i++) { Highway hway = highwayList.Find(item => item.id == node.wayIds[i]); Vector3 forward = (node.pointList[i] - node.node.meterPosition).normalized; Vector3 right = Vector3.Cross(Vector3.up,forward); Vector3 left = -right; if(node.intersectionTypes[i] == IntersectionType.front) { hway.leftSideVertexes[0] = hway.way.nodes[0].meterPosition + left * (hway.waySize/2.0f); hway.rightSideVertexes[0] = hway.way.nodes[0].meterPosition + right * (hway.waySize / 2.0f); } else if(node.intersectionTypes[i] == IntersectionType.end) { hway.leftSideVertexes[hway.leftSideVertexes.Count - 1] = hway.way.nodes[hway.way.nodes.Count-1].meterPosition + right * (hway.waySize / 2.0f); hway.rightSideVertexes[hway.rightSideVertexes.Count - 1] = hway.way.nodes[hway.way.nodes.Count - 1].meterPosition + left * (hway.waySize / 2.0f); } } }
/// <summary> /// Tries to intersect way1 and way2 (if the angle <180) /// </summary> private intersectionside intersectWay(int way1No, int way2No, IntersectionNode intersection, LineSegment segment1, LineSegment segment2,Vector3 fwd1, Vector3 fwd2,double angle) { int highwayIndex1 = getHighwayIndex(intersection.wayIds[way1No]); int highwayIndex2 = getHighwayIndex(intersection.wayIds[way2No]); Vector2 intersect = new Vector2(); if (Geometry.getHalfVectorIntersection(ref intersect, segment1.Left1, segment1.Left2, fwd1, segment2.Right1, segment2.Right2, fwd2)) { Vector3 newVertex = new Vector3(intersect.x, terrain.getTerrainHeight2(intersect.y + terrain.terrainInfo.shiftZ, intersect.x + terrain.terrainInfo.shiftX), intersect.y); applyIntersection(way1No, way2No,highwayIndex1,highwayIndex2, newVertex, intersection, true); intersectionside s = new intersectionside(); s.vertex = newVertex; s.leftright = true; s.success = true; return s; } else if (Geometry.getHalfVectorIntersection(ref intersect, segment1.Right1, segment1.Right2, fwd1, segment2.Left1, segment2.Left2, fwd2)) { Vector3 newVertex = new Vector3(intersect.x, terrain.getTerrainHeight2(intersect.y + terrain.terrainInfo.shiftZ, intersect.x + terrain.terrainInfo.shiftX), intersect.y); applyIntersection(way1No, way2No,highwayIndex1,highwayIndex2, newVertex, intersection, false); intersectionside s = new intersectionside(); s.vertex = newVertex; s.leftright = false; s.success = true; return s; } else { throw new System.InvalidOperationException("Bormal Intersection Failed!!"); Debug.Log("------------------------------"); intersectionside s = new intersectionside(); s.vertex = new Vector3(0, 0, 0); s.leftright = false; s.success = false; return s; } }
/// <summary> /// Search for wayList and detects shared nodes, return the result as IntersectionNodeList /// </summary> /// <returns></returns> private List<IntersectionNode> generateIntersectionList() { List<IntersectionNode> intersectionList = new List<IntersectionNode>(); for (int i = 0; i < wayList.Count; i++) { for (int j = 0; j < wayList[i].nodes.Count; j++) { highwayType type = Highway.getHighwayType(wayList[i].tags); if (type == highwayType.Railway || type == highwayType.River || type == highwayType.none || type == highwayType.HighwayFootway || type == highwayType.HighwayPedestrian) continue; int index = intersectionList.FindIndex(item => item.node.id == wayList[i].nodes[j].id); if (index == -1) // if node not exist, add it to the intersectionList { IntersectionNode nd = new IntersectionNode(); nd.node = wayList[i].nodes[j]; nd.pointList = new List<Vector3>(); nd.wayIds = new List<string>(); nd.intersectionIndex = new List<int>(); nd.intersectionTypes = new List<IntersectionType>(); nd.count = 0; nd.hasRoadDivision = false; intersectionList.Add(nd); index = intersectionList.Count - 1; } if (j == 0) //connect from beginning { Vector3 point = wayList[i].nodes[1].meterPosition; IntersectionNode nd = intersectionList[index]; nd.pointList.Add(point); nd.wayIds.Add(wayList[i].id); nd.intersectionTypes.Add(IntersectionType.front); nd.count++; nd.intersectionIndex.Add(j); intersectionList[index] = nd; } else if (j == wayList[i].nodes.Count - 1) //connect from ending { Vector3 point = wayList[i].nodes[j - 1].meterPosition; IntersectionNode nd = intersectionList[index]; nd.intersectionTypes.Add(IntersectionType.end); nd.pointList.Add(point); nd.wayIds.Add(wayList[i].id); nd.count++; nd.intersectionIndex.Add(j); intersectionList[index] = nd; } else // connect from the middle (split way into 2) { IntersectionNode nd = intersectionList[index]; Vector3 point1 = wayList[i].nodes[j + 1].meterPosition; nd.pointList.Add(point1); nd.wayIds.Add(wayList[i].id); nd.intersectionIndex.Add(j + 1); nd.intersectionTypes.Add(IntersectionType.middle_from); Vector3 point2 = wayList[i].nodes[j - 1].meterPosition; nd.pointList.Add(point2); nd.wayIds.Add(wayList[i].id); nd.intersectionIndex.Add(j - 1); nd.intersectionTypes.Add(IntersectionType.middle_to); nd.count += 2; nd.hasRoadDivision = true; intersectionList[index] = nd; } } } intersectionList.RemoveAll(item => (item.count < 2 || (item.count == 2 && item.hasRoadDivision == true))); return intersectionList; }
public void OnEnable() { m_Instance = target as IntersectionNode; m_fields = ExposeProperties.GetProperties(m_Instance); }
/// <summary> /// When on a node, keep at a certain distance of the player /// </summary> /// <param name="node"></param> private void OnNodeTriggerDistance(IntersectionNode node) { }
/// <summary> /// When on a node, try to get behind the player /// </summary> /// <param name="node"></param> private void OnNodeTriggerBehind(IntersectionNode node) { }
/// <summary> /// When on a node, try to get in front of the player /// </summary> /// <param name="node"></param> private void OnNodeTriggerInFront(IntersectionNode node) { }
private void correctPavementIntersection(IntersectionNode intersection,string wayID, Pavement.pavementSide side) { Highway highwayOrg, highwayFound; Vector3 intersectionPoint; Vector2 calculatedIntersection = new Vector2(); int intersectCondition = -1; //0:start-left, 1:start-right, 2:end-left,3:end-right int intersectCondition2 = -1; // 0: start , 1 : left; //1. Find the point that given highway intersect with the other highway from the given side highwayOrg = highwayList.Find(item => item.id == wayID); highwayFound = null; int indexOrg = intersection.wayIds.FindIndex(item => item == wayID); if (intersection.intersectionTypes[indexOrg] == IntersectionType.front) { if (side == Pavement.pavementSide.left) { intersectionPoint = highwayOrg.leftSideVertexes[0]; //start-left intersectCondition = 0; } else { intersectionPoint = highwayOrg.rightSideVertexes[0]; //start-right intersectCondition = 1; } } else { if (side == Pavement.pavementSide.left) { intersectionPoint = highwayOrg.leftSideVertexes[highwayOrg.leftSideVertexes.Count - 1]; //end - left intersectCondition = 2; } else { intersectionPoint = highwayOrg.rightSideVertexes[highwayOrg.rightSideVertexes.Count - 1]; // end - right intersectCondition = 3; } } //2. For the intersection point compare all other highways in the intersection to Detect the highway we are looking for for(int i = 0 ; i < intersection.wayIds.Count ;i++) { if (intersection.wayIds[i] == wayID) continue; highwayFound = highwayList.Find(item => item.id == intersection.wayIds[i]); if(intersection.intersectionTypes[i] == IntersectionType.front) //start { if (highwayFound.leftSideVertexes[0] == intersectionPoint || highwayFound.rightSideVertexes[0] == intersectionPoint) { intersectCondition2 = 0; break; } } else //end { if (highwayFound.leftSideVertexes[highwayFound.leftSideVertexes.Count - 1] == intersectionPoint || highwayFound.rightSideVertexes[highwayFound.rightSideVertexes.Count - 1] == intersectionPoint) { intersectCondition2 = 1; break; } } } //3. We found the 2 highways that are intersecting from the side stated. Lets Get appropriate pavement obj and calculate //intersection points Vector2 p1, p2, p3, p4; int pavOrgIndex=-1, pavFoundIndex=-1; pavOrgIndex = pavementList.FindIndex(item => item.Pavementid == wayID && item.side == side); if(intersectCondition == 1 || intersectCondition == 2) { if (intersectCondition2 == 0) pavFoundIndex = pavementList.FindIndex(item => item.Pavementid == highwayFound.id && item.side == Pavement.pavementSide.left); else if (intersectCondition2 == 1) pavFoundIndex = pavementList.FindIndex(item => item.Pavementid == highwayFound.id && item.side == Pavement.pavementSide.right); else { Debug.Log("PAVEMENT NOT FOUND EXCEPTION"); return; } } if (intersectCondition == 0 || intersectCondition == 3) { if (intersectCondition2 == 0) pavFoundIndex = pavementList.FindIndex(item => item.Pavementid == highwayFound.id && item.side == Pavement.pavementSide.right); else if (intersectCondition2 == 1) pavFoundIndex = pavementList.FindIndex(item => item.Pavementid == highwayFound.id && item.side == Pavement.pavementSide.left); else { Debug.Log("PAVEMENT NOT FOUND EXCEPTION"); return; } } //3.1 Check if other highway has sidewalk at the given side if (pavFoundIndex == -1) { //Debug.Log("No Pavement to Intersect"); return; } //3.2 Calculate Points for intersection if (intersectCondition == 0) //start-left { p1 = new Vector2(pavementList[pavOrgIndex].leftSideVertexes[0].x, pavementList[pavOrgIndex].leftSideVertexes[0].z); p2 = new Vector2(pavementList[pavOrgIndex].leftSideVertexes[1].x, pavementList[pavOrgIndex].leftSideVertexes[1].z); if(intersectCondition2 == 0) // start-right { p3 = new Vector2(pavementList[pavFoundIndex].rightSideVertexes[0].x, pavementList[pavFoundIndex].rightSideVertexes[0].z); p4 = new Vector2(pavementList[pavFoundIndex].rightSideVertexes[1].x, pavementList[pavFoundIndex].rightSideVertexes[1].z); Geometry.getInfiniteLineIntersection(ref calculatedIntersection, p1, p2, p3, p4); Vector3 newVertice = new Vector3(calculatedIntersection.x, terrain.getTerrainHeight2(calculatedIntersection.y + terrain.terrainInfo.shiftZ, calculatedIntersection.x + terrain.terrainInfo.shiftX), calculatedIntersection.y); pavementList[pavFoundIndex].updateMesh(newVertice, Pavement.pavementSide.right,1); pavementList[pavOrgIndex].updateMesh(newVertice, Pavement.pavementSide.left, 0); } else //end-left { int ind = pavementList[pavFoundIndex].leftSideVertexes.Count; p3 = new Vector2(pavementList[pavFoundIndex].leftSideVertexes[ind-2].x, pavementList[pavFoundIndex].leftSideVertexes[ind-2].z); p4 = new Vector2(pavementList[pavFoundIndex].leftSideVertexes[ind-1].x, pavementList[pavFoundIndex].leftSideVertexes[ind-1].z); Geometry.getInfiniteLineIntersection(ref calculatedIntersection, p1, p2, p3, p4); Vector3 newVertice = new Vector3(calculatedIntersection.x, terrain.getTerrainHeight2(calculatedIntersection.y + terrain.terrainInfo.shiftZ, calculatedIntersection.x + terrain.terrainInfo.shiftX), calculatedIntersection.y); pavementList[pavFoundIndex].updateMesh(newVertice, Pavement.pavementSide.left,ind-2); pavementList[pavOrgIndex].updateMesh(newVertice, Pavement.pavementSide.left, 0); } } else if (intersectCondition == 1) //start-right { p1 = new Vector2(pavementList[pavOrgIndex].rightSideVertexes[0].x, pavementList[pavOrgIndex].rightSideVertexes[0].z); p2 = new Vector2(pavementList[pavOrgIndex].rightSideVertexes[1].x, pavementList[pavOrgIndex].rightSideVertexes[1].z); if (intersectCondition2 == 0) // start-left { p3 = new Vector2(pavementList[pavFoundIndex].leftSideVertexes[0].x, pavementList[pavFoundIndex].leftSideVertexes[0].z); p4 = new Vector2(pavementList[pavFoundIndex].leftSideVertexes[1].x, pavementList[pavFoundIndex].leftSideVertexes[1].z); Geometry.getInfiniteLineIntersection(ref calculatedIntersection, p1, p2, p3, p4); Vector3 newVertice = new Vector3(calculatedIntersection.x, terrain.getTerrainHeight2(calculatedIntersection.y + terrain.terrainInfo.shiftZ, calculatedIntersection.x + terrain.terrainInfo.shiftX), calculatedIntersection.y); pavementList[pavFoundIndex].updateMesh(newVertice, Pavement.pavementSide.left, 0); pavementList[pavOrgIndex].updateMesh(newVertice, Pavement.pavementSide.right, 1); } else //end-right { int ind = pavementList[pavFoundIndex].leftSideVertexes.Count; p3 = new Vector2(pavementList[pavFoundIndex].rightSideVertexes[ind - 2].x, pavementList[pavFoundIndex].rightSideVertexes[ind - 2].z); p4 = new Vector2(pavementList[pavFoundIndex].rightSideVertexes[ind - 1].x, pavementList[pavFoundIndex].rightSideVertexes[ind - 1].z); Geometry.getInfiniteLineIntersection(ref calculatedIntersection, p1, p2, p3, p4); Vector3 newVertice = new Vector3(calculatedIntersection.x, terrain.getTerrainHeight2(calculatedIntersection.y + terrain.terrainInfo.shiftZ, calculatedIntersection.x + terrain.terrainInfo.shiftX), calculatedIntersection.y); pavementList[pavFoundIndex].updateMesh(newVertice, Pavement.pavementSide.right, ind-1); pavementList[pavOrgIndex].updateMesh(newVertice, Pavement.pavementSide.right, 1); } } else if (intersectCondition == 2) //end-left { int ind = pavementList[pavOrgIndex].leftSideVertexes.Count; p1 = new Vector2(pavementList[pavOrgIndex].leftSideVertexes[ind-2].x, pavementList[pavOrgIndex].leftSideVertexes[ind-2].z); p2 = new Vector2(pavementList[pavOrgIndex].leftSideVertexes[ind-1].x, pavementList[pavOrgIndex].leftSideVertexes[ind-1].z); if (intersectCondition2 == 0) // start-left { p3 = new Vector2(pavementList[pavFoundIndex].leftSideVertexes[0].x, pavementList[pavFoundIndex].leftSideVertexes[0].z); p4 = new Vector2(pavementList[pavFoundIndex].leftSideVertexes[1].x, pavementList[pavFoundIndex].leftSideVertexes[1].z); Geometry.getInfiniteLineIntersection(ref calculatedIntersection, p1, p2, p3, p4); Vector3 newVertice = new Vector3(calculatedIntersection.x, terrain.getTerrainHeight2(calculatedIntersection.y + terrain.terrainInfo.shiftZ, calculatedIntersection.x + terrain.terrainInfo.shiftX), calculatedIntersection.y); pavementList[pavFoundIndex].updateMesh(newVertice, Pavement.pavementSide.left, 0); pavementList[pavOrgIndex].updateMesh(newVertice, Pavement.pavementSide.left, ind-2); } else //end-right { int ind2 = pavementList[pavFoundIndex].rightSideVertexes.Count; p3 = new Vector2(pavementList[pavFoundIndex].rightSideVertexes[ind2 - 2].x, pavementList[pavFoundIndex].rightSideVertexes[ind2 - 2].z); p4 = new Vector2(pavementList[pavFoundIndex].rightSideVertexes[ind2 - 1].x, pavementList[pavFoundIndex].rightSideVertexes[ind2 - 1].z); Geometry.getInfiniteLineIntersection(ref calculatedIntersection, p1, p2, p3, p4); Vector3 newVertice = new Vector3(calculatedIntersection.x, terrain.getTerrainHeight2(calculatedIntersection.y + terrain.terrainInfo.shiftZ, calculatedIntersection.x + terrain.terrainInfo.shiftX), calculatedIntersection.y); pavementList[pavFoundIndex].updateMesh(newVertice, Pavement.pavementSide.right, ind2-1); pavementList[pavOrgIndex].updateMesh(newVertice, Pavement.pavementSide.left, ind-2); } } else if (intersectCondition == 3) //end-right { int ind = pavementList[pavOrgIndex].rightSideVertexes.Count; p1 = new Vector2(pavementList[pavOrgIndex].rightSideVertexes[ind-2].x, pavementList[pavOrgIndex].rightSideVertexes[ind-2].z); p2 = new Vector2(pavementList[pavOrgIndex].rightSideVertexes[ind-1].x, pavementList[pavOrgIndex].rightSideVertexes[ind-1].z); if (intersectCondition2 == 0) // start-right { p3 = new Vector2(pavementList[pavFoundIndex].rightSideVertexes[0].x, pavementList[pavFoundIndex].rightSideVertexes[0].z); p4 = new Vector2(pavementList[pavFoundIndex].rightSideVertexes[1].x, pavementList[pavFoundIndex].rightSideVertexes[1].z); Geometry.getInfiniteLineIntersection(ref calculatedIntersection, p1, p2, p3, p4); Vector3 newVertice = new Vector3(calculatedIntersection.x, terrain.getTerrainHeight2(calculatedIntersection.y + terrain.terrainInfo.shiftZ, calculatedIntersection.x + terrain.terrainInfo.shiftX), calculatedIntersection.y); pavementList[pavFoundIndex].updateMesh(newVertice, Pavement.pavementSide.right, 1); pavementList[pavOrgIndex].updateMesh(newVertice, Pavement.pavementSide.right, ind-1); } else //end-left { int ind2 = pavementList[pavFoundIndex].leftSideVertexes.Count; p3 = new Vector2(pavementList[pavFoundIndex].leftSideVertexes[ind2 - 2].x, pavementList[pavFoundIndex].leftSideVertexes[ind2 - 2].z); p4 = new Vector2(pavementList[pavFoundIndex].leftSideVertexes[ind2 - 1].x, pavementList[pavFoundIndex].leftSideVertexes[ind2 - 1].z); Geometry.getInfiniteLineIntersection(ref calculatedIntersection, p1, p2, p3, p4); Vector3 newVertice = new Vector3(calculatedIntersection.x, terrain.getTerrainHeight2(calculatedIntersection.y + terrain.terrainInfo.shiftZ, calculatedIntersection.x + terrain.terrainInfo.shiftX), calculatedIntersection.y); pavementList[pavFoundIndex].updateMesh(newVertice, Pavement.pavementSide.left, ind2-2); pavementList[pavOrgIndex].updateMesh(newVertice, Pavement.pavementSide.right, ind-1); } } }
/// <summary> /// Force function to intersect way1 and way2 according to leftright or rightleft orientation /// </summary> private intersectionside forcedIntersectWay(int way1No, int way2No, IntersectionNode intersection, LineSegment segment1, LineSegment segment2, Vector3 fwd1, Vector3 fwd2, bool leftright) { int highwayIndex1 = getHighwayIndex(intersection.wayIds[way1No]); int highwayIndex2 = getHighwayIndex(intersection.wayIds[way2No]); Vector2 intersect = new Vector2(); if(leftright) { Geometry.getVectorIntersection(ref intersect, segment1.Left1, segment1.Left2, fwd1, segment2.Right1, segment2.Right2, fwd2); if (intersect.magnitude == 0) throw new System.InvalidOperationException("Intersection Failed!"); Vector3 newVertex = new Vector3(intersect.x, terrain.getTerrainHeight2(intersect.y + terrain.terrainInfo.shiftZ, intersect.x + terrain.terrainInfo.shiftX), intersect.y); applyIntersection(way1No, way2No, highwayIndex1, highwayIndex2, newVertex, intersection, true); intersectionside s = new intersectionside(); s.vertex = newVertex; s.leftright = leftright; s.success = true; return s; } else { Geometry.getVectorIntersection(ref intersect, segment1.Right1, segment1.Right2, fwd1, segment2.Left1, segment2.Left2, fwd2); if (intersect.magnitude == 0) throw new System.InvalidOperationException("Intersection Failed!"); Vector3 newVertex = new Vector3(intersect.x, terrain.getTerrainHeight2(intersect.y + terrain.terrainInfo.shiftZ, intersect.x + terrain.terrainInfo.shiftX), intersect.y); applyIntersection(way1No, way2No, highwayIndex1, highwayIndex2, newVertex, intersection, false); intersectionside s = new intersectionside(); s.vertex = newVertex; s.leftright = leftright; s.success = true; return s; } }
//public InputField SpeedIn; void Start() { _selected = MainManager.Main.LastSelectedGameObject.GetComponent <IntersectionNode>(); _lightSwitchRate = _selected.LightSwitchingRate; //_speedLimit = _selected.SpeedLimit; }