Esempio n. 1
0
        public static double CreateExtendedFit(MyCubeBlock shield, Vector3D gridHalfExtents, List <IMySlimBlock> fitblocks, Vector3D[] fitPoints)
        {
            var subGrids = MyAPIGateway.GridGroups.GetGroup(shield.CubeGrid, GridLinkTypeEnum.Mechanical);

            foreach (var grid in subGrids)
            {
                if (grid.MarkedForClose)
                {
                    continue;
                }
                fitblocks.AddRange(((MyCubeGrid)grid).GetBlocks());
            }

            var bQuaternion = Quaternion.CreateFromRotationMatrix(shield.CubeGrid.PositionComp.WorldMatrixRef);

            var sqrt3  = Math.Sqrt(3);
            var sqrt5  = Math.Sqrt(5);
            var last   = 0;
            var repeat = 0;

            for (int i = 0; i <= 10; i++)
            {
                var ellipsoidAdjust = MathHelper.Lerp(sqrt3, sqrt5, i * 0.1);

                var shieldSize   = gridHalfExtents * ellipsoidAdjust;
                var mobileMatrix = MatrixD.CreateScale(shieldSize);
                mobileMatrix.Translation = shield.CubeGrid.PositionComp.LocalVolume.Center;
                var matrixInv = MatrixD.Invert(mobileMatrix * shield.CubeGrid.PositionComp.WorldMatrixRef);

                var c = 0;
                for (int j = 0; j < fitblocks.Count; j++)
                {
                    var block = fitblocks[j];
                    var fat   = block.FatBlock as MyCubeBlock;

                    if (fat != null && fat.MarkedForClose || block.IsDestroyed)
                    {
                        continue;
                    }

                    BoundingBoxD blockBox;
                    Vector3D     center;
                    if (fat != null)
                    {
                        blockBox = fat.PositionComp.LocalAABB;
                        center   = fat.PositionComp.WorldAABB.Center;
                    }
                    else
                    {
                        Vector3 halfExt;
                        block.ComputeScaledHalfExtents(out halfExt);
                        blockBox = new BoundingBoxD(-halfExt, halfExt);
                        block.ComputeWorldCenter(out center);
                    }

                    var bOriBBoxD = new MyOrientedBoundingBoxD(center, blockBox.HalfExtents, bQuaternion);

                    bOriBBoxD.GetCorners(fitPoints, 0);
                    foreach (var point in fitPoints)
                    {
                        if (!CustomCollision.PointInShield(point, matrixInv) && ((MyCubeGrid)block.CubeGrid).IsSameConstructAs(shield.CubeGrid))
                        {
                            c++;
                        }
                    }
                }

                if (c == last)
                {
                    repeat++;
                }
                else
                {
                    repeat = 0;
                }

                if (c == 0)
                {
                    return(MathHelper.Lerp(sqrt3, sqrt5, i * 0.1));
                }
                last = c;
                if (i == 10 && repeat > 2)
                {
                    return(MathHelper.Lerp(sqrt3, sqrt5, ((10 - repeat) + 1) * 0.1));
                }
            }
            return(sqrt5);
        }
