public NavGraphNode FindNearNode(Vector2 pos) { Dictionary <int, float> magList = new Dictionary <int, float> (); List <Vector2> sort = new List <Vector2> (); int index = 0; float outValue; NavGraphNode findNode = null; foreach (NavGraphNode node in m_Nodes) { if (null != node) { magList.Add(index, (pos - node.Pos()).sqrMagnitude); } index++; } magList = magList.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value); foreach (int nodeNum in magList.Keys) { if (false == this.ClosedNode(nodeNum)) { findNode = m_Nodes.ElementAt(nodeNum) as NavGraphNode; break; } } return(findNode); }
public NodeToEdges(NavGraphNode node) { this.node = node; edges = new List <GraphEdge>(); node.SetActive(true); }
//public object ExtraInfo() {return m_ExtraInfo;} //public void SetExtraInfo(object info){m_ExtraInfo = info;} public override object Clone() { NavGraphNode node = base.Clone() as NavGraphNode; node.m_vPosition = this.m_vPosition; return((object)node); }
public override void Activate() { Status = Status.Active; NavGraphNode edge = _path.Pop(); AddSubgoal(new GoalTraverseEdge(owner: Owner, destination: edge.Position, isFinalEdge: _path.Count == 0)); }
private void CheckNodes(Graph <NavGraphNode, GraphEdge> graph, Graph <NavGraphNode, GraphEdge> loadedGraph, int i) { NavGraphNode expectedNode = graph.GetNode(i); NavGraphNode actualNode = loadedGraph.GetNode(i); Assert.AreEqual(expectedNode.Position, actualNode.Position); Assert.AreEqual(expectedNode.ExtraInfo, actualNode.ExtraInfo); }
/// <summary> /// 生成导航图的方法 /// </summary> void CreateMap() { var startPoint = GameObject.Find("StartPoint"); distanceBetweenDiagonal = Mathf.Sqrt(distanceBetweenNode * distanceBetweenNode + distanceBetweenNode * distanceBetweenNode); NavGraphNode startNode = new NavGraphNode(NavGraphNode.activeID++, startPoint.transform.position.x, startPoint.transform.position.y); NavGraph.AddNode(startNode); DebugController.instance.AddPoint(NavGraphNode.activeID - 1, startPoint); FloodFill(startNode); }
public NavGraphNode GetNodeAtPosition(Vector3 position) { NavGraphNode graphNode = null; var nodeView = nodeViews.SingleOrDefault(x => Vector3.Distance(x.transform.position, position) <= 0.001f); if (nodeView != null) { graphNode = m_graph.GetNode(nodeView.nodeIndex); } return(graphNode); }
public void AddPoint(NavGraphNode node) { var pointObject = GameObject.Instantiate(pointPrefab, new Vector3(node.Pos.x, node.Pos.y, -2), Quaternion.identity); pointObject.transform.parent = pointsParent; var component = pointObject.AddComponent(typeof(DebugPoint)) as DebugPoint; component.ID = node.ID; points.Add(component); }
//this method calculates all a target's neighbors and stores them in //the neighbor vector. After you have called this method use the begin, //next and end methods to iterate through the vector. public void CalculateNeighbors(Vector2 targetPos, float queryRadius) { if (neighbors_.Length <= 0) { Debug.LogError("CellSpacePartition::CalculateNeighbors: invalid neighbors since it is empty"); return; } //create an iterator and set it to the beginning of the neighbor vector int currNeighborIndex = 0; //NavGraphNode curNbor = neighbors_.begin(); //create the query box that is the bounding box of the target's query //area InvertedAABBox2D queryBox = new InvertedAABBox2D(targetPos - new Vector2(queryRadius, queryRadius), targetPos + new Vector2(queryRadius, queryRadius)); //iterate through each cell and test to see if its bounding box overlaps //with the query box. If it does and it also contains entities then //make further proximity tests. Cell currCell = null; NavGraphNode currNode = null; for (int i = 0; i < cells_.Count; ++i) { currCell = cells_[i]; //test to see if this cell contains members and if it overlaps the //query box if (currCell.bbox.isOverlappedWith(queryBox) && currCell.members.Count > 0) { //add any entities found within query radius to the neighbor list //std::list<NavGraphNode>::iterator it = currCell->memebers.begin(); for (int j = 0; j < currCell.members.Count; ++j) { currNode = currCell.members[j]; if ((currNode.Position() - targetPos).sqrMagnitude < queryRadius * queryRadius) { neighbors_[currNeighborIndex++] = currNode; //*curNbor++ = *it; } } } } //next cell //mark the end of the list with a zero. neighbors_[currNeighborIndex] = null; }
public int AddNode(NavGraphNode node) { if (node.Index < navNodes.Count) { navNodes[node.Index] = node; return nextNodeIndex; } else { navNodes.Add(node); nodeEdges.Add(nextNodeIndex, new List<Edge>()); return nextNodeIndex++; } }
//adds entities to the class by allocating them to the appropriate cell public void AddEntity(NavGraphNode node) { if (node == null) { Debug.LogError("CellSpacePartition::AddEntity: invalid node"); return; } //int sz = cells_.Count; int idx = PositionToIndex(node.Position()); if (idx >= cells_.Count) { Debug.LogError("CellSpacePartition::AddEntity: idx out of range! idx: " + idx + ", cells_.Count: " + cells_.Count); } cells_[idx].members.Add(node); }
//update an NavGraphNode's cell by calling this from your NavGraphNode's Update method public void UpdateEntity(NavGraphNode node, Vector2 oldPos) { //if the index for the old pos and the new pos are not equal then //the NavGraphNode has moved to another cell. int oldIdx = PositionToIndex(oldPos); int newIdx = PositionToIndex(node.Position()); if (newIdx == oldIdx) { return; } //the NavGraphNode has moved into another cell so delete from current cell //and add to new one cells_[oldIdx].members.Remove(node); cells_[newIdx].members.Add(node); }
/// </summary> public int SearchNonAlloc(Vector3 srcPos, Vector3 destPos, ref Stack <Vector3> pathPos) { NavGraphNode destNode = _graph.FindNearNode(destPos); NavGraphNode srcNode = _graph.FindNearNode(srcPos); NavGraphNode tempNode = null; if (null == pathPos) { return(0); } if (true == this.PossibleLinearMove(srcPos, destPos, GlobalConstants.Layer.Mask.building)) { if (null != pathPos) { pathPos.Clear(); pathPos.Push(destPos); } return(1); } _searchDFS.Init(_graph, srcNode.Index(), destNode.Index()); List <int> pathList = _searchDFS.GetPathToTarget(); //-------- chamto test -------- // string nodeChaine = "nodeChaine : "; // foreach (int node in pathList) // { // nodeChaine += node + "<-"; // } // Debug.Log (nodeChaine); //-------- ------------ -------- pathPos.Clear(); pathPos.Push(destPos); foreach (int node in pathList) { tempNode = _graph.GetNode(node) as NavGraphNode; pathPos.Push(tempNode.Pos()); } //pathPos.Push (srcPos); return(pathPos.Count); }
public void SetFromNode(NavGraphNode node) { var parent = this.GetComponentInParent <GraphView>(); gameObject.name = "Node (" + nodeIndex + ")"; gameObject.transform.position = new Vector3(node.position.x, node.position.y, node.position.z); gameObject.transform.localScale = new Vector3(1f - borderSize, 1f, 1f - borderSize); m_node = node; m_coordinate = new Vector2(xIndex, yIndex); nodeType = node.nodeType; nodeColor = parent.m_tileColors.GetNodeTypeColor(nodeType); xIndex = node.xIndex; yIndex = node.yIndex; EnableObject(arrow, true); }
/// <summary> /// 添加节点 /// </summary> public void AddNode(int id, float x, float y, float cost = 0) { if (id > tables.Count) { Debug.Log("AddNode中:要进行添加的节点的id号错误"); return; } NavGraphNode node = new NavGraphNode(id, x, y, cost); NodeToEdges t = new NodeToEdges(node); tables.Add(t); totalV++; activeV++; }
private NavGraphNode AddPointAndNode(NavGraphNode start, Vector2 pos, float ncost = 0, float ecost = 0) { NavGraphNode node = new NavGraphNode(NavGraphNode.activeID++, pos, ncost); NavGraph.AddNode(node); //将这个点添加到对应的单元中 cellSpacePartition.AddEntity(node, node.Pos, node.ID); //创建顶点时两个方向上都要有连线进行链接 NavGraph.AddEdge(start.ID, node.ID, ecost); DebugController.instance.AddPoint(node); DebugController.instance.AddLine(start.Pos, node.Pos); return(node); }
//adds a node to the graph and returns its index public int AddNode(NavGraphNode node) { if ( node.Index() < nodes_.Count ) { //make sure the client is not trying to add a node with the same ID as //a currently active node //DebugUtils.Assert( nodes_[node.GetIndex()].GetIndex() == NavGraphNode.invalid_node_index, "<NavGraph::AddNode>: Attempting to add a node with a duplicate ID" ); nodes_[node.Index()] = node; return nextNodeIndex_; } else { //make sure the new node has been indexed correctly DebugUtils.Assert( node.Index() == nextNodeIndex_, "<NavGraph::AddNode>:invalid index" ); nodes_.Add(node); edges_.Add(new List<NavGraphEdge>()); return nextNodeIndex_++; } }
public NavGraphNode GetNodeWithPos(Vector2 mapPos) { NavGraphNode currNode = null; NavGraphNode resultNode = null; for (int i = 0; i < nodes_.Count; ++i) { currNode = nodes_[i]; if ((int)currNode.Position().x == (int)mapPos.x && (int)currNode.Position().y == (int)mapPos.y) { resultNode = currNode; } } //if ( resultNode == null ) { // Debug.LogError ( "NavGraph::GetNodeWIthPos, node not find with position: " + mapPos.x + ", " + mapPos.y ); //} return(resultNode); }
//adds a node to the graph and returns its index public int AddNode(NavGraphNode node) { if (node.Index() < nodes_.Count) { //make sure the client is not trying to add a node with the same ID as //a currently active node //DebugUtils.Assert( nodes_[node.GetIndex()].GetIndex() == NavGraphNode.invalid_node_index, "<NavGraph::AddNode>: Attempting to add a node with a duplicate ID" ); nodes_[node.Index()] = node; return(nextNodeIndex_); } else { //make sure the new node has been indexed correctly DebugUtils.Assert(node.Index() == nextNodeIndex_, "<NavGraph::AddNode>:invalid index"); nodes_.Add(node); edges_.Add(new List <NavGraphEdge>()); return(nextNodeIndex_++); } }
public void AddNode(NavGraphNode node) { if (HasNode(node.ID)) { Debug.Log("要进行添加的顶点已经存在"); return; } if (node.ID > tables.Count) { Debug.Log("要进行添加的节点的id号错误"); return; } NodeToEdges t = new NodeToEdges(node); tables.Add(t); totalV++; activeV++; }
public Stack <Vector3> Search(Vector3 srcPos, Vector3 destPos) { NavGraphNode destNode = _graph.FindNearNode(destPos); NavGraphNode srcNode = _graph.FindNearNode(srcPos); NavGraphNode tempNode = null; Stack <Vector3> pathPos = new Stack <Vector3>(); if (true == this.PossibleLinearMove(srcPos, destPos, GlobalConstants.Layer.Mask.building)) { pathPos.Push(destPos); return(pathPos); } _searchDFS.Init(_graph, srcNode.Index(), destNode.Index()); List <int> pathList = _searchDFS.GetPathToTarget(); //-------- chamto test -------- string nodeChaine = "nodeChaine : "; foreach (int node in pathList) { nodeChaine += node + "<-"; } Debug.Log(nodeChaine); //-------- ------------ -------- pathPos.Push(destPos); foreach (int node in pathList) { tempNode = _graph.GetNode(node) as NavGraphNode; pathPos.Push(tempNode.Pos()); } //pathPos.Push (srcPos); return(pathPos); }
public int AddNode(NavGraphNode node) { return(m_sparseGraph.AddNode(node)); }
public override void Draw(SpriteBatch batch) { batch.Begin(transformMatrix: camera.GetViewMatrix()); foreach (Actor a in scene.GetActors()) { a.Draw(batch); } batch.End(); DrawFoliageWithAlphaMask(batch); foreach (ParticleEffectManager particleEffect in scene.ParticleEffects) { particleEffect.Draw(batch, camera); } scene.ParticleEffects.RemoveAll(pEffect => (pEffect.EffectDuration <= 0 && !pEffect.Continuous)); scene.LightningManager.Draw(batch); batch.Begin(transformMatrix: camera.GetViewMatrix()); //Debug code, TODO: REMOVE LATER SpriteFont debugFont = hud.fonts[1]; /*Draw outlines for each of the collidables * foreach (Collidable polygon in scene.GetCollidables()) * { * DrawPolygon(batch, polygon, Color.Red, false); * } * * //character Boundingbox * Collidable boundingBox = Polygon.RectangleToPolygon(character.CharacterPhysics.BoundingBox); * DrawPolygon(batch, boundingBox, Color.Green, false);*/ //Draw MinX and MaxX for each walkable polygon /*foreach (Collidable polygon in scene.WalkableCollidables) * { * DrawLine(batch, new Vector2(polygon.WalkableMinMax.X, polygon.MaxHeight), new Vector2(polygon.WalkableMinMax.Y, polygon.MinHeight), Color.Red); * } * * //Draw the same numbers for climbable collidables that are connected to each other (= ClimbGroup) * int counter = 0; * * foreach (ClimbGroup climbGroup in scene.NavigationGraph.CurrClimbGroup) * { * foreach (Collidable c in climbGroup.ClimbCollidables) * batch.DrawString(hud.defaultFont, counter.ToString(), new Vector2(c.Center.X, c.CenterYClimbGroup), Color.White); * * counter++; * } * * counter = 0; * //Draw the same numbers for platforms that are connected to each other via climbable polygon * foreach (NavGraphNode nav1 in scene.NavigationGraph.NavGraphNodes) * { * if (nav1.ReachableNodes.Count == 0) continue; * * * * foreach (var nav2 in nav1.ReachableNodes) * { * batch.DrawString(hud.defaultFont, counter.ToString(), nav2.Item2, Color.White); * batch.DrawString(hud.defaultFont, counter.ToString(), nav2.Item3, Color.White); * } * * counter++; * } * * foreach (NavGraphNode node in scene.NavigationGraph.NavGraphNodes) * { * DrawPolygon(batch, node.Platform, Color.White, true); * }*/ #if DEBUG // Draw the character's query point for the platforms for (int i = 0; i < scene.Characters.Count; i++) { DefaultCharacter c = (DefaultCharacter)scene.Characters[i]; if (c?.CharacterPhysics == null) { continue; } /*float tempX, tempY, tempWidth, tempHeight; * tempX = (float)Math.Round(c.CharacterPhysics.GetPosition().X + c.CharacterPhysics.hitboxOffset.X - 1, MidpointRounding.AwayFromZero); * tempY = (float)Math.Round(c.CharacterPhysics.GetPosition().Y + c.CharacterPhysics.hitboxOffset.Y - 1, MidpointRounding.AwayFromZero); * tempWidth = c.CharacterPhysics.BoundingBox.Width; * tempHeight = c.CharacterPhysics.BoundingBox.Height; * * // Point below character * Vector2 pos = new Vector2(tempX + 0.5f * tempWidth, tempY); * Vector2 p = new Vector2(pos.X, tempY + 1.5f * tempHeight); * * //batch.DrawString(hud.defaultFont, "o", p, Color.White); * DrawLine(batch, pos, p, Color.AntiqueWhite);*/ Collidable col = c.CharacterPhysics.QueryCurrentPlatform(); DrawPolygon(batch, col, Color.Blue, false, "p" + i); } // Debug Rendering of Navigational-Graph for (int i = 0; i < scene.NavigationGraph.NavGraphNodes.Length; i++) { NavGraphNode n = scene.NavigationGraph.NavGraphNodes[i]; DrawLine(batch, new Vector2(n.Platform.WalkableMinMax.X, n.Platform.MinHeight), new Vector2(n.Platform.WalkableMinMax.Y, n.Platform.MinHeight), Color.Yellow); batch.DrawString(debugFont, "n_" + i, n.Platform.Center + new Vector2(0, 10), Color.Orange); foreach (Tuple <NavGraphNode, Vector2, Vector2> t in n.ReachableNodes) { DrawLine(batch, t.Item2, t.Item3, Color.Orange); batch.DrawString(debugFont, "rn_" + i, t.Item2 + new Vector2(0, 0), Color.OrangeRed); } } // Debug Rendering of the Path of the Ai-Characters List <AiController> lc = scene.GetAiControllers(); for (int i = 0; i < lc.Count; i++) { AiIndigenController c = (AiIndigenController)lc[i]; if (c == null || c.Path.Count == 0) { continue; } Vector3 pos = c.PossesedCharacters[0].GetCenterPosition(); DrawLine(batch, pos, c.Path[0].Position, Color.White); batch.DrawString(debugFont, "w" + i, new Vector2(pos.X, pos.Y), Color.White); for (int j = 0; j < c.Path.Count - 1; j++) { DrawLine(batch, c.Path[j].Position, c.Path[j + 1].Position, Color.White); batch.DrawString(debugFont, (j + 1).ToString(), new Vector2(c.Path[j].Position.X, c.Path[j].Position.Y), Color.White); } } #endif hud.DrawHud(batch); if (scene.MatchPaused) { scene.MatchPauseScreen.Draw(batch); } batch.End(); }
//update an NavGraphNode's cell by calling this from your NavGraphNode's Update method public void UpdateEntity(NavGraphNode node, Vector2 oldPos) { //if the index for the old pos and the new pos are not equal then //the NavGraphNode has moved to another cell. int oldIdx = PositionToIndex(oldPos); int newIdx = PositionToIndex(node.Position()); if (newIdx == oldIdx) return; //the NavGraphNode has moved into another cell so delete from current cell //and add to new one cells_[oldIdx].members.Remove(node); cells_[newIdx].members.Add(node); }
//adds entities to the class by allocating them to the appropriate cell public void AddEntity(NavGraphNode node) { if ( node == null ) { Debug.LogError( "CellSpacePartition::AddEntity: invalid node" ); return ; } //int sz = cells_.Count; int idx = PositionToIndex(node.Position()); if ( idx >= cells_.Count ) { Debug.LogError ( "CellSpacePartition::AddEntity: idx out of range! idx: " + idx + ", cells_.Count: " + cells_.Count ); } cells_[idx].members.Add(node); }
/// <summary> /// (八向)洪水填充算法 /// </summary> private void FloodFill(NavGraphNode startPoint) { Queue <NavGraphNode> queue = new Queue <NavGraphNode>(); queue.Enqueue(startPoint); FillState state; int id = 0; while (queue.Count != 0) { var node = queue.Dequeue(); #region 进行八个方向的填充 //上方 if ((state = isFill(node.Pos, Vector2.up, distanceBetweenNode, ref id)) == FillState.Success) { var n = AddPointAndNode(node, node.Pos + up * distanceBetweenNode, 0, lowCost); queue.Enqueue(n); } else if (state == FillState.FailByOtherPoint) { if (id >= 0 && !navGraph.HasEdge(node.ID, id)) { NavGraph.AddEdge(node.ID, id, lowCost); //NavGraph.AddEdge(id, node.ID, lowCost); DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos); } } //下方 if ((state = isFill(node.Pos, Vector2.down, distanceBetweenNode, ref id)) == FillState.Success) { var n = AddPointAndNode(node, node.Pos + down * distanceBetweenNode, 0, lowCost); queue.Enqueue(n); } else if (state == FillState.FailByOtherPoint) { if (id >= 0 && !navGraph.HasEdge(node.ID, id)) { NavGraph.AddEdge(node.ID, id, lowCost); //NavGraph.AddEdge(id, node.ID, lowCost); DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos); } } //左边 if ((state = isFill(node.Pos, Vector2.left, distanceBetweenNode, ref id)) == FillState.Success) { var n = AddPointAndNode(node, node.Pos + left * distanceBetweenNode, 0, lowCost); queue.Enqueue(n); } else if (state == FillState.FailByOtherPoint) { if (id >= 0 && !navGraph.HasEdge(node.ID, id)) { NavGraph.AddEdge(node.ID, id, lowCost); //NavGraph.AddEdge(id, node.ID, lowCost); DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos); } } //右边 if ((state = isFill(node.Pos, Vector2.right, distanceBetweenNode, ref id)) == FillState.Success) { var n = AddPointAndNode(node, node.Pos + right * distanceBetweenNode, 0, lowCost); queue.Enqueue(n); } else if (state == FillState.FailByOtherPoint) { if (id >= 0 && !navGraph.HasEdge(node.ID, id)) { NavGraph.AddEdge(node.ID, id, lowCost); //NavGraph.AddEdge(id, node.ID, lowCost); DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos); } } //左上角 if ((state = isFill(node.Pos, Vector2.up + Vector2.left, distanceBetweenDiagonal, ref id)) == FillState.Success) { var n = AddPointAndNode(node, node.Pos + leftUp * distanceBetweenDiagonal, 0, heightCost); queue.Enqueue(n); } else if (state == FillState.FailByOtherPoint) { if (id >= 0 && !navGraph.HasEdge(node.ID, id)) { NavGraph.AddEdge(node.ID, id, heightCost); //NavGraph.AddEdge(id, node.ID, heightCost); DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos); } } //右上角 if ((state = isFill(node.Pos, Vector2.up + Vector2.right, distanceBetweenDiagonal, ref id)) == FillState.Success) { var n = AddPointAndNode(node, node.Pos + rightUp * distanceBetweenDiagonal, 0, heightCost); queue.Enqueue(n); } else if (state == FillState.FailByOtherPoint) { if (id >= 0 && !navGraph.HasEdge(node.ID, id)) { NavGraph.AddEdge(node.ID, id, heightCost); //NavGraph.AddEdge(id, node.ID, heightCost); DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos); } } //左下角 if ((state = isFill(node.Pos, Vector2.down + Vector2.left, distanceBetweenDiagonal, ref id)) == FillState.Success) { var n = AddPointAndNode(node, node.Pos + leftDown * distanceBetweenDiagonal, 0, heightCost); queue.Enqueue(n); } else if (state == FillState.FailByOtherPoint) { if (id >= 0 && !navGraph.HasEdge(node.ID, id)) { NavGraph.AddEdge(node.ID, id, heightCost); //NavGraph.AddEdge(id, node.ID, heightCost); DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos); } } //右下角 if ((state = isFill(node.Pos, Vector2.down + Vector2.right, distanceBetweenDiagonal, ref id)) == FillState.Success) { var n = AddPointAndNode(node, node.Pos + rightDown * distanceBetweenDiagonal, 0, heightCost); queue.Enqueue(n); } else if (state == FillState.FailByOtherPoint) { if (id >= 0 && !navGraph.HasEdge(node.ID, id)) { NavGraph.AddEdge(node.ID, id, heightCost); //NavGraph.AddEdge(id, node.ID, heightCost); DebugController.instance.AddLine(node.Pos, navGraph.GetNode(id).Pos); } } #endregion } }
public void UpdatePlayerNode() { playerNode = FindPlayerNode(); }