public void CalculateRaySpacing(BoundingBox box) { horizontalRayCount = FixedMath.RoundToInt(box.height.Div(raySpacing)); verticalRayCount = FixedMath.RoundToInt(box.width.Div(raySpacing)); horizontalRayCount = (int)FixedMath.Max(horizontalRayCount, 2); verticalRayCount = (int)FixedMath.Max(verticalRayCount, 2); horizontalRaySpacing = box.height / (horizontalRayCount - 1); verticalRaySpacing = box.width / (verticalRayCount - 1); }
public GridNode GetNode() { //Calculated closest side to raycast in first long xDif = OffsettedPos.x - XGrid; xDif = xDif.ClampOne(); long yDif = OffsettedPos.y - YGrid; yDif = yDif.ClampOne(); long nodeHalfWidth = FixedMath.One / 2; //Check to see if we should raycast towards corner first if ((xDif.Abs() >= nodeHalfWidth / 2) && (yDif.Abs() >= nodeHalfWidth / 2)) { dirX = FixedMath.RoundToInt(xDif); dirY = FixedMath.RoundToInt(yDif); } else { if (xDif.Abs() < yDif.Abs()) { dirX = 0; dirY = yDif.RoundToInt(); } else { dirX = xDif.RoundToInt(); dirY = 0; } } int layerStartX = dirX, layerStartY = dirY; int iterations = 0; // <- this is for debugging for (layer = 1; layer <= this.MaxTestDistance;) { this.CheckPathNode(GridManager.GetNode(XGrid + dirX, YGrid + dirY)); if (this.castNodeFound) { return(this.closestNode); } AdvanceRotation(); //If we make a full loop if (layerStartX == dirX && layerStartY == dirY) { layer++; //Advance a layer instead of rotation if (dirX > 0) { dirX = layer; } else if (dirX < 0) { dirX = -layer; } if (dirY > 0) { dirY = layer; } else if (dirY < 0) { dirY = -layer; } layerStartX = dirX; layerStartY = dirY; } iterations++; if (iterations > 500) { Debug.Log("tew many"); break; } } //If the cast node is found or the side has been checked, do not raycast on that side if (!castNodeFound) { return(null); } return(closestNode); }