public bool Intersect(ref Line localLine, out MyIntersectionResultLineTriangle result, IntersectionFlags flags) { MyIntersectionResultLineTriangle valueOrDefault; m_sweepResultCache.Clear(); MyGridIntersection.Calculate(m_sweepResultCache, 8f, localLine.From, localLine.To, new Vector3I(0, 0, 0), this.m_cellsCount - 1); float? minDistanceUntilNow = null; MyCellCoord cell = new MyCellCoord(); MyIntersectionResultLineTriangle?nullable2 = null; for (int i = 0; i < m_sweepResultCache.Count; i++) { BoundingBox box; cell.CoordInLod = m_sweepResultCache[i]; MyVoxelCoordSystems.GeometryCellCoordToLocalAABB(ref cell.CoordInLod, out box); float?lineBoundingBoxIntersection = MyUtils.GetLineBoundingBoxIntersection(ref localLine, ref box); if ((minDistanceUntilNow != null) && (lineBoundingBoxIntersection != null)) { float?nullable1; float?nullable5 = minDistanceUntilNow; float num3 = 15.58846f; if (nullable5 != null) { nullable1 = new float?(nullable5.GetValueOrDefault() + num3); } else { nullable1 = null; } float?nullable4 = nullable1; float num2 = lineBoundingBoxIntersection.Value; if ((nullable4.GetValueOrDefault() < num2) & (nullable4 != null)) { break; } } CellData cachedDataCell = this.GetCell(ref cell); if ((cachedDataCell != null) && (cachedDataCell.VoxelTrianglesCount != 0)) { this.GetCellLineIntersectionOctree(ref nullable2, ref localLine, ref minDistanceUntilNow, cachedDataCell, flags); } } MyIntersectionResultLineTriangle?nullable7 = nullable2; if (nullable7 != null) { valueOrDefault = nullable7.GetValueOrDefault(); } else { valueOrDefault = new MyIntersectionResultLineTriangle(); } result = valueOrDefault; return(nullable2 != null); }
/// <summary> /// Populates outHitPositions with cell positions of the grid, regardless of whether or not there are blocks occupying those cells. /// </summary> /// <param name="grid">The grid to get cell positions of.</param> /// <param name="localStart">The local position to start from.</param> /// <param name="localEnd">The local position to end at.</param> /// <param name="outHitPositions">Populated with cells that the line passes through.</param> public static void RayCastCellsLocal(this IMyCubeGrid grid, ref Vector3D localStart, ref Vector3D localEnd, List <Vector3I> outHitPositions) { Vector3D offset = ((MyCubeGrid)grid).GridSizeHalfVector; Vector3D offStart; Vector3D.Add(ref localStart, ref offset, out offStart); Vector3D offEnd; Vector3D.Add(ref localEnd, ref offset, out offEnd); Vector3I min = grid.Min - Vector3I.One; Vector3I max = grid.Max + Vector3I.One; MyGridIntersection.Calculate(outHitPositions, grid.GridSize, offStart, offEnd, min, max); }
internal bool Intersect(ref Line worldLine, out MyIntersectionResultLineTriangleEx?result, IntersectionFlags flags) { Line localLine = new Line(worldLine.From - m_voxelMap.PositionLeftBottomCorner, worldLine.To - m_voxelMap.PositionLeftBottomCorner, true); Profiler.Begin("VoxelMap.LineIntersection AABB sweep"); m_sweepResultCache.Clear(); MyGridIntersection.Calculate( m_sweepResultCache, (int)MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_METRES, localLine.From, localLine.To, new Vector3I(0, 0, 0), m_cellsCount - 1 ); Profiler.End(); Profiler.Begin("VoxelMap.LineIntersection test AABBs"); float? minDistanceUntilNow = null; BoundingBox cellBoundingBox; Vector3I cellCoord; MyIntersectionResultLineTriangle?tmpResult = null; for (int index = 0; index < m_sweepResultCache.Count; index++) { var coord = m_sweepResultCache[index]; cellCoord = coord; GetDataCellBoundingBox(ref cellCoord, out cellBoundingBox); float?distanceToBoundingBox = MyUtils.GetLineBoundingBoxIntersection(ref worldLine, ref cellBoundingBox); // Sweep results are sorted; when we get far enough, make an early exit const float earlyOutDistance = 1.948557f * MyVoxelConstants.GEOMETRY_CELL_SIZE_IN_METRES; // = sqrt(3) * 9/8 * cell_side if (minDistanceUntilNow != null && distanceToBoundingBox != null && minDistanceUntilNow + earlyOutDistance < distanceToBoundingBox.Value) { break; } // Get cell from cache. If not there, precalc it and store in the cache. // If null is returned, we know that cell doesn't contain any triangleVertexes so we don't need to do intersections. CellData cachedDataCell = GetCell(MyLodTypeEnum.LOD0, ref cellCoord); if (cachedDataCell == null || cachedDataCell.VoxelTrianglesCount == 0) { continue; } GetCellLineIntersectionOctree(ref tmpResult, ref localLine, ref minDistanceUntilNow, cachedDataCell, flags); } Profiler.End(); if (tmpResult.HasValue) { result = new MyIntersectionResultLineTriangleEx(tmpResult.Value, m_voxelMap, ref worldLine); return(true); } else { result = null; return(false); } }