/// <summary> /// 生成最终的路径点 /// </summary> /// <param name="startPos">起始点</param> /// <param name="endPos">终点</param> /// <param name="triPathList">三角形路径列表</param> /// <param name="wayPoints">路径点</param> /// <param name="offSet">移动物体宽度</param> /// <returns></returns> private PathResCode CreateWayPoints(Vector2 startPos, Vector2 endPos , List<NavTriangle> triPathList, out List<Vector2> wayPoints, int offSet) { wayPoints = new List<Vector2>(); if (triPathList.Count == 0 || startPos == null || endPos == null) return PathResCode.Failed; // 保证从起点到终点的顺序 triPathList.Reverse(); // 保存出边编号 for (int i = 0; i < triPathList.Count; i++) { NavTriangle tri = triPathList[i]; if (i != triPathList.Count - 1) { NavTriangle nextTri = triPathList[i + 1]; tri.SetOutWallIndex(tri.GetWallIndex(nextTri.GetID())); } } wayPoints.Add(startPos); //起点与终点在同一三角形中 if (triPathList.Count == 1) { wayPoints.Add(endPos); return PathResCode.Success; } WayPoint way = new WayPoint(startPos, triPathList[0]); while (!NMath.IsEqualZero(way.GetPoint() - endPos)) { way = GetFurthestWayPoint(way, triPathList, endPos, offSet); if (way == null) return PathResCode.CanNotGetNextWayPoint; wayPoints.Add(way.GetPoint()); } return PathResCode.Success; }
/// <summary> /// 根据拐点计算法获得导航网格的下一个拐点 /// </summary> /// <param name="way"></param> /// <param name="triPathList"></param> /// <param name="endPos"></param> /// <param name="offSet"></param> /// <returns></returns> private WayPoint GetFurthestWayPoint(WayPoint way, List<NavTriangle> triPathList, Vector2 endPos, int offSet) { WayPoint nextWay = null; Vector2 currPnt = way.GetPoint(); NavTriangle currTri = way.GetTriangle(); NavTriangle lastTriA = currTri; NavTriangle lastTriB = currTri; int startIndex = triPathList.IndexOf(currTri);//开始路点所在的网格索引 Line2D outSide = currTri.GetSide(currTri.GetOutWallIndex());//路径线在网格中的穿出边? Vector2 lastPntA = outSide.GetStartPoint(); Vector2 lastPntB = outSide.GetEndPoint(); Line2D lastLineA = new Line2D(currPnt, lastPntA); Line2D lastLineB = new Line2D(currPnt, lastPntB); Vector2 testPntA, testPntB; for (int i = startIndex + 1; i < triPathList.Count; i++) { currTri = triPathList[i]; outSide = currTri.GetSide(currTri.GetOutWallIndex()); if (i == triPathList.Count - 1) { testPntA = endPos; testPntB = endPos; } else { testPntA = outSide.GetStartPoint(); testPntB = outSide.GetEndPoint(); } if (lastPntA != testPntA) { if (lastLineB.ClassifyPoint(testPntA) == PointSide.RIGHT_SIDE) { nextWay = new WayPoint(lastPntB, lastTriB); return nextWay; } else if (lastLineA.ClassifyPoint(testPntA) != PointSide.LEFT_SIDE) { lastPntA = testPntA; lastTriA = currTri; //重设直线 lastLineA = new Line2D(lastLineA.GetStartPoint(), lastPntA); } } if (lastPntB != testPntB) { if (lastLineA.ClassifyPoint(testPntB) == PointSide.LEFT_SIDE) { nextWay = new WayPoint(lastPntA, lastTriA); return nextWay; } else if (lastLineB.ClassifyPoint(testPntB) != PointSide.RIGHT_SIDE) { lastPntB = testPntB; lastTriB = currTri; //重设直线 lastLineB = new Line2D(lastLineB.GetStartPoint(), lastPntB); } } } //到达终点 nextWay = new WayPoint(endPos, triPathList[triPathList.Count - 1]); return nextWay; }
/// <summary> /// 根据拐点计算法获得导航网格的下一个拐点 /// </summary> /// <param name="way"></param> /// <param name="triPathList"></param> /// <param name="endPos"></param> /// <param name="offSet"></param> /// <returns></returns> private WayPoint GetFurthestWayPoint(WayPoint way, List <NavTriangle> triPathList, Vector2 endPos, int offSet) { WayPoint nextWay = null; Vector2 currPnt = way.GetPoint(); NavTriangle currTri = way.GetTriangle(); NavTriangle lastTriA = currTri; NavTriangle lastTriB = currTri; int startIndex = triPathList.IndexOf(currTri); //开始路点所在的网格索引 Line2D outSide = currTri.GetSide(currTri.GetOutWallIndex()); //路径线在网格中的穿出边? Vector2 lastPntA = outSide.GetStartPoint(); Vector2 lastPntB = outSide.GetEndPoint(); Line2D lastLineA = new Line2D(currPnt, lastPntA); Line2D lastLineB = new Line2D(currPnt, lastPntB); Vector2 testPntA, testPntB; for (int i = startIndex + 1; i < triPathList.Count; i++) { currTri = triPathList[i]; outSide = currTri.GetSide(currTri.GetOutWallIndex()); if (i == triPathList.Count - 1) { testPntA = endPos; testPntB = endPos; } else { testPntA = outSide.GetStartPoint(); testPntB = outSide.GetEndPoint(); } if (lastPntA != testPntA) { if (lastLineB.ClassifyPoint(testPntA) == PointSide.RIGHT_SIDE) { nextWay = new WayPoint(lastPntB, lastTriB); return(nextWay); } else if (lastLineA.ClassifyPoint(testPntA) != PointSide.LEFT_SIDE) { lastPntA = testPntA; lastTriA = currTri; //重设直线 lastLineA = new Line2D(lastLineA.GetStartPoint(), lastPntA); } } if (lastPntB != testPntB) { if (lastLineA.ClassifyPoint(testPntB) == PointSide.LEFT_SIDE) { nextWay = new WayPoint(lastPntA, lastTriA); return(nextWay); } else if (lastLineB.ClassifyPoint(testPntB) != PointSide.RIGHT_SIDE) { lastPntB = testPntB; lastTriB = currTri; //重设直线 lastLineB = new Line2D(lastLineB.GetStartPoint(), lastPntB); } } } //到达终点 nextWay = new WayPoint(endPos, triPathList[triPathList.Count - 1]); return(nextWay); }