public SerializedNormalConnection(NavmeshLayserSerializer ns, GraphSerializer gs, CellContentGenericConnection connection) { interconnection = connection.interconnection; fromCell = ns.GetCellID(connection.from); connectedCell = ns.GetCellID(connection.connection); data = connection.cellData; intersection = connection.intersection; costFrom = connection.costFrom; costTo = connection.costTo; }
//called externaly cause PathFinder.gridSize can be zero in that case public static void Init() { Vector2 A = new Vector2(0, 0); Vector2 B = new Vector2(PathFinder.gridSize, 0); Vector2 C = new Vector2(0, PathFinder.gridSize); Vector2 D = new Vector2(PathFinder.gridSize, PathFinder.gridSize); raycastSamplesTemplate[0] = new CellContentData(C, A); raycastSamplesTemplate[1] = new CellContentData(D, C); raycastSamplesTemplate[2] = new CellContentData(B, D); raycastSamplesTemplate[3] = new CellContentData(A, B); }
private void Raycast(Vector3 origin, Vector3 direction, out RaycastHitNavMesh hit, float length, int maxIterations, Passability expectedPassability, Area expectedArea, Cell cell) { HashSet <CellContentData> raycastExclude = new HashSet <CellContentData>(); List <RaycastSomeData> raycastTempData = new List <RaycastSomeData>(); float maxLengthSqr = length * length; for (int iteration = 0; iteration < maxIterations; iteration++) { raycastTempData.Clear();//iteration data cleared foreach (var pair in cell.dataContentPairs) { CellContentData curData = pair.Key; if (!raycastExclude.Add(curData))//mean it's already contain this { continue; } Vector3 intersect; if (SomeMath.RayIntersectXZ(origin, direction, curData.leftV3, curData.rightV3, out intersect)) { if (pair.Value != null) { Cell otherCell = pair.Value.connection; if (otherCell == cell | !otherCell.canBeUsed) { continue; } if (cell.passability != otherCell.passability || cell.area != otherCell.area) { hit = new RaycastHitNavMesh(intersect, SomeMath.SqrDistance(origin, intersect) < maxLengthSqr);//!!! return; } raycastTempData.Add(new RaycastSomeData(intersect, otherCell)); } else { raycastTempData.Add(new RaycastSomeData(intersect, null)); } } } //check if there possible connection for (int i = 0; i < raycastTempData.Count; i++) { if (raycastTempData[i].cell != null) { cell = raycastTempData[i].cell; goto CONTINUE; } } //now we definetly hit something and now find furthest float furthestSqrDist = 0f; Vector3 furthest = origin; for (int i = 0; i < raycastTempData.Count; i++) { float curSqrDist = SomeMath.SqrDistance(raycastTempData[i].point, origin); if (curSqrDist > furthestSqrDist) { furthestSqrDist = curSqrDist; furthest = raycastTempData[i].point; } } hit = new RaycastHitNavMesh(furthest, SomeMath.SqrDistance(origin, furthest) < maxLengthSqr); return; CONTINUE : { continue; } } hit = new RaycastHitNavMesh(origin, true, true); return; }
public static void Raycast2Body2(float posX, float posY, float posZ, float rayDirX, float rayDirY, Cell cell, float maxLength, bool checkArea, bool checkPass, Area expArea, Passability expPass, RaycastAllocatedData rad, out RaycastHitNavMesh2 hit) { if (SomeMath.SqrMagnitude(rayDirX, rayDirY) == 0f) { hit = new RaycastHitNavMesh2(posX, posY, posZ, NavmeshRaycastResultType2.ReachMaxDistance, cell); return; } rad.chunkPixelSize = PathFinder.gridSize / PathFinder.CELL_GRID_SIZE; //trick to fix case when raycast start on "near" edge //currently can't be on chunk edge so we dont care if chunk changed rad.currentChunkData = cell.graph.chunk; int curGridX = (int)((posX - rad.currentChunkData.realX) / rad.chunkPixelSize); int curGridY = (int)((posZ - rad.currentChunkData.realZ) / rad.chunkPixelSize); //if (curGridX < 0) curGridX = 0; else if (curGridX > 9) curGridX = 9; //if (curGridY < 0) curGridY = 0; else if (curGridY > 9) curGridY = 9; rad.startIntX = (rad.currentChunkData.x * 10) + curGridX; rad.startIntY = (rad.currentChunkData.z * 10) + curGridY; var tempVal = maxLength / rad.chunkPixelSize; if (tempVal > 10000) //too big number anyway { rad.gridDistanceTreshold = SomeMath.Sqr(10000); } else { rad.gridDistanceTreshold = (int)SomeMath.Sqr(maxLength / rad.chunkPixelSize) + 1; } rad.dataArray = cell.graph.dataMap[curGridX][curGridY]; if (rad.dataArray != null) { for (int i = 0; i < rad.dataArray.Length; i++) { CellDataMapValue mapData = rad.dataArray[i]; if (mapData.from != cell) { continue; } if (mapData.data.RotateRightAndReturnDot(rayDirX, rayDirY) < 0) { continue; } float sqrDist = SomeMath.SqrDistance(mapData.data.NearestPointXZ(posX, posZ), new Vector2(posX, posZ)); if (sqrDist < 0.0001f) { Vector2 dirToCellCenter = (cell.centerVector2 - new Vector2(posX, posZ)).normalized * 0.001f; posX += dirToCellCenter.x; posZ += dirToCellCenter.y; //if (dot < 0.001f) {//oh wow. start point exactly on edge and ray alond side //} break; } } } //if (DEBUG) { // Debuger_K.AddRay(new Vector3(posX, posY + 0.1f, posZ), new Vector3(rayDirX, 0, rayDirY), Color.gray); //} rad.posX = posX; rad.posY = posY; rad.posZ = posZ; rad.rayDirX = rayDirX; rad.rayDirY = rayDirY; rad.checkPass = checkPass; rad.checkArea = checkArea; rad.expPass = expPass; rad.expArea = expArea; rad.raycastType = NavmeshRaycastResultType2.Nothing; rad.maxSqrLength = SomeMath.Sqr(maxLength); rad.curCell = cell; rad.prevCell = null; rad.raycastDone = false; float chunkX, chunkZ, curHullX, curHullZ, lastHullX = posX, lastHullZ = posZ; for (int i = 0; i < 4; i++) { rad.raycastSamples[i] = raycastSamplesTemplate[i].RotateRightAndReturnDot(rayDirX, rayDirY) < 0; } int chunkIteration = 0; while (rad.raycastDone == false) { chunkIteration++; if (chunkIteration > 50) { string s = string.Format("chunkIteration too large. x {0}, y {1}, z {2}, dx {3}, dy {4}, max {5}", posX, posY, posZ, rayDirX, rayDirY, maxLength); //HandleTextFile.WriteString(s); //Debuger_K.AddRay(new Vector3(posX, posY, posZ), Vector3.down, Color.cyan); //Debuger_K.AddRay(new Vector3(posX, posY, posZ), new Vector3(rayDirX, 0, rayDirY), Color.yellow, 50); //Debuger_K.UserfulPublicFlag = true; Debug.LogError(s); break; } rad.currentChunkData = rad.curCell.graph.chunk; rad.curChunkIntX = rad.currentChunkData.x * 10; rad.curChunkIntY = rad.currentChunkData.z * 10; rad.dataMap = rad.curCell.graph.dataMap; chunkX = rad.currentChunkData.realX; chunkZ = rad.currentChunkData.realZ; #region border points curHullX = posX; curHullZ = posZ; for (int i = 0; i < 4; i++) { if (rad.raycastSamples[i]) { CellContentData curSide = raycastSamplesTemplate[i]; float rX, rZ; if (SomeMath.RayIntersectSegment(posX, posZ, rayDirX, rayDirY, curSide.xLeft + chunkX, curSide.zLeft + chunkZ, curSide.xRight + chunkX, curSide.zRight + chunkZ, out rX, out rZ)) { curHullX = rX; curHullZ = rZ; } //if (DEBUG) // Debuger_K.AddLine(curSide.a, curSide.b, Color.red, chunkIteration); } } #region debug //if (DEBUG) { // Debuger_K.AddLine(new Vector3(curHullX, 0, curHullZ), new Vector3(lastHullX, 0, lastHullZ), Color.yellow, chunkIteration); // for (int x = 0; x < PathFinder.CELL_GRID_SIZE + 1; x++) { // Debuger_K.AddLine( // currentChunkData.realPositionV3 + new Vector3(x * chunkPixelSize, 0, 0), // currentChunkData.realPositionV3 + new Vector3(x * chunkPixelSize, 0, PathFinder.gridSize), // Color.red); // } // for (int z = 0; z < PathFinder.CELL_GRID_SIZE + 1; z++) { // Debuger_K.AddLine( // currentChunkData.realPositionV3 + new Vector3(0, 0, z * chunkPixelSize), // currentChunkData.realPositionV3 + new Vector3(PathFinder.gridSize, 0, z * chunkPixelSize), // Color.red); // } //} #endregion #endregion DDARasterization.DrawLine( lastHullX - chunkX, lastHullZ - chunkZ, curHullX - chunkX, curHullZ - chunkZ, rad.chunkPixelSize, rad, RaycastDelegate); lastHullX = curHullX; lastHullZ = curHullZ; } hit = new RaycastHitNavMesh2(rad.raycastResultX, rad.raycastResultY, rad.raycastResultZ, rad.raycastType, rad.curCell); }
private static bool RaycastDelegate(int x, int y, RaycastAllocatedData rad) { if (rad.raycastDone) { return(true); } if (x < 0) { x = 0; } else if (x > 9) { x = 9; //x = SomeMath.Clamp(0, CELL_GRID_SIZE - 1, x); } if (y < 0) { y = 0; } else if (y > 9) { y = 9; //y = SomeMath.Clamp(0, CELL_GRID_SIZE - 1, y); } if (SomeMath.SqrDistance(rad.startIntX, rad.startIntY, rad.curChunkIntX + x, rad.curChunkIntY + y) > rad.gridDistanceTreshold) { rad.raycastType = NavmeshRaycastResultType2.ReachMaxDistance; rad.raycastDone = true; } //IMPORTANT: edges in this list are sorted. "connection != null" at the begining and "connection == null" at the end. //some logic here based on this order rad.dataArray = rad.dataMap[x][y]; if (rad.dataArray == null) { return(false); } int dataArrayLength = rad.dataArray.Length; #region debug //if (DEBUG) { // Vector3 p = currentChunkData.realPositionV3 + new Vector3((x * chunkPixelSize) + (chunkPixelSize * 0.5f), 0, (y * chunkPixelSize) + (chunkPixelSize * 0.5f)); // Debuger_K.AddDot(curCell.centerV3, Color.cyan); // Debuger_K.AddDot(p, Color.red, 0.05f); // //list.ForEach(item => Debuger_K.AddLine(item.data.NearestPoint(p), p, Color.blue)); //} #endregion int cellLoop = 0; bool doCellLoop = true; while (doCellLoop) { cellLoop++; if (cellLoop > 50) { Debug.LogErrorFormat("cellLoop too large. x {0}, y {1}, z {2}, dx {3}, dy {4}, max {5}", rad.posX, rad.posY, rad.posZ, rad.rayDirX, rad.rayDirY, Mathf.Sqrt(rad.maxSqrLength)); break; } doCellLoop = false; for (int i = 0; i < dataArrayLength; i++) { CellDataMapValue mapData = rad.dataArray[i]; if (mapData.from != rad.curCell) { continue; } CellContentData ccd = mapData.data; if ((-(ccd.zRight - ccd.zLeft) * rad.rayDirX) + ((ccd.xRight - ccd.xLeft) * rad.rayDirY) < 0) { continue; } float ix, iy, iz; if (SomeMath.RayIntersectXZ(rad.posX, rad.posZ, rad.rayDirX, rad.rayDirY, ccd.xLeft, ccd.yLeft, ccd.zLeft, ccd.xRight, ccd.yRight, ccd.zRight, out ix, out iy, out iz) == false) { continue; } rad.raycastResultX = ix; rad.raycastResultY = iy; rad.raycastResultZ = iz; rad.prevCell = rad.curCell; if (SomeMath.SqrDistance(rad.posX, rad.posY, rad.posZ, ix, iy, iz) >= rad.maxSqrLength) { rad.raycastType = NavmeshRaycastResultType2.ReachMaxDistance; rad.raycastDone = true; return(true); } if (mapData.connection != null) { #region debug //if (DEBUG) { // Vector3 p = currentChunkData.realPositionV3 + new Vector3((x * chunkPixelSize) + (chunkPixelSize * 0.5f), 0, (y * chunkPixelSize) + (chunkPixelSize * 0.5f)); // //Debuger_K.AddLine(ToV3(curHullIntersection), resultVector); // if (prevCell != null) { // Vector3 p1p = SomeMath.MidPoint(curCell.centerV3, prevCell.centerV3); // //Vector3 p2p = SomeMath.MidPoint(p1p, p); // Debuger_K.AddLine(curCell.centerV3, prevCell.centerV3, Color.green); // Debuger_K.AddLine(p1p, p, Color.cyan); // } //} #endregion doCellLoop = true; rad.curCell = mapData.connection; if (rad.checkPass && rad.curCell.passability != rad.expPass) { rad.raycastType = NavmeshRaycastResultType2.PassabilityChange; rad.raycastDone = true; return(true); } else if (rad.checkArea && rad.curCell.area != rad.expArea) { rad.raycastType = NavmeshRaycastResultType2.AreaChange; rad.raycastDone = true; return(true); } } else { rad.curCell = null; rad.raycastType = NavmeshRaycastResultType2.NavmeshBorderHit; rad.raycastDone = true; return(true); } break; } } return(rad.raycastDone); }
public CellDataMapValue(Cell source, Cell connection, CellContentData data) { this.from = source; this.connection = connection; this.data = data; }