/// <summary> /// Travels toward next Node depending on the PlayerTravelSpeed /// </summary> /// <returns></returns> private IEnumerator TravelToNextCrossRoads() { IsTraveling = true; Vector3 vecToNextNode = NextNode.Position + PlayerOffset - this.transform.position; while (vecToNextNode.sqrMagnitude > EPSILON) { // while we have not reached the next node, continue traveling to the next node this.transform.position += vecToNextNode.normalized * Time.deltaTime * PlayerTravelSpeed; vecToNextNode = NextNode.Position + PlayerOffset - this.transform.position; yield return(null); } // if we have reached next node, but it isn't a crossroads or a dead end --> continue if (2 == NextNode.Neighbours.Count) { // set the next node to NOT be the current node HighwayNode newNextNode = CurrentNode == NextNode.Neighbours[0] ? NextNode.Neighbours[1] : NextNode.Neighbours[0]; CurrentNode = NextNode; NextNode = newNextNode; // Continue with the traveling StartCoroutine(TurnToNextNode()); StartCoroutine(TravelToNextCrossRoads()); } else { // if we reach deadend or crossroads, turn to the next node that's nearest to the forward direction CurrentNode = NextNode; IsTraveling = false; } yield return(null); }
private void ConnectHighwayNodesFromWay(OSMWay osmWay) { if (osmWay.Visible && osmWay.NodeIDs.Count > 0) { HighwayNode currentNode = NavInfo.Nodes[osmWay.NodeIDs[0]]; for (int i = 1; i < osmWay.NodeIDs.Count; ++i) { HighwayNode nextNode = NavInfo.Nodes[osmWay.NodeIDs[i]]; currentNode.Neighbours.Add(nextNode); nextNode.Neighbours.Add(currentNode); currentNode = nextNode; } } }
/// <summary> /// For up and down neighbour finding we are searching for the highest dot product on the up / down axis /// </summary> /// <param name="axisToSearchOn"></param> private void FindNewNextNodeVertical(Vector3 axisToSearchOn) { if (CurrentNode.Neighbours.Count < 1) { return; } HighwayNode nearestNeighbour = CurrentNode.Neighbours[0]; float currentBestDotProduct = 0.0f; foreach (HighwayNode neighbour in CurrentNode.Neighbours) { Vector3 toNeighBour = (neighbour.Position - CurrentNode.Position).normalized; float dotOnSearchAxis = Vector3.Dot(axisToSearchOn, toNeighBour); if (dotOnSearchAxis > 0.0f && dotOnSearchAxis > currentBestDotProduct) { nearestNeighbour = neighbour; currentBestDotProduct = dotOnSearchAxis; } } NextNode = nearestNeighbour; }
private void CreateHighwayNodesFromWay(OSMWay osmWay) { foreach (ulong nodeID in osmWay.NodeIDs) { HighwayNode highwayNode; NavInfo.Nodes.TryGetValue(nodeID, out highwayNode); if (null == highwayNode) { OSMNode osmNode = MapInfo.Nodes[nodeID]; highwayNode = new HighwayNode(osmNode, MapInfo.Bounds.Center, osmWay.Name); NavInfo.Nodes[highwayNode.ID] = highwayNode; } else { if (highwayNode.Name.Equals("") && !osmWay.Name.Equals("")) { highwayNode.Name = osmWay.Name; NavInfo.Nodes[highwayNode.ID] = highwayNode; } } } }
private IEnumerator Start() { while (!MapReader.IsReady) { yield return(null); } // Choose random first node List <HighwayNode> nodes = Enumerable.ToList(MapReader.NavInfo.Nodes.Values); int size = MapReader.NavInfo.Nodes.Count; Random.InitState(RandomSeed); CurrentNode = nodes[Random.Range(0, size)]; // choose random next node int neighbourCount = CurrentNode.Neighbours.Count; NextNode = CurrentNode.Neighbours[Random.Range(0, neighbourCount)]; TurnForward(); this.transform.position = CurrentNode.Position + PlayerOffset; }