示例#1
0
        /*
         * public static bool PosInVoxel(MyVoxelBase voxelBase, Vector3D pos, MyStorageData cache)
         * {
         *  if (voxelBase.Storage.Closed) return false;
         *  //cache.Clear(MyStorageDataTypeEnum.Content, 0);
         *  //cache.Resize(Vector3I.One);
         *  Vector3I voxelCoord;
         *  MyVoxelCoordSystems.WorldPositionToVoxelCoord(voxelBase.RootVoxel.PositionLeftBottomCorner, ref pos, out voxelCoord);
         *  var flag = MyVoxelRequestFlags.EmptyContent;
         *  voxelBase.RootVoxel.Storage.ReadRange(cache, MyStorageDataTypeFlags.Content, 0, voxelCoord, voxelCoord, ref flag);
         *  if (cache.Content(ref Vector3I.Zero) != (byte)0)
         *  {
         *      return true;
         *  }
         *  return false;
         * }
         */

        public static bool VoxelContact(Vector3D[] physicsVerts, MyVoxelBase voxelBase)
        {
            try
            {
                if (voxelBase.RootVoxel.MarkedForClose || voxelBase.RootVoxel.Storage.Closed)
                {
                    return(false);
                }
                var planet = voxelBase as MyPlanet;
                var map    = voxelBase as MyVoxelMap;

                if (planet != null)
                {
                    for (int i = 0; i < 162; i++)
                    {
                        var      from          = physicsVerts[i];
                        var      localPosition = (Vector3)(from - planet.PositionLeftBottomCorner);
                        var      v             = localPosition / 1f;
                        Vector3I voxelCoord;
                        Vector3I.Floor(ref v, out voxelCoord);

                        var hit = new VoxelHit();
                        planet.Storage.ExecuteOperationFast(ref hit, MyStorageDataTypeFlags.Content, ref voxelCoord, ref voxelCoord, notifyRangeChanged: false);

                        if (hit.HasHit)
                        {
                            return(true);
                        }
                    }
                }
                else if (map != null)
                {
                    for (int i = 0; i < 162; i++)
                    {
                        var      from          = physicsVerts[i];
                        var      localPosition = (Vector3)(from - map.PositionLeftBottomCorner);
                        var      v             = localPosition / 1f;
                        Vector3I voxelCoord;
                        Vector3I.Floor(ref v, out voxelCoord);

                        var hit = new VoxelHit();
                        map.Storage.ExecuteOperationFast(ref hit, MyStorageDataTypeFlags.Content, ref voxelCoord, ref voxelCoord, notifyRangeChanged: false);

                        if (hit.HasHit)
                        {
                            return(true);
                        }
                    }
                }
            }
            catch (Exception ex) { Log.Line($"Exception in VoxelContact: {ex}"); }

            return(false);
        }
示例#2
0
        /*
         * public static bool VoxelContact(Vector3D[] physicsVerts, MyVoxelBase voxelBase, MyStorageData cache)
         * {
         *  try
         *  {
         *      if (voxelBase.RootVoxel.MarkedForClose || voxelBase.RootVoxel.Storage.Closed) return false;
         *      var planet = voxelBase as MyPlanet;
         *      var map = voxelBase as MyVoxelMap;
         *      var isPlanet = voxelBase is MyPlanet;
         *      if (isPlanet)
         *      {
         *          for (int i = 0; i < 162; i++)
         *          {
         *              var hit = PosInVoxel(planet, physicsVerts[i], cache);
         *              //var hit = planet.DoOverlapSphereTest(0.1f, from);
         *              if (hit) return true;
         *          }
         *      }
         *      else
         *      {
         *          for (int i = 0; i < 162; i++)
         *          {
         *              if (map == null) continue;
         *              //var hit = map.DoOverlapSphereTest(0.1f, from);
         *              var hit = PosInVoxel(map, physicsVerts[i], cache);
         *              if (hit) return true;
         *          }
         *      }
         *  }
         *  catch (Exception ex) { Log.Line($"Exception in VoxelCollisionSphere: {ex}"); }
         *  return false;
         * }
         */
        public static Vector3D?VoxelEllipsoidCheck(IMyCubeGrid shieldGrid, Vector3D[] physicsVerts, MyVoxelBase voxelBase)
        {
            var collisionAvg = Vector3D.Zero;

            try
            {
                if (voxelBase.RootVoxel.MarkedForClose || voxelBase.RootVoxel.Storage.Closed)
                {
                    return(null);
                }
                var planet = voxelBase as MyPlanet;
                var map    = voxelBase as MyVoxelMap;

                var collision    = Vector3D.Zero;
                var collisionCnt = 0;

                if (planet != null)
                {
                    for (int i = 0; i < 162; i++)
                    {
                        var      from          = physicsVerts[i];
                        var      localPosition = (Vector3)(from - planet.PositionLeftBottomCorner);
                        var      v             = localPosition / 1f;
                        Vector3I voxelCoord;
                        Vector3I.Floor(ref v, out voxelCoord);

                        var hit = new VoxelHit();
                        planet.Storage.ExecuteOperationFast(ref hit, MyStorageDataTypeFlags.Content, ref voxelCoord, ref voxelCoord, notifyRangeChanged: false);

                        if (hit.HasHit)
                        {
                            collision += from;
                            collisionCnt++;
                        }
                    }
                }
                else if (map != null)
                {
                    for (int i = 0; i < 162; i++)
                    {
                        var      from          = physicsVerts[i];
                        var      localPosition = (Vector3)(from - map.PositionLeftBottomCorner);
                        var      v             = localPosition / 1f;
                        Vector3I voxelCoord;
                        Vector3I.Floor(ref v, out voxelCoord);

                        var hit = new VoxelHit();
                        map.Storage.ExecuteOperationFast(ref hit, MyStorageDataTypeFlags.Content, ref voxelCoord, ref voxelCoord, notifyRangeChanged: false);

                        if (hit.HasHit)
                        {
                            collision += from;
                            collisionCnt++;
                        }
                    }
                }

                if (collisionCnt == 0)
                {
                    return(null);
                }
                var sPhysics = shieldGrid.Physics;
                var lSpeed   = sPhysics.LinearVelocity.Length();
                var aSpeed   = sPhysics.AngularVelocity.Length() * 20;
                var speed    = 0f;
                speed = lSpeed > aSpeed ? lSpeed : aSpeed;

                collisionAvg = collision / collisionCnt;

                shieldGrid.Physics.AddForce(MyPhysicsForceType.APPLY_WORLD_FORCE, -(collisionAvg - sPhysics.CenterOfMassWorld) * ((MyCubeGrid)shieldGrid).GetCurrentMass() * speed, null, Vector3D.Zero, MathHelper.Clamp(speed, 1f, 20f));
            }
            catch (Exception ex) { Log.Line($"Exception in VoxelCollisionSphere: {ex}"); }

            return(collisionAvg);
        }