private void TestPlanet() { MyPlanet planet = m_closestPlanet; if (planet == null) { return; } IMyCubeGrid grid = m_block.CubeGrid; Vector3D myPos = grid.GetCentre(); Vector3D planetCentre = planet.GetCentre(); double distSqToPlanet = Vector3D.DistanceSquared(myPos, planetCentre); if (distSqToPlanet > planet.MaximumRadius * planet.MaximumRadius) { Log.DebugLog("higher than planet maximum"); m_planetObstruction = false; return; } Vector3D closestPoint = planet.GetClosestSurfacePointGlobal(ref myPos); if (distSqToPlanet < Vector3D.DistanceSquared(closestPoint, planetCentre)) { Log.DebugLog("below surface"); return; } float longest = grid.GetLongestDim(); if (Vector3D.DistanceSquared(myPos, closestPoint) < longest * longest) { Log.DebugLog("near surface"); m_planetObstruction = true; return; } Log.DebugLog("clear"); m_planetObstruction = false; return; }
private void TestPlanet() { MyPlanet planet = ClosestPlanet; if (planet == null) { return; } Vector3D myPos = m_grid.GetCentre(); Vector3D planetCentre = planet.GetCentre(); double distSqToPlanet = Vector3D.DistanceSquared(myPos, planetCentre); if (distSqToPlanet > planet.MaximumRadius * planet.MaximumRadius) { m_logger.debugLog("higher than planet maximum"); PlanetState = Pathfinder.PathState.No_Obstruction; return; } using (lock_closestPoint.AcquireExclusiveUsing()) m_closestPoint = planet.GetClosestSurfacePointGlobal(ref myPos); if (distSqToPlanet < Vector3D.DistanceSquared(m_closestPoint, planetCentre)) { m_logger.debugLog("below surface"); PlanetState = Pathfinder.PathState.Path_Blocked; return; } float longest = m_grid.GetLongestDim(); if (Vector3D.DistanceSquared(myPos, m_closestPoint) < longest * longest) { m_logger.debugLog("near surface"); PlanetState = Pathfinder.PathState.Path_Blocked; return; } m_logger.debugLog("clear"); PlanetState = Pathfinder.PathState.No_Obstruction; return; }
/// <summary> /// Find a sphere free from voxel. /// </summary> /// <param name="voxel">The voxel to avoid.</param> /// <param name="startPosition">The position to start the search.</param> /// <param name="minRadius">The minimum radius around freePosition.</param> /// <param name="freePosition">A position that is minRadius from voxel</param> public static void FindFreeSpace(this MyVoxelBase voxel, Vector3D startPosition, double minRadius, out Vector3D freePosition) { MyPlanet planet = voxel as MyPlanet; if (planet != null) { Vector3D centre = planet.GetCentre(); Vector3D direction; Vector3D.Subtract(ref startPosition, ref centre, out direction); direction.Normalize(); FindFreeSpace(voxel, startPosition, direction, minRadius, out freePosition); return; } BoundingSphereD testSphere; testSphere.Radius = minRadius; for (double testDist = minRadius; ; testDist += minRadius) { foreach (Vector3I neighbour in Globals.Neighbours) { Vector3D disp = Vector3D.Multiply(neighbour, testDist); Vector3D.Add(ref startPosition, ref disp, out testSphere.Center); if (!ContainsOrIntersects(voxel, ref testSphere, true)) { CapsuleD capsule; capsule.P0 = testSphere.Center; Vector3D disp1 = Vector3D.Multiply(neighbour, testDist * 0.5d); Vector3D.Add(ref startPosition, ref disp1, out capsule.P1); capsule.Radius = (float)minRadius; if (!CapsuleDExtensions.Intersects(ref capsule, voxel, out freePosition)) { freePosition = capsule.P1; } return; } } } }