コード例 #1
0
        public static float GetBlockSurface(IMySlimBlock block)
        {
            Vector3 blockSize;

            block.ComputeScaledHalfExtents(out blockSize);
            blockSize *= 2;
            return((blockSize.X * blockSize.Y) + (blockSize.Y * blockSize.Z) + (blockSize.Z * blockSize.X) / 6);
        }
コード例 #2
0
        /// <summary>
        /// Checks if block is inside the given BoundingBox
        /// </summary>
        /// <param name="block"></param>
        /// <param name="areaBox"></param>
        /// <returns></returns>
        public static bool IsInRange(this IMySlimBlock block, ref MyOrientedBoundingBoxD areaBox, out double distance)
        {
            Vector3 halfExtents;

            block.ComputeScaledHalfExtents(out halfExtents);
            var matrix = block.CubeGrid.WorldMatrix;

            matrix.Translation = block.CubeGrid.GridIntegerToWorld(block.Position);
            var box     = new MyOrientedBoundingBoxD(new BoundingBoxD(-(halfExtents), (halfExtents)), matrix);
            var inRange = areaBox.Intersects(ref box);

            distance = inRange ? (areaBox.Center - box.Center).Length() : 0;
            return(inRange);
        }
コード例 #3
0
        public static double?GridHitCheck(IMyCubeGrid cubeGrid, LineD lineCheck, Vector3D lineStartPoint, Vector3D lineEndPoint)
        {
            // Hit a grid, do a raycast to it for block hit.
            Vector3I?gridBlockHit = cubeGrid.RayCastBlocks(lineStartPoint, lineEndPoint);

            if (gridBlockHit.HasValue)
            {
                // Get the block on hit grid
                IMySlimBlock blk = cubeGrid.GetCubeBlock(gridBlockHit.Value);
                if (blk.FatBlock != null)
                {
                    var blockBBox = blk.FatBlock.LocalAABB;
                    MyOrientedBoundingBoxD blockOBB = new MyOrientedBoundingBoxD(blockBBox, blk.FatBlock.WorldMatrix);
                    double?blockIntersect           = blockOBB.Intersects(ref lineCheck);
                    if (blockIntersect != null)
                    {
                        var hitDist = blockOBB.Intersects(ref lineCheck);
                        return(hitDist);
                    }

                    //DrawOBB(blockOBB, Color.Red, MySimpleObjectRasterizer.Wireframe, 0.01f);
                }
                else
                {
                    var center = blk.GetPosition();

                    Vector3 halfExt;
                    blk.ComputeScaledHalfExtents(out halfExt);

                    BoundingBoxD           blockBBox = new BoundingBoxD(-halfExt, halfExt);
                    Quaternion             rotMatrix = Quaternion.CreateFromRotationMatrix(blk.CubeGrid.WorldMatrix);
                    MyOrientedBoundingBoxD blockOBB  = new MyOrientedBoundingBoxD(center, blockBBox.HalfExtents, rotMatrix);

                    var hitDist = blockOBB.Intersects(ref lineCheck);
                    return(hitDist);

                    //DrawOBB(blockOBB, Color.Green, MySimpleObjectRasterizer.Wireframe, 0.01f);
                }
            }

            return(double.MaxValue);
        }
コード例 #4
0
            private void UpdateSlimBlock()
            {
                var grid = _root as IMyCubeGrid;

                _block = grid?.FirstBlock(_hit.Position,
                                          _hit.Position + _maxPrediction * _ray.Direction);
                if (_block == null)
                {
                    return;
                }

                Vector3 halfExtents;

                _block.ComputeScaledHalfExtents(out halfExtents);
                Vector3D center;

                _block.ComputeScaledCenter(out center);
                var   bb = new BoundingBoxD(center - halfExtents, center + halfExtents);
                LineD l;
                var   m = grid.WorldMatrixNormalizedInv;

                l.From      = Vector3D.Transform(_ray.From, ref m);
                l.To        = Vector3D.Transform(_ray.To, ref m);
                l.Direction = Vector3D.TransformNormal(_ray.Direction, ref m);
                l.Length    = _ray.Length;
                double t1, t2;

                if (bb.Intersect(ref l, out t1, out t2))
                {
                    _hitPosition = _ray.From + _ray.Direction * t1;
                }
                else
                {
                    _hitPosition = _ray.From + (Vector3.Transform(center, grid.WorldMatrix) - _ray.From).Dot(_ray.Direction) * _ray.Direction;
                }
            }