Esempio n. 2
0
        public static double CreateNormalFit(MyCubeBlock shield, Vector3D gridHalfExtents, List <IMySlimBlock> fitblocks, Vector3D[] fitPoints)
        {
            var subGrids = MyAPIGateway.GridGroups.GetGroup(shield.CubeGrid, GridLinkTypeEnum.Mechanical);

            foreach (var grid in subGrids)
            {
                if (grid.MarkedForClose)
                {
                    continue;
                }
                fitblocks.AddRange(((MyCubeGrid)grid).GetBlocks());
            }

            var bQuaternion = Quaternion.CreateFromRotationMatrix(shield.CubeGrid.PositionComp.WorldMatrixRef);

            var end        = 10;
            var wasOutside = false;

            for (int i = 0; i <= end + 1; i++)
            {
                var fitSeq = Session.Instance.FitSeq[i];

                var ellipsoidAdjust = MathHelper.Lerp(fitSeq.SqrtStart, fitSeq.SqrtEnd, fitSeq.SeqMulti);

                var shieldSize   = gridHalfExtents * ellipsoidAdjust;
                var mobileMatrix = MatrixD.CreateScale(shieldSize);
                mobileMatrix.Translation = shield.CubeGrid.PositionComp.LocalAABB.Center;
                var matrixInv = MatrixD.Invert(mobileMatrix * shield.CubeGrid.PositionComp.WorldMatrixRef);

                var pointOutside = false;
                for (int j = 0; j < fitblocks.Count; j++)
                {
                    var block = fitblocks[j];
                    var fat   = block.FatBlock as MyCubeBlock;
                    if (fat != null && fat.MarkedForClose || block.IsDestroyed)
                    {
                        continue;
                    }

                    BoundingBoxD blockBox;
                    Vector3D     center;
                    if (fat != null)
                    {
                        blockBox = fat.PositionComp.LocalAABB;
                        center   = fat.PositionComp.WorldAABB.Center;
                    }
                    else
                    {
                        Vector3 halfExt;
                        block.ComputeScaledHalfExtents(out halfExt);
                        blockBox = new BoundingBoxD(-halfExt, halfExt);
                        block.ComputeWorldCenter(out center);
                    }

                    var bOriBBoxD = new MyOrientedBoundingBoxD(center, blockBox.HalfExtents, bQuaternion);

                    bOriBBoxD.GetCorners(fitPoints, 0);
                    foreach (var point in fitPoints)
                    {
                        if (!CustomCollision.PointInShield(point, matrixInv) && ((MyCubeGrid)block.CubeGrid).IsSameConstructAs(shield.CubeGrid))
                        {
                            pointOutside = true;
                            break;
                        }
                    }
                }

                if (pointOutside)
                {
                    wasOutside = true;
                    if (i == 0)
                    {
                        return(ellipsoidAdjust);
                    }

                    if (i == 2)
                    {
                        i   = 10;
                        end = 19;
                    }
                }

                if (!pointOutside)
                {
                    if (i == 1)
                    {
                        return(ellipsoidAdjust);
                    }
                    if (wasOutside)
                    {
                        return(ellipsoidAdjust);
                    }
                }

                if (i == end)
                {
                    return(ellipsoidAdjust);
                }
            }
            return(Math.Sqrt(5));
        }
Esempio n. 3
0
        public static double CreateExtendedFit(IMyCubeBlock shield, Vector3D gridHalfExtents)
        {
            var blockPoints = new Vector3D[8];
            var blocks      = new List <IMySlimBlock>();

            var subGrids = MyAPIGateway.GridGroups.GetGroup(shield.CubeGrid, GridLinkTypeEnum.Mechanical);

            foreach (var grid in subGrids)
            {
                grid.GetBlocks(blocks);
            }

            var bQuaternion = Quaternion.CreateFromRotationMatrix(shield.CubeGrid.WorldMatrix);
            var sqrt3       = Math.Sqrt(3);
            var sqrt5       = Math.Sqrt(5);
            var last        = 0;
            var repeat      = 0;

            for (int i = 0; i <= 10; i++)
            {
                var ellipsoidAdjust = MathHelper.Lerp(sqrt3, sqrt5, i * 0.1);

                var shieldSize   = gridHalfExtents * ellipsoidAdjust;
                var mobileMatrix = MatrixD.CreateScale(shieldSize);
                mobileMatrix.Translation = shield.CubeGrid.PositionComp.LocalVolume.Center;
                var matrixInv = MatrixD.Invert(mobileMatrix * shield.CubeGrid.WorldMatrix);

                var c = 0;
                foreach (var block in blocks)
                {
                    BoundingBoxD blockBox;
                    Vector3D     center;
                    if (block.FatBlock != null)
                    {
                        blockBox = block.FatBlock.LocalAABB;
                        center   = block.FatBlock.WorldAABB.Center;
                    }
                    else
                    {
                        Vector3 halfExt;
                        block.ComputeScaledHalfExtents(out halfExt);
                        blockBox = new BoundingBoxD(-halfExt, halfExt);
                        block.ComputeWorldCenter(out center);
                    }

                    var bOriBBoxD = new MyOrientedBoundingBoxD(center, blockBox.HalfExtents, bQuaternion);

                    bOriBBoxD.GetCorners(blockPoints, 0);
                    foreach (var point in blockPoints)
                    {
                        if (!CustomCollision.PointInShield(point, matrixInv))
                        {
                            c++;
                        }
                    }
                }

                if (c == last)
                {
                    repeat++;
                }
                else
                {
                    repeat = 0;
                }

                if (c == 0)
                {
                    return(MathHelper.Lerp(sqrt3, sqrt5, i * 0.1));
                }
                last = c;
                if (i == 10 && repeat > 2)
                {
                    return(MathHelper.Lerp(sqrt3, sqrt5, ((10 - repeat) + 1) * 0.1));
                }
            }
            return(sqrt5);
        }
Esempio n. 4
0
        public static double CreateNormalFit(IMyCubeBlock shield, Vector3D gridHalfExtents)
        {
            var blockPoints = new Vector3D[8];
            var blocks      = new List <IMySlimBlock>();

            var subGrids = MyAPIGateway.GridGroups.GetGroup(shield.CubeGrid, GridLinkTypeEnum.Mechanical);

            foreach (var grid in subGrids)
            {
                grid.GetBlocks(blocks);
            }

            var bQuaternion = Quaternion.CreateFromRotationMatrix(shield.CubeGrid.WorldMatrix);

            var end        = 10;
            var wasOutside = false;

            for (int i = 0; i <= end + 1; i++)
            {
                var fitSeq = Session.Instance.FitSeq[i];

                var ellipsoidAdjust = MathHelper.Lerp(fitSeq.SqrtStart, fitSeq.SqrtEnd, fitSeq.SeqMulti);

                var shieldSize   = gridHalfExtents * ellipsoidAdjust;
                var mobileMatrix = MatrixD.CreateScale(shieldSize);
                mobileMatrix.Translation = shield.CubeGrid.PositionComp.LocalAABB.Center;
                var matrixInv = MatrixD.Invert(mobileMatrix * shield.CubeGrid.WorldMatrix);

                var pointOutside = false;
                foreach (var block in blocks)
                {
                    BoundingBoxD blockBox;
                    Vector3D     center;
                    if (block.FatBlock != null)
                    {
                        blockBox = block.FatBlock.LocalAABB;
                        center   = block.FatBlock.WorldAABB.Center;
                    }
                    else
                    {
                        Vector3 halfExt;
                        block.ComputeScaledHalfExtents(out halfExt);
                        blockBox = new BoundingBoxD(-halfExt, halfExt);
                        block.ComputeWorldCenter(out center);
                    }

                    var bOriBBoxD = new MyOrientedBoundingBoxD(center, blockBox.HalfExtents, bQuaternion);

                    bOriBBoxD.GetCorners(blockPoints, 0);
                    foreach (var point in blockPoints)
                    {
                        if (!CustomCollision.PointInShield(point, matrixInv))
                        {
                            pointOutside = true;
                            break;
                        }
                    }
                }

                //Log.Line($"i:{i} - end:{end} - sqrtStart:{fitSeq.SqrtStart} - sqrtEnd:{fitSeq.SqrtEnd} - multi:{fitSeq.SeqMulti}");
                if (pointOutside)
                {
                    wasOutside = true;
                    //Log.Line($"Point is Outside");
                    if (i == 0)
                    {
                        //Log.Line("i == 0 and points outside returning maxSize");
                        return(ellipsoidAdjust);
                    }

                    if (i == 2)
                    {
                        //Log.Line($"Mid outside, jumping to 11");
                        i   = 10;
                        end = 19;
                    }
                }

                if (!pointOutside)
                {
                    //Log.Line($"Point is Inside");
                    if (i == 1)
                    {
                        //Log.Line($"i == 1, returning minValue");
                        return(ellipsoidAdjust);
                    }
                    if (wasOutside)
                    {
                        //Log.Line($"wasOutside and now in, returning value");
                        return(ellipsoidAdjust);
                    }
                }

                if (i == end)
                {
                    //Log.Line($"reached end, returning current value");
                    return(ellipsoidAdjust);
                }
            }
            //Log.Line($"fitNoMatch");
            return(Math.Sqrt(5));
        }
 public void Transform(Matrix matrix)
 {
     Center      = Vector3.Transform(Center, matrix);
     Orientation = Quaternion.CreateFromRotationMatrix(Matrix.CreateFromQuaternion(Orientation) * matrix);
 }
Esempio n. 6
0
 public MyTransform(ref Matrix matrix)
 {
     Quaternion.CreateFromRotationMatrix(ref matrix, out this.Rotation);
     this.Position = matrix.Translation;
 }