private AbstractWayInfo[] AbstractWayInfosForMultiwayIntersection(Intersection intersection, Road[] connectedRoads) { // convert roads to vectos Vector3[] vectors = connectedRoads.Select(rd => IntersectionMath.getOutgoingVector(intersection, rd)).ToArray(); float[] widths = connectedRoads.Select(rd => rd.GetRoadWidth()).ToArray(); AbstractWayInfo[] infos; // handle when connectedRoads.Length == 3 << build a four-way intersection instead if (connectedRoads.Length == 3) { // find the vector whose direction is of greatest difference to other two Vector3 maxVector = vectors[0]; float maxRoadWidth = widths[0]; float maxAngles = 0; for (int i = 0; i < 3; i++) { float accum = 0; for (int j = 0; j < 3; j++) { float zeroTo180Angle = Vector3.Angle(vectors[i], vectors[j]); float distance = zeroTo180Angle > 90 ? 180 - zeroTo180Angle : zeroTo180Angle; accum += distance; } if (accum > maxAngles) { maxVector = vectors[i]; maxRoadWidth = widths[i]; maxAngles = accum; } } Vector3[] resultingVectors = { vectors[0], vectors[1], vectors[2], -maxVector }; float[] resultingWidths = { widths[0], widths[1], widths[2], maxRoadWidth }; infos = AbstractWayInfo.InfosWithDirectionsAndWidths(resultingVectors, resultingWidths); } else { infos = AbstractWayInfo.InfosWithDirectionsAndWidths(vectors, widths); } return(infos); }
private AbstractWayInfo[] AbstractWayInfosForTwoWayIntersection(Intersection intersection, Road road1, Road road2) { Vector3 vec1 = IntersectionMath.getOutgoingVector(intersection, road1).normalized; Vector3 vec2 = IntersectionMath.getOutgoingVector(intersection, road2).normalized; // create a four way intersection instead of a two way intersection if the angle is less than 90 Vector3[] vecs = null; float[] roadWidths = null; switch (GetTwoWayIntersectionType(intersection)) { case TwoWayIntersectionType.SKEW: vecs = new[] { vec1, vec2, -vec1, -vec2 }; roadWidths = new[] { road1.GetRoadWidth(), road2.GetRoadWidth(), road1.GetRoadWidth(), road2.GetRoadWidth() }; break; case TwoWayIntersectionType.TRANSITION: // create a halfway rotation Quaternion rotation = Quaternion.Euler(Quaternion.FromToRotation(vec1, vec2).eulerAngles / 2); // this is the virtual directional vector Vector3 newVec = rotation * vec1; // this is the virtual road width, set it to be the average of the two float newWidth = road1.GetRoadWidth() + road2.GetRoadWidth() / 2; vecs = new[] { vec1, vec2, newVec, -newVec }; roadWidths = new[] { road1.GetRoadWidth(), road2.GetRoadWidth(), newWidth, newWidth }; break; case TwoWayIntersectionType.SMOOTH: Debug.LogError("Code Error, this method is to be called without smooth transition"); break; } // zip vecs and roadWidths into abstract road infos AbstractWayInfo[] infos = AbstractWayInfo.InfosWithDirectionsAndWidths(vecs, roadWidths); return(infos); }