void Update() { GroundGrid gridScript = floorGrid.GetComponent <GroundGrid>(); if (gridScript.grid != null) { currentOver = SectionFromWorldPoint(transform.position, gridScript.grid, gridScript.gridSize); if (previousOver != currentOver) { if (previousOver != null) { previousOver.overHead = false; } previousOver = currentOver; } currentOver.overHead = true; if (transform.position.y - currentOver.position.y < 0.5 && currentOver.occupied == false) { LockPosition(currentOver); } else if (transform.position.y - currentOver.position.y < 1 && currentOver.occupied == true) { buildingCollide(currentOver); } } }
void Start() { floorGrid = GameObject.Find("GroundGrid"); floorGridScript = floorGrid.GetComponent <GroundGrid>(); gridSize = floorGridScript.gridSize; currentGridPositionX = gridSize / 2; currentGridPositionY = gridSize / 2; }
} // Instance of this grid public void Start() { // Singleton instance if (Instance == null) { Instance = this; } else if (Instance != this) { Destroy(this); } }
// Start is called before the first frame update void Start() { if (groundReference == null) { Debug.LogError("Ground reference NOT SET!"); } grid = groundReference.GetComponent <GroundGrid>(); GroundNode nodeStart = grid.GetStartingNode(); currentPlayerPosition = nodeStart.gridPosition; transform.position = new Vector3(nodeStart.worldPosition.x, 10, nodeStart.worldPosition.z); }
static bool RRT_StepTowards(int start, int end, ref int newNode, GroundGrid groundGrid, Actor actor) { //Only able to move one square (in any direction) in a single step float epsilon = 1.45f * 100 / groundGrid.Columns; float dist = Mathf.Min(epsilon, Vector3.Distance(groundGrid.GetNodePosition(start), groundGrid.GetNodePosition(end))); RaycastHit hit; if (Physics.Raycast(groundGrid.GetNodePosition(start), groundGrid.GetNodePosition(end) - groundGrid.GetNodePosition(start), out hit, dist, 1 << LayerMask.NameToLayer("Obstacles"))) { //Obstacle between start and end nodes newNode = -1; return(false); } RaycastHit[] hits; hits = Physics.RaycastAll(groundGrid.GetNodePosition(start), groundGrid.GetNodePosition(end) - groundGrid.GetNodePosition(start), dist, 1 << LayerMask.NameToLayer("Node")); float maxDist = 0.0f; for (int i = 0; i < hits.Length; ++i) { Node curNode = hits[i].collider.GetComponent <Node>(); if (curNode.IsOccupied()) { //Occupied node between start and end nodes newNode = -1; return(false); } if (hits[i].distance > maxDist) { maxDist = hits[i].distance; newNode = curNode.ID; } } actor.transform.position = groundGrid.GetNodePosition(start); int numSteps = 5; for (int step = 0; step < numSteps; ++step) { actor.transform.position = Vector3.Lerp(groundGrid.GetNodePosition(start), groundGrid.GetNodePosition(newNode), (float)step / (numSteps - 1)); Collider[] colliderHits = Physics.OverlapBox(actor.transform.position, actor.GetComponent <Collider>().bounds.extents, actor.transform.rotation, 1 << LayerMask.NameToLayer("Obstacles")); for (int i = 0; i < colliderHits.Length; ++i) { //Actor intersects an obstacle on its path from the start to end node newNode = -1; return(false); } } return(true); }
static int RRT_GetNearestNeighbor(int randNode, List <int> nodes, GroundGrid groundGrid) { //TODO: Make sure the nearest neighbor which is selected has Line-of-Sight to the selected randNode int nearestNeighbor = nodes.ElementAt(0); float minDist = Vector3.Distance(groundGrid.GetNodePosition(nearestNeighbor), groundGrid.GetNodePosition(randNode)); for (int n = 0; n < nodes.Count; ++n) { float newDist = Vector3.Distance(groundGrid.GetNodePosition(nodes.ElementAt(n)), groundGrid.GetNodePosition(randNode)); if (newDist < minDist) { minDist = newDist; nearestNeighbor = nodes.ElementAt(n); } } return(nearestNeighbor); }
/// <summary> /// 合并室内栅格的场强 /// </summary> /// <param name="curGrid3D">入地点栅格</param> /// <param name="rays"></param> /// <param name="Pwr"></param> /// <param name="buildingID"></param> public void mergeIndoorGridStrength(Grid3D curGrid3D, List <NodeInfo> rays, double Pwr, int buildingID) { GridStrength gs; string key = String.Format("{0},{1},{2}", curGrid3D.gxid, curGrid3D.gyid, curGrid3D.gzid); if (this.gridStrengths.ContainsKey(key)) { gs = this.gridStrengths[key]; gs.TransmitBuildingID += buildingID.ToString(); updateBuildingID(ref gs, rays); gs.TransNum += 1; gs.TransPwrW += Pwr; gs.MaxTransPwrW = Math.Max(gs.MaxTransPwrW, Pwr); this.gridStrengths[key] = gs; } else { gs = new GridStrength(); gs.GXID = curGrid3D.gxid; gs.GYID = curGrid3D.gyid; gs.Level = curGrid3D.gzid; gs.RefBuildingID = ""; gs.DiffBuildingID = ""; gs.TransmitBuildingID = buildingID.ToString(); gs.GCenter = GroundGrid.getBGridCenter(curGrid3D.gxid, curGrid3D.gyid, curGrid3D.gzid); if (gs.GCenter == null) { gs.GCenter = new Point(-1, -1, -1); } updateBuildingID(ref gs, rays); gs.TransNum = 1; gs.ReceivedPowerW = gs.MaxTransPwrW = gs.TransPwrW = Pwr; this.gridStrengths.Add(key, gs); } }
public static IEnumerator A_Star(int startNode, int endNode, GroundGrid groundGrid, List <int> outPath) { int numNodes = groundGrid.AdjacencyMatrix.GetLength(0); int numNeighbors = groundGrid.AdjacencyMatrix.GetLength(1); //Debug.Log("num nodes: " + numNodes); //Debug.Log("num neighbors: " + numNeighbors); List <int> toVisit = new List <int>(); List <int> visited = new List <int>(); int[] parent = new int[numNodes]; float[] gCost = new float[numNodes]; float[] fCost = new float[numNodes]; for (int i = 0; i < numNodes; ++i) { parent[i] = -1; fCost[i] = gCost[i] = float.PositiveInfinity; } toVisit.Add(startNode); gCost[startNode] = 0.0f; fCost[startNode] = groundGrid.Heuristic(startNode, endNode); int count = 0; while (toVisit.Any()) { int cur = A_Star_getLowestCost(toVisit, fCost); if (cur == endNode) { foreach (int item in CreatePathFromParent(endNode, parent)) { outPath.Add(item); } groundGrid.DisplayPath(outPath); yield break; } toVisit.Remove(cur); visited.Add(cur); groundGrid.SetNodeExplored(cur, true); for (int i = 0; i < numNeighbors; ++i) { int neighbor = groundGrid.ConvertNeighborIndexToNodeIndex(cur, i); if (groundGrid.AdjacencyMatrix[cur, i] == Mathf.Infinity || visited.Contains(neighbor) || neighbor == cur || neighbor == -1) { continue; } float gScore = gCost[cur] + groundGrid.AdjacencyMatrix[cur, i]; if (gScore < gCost[neighbor]) { parent[neighbor] = cur; gCost[neighbor] = gScore; fCost[neighbor] = gCost[neighbor] + groundGrid.Heuristic(neighbor, endNode); if (!toVisit.Contains(neighbor)) { toVisit.Add(neighbor); } } } count++; const int nodesExploredPerFrame = 50; if (count >= nodesExploredPerFrame) { count -= nodesExploredPerFrame; yield return(null); } } }
public static IEnumerator RRT(int startNode, int endNode, GroundGrid groundGrid, Actor actor, List <int> outPath) { outPath.Clear(); foreach (Transform child in groundGrid.transform) { if (child.name == "Line") { GameObject.Destroy(child.gameObject); } } int numNodes = groundGrid.Rows * groundGrid.Columns; const int numAttempts = 2000; int[] parent = new int[numNodes]; List <int> possibleNodesToPick = new List <int>(); for (int i = 0; i < numNodes; ++i) { if (!groundGrid.NodeIsOccupied(i)) { possibleNodesToPick.Add(i); } parent[i] = -1; } List <int> nodes = new List <int>(); nodes.Add(startNode); possibleNodesToPick.Remove(startNode); for (int i = 0; i < numAttempts; ++i) { if (possibleNodesToPick.Count == 0) { yield break; } int randNode = RRT_GetRandomState(endNode, possibleNodesToPick); int nearestNeighbor = RRT_GetNearestNeighbor(randNode, nodes, groundGrid); //Select input to use //For now any input is valid //Determine new state int newNode = -1; if (RRT_StepTowards(nearestNeighbor, randNode, ref newNode, groundGrid, actor)) { possibleNodesToPick.Remove(newNode); nodes.Add(newNode); DrawLine(groundGrid.GetNodePosition(nearestNeighbor), groundGrid.GetNodePosition(newNode), Color.red, groundGrid.transform, -1); parent[newNode] = nearestNeighbor; if (newNode == endNode) { foreach (int item in CreatePathFromParent(endNode, parent)) { outPath.Add(item); } groundGrid.DisplayPath(outPath); yield break; } groundGrid.SetNodeExplored(newNode, true); } else { continue; } yield return(null); } yield break; }
void Start() { grid = FindObjectOfType <GroundGrid>(); TextManager.instance.UpdateMaterials(materialsCount); }
private void Awake() { groundGrid = FindObjectOfType <GroundGrid>(); toolBelt = FindObjectOfType <ToolBelt>(); }