/// <summary> /// Get the angle of the road /// </summary> /// <param name="roadNetworkNode">The main node</param> /// <param name="index">Index of the road</param> /// <returns>The angle of the road</returns> public static float GetAngleOfRoadClampped(RoadNetworkNode roadNetworkNode, int index) { GameObject StartObj = roadNetworkNode.gameObject; GameObject EndObj = roadNetworkNode.Details.Roads[index].gameObject; Vector3 startPosition = StartObj.transform.position; Vector3 endPosition = EndObj.transform.position; Vector3 diff = startPosition - endPosition; return(MathsHelper.ClampAngle(MathsHelper.GetAngleFrom(diff.x, diff.z))); }
private void AddAngle(Guid id, RoadNetworkNode node) { RoadCrossSection road = IntersectionManager.Instance[id]; float angle = MathsHelper.ClampAngle(road.Angle); while (_angles.ContainsKey(angle)) { angle = angle + 0.001f; Debug.Log("Overlap node " + node.name); } _angles.Add(angle, road); _nodes.Add(angle, node); _orderedAngles.Add(angle); }
/// <summary> /// Standared constructor /// </summary> /// <param name="roadIds">The list of ids</param> /// <param name="roadNodes">The list of roads</param> public RoadFiveRoadOrder(List <Guid> roadIds, List <RoadNetworkNode> roadNodes) { _orderedAngles = new List <float>(); _angles = new Dictionary <float, RoadCrossSection>(); _nodes = new Dictionary <float, RoadNetworkNode>(); for (int i = 0; i < roadIds.Count; i++) { RoadCrossSection road = IntersectionManager.Instance[roadIds[i]]; float angle = MathsHelper.ClampAngle(road.Angle); _angles.Add(angle, road); _nodes.Add(angle, roadNodes[i]); _orderedAngles.Add(angle); } _orderedAngles.Sort(); }
/// <summary> /// Modify the terrain for the corner /// </summary> /// <param name="sections">The number of sections</param> /// <param name="tm">The terrain modifier</param> private void CreateCornerTerrain(int sections, TerrainModifier tm) { // this is not the best way - but it's a start // maybe have a function that returns a list of cross sections // find the point where the two roads meet Vector3 pos = _roadNetworkNode.gameObject.transform.position; // create a corne at this postion Vector3 newpos = pos; float roadAngleA = RoadUnionHelper.AngleClamp(RoadUnionHelper.GetAngleOfRoadClampped(_roadNetworkNode, 0) - Mathf.PI / 2); float roadAngleB = RoadUnionHelper.AngleClamp(RoadUnionHelper.GetAngleOfRoadClampped(_roadNetworkNode, 1) + Mathf.PI / 2); float roadAngleDifference = roadAngleB - roadAngleA; float couveSize = 1.85f; float x, z; float road_A_length = GetLengthOfRoad(0); float road_B_length = GetLengthOfRoad(1); float minLength = Mathf.Min(road_A_length, road_B_length); float offSetDownRoad = RoadConstructorHelper.CrossSectionDetails.RoadWidthValue * couveSize; if (offSetDownRoad > minLength / 2) { offSetDownRoad = minLength / 2; } RoadNetworkNode oppositeEnd = _roadNetworkNode.Details.Roads[1].GetComponent <RoadNetworkNode>(); Vector3 roadPointA = oppositeEnd.GetOffSetDownRoad(pos, (offSetDownRoad)); Vector3 outA; bool offSetPos = _roadNetworkNode.GetInnerCorner(0, 1, (offSetDownRoad), out outA); Vector3 CornerPoint = outA; newpos = CornerPoint; // get the gap form point to point Vector3 gap = CornerPoint - roadPointA; couveSize = offSetPos ? gap.magnitude : 0; couveSize = offSetPos ? gap.magnitude : 0; Radian currentAngle = new Radian(roadAngleA - (float)(Math.PI / 2)); roadAngleDifference = MathsHelper.ClampAngle(roadAngleDifference); if (roadAngleDifference > Mathf.PI) { roadAngleDifference = (Mathf.PI * 2) - roadAngleDifference; currentAngle = new Radian(roadAngleB + (float)(Math.PI / 2)); } float diff = roadAngleDifference; x = Mathf.Sin(currentAngle.Value) * (couveSize); z = Mathf.Cos(currentAngle.Value) * (couveSize); newpos.x -= x; newpos.z += z; ICrossSection crossSectionMiddle = RoadConstructorHelper.CrossSection(_roadNetworkNode); IMaterialFrequency materialFrequency = _roadNetworkNode.gameObject.GetComponent <IMaterialFrequency>(); if (materialFrequency == null) { materialFrequency = RoadConstructorHelper.MaterialFrequencySet; } RoadCrossSection rA = new RoadCrossSection(newpos, currentAngle.Value, crossSectionMiddle, materialFrequency); float angleStep = Mathf.Abs(diff / sections); for (int i = 0; i < sections; i++) { newpos = CornerPoint; currentAngle.Value += angleStep; x = Mathf.Sin(currentAngle.Value) * (couveSize); z = Mathf.Cos(currentAngle.Value) * (couveSize); newpos.x -= x; newpos.z += z; RoadCrossSection rB = new RoadCrossSection(newpos, currentAngle.Value, crossSectionMiddle, materialFrequency); tm.ApplyToTerrain(rA, rB); rA = rB; } RoadConstructorHelper.ApplyLeadingStrights(_roadNetworkNode, tm, 0); RoadConstructorHelper.ApplyLeadingStrights(_roadNetworkNode, tm, 1); }
/// <summary> /// Create the road object /// </summary> /// <param name="roadObject">The base object to add the road to</param> /// <param name="sections">The number of sections to use for this road</param> private void CreateCorner(RoadBuilder roadObject, int sections) { // find the point where the two roads meet Vector3 pos = _roadNetworkNode.gameObject.transform.position; // create a corne at this postion Vector3 newpos = pos; float roadAngleA = RoadUnionHelper.AngleClamp(RoadUnionHelper.GetAngleOfRoadClampped(_roadNetworkNode, 0) - (Mathf.PI / 2)); float roadAngleB = RoadUnionHelper.AngleClamp(RoadUnionHelper.GetAngleOfRoadClampped(_roadNetworkNode, 1) + (Mathf.PI / 2)); float roadAngleDifference = roadAngleB - roadAngleA; float couveSize = 1.85f; float x, z; float road_A_length = GetLengthOfRoad(0); float road_B_length = GetLengthOfRoad(1); float minLength = Mathf.Min(road_A_length, road_B_length); float offSetDownRoad = RoadConstructorHelper.CrossSectionDetails.RoadWidthValue * couveSize; if (offSetDownRoad > minLength / 2) { offSetDownRoad = minLength / 2; } RoadNetworkNode oppositeEnd = _roadNetworkNode.Details.Roads[1].GetComponent <RoadNetworkNode>(); Vector3 roadPointA = oppositeEnd.GetOffSetDownRoad(pos, (offSetDownRoad)); Vector3 outA; bool offSetPos = _roadNetworkNode.GetInnerCorner(0, 1, (offSetDownRoad), out outA); Vector3 CornerPoint = outA; newpos = CornerPoint; // get the gap form point to point Vector3 gap = CornerPoint - roadPointA; couveSize = offSetPos ? gap.magnitude : 0; couveSize = offSetPos ? gap.magnitude : 0; bool backward = false; Radian currentAngle = new Radian(roadAngleA - (float)(Math.PI / 2)); roadAngleDifference = MathsHelper.ClampAngle(roadAngleDifference); if (roadAngleDifference > Mathf.PI) { roadAngleDifference = (Mathf.PI * 2) - roadAngleDifference; currentAngle = new Radian(roadAngleB + (float)(Math.PI / 2)); backward = true; } float diff = roadAngleDifference; x = Mathf.Sin(currentAngle.Value) * (couveSize); z = Mathf.Cos(currentAngle.Value) * (couveSize); newpos.x -= x; newpos.z += z; ICrossSection crossSectionMiddle = RoadConstructorHelper.CrossSection(_roadNetworkNode); IMaterialFrequency materialFrequency = _roadNetworkNode.gameObject.GetComponent <IMaterialFrequency>(); if (materialFrequency == null) { materialFrequency = RoadConstructorHelper.MaterialFrequencySet; } RoadCrossSection rA = new RoadCrossSection(newpos, currentAngle.Value, crossSectionMiddle, materialFrequency); RoadCrossSection Start = rA; float angleStep = Mathf.Abs(diff / sections); for (int i = 0; i < sections; i++) { newpos = CornerPoint; currentAngle.Value += angleStep; x = Mathf.Sin(currentAngle.Value) * (couveSize); z = Mathf.Cos(currentAngle.Value) * (couveSize); newpos.x -= x; newpos.z += z; RoadCrossSection rB = new RoadCrossSection(newpos, currentAngle.Value, crossSectionMiddle, materialFrequency); // TODO Sub divide corners - need to update before the add basic road! _meshSection.AddBasicRoad(IntersectionManager.Instance.AddLinkedIntersecions(rA, rB), RoadConstructorHelper.GetMainMaterial(materialFrequency), 0); rA = rB; } RoadCrossSection End = rA; if (backward) { StreetManager.Instance.AddStreetEnd(_roadNetworkNode.name, _roadNetworkNode.Details.Roads[0].name, crossSectionMiddle, materialFrequency, End); StreetManager.Instance.AddStreetEnd(_roadNetworkNode.name, _roadNetworkNode.Details.Roads[1].name, crossSectionMiddle, materialFrequency, Start); } else { StreetManager.Instance.AddStreetEnd(_roadNetworkNode.name, _roadNetworkNode.Details.Roads[1].name, crossSectionMiddle, materialFrequency, End); StreetManager.Instance.AddStreetEnd(_roadNetworkNode.name, _roadNetworkNode.Details.Roads[0].name, crossSectionMiddle, materialFrequency, Start); } }