public List <Vector3> FindPath(Vector3 from, Vector3 to, int bodysize) { List <Vector3> result = new List <Vector3>(); CellPos start, end; ICellMapView view = cell_manager_.GetCellMapView(bodysize); CellPos start0; view.GetCell(from, out start0.row, out start0.col); view.GetCell(to, out end.row, out end.col); start = view.GetFirstWalkableCell(from, to); if (start.row >= 0 && start.col >= 0) { List <CellPos> path = PathFinder.FindPath(cell_manager_.GetCellMapView(bodysize), start, end); if (start0.row != start.row || start0.col != start.col) { result.Add(view.GetCellCenter(start0.row, start0.col)); } for (int ix = path.Count - 1; ix >= 0; --ix) { CellPos pos = path[ix]; result.Add(view.GetCellCenter(pos.row, pos.col)); } } else { result.Add(view.GetCellCenter(start0.row, start0.col)); result.Add(view.GetCellCenter(end.row, end.col)); } return(result); }
public List <Vector3> FindPath(Vector3 from, Vector3 to, int scope, int bodysize) { List <Vector3> result = new List <Vector3>(); CellPos start = new CellPos(), end = new CellPos(), minCell = new CellPos(), maxCell = new CellPos(); ICellMapView view = cell_manager_.GetCellMapView(bodysize); CellPos start0 = new CellPos(); view.GetCell(from, out start0.row, out start0.col); view.GetCell(to, out end.row, out end.col); start = view.GetFirstWalkableCell(from, to); if (start.row >= 0 && start.col >= 0) { minCell.row = Math.Min(start.row, end.row) - scope; minCell.col = Math.Min(start.col, end.col) - scope; maxCell.row = Math.Max(start.row, end.row) + scope; maxCell.col = Math.Max(start.col, end.col) + scope; if (minCell.row < 0) { minCell.row = 0; } if (minCell.col < 0) { minCell.col = 0; } if (maxCell.row >= view.MaxRowCount) { maxCell.row = view.MaxRowCount - 1; } if (maxCell.col >= view.MaxColCount) { maxCell.col = view.MaxColCount - 1; } List <CellPos> path = PathFinder.FindPath(cell_manager_.GetCellMapView(bodysize), start, end, minCell, maxCell); if (start0.row != start.row || start0.col != start.col) { result.Add(view.GetCellCenter(start0.row, start0.col)); } for (int ix = path.Count - 1; ix >= 0; --ix) { CellPos pos = path[ix]; result.Add(view.GetCellCenter(pos.row, pos.col)); } } return(result); }
public static CellPos GetFirstWalkableCell(ICellMapView view, Vector3 from, Vector3 to) { CellPos start = new CellPos(-1, -1), end = new CellPos(-1, -1), ret = new CellPos(-1, -1); view.GetCell(from, out start.row, out start.col); if (view.CanPass(start.row, start.col)) { ret = start; } else { view.GetCell(to, out end.row, out end.col); from = view.GetCellCenter(start.row, start.col); to = view.GetCellCenter(end.row, end.col); from.Y = 0; to.Y = 0; float step = view.RadiusLength * 2; float angle = Geometry.GetYAngle(new Vector2(from.X, from.Z), new Vector2(to.X, to.Z)); float xstep = step * (float)Math.Sin(angle); float zstep = step * (float)Math.Cos(angle); int count = (int)((to - from).Length() / step); for (int ix = 0; ix < count; ++ix) { from.X += xstep; from.Z += zstep; int row, col; view.GetCell(from, out row, out col); if (view.CanPass(row, col)) { ret.row = row; ret.col = col; break; } } } return(ret); }
public static bool IsWalkable(ICellMapView view, Vector3 srcPos, Vector3 dir) { int row = 0; int col = 0; view.GetCell(srcPos, out row, out col); float r = view.RadiusLength * 2; Vector3 targetPos = view.GetCellCenter(row, col); dir.Normalize(); targetPos.X += r * dir.X; targetPos.Z += r * dir.Z; int oRow = 0; int oCol = 0; view.GetCell(targetPos, out oRow, out oCol); bool ret = true; if (oRow != row || oCol != col) { ret = view.CanPass(oRow, oCol); } return(ret); }
public static bool GetWalkablePosition(ICellMapView view, Vector3 targetPos, Vector3 srcPos, ref Vector3 pos) { bool ret = false; const int c_MaxCheckCells = 3; int row = 0; int col = 0; view.GetCell(targetPos, out row, out col); float radian = Geometry.GetYAngle(new Vector2(targetPos.X, targetPos.Z), new Vector2(srcPos.X, srcPos.Z)); if (radian >= Math.PI / 4 && radian < Math.PI * 3 / 4)//右边 { for (int ci = 1; ci <= c_MaxCheckCells; ++ci) { for (int ri = 0; ri <= c_MaxCheckCells; ++ri) { int row_ = row + ri; int col_ = col + ci; if (view.IsCellValid(row_, col_)) { if (view.CanPass(row_, col_)) { pos = view.GetCellCenter(row_, col_); ret = true; goto exit; } } if (ri > 0) { row_ = row - ri; if (view.IsCellValid(row_, col_)) { if (view.CanPass(row_, col_)) { pos = view.GetCellCenter(row_, col_); ret = true; goto exit; } } } } } } else if (radian >= Math.PI * 3 / 4 && radian < Math.PI * 5 / 4)//上边 { for (int ri = 1; ri <= c_MaxCheckCells; ++ri) { for (int ci = 0; ci <= c_MaxCheckCells; ++ci) { int row_ = row - ri; int col_ = col + ci; if (view.IsCellValid(row_, col_)) { if (view.CanPass(row_, col_)) { pos = view.GetCellCenter(row_, col_); ret = true; goto exit; } } if (ci > 0) { col_ = col - ci; if (view.IsCellValid(row_, col_)) { if (view.CanPass(row_, col_)) { pos = view.GetCellCenter(row_, col_); ret = true; goto exit; } } } } } } else if (radian >= Math.PI * 5 / 4 && radian < Math.PI * 7 / 4)//左边 { for (int ci = 1; ci <= c_MaxCheckCells; ++ci) { for (int ri = 0; ri <= c_MaxCheckCells; ++ri) { int row_ = row + ri; int col_ = col - ci; if (view.IsCellValid(row_, col_)) { if (view.CanPass(row_, col_)) { pos = view.GetCellCenter(row_, col_); ret = true; goto exit; } } if (ri > 0) { row_ = row - ri; if (view.IsCellValid(row_, col_)) { if (view.CanPass(row_, col_)) { pos = view.GetCellCenter(row_, col_); ret = true; goto exit; } } } } } } else//下边 { for (int ri = 1; ri <= c_MaxCheckCells; ++ri) { for (int ci = 0; ci <= c_MaxCheckCells; ++ci) { int row_ = row + ri; int col_ = col + ci; if (view.IsCellValid(row_, col_)) { if (view.CanPass(row_, col_)) { pos = view.GetCellCenter(row_, col_); ret = true; goto exit; } } if (ci > 0) { col_ = col - ci; if (view.IsCellValid(row_, col_)) { if (view.CanPass(row_, col_)) { pos = view.GetCellCenter(row_, col_); ret = true; goto exit; } } } } } } exit: return(ret); }