/// <summary>
        /// Writes all surrounding blocks around the given one with the given size.
        /// </summary>
        private void GetSurroundingBlocksFromStaticGrids(MySlimBlock block, MyCubeSize cubeSizeEnum, HashSet <MyCubeBlock> outBlocks)
        {
            outBlocks.Clear();

            BoundingBoxD aabb = new BoundingBoxD(block.Min * block.CubeGrid.GridSize - block.CubeGrid.GridSize / 2, block.Max * block.CubeGrid.GridSize + block.CubeGrid.GridSize / 2);

            if (block.FatBlock != null)
            {
                var aabbCenter = aabb.Center;
                aabb = (BoundingBoxD)block.FatBlock.Model.BoundingBox;
                Matrix m;
                block.FatBlock.Orientation.GetMatrix(out m);
                aabb = aabb.Transform(m);
                aabb.Translate(aabbCenter);
            }

            aabb = aabb.Transform(block.CubeGrid.WorldMatrix);
            aabb.Inflate(0.125);

            List <MyEntity> boxOverlapList = new List <MyEntity>();

            MyEntities.GetElementsInBox(ref aabb, boxOverlapList);

            for (int i = 0; i < boxOverlapList.Count; i++)
            {
                var otherBlock = boxOverlapList[i] as MyCubeBlock;
                if (otherBlock != null && otherBlock.SlimBlock != block &&
                    otherBlock.CubeGrid.IsStatic && otherBlock.CubeGrid.EnableSmallToLargeConnections && otherBlock.CubeGrid.SmallToLargeConnectionsInitialized &&
                    otherBlock.CubeGrid != block.CubeGrid && otherBlock.CubeGrid.GridSizeEnum == cubeSizeEnum && !(otherBlock is MyFracturedBlock) &&
                    !(otherBlock.Components.Has <MyFractureComponentBase>()))
                {
                    var compound = otherBlock as MyCompoundCubeBlock;
                    if (compound != null)
                    {
                        foreach (var blockInCompound in compound.GetBlocks())
                        {
                            if (blockInCompound != block && !(blockInCompound.FatBlock.Components.Has <MyFractureComponentBase>()))
                            {
                                outBlocks.Add(blockInCompound.FatBlock);
                            }
                        }
                    }
                    else
                    {
                        outBlocks.Add(otherBlock);
                    }
                }
            }

            boxOverlapList.Clear();
        }
        /// <summary>
        /// Writes all surrounding blocks around the given one with the given size.
        /// </summary>
        private void GetSurroundingBlocksFromStaticGrids(MySlimBlock block, MyCubeSize cubeSizeEnum, HashSet <MyCubeBlock> outBlocks)
        {
            outBlocks.Clear();

            BoundingBoxD aabb = new BoundingBoxD(block.Min * block.CubeGrid.GridSize - block.CubeGrid.GridSize / 2, block.Max * block.CubeGrid.GridSize + block.CubeGrid.GridSize / 2);

            if (block.FatBlock != null)
            {
                aabb = (BoundingBoxD)block.FatBlock.Model.BoundingBox;
                aabb.Translate(block.Position * block.CubeGrid.GridSize);
            }

            aabb = aabb.Transform(block.CubeGrid.WorldMatrix);
            aabb.Inflate(0.125);

            List <MyEntity> boxOverlapList = new List <MyEntity>();

            MyEntities.GetElementsInBox(ref aabb, boxOverlapList);

            for (int i = 0; i < boxOverlapList.Count; i++)
            {
                var otherBlock = boxOverlapList[i] as MyCubeBlock;
                if (otherBlock != null && otherBlock.CubeGrid.IsStatic && otherBlock.CubeGrid.EnableSmallToLargeConnections && otherBlock.SlimBlock != block && otherBlock.CubeGrid != block.CubeGrid &&
                    otherBlock.CubeGrid.GridSizeEnum == cubeSizeEnum && !(otherBlock is MyCompoundCubeBlock) && !(otherBlock is MyFracturedBlock))
                {
                    outBlocks.Add(otherBlock);
                }
            }

            boxOverlapList.Clear();
        }
예제 #3
0
        public override BoundingBoxD GetWorldBoundaries()
        {
            //return new BoundingBoxD(Center - Radius, Center + Radius);
            var bbox = new BoundingBoxD(Center - Radius, Center + Radius);

            return(bbox.Transform(Transformation));
        }
예제 #4
0
        public override BoundingBoxD PeekWorldBoundaries(ref Vector3D targetPosition)
        {
            MatrixD newTransformation = Transformation;

            newTransformation.Translation = targetPosition;
            var bbox = new BoundingBoxD(A - Radius, B + Radius);

            return(bbox.Transform(newTransformation));
        }
        public void Init(IMy2DClipmapManager parent, int x, int y, int lod, ref BoundingBox2D bounds)
        {
            m_manager = (MyPlanetEnvironmentComponent)parent;

            var bounds3D = new BoundingBoxD(new Vector3D(bounds.Min, 0), new Vector3D(bounds.Max, 0));

            Lod = lod;

            Face = m_manager.ActiveFace;

            var matrix = m_manager.ActiveClipmap.WorldMatrix;

            bounds3D = bounds3D.Transform(matrix);

            Coords = new Vector2I(x, y);

            Id = MyPlanetSectorId.MakeSectorId(x, y, m_manager.ActiveFace, lod);

            m_manager.RegisterProxy(this);

            MyEnvironmentSectorParameters sectorParams;

            matrix.Translation = Vector3D.Zero;

            sectorParams.SurfaceBasisX = Vector3.Transform(new Vector3(bounds.Width / 2, 0, 0), matrix);
            sectorParams.SurfaceBasisY = Vector3.Transform(new Vector3(0, bounds.Height / 2, 0), matrix);
            sectorParams.Center        = bounds3D.Center;

            if (lod > m_manager.MaxLod)
            {
                return;
            }

            if (!m_manager.TryGetSector(Id, out EnvironmentSector))
            {
                sectorParams.SectorId = Id;
                sectorParams.EntityId = MyPlanetSectorId.MakeSectorId(x, y, m_manager.ActiveFace, lod);

                sectorParams.Bounds = m_manager.GetBoundingShape(ref sectorParams.Center, ref sectorParams.SurfaceBasisX, ref sectorParams.SurfaceBasisY);;

                sectorParams.Environment = m_manager.EnvironmentDefinition;

                sectorParams.DataRange = new BoundingBox2I(Coords << lod, ((Coords + 1) << lod) - 1);

                sectorParams.Provider = m_manager.Providers[m_manager.ActiveFace];

                EnvironmentSector = m_manager.EnvironmentDefinition.CreateSector();
                EnvironmentSector.Init(m_manager, ref sectorParams);

                m_manager.Planet.AddChildEntity((MyEntity)EnvironmentSector);
            }

            m_manager.EnqueueOperation(this, lod);
            LodSet = lod;

            EnvironmentSector.OnLodCommit += sector_OnMyLodCommit;
        }
        public void Init(IMy2DClipmapManager parent, int x, int y, int lod, ref BoundingBox2D bounds)
        {
            m_manager = (MyPlanetEnvironmentComponent)parent;

            var bounds3D = new BoundingBoxD(new Vector3D(bounds.Min, 0), new Vector3D(bounds.Max, 0));
            Lod = lod;

            Face = m_manager.ActiveFace;

            var matrix = m_manager.ActiveClipmap.WorldMatrix;

            bounds3D = bounds3D.Transform(matrix);

            Coords = new Vector2I(x, y);

            Id = MyPlanetSectorId.MakeSectorId(x, y, m_manager.ActiveFace, lod);

            m_manager.RegisterProxy(this);

            MyEnvironmentSectorParameters sectorParams;

            matrix.Translation = Vector3D.Zero;

            sectorParams.SurfaceBasisX = Vector3.Transform(new Vector3(bounds.Width / 2, 0, 0), matrix);
            sectorParams.SurfaceBasisY = Vector3.Transform(new Vector3(0, bounds.Height / 2, 0), matrix);
            sectorParams.Center = bounds3D.Center;

            if (lod > m_manager.MaxLod) return;

            if (!m_manager.TryGetSector(Id, out EnvironmentSector))
            {
                sectorParams.SectorId = Id;
                sectorParams.EntityId = MyPlanetSectorId.MakeSectorId(x, y, m_manager.ActiveFace, lod);

                sectorParams.Bounds = m_manager.GetBoundingShape(ref sectorParams.Center, ref sectorParams.SurfaceBasisX, ref sectorParams.SurfaceBasisY); ;

                sectorParams.Environment = m_manager.EnvironmentDefinition;

                sectorParams.DataRange = new BoundingBox2I(Coords << lod, ((Coords + 1) << lod) - 1);

                sectorParams.Provider = m_manager.Providers[m_manager.ActiveFace];

                EnvironmentSector = m_manager.EnvironmentDefinition.CreateSector();
                EnvironmentSector.Init(m_manager, ref sectorParams);

                m_manager.Planet.AddChildEntity((MyEntity)EnvironmentSector);
            }

            m_manager.EnqueueOperation(this, lod);
            LodSet = lod;

            EnvironmentSector.OnLodCommit += sector_OnMyLodCommit;
        }
        /// <summary>
        /// Writes all surrounding blocks around the given one with the given size.
        /// </summary>
        private void GetSurroundingBlocksFromStaticGrids(MySlimBlock block, MyCubeSize cubeSizeEnum, HashSet <MyCubeBlock> outBlocks)
        {
            outBlocks.Clear();

            BoundingBoxD aabb = new BoundingBoxD(block.Min * block.CubeGrid.GridSize - block.CubeGrid.GridSize / 2, block.Max * block.CubeGrid.GridSize + block.CubeGrid.GridSize / 2);

            if (block.FatBlock != null)
            {
                aabb = (BoundingBoxD)block.FatBlock.Model.BoundingBox;
                aabb.Translate(block.Position * block.CubeGrid.GridSize);
            }

            aabb = aabb.Transform(block.CubeGrid.WorldMatrix);
            aabb.Inflate(0.125);

            List <MyEntity> boxOverlapList = new List <MyEntity>();

            MyEntities.GetElementsInBox(ref aabb, boxOverlapList);

            for (int i = 0; i < boxOverlapList.Count; i++)
            {
                var otherGrid = boxOverlapList[i] as MyCubeGrid;
                if (otherGrid != null)
                {
                    foreach (var gridBlock in otherGrid.CubeBlocks)
                    {
                        if (IsValidForConnection(gridBlock.FatBlock, block, cubeSizeEnum))
                        {
                            if (!IsCompoundAndProcessed(gridBlock.FatBlock, block, outBlocks))
                            {
                                outBlocks.Add(gridBlock.FatBlock);
                            }
                        }
                    }
                }
                else
                {
                    var otherBlock = boxOverlapList[i] as MyCubeBlock;
                    if (IsValidForConnection(otherBlock, block, cubeSizeEnum))
                    {
                        if (!IsCompoundAndProcessed(otherBlock, block, outBlocks))
                        {
                            outBlocks.Add(otherBlock);
                        }
                    }
                }
            }

            boxOverlapList.Clear();
        }
예제 #8
0
        private void buildAttached(int buildID)
        {
            if (last_build_ID == buildID)
            {
                //log("already building for : " + myGrid.DisplayName, "buildAttached()", Logger.severity.TRACE);
                return;
            }
            last_build_ID = buildID;
            log("building for : " + myGrid.DisplayName, "buildAttached()", Logger.severity.TRACE);

            // get all the potentially attached grids
            BoundingBoxD world     = myGrid.WorldAABB;
            MatrixD      scale     = MatrixD.CreateScale(1.1);
            BoundingBoxD searchBox = world.Transform(scale);

            searchBox = searchBox.Translate(world.Center - searchBox.Center);
            foreach (IMyEntity entity in MyAPIGateway.Entities.GetEntitiesInAABB(ref searchBox))
            {
                IMyCubeGrid grid = entity as IMyCubeGrid;
                if (grid == null || grid == myGrid)
                {
                    continue;
                }
                AttachedGrids partner = getFor(grid);
                if (attachedToMe.Contains(partner))
                {
                    //log("already attached: " + grid.DisplayName, "buildAttached()", Logger.severity.TRACE);
                    continue;
                }

                // check each grid for isAttached
                log("checking grid: " + grid.DisplayName, "buildAttached()", Logger.severity.TRACE);
                if (isAttached_piston(partner) || partner.isAttached_piston(this) ||
                    isAttached_motor(partner) || partner.isAttached_motor(this) ||
                    isAttached_connector(partner) || partner.isAttached_connector(this) ||
                    isAttached_landingGear(partner) || partner.isAttached_landingGear(this))
                {
                    continue;
                }
            }
            HashSet <AttachedGrids> copy = new HashSet <AttachedGrids>(attachedToMe);

            foreach (AttachedGrids attached in copy)
            {
                //log("building for attached: " + grid.DisplayName, "buildAttached()", Logger.severity.TRACE);
                attached.buildAttached(buildID);
            }
        }
예제 #9
0
        public override void UpdateWorldAABB()
        {
            CulledObjects.GetAll(m_list, true);

            m_localAABB = BoundingBoxD.CreateInvalid();


            foreach (var element in m_list)
            {
                m_localAABB = m_localAABB.Include(element.WorldAABB);
            }

            m_aabb = m_localAABB.Transform(ref m_worldMatrix);

            base.UpdateWorldAABB();
        }
        public override void UpdateWorldAABB()
        {
            CulledObjects.GetAll(m_list, true);

            m_localAABB = BoundingBoxD.CreateInvalid();
            

            foreach (var element in m_list)
            {
                m_localAABB = m_localAABB.Include(element.WorldAABB);
            }

            m_aabb = m_localAABB.Transform(ref m_worldMatrix);

            base.UpdateWorldAABB();
        }
        /// <summary>
        /// Checks if blocks are neigbors to block(s) in aabbForNeighbors.
        /// </summary>
        private static void CheckNeighborBlocks(MySlimBlock block, BoundingBoxD aabbForNeighbors, MyCubeGrid cubeGrid, List <MySlimBlock> blocks)
        {
            var      compositeTransformToGrid = block.CubeGrid.WorldMatrix * cubeGrid.PositionComp.WorldMatrixNormalizedInv;
            var      aabbForNeighborsInGrid   = aabbForNeighbors.Transform(ref compositeTransformToGrid);
            Vector3I start   = Vector3I.Round(cubeGrid.GridSizeR * aabbForNeighborsInGrid.Min);
            Vector3I end     = Vector3I.Round(cubeGrid.GridSizeR * aabbForNeighborsInGrid.Max);
            Vector3I startIt = Vector3I.Min(start, end);
            Vector3I endIt   = Vector3I.Max(start, end);

            for (int slimBlockIndex = blocks.Count - 1; slimBlockIndex >= 0; --slimBlockIndex)
            {
                var  slimBlock = blocks[slimBlockIndex];
                bool found     = false;

                Vector3I_RangeIterator itBlockInGridPos = new Vector3I_RangeIterator(ref slimBlock.Min, ref slimBlock.Max);
                var posInGrid = itBlockInGridPos.Current;
                for (; itBlockInGridPos.IsValid(); itBlockInGridPos.GetNext(out posInGrid))
                {
                    Vector3I_RangeIterator itBlockPos = new Vector3I_RangeIterator(ref startIt, ref endIt);
                    var pos = itBlockPos.Current;
                    for (; itBlockPos.IsValid(); itBlockPos.GetNext(out pos))
                    {
                        Vector3I diff = Vector3I.Abs(posInGrid - pos);
                        if (pos == posInGrid || diff.X + diff.Y + diff.Z == 1)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found)
                    {
                        break;
                    }
                }

                if (!found)
                {
                    blocks.RemoveAt(slimBlockIndex);
                }
            }
        }
예제 #12
0
        private bool QueryEmptyOrFull(int minX, int minY, int minZ, int maxX, int maxY, int maxZ)
        {
            ////return false;
            var bb = new BoundingBox(new Vector3(minX, minY, minZ), new Vector3(maxX, maxY, maxZ));

            if (bb.Volume() < 100)
            {
                return(false);
            }

            //bb.Translate(m_voxelMap.StorageMin);
            var result = m_voxelMap.Storage.Intersect(ref bb, false) != ContainmentType.Intersects;

            {
                var bbd = new BoundingBoxD(new Vector3(minX, minY, minZ) * 8, new Vector3(maxX, maxY, maxZ) * 8);
                bbd.Transform(Entity.WorldMatrix);
                var obb = new MyOrientedBoundingBoxD(bbd, Entity.WorldMatrix);
                MyRenderProxy.DebugDrawAABB(bbd, result ? Color.Green : Color.Red, 1, 1, false);
            }
            return(result);
        }
                public void Init(IMy2DClipmapManager parent, int x, int y, int lod, ref BoundingBox2D bounds)
                {
                    m_parent = (SectorTreeComponent)parent;

                    Bounds = new BoundingBoxD(new Vector3D(bounds.Min, 0), new Vector3D(bounds.Max, 50));
                    Lod    = lod;

                    var matrix = m_parent.m_tree[m_parent.m_activeClipmap].WorldMatrix;

                    Bounds = Bounds.Transform(matrix);

                    Coords = new Vector2I(x, y);

                    m_parent.m_handlers.Add(this);

                    var center = Bounds.Center;

                    // Sector Frustum
                    Vector3D[] v = new Vector3D[8];

                    v[0] = Vector3D.Transform(new Vector3D(bounds.Min.X, bounds.Min.Y, 0), matrix);
                    v[1] = Vector3D.Transform(new Vector3D(bounds.Max.X, bounds.Min.Y, 0), matrix);
                    v[2] = Vector3D.Transform(new Vector3D(bounds.Min.X, bounds.Max.Y, 0), matrix);
                    v[3] = Vector3D.Transform(new Vector3D(bounds.Max.X, bounds.Max.Y, 0), matrix);

                    for (int i = 0; i < 4; ++i)
                    {
                        //v[i] -= WorldMatrix.Translation;
                        v[i].Normalize();
                        v[i + 4] = v[i] * m_parent.Radius;
                        v[i]    *= m_parent.Radius + 50;

                        //v[i] += WorldMatrix.Translation;
                        //v[i + 4] += WorldMatrix.Translation;
                    }

                    FrustumBounds = v;
                }
        private static bool TestPlacementVoxelMapPenetration(
            MyVoxelBase voxelMap, 
            ref MyGridPlacementSettings settings,
            ref BoundingBoxD localAabb,
            ref MatrixD worldMatrix,
            bool touchingStaticGrid = false)
        {
            ProfilerShort.Begin("TestPlacementVoxelMapPenetration");
            var worldAabb = localAabb.Transform(ref worldMatrix);

            float penetrationAmountNormalized = 0f;
            float penetrationRatio = 0f;
            float penetrationVolume = 0f;
            if (voxelMap != null)
            {
                float unused;
                penetrationAmountNormalized = voxelMap.GetVoxelContentInBoundingBox_Obsolete(worldAabb, out unused);
                penetrationVolume = penetrationAmountNormalized * MyVoxelConstants.VOXEL_VOLUME_IN_METERS;
                penetrationRatio = penetrationVolume / (float)worldAabb.Volume;
            }

            bool penetrationTestPassed = true;
            switch (settings.Penetration.Unit)
            {
                case MyGridPlacementSettings.PenetrationUnitEnum.Absolute:
                    penetrationTestPassed = penetrationVolume <= settings.Penetration.MaxAllowed &&
                        (penetrationVolume >= settings.Penetration.MinAllowed || (settings.CanAnchorToStaticGrid && touchingStaticGrid));
                    break;

                case MyGridPlacementSettings.PenetrationUnitEnum.Ratio:
                    penetrationTestPassed = penetrationRatio <= settings.Penetration.MaxAllowed &&
                        (penetrationRatio >= settings.Penetration.MinAllowed || (settings.CanAnchorToStaticGrid && touchingStaticGrid));
                    break;

                default:
                    Debug.Fail("Invalid branch.");
                    break;
            }

            ProfilerShort.End();

            return penetrationTestPassed;
        }
        private static bool TestPlacementAreaInternal(MyCubeGrid targetGrid,
           bool targetGridIsStatic,
           ref MyGridPlacementSettings settings,
           MyCubeBlockDefinition blockDefinition,
           MyBlockOrientation? blockOrientation,
           ref BoundingBoxD localAabb,
           MyEntity ignoredEntity,
           ref MatrixD worldMatrix,
           out MyCubeGrid touchingGrid,
           bool dynamicBuildMode = false,
           bool ignoreFracturedPieces = false,
           bool testVoxels = true)
        {
            ProfilerShort.Begin("TestPlacementAreaInternal");

            touchingGrid = null;

            float gridSize = targetGrid != null ? targetGrid.GridSize : (blockDefinition != null ? MyDefinitionManager.Static.GetCubeSize(blockDefinition.CubeSize) : MyDefinitionManager.Static.GetCubeSize(MyCubeSize.Large));
            bool isStatic = targetGridIsStatic;

            var worldAabb = localAabb.Transform(ref worldMatrix);

            bool entityOverlap = false;
            MyVoxelBase overlappedVoxelMap = null;
            bool touchingStaticGrid = false;
            foreach (var collison in m_physicsBoxQueryList)
            {
                var entity = collison.Body.GetEntity(0) as MyEntity;
                if (entity == null)
                    continue;

                if (ignoreFracturedPieces && (entity is MyFracturedPiece))
                    continue;

                if (entity.GetPhysicsBody().WeldInfo.Children.Count == 0 && ignoredEntity != null && (entity == ignoredEntity || entity.GetTopMostParent() == ignoredEntity))
                    continue;

                var body = entity.Physics;
                if (body != null && body.IsPhantom)
                    continue;

                var voxelMap = entity as MyVoxelBase;
                if (voxelMap != null)
                {
                    overlappedVoxelMap = voxelMap;
                    continue;
                }

                var grid = entity as MyCubeGrid;
				if (grid == null)
				{
					var subPart = entity as MyEntitySubpart;
					if (subPart != null)
					{
						grid = subPart.GetTopMostParent() as MyCubeGrid;
					}
				}

                if(entity.GetPhysicsBody().WeldInfo.Children.Count > 0)
                {
                    if(entity != ignoredEntity)
                    {
                        //Vector4 min,max;
                        //entity.Physics.GetShape().GetLocalAABB(0.05f, out min, out max);
                        //MyOrientedBoundingBoxD obb = new MyOrientedBoundingBoxD(new BoundingBoxD(new Vector3D(min.X, min.Y, min.Z), new Vector3D(max.X, max.Y, max.Z)), entity.WorldMatrix);
                        //MyRenderProxy.DebugDrawOBB(obb, Color.Green, 0.5f, false, false);
                        //obb = new MyOrientedBoundingBoxD(new BoundingBoxD(-m_lastQueryBox.HalfExtents, m_lastQueryBox.HalfExtents), m_lastQueryTransform);
                        //MyRenderProxy.DebugDrawOBB(obb, Color.Red, 0.5f, false, false);
                        if (TestQueryIntersection(entity.GetPhysicsBody().GetShape(), entity.WorldMatrix))
                        {
                            entityOverlap = true;
                            if(touchingGrid == null)
                                touchingGrid = entity as MyCubeGrid;
                            break;
                        }
                    }

                    foreach(var child in entity.GetPhysicsBody().WeldInfo.Children)
                    {
                        if(child.Entity == ignoredEntity)
                            continue;
                        if(TestQueryIntersection(child.WeldedRigidBody.GetShape(), child.Entity.WorldMatrix))
                        {
                            if (touchingGrid == null)
                                touchingGrid = child.Entity as MyCubeGrid;
                            entityOverlap = true;
                            break;
                        }
                    }
                    if (entityOverlap)
                        break;
                    continue;
                }

                if (grid != null && ((isStatic && grid.IsStatic)
                    || (MyFakes.ENABLE_DYNAMIC_SMALL_GRID_MERGING && !isStatic && !grid.IsStatic && blockDefinition != null && blockDefinition.CubeSize == grid.GridSizeEnum)
                    || (MyFakes.ENABLE_BLOCK_PLACEMENT_ON_VOXEL && isStatic && grid.IsStatic && blockDefinition != null && blockDefinition.CubeSize == grid.GridSizeEnum)))
                {
                    // Small on large (or large on small) always possible
                    if (isStatic == grid.IsStatic && gridSize != grid.GridSize)
                        continue;

                    // first check if world orientation is correct.
                    if(!IsOrientationsAligned(grid.WorldMatrix, worldMatrix))
                    {
                        entityOverlap = true;
                        continue;
                    }

                    TestGridPlacement(ref settings, ref worldMatrix, ref touchingGrid, gridSize, isStatic, ref localAabb, blockDefinition, blockOrientation, ref entityOverlap, ref touchingStaticGrid, grid);
                    if (entityOverlap)
                    {
                        break;
                    }
                    continue;
                }


                entityOverlap = true;
                break;
            }
            m_tmpResultList.Clear();
            m_physicsBoxQueryList.Clear();
            ProfilerShort.End();

            if (entityOverlap)
                return false;
            if (testVoxels)
                return TestVoxelOverlap(ref settings, ref localAabb, ref worldMatrix, ref worldAabb, ref overlappedVoxelMap, touchingStaticGrid);
            return true;
        }
        public static bool TestPlacementVoxelMapOverlap(
            MyVoxelBase voxelMap,
            ref MyGridPlacementSettings settings,
            ref BoundingBoxD localAabb,
            ref MatrixD worldMatrix,
            bool touchingStaticGrid = false)
        {
            ProfilerShort.Begin("TestPlacementVoxelMapOverlap");

            var worldAabb = localAabb.Transform(ref worldMatrix);

            const int IntersectsOrInside = 1;
            const int Outside = 2;

            int overlapState = Outside;

            if (voxelMap == null)
                voxelMap = MySession.Static.VoxelMaps.GetVoxelMapWhoseBoundingBoxIntersectsBox(ref worldAabb, null);

            if (voxelMap != null && voxelMap.IsAnyAabbCornerInside(ref worldMatrix, localAabb))
            {
                overlapState = IntersectsOrInside;
            }

            bool testPassed = true;

            switch (overlapState)
            {
                case IntersectsOrInside:
                    testPassed = settings.Penetration.MaxAllowed > 0;
                    break;
                case Outside:
                    testPassed = settings.Penetration.MinAllowed <= 0 || (settings.CanAnchorToStaticGrid && touchingStaticGrid);
                    break;
                default:
                    Debug.Fail("Invalid branch.");
                    break;
            }

            ProfilerShort.End();

            return testPassed;
        }
        private static bool TestPlacementAreaInternalWithEntities(MyCubeGrid targetGrid,
         bool targetGridIsStatic,
         ref MyGridPlacementSettings settings,
         ref BoundingBoxD localAabb,
         MyEntity ignoredEntity,
         ref MatrixD worldMatrix,
         bool dynamicBuildMode = false)
        {
            ProfilerShort.Begin("TestPlacementAreaInternalWithEntities");

            MyCubeGrid touchingGrid = null;

            float gridSize = targetGrid.GridSize;
            bool isStatic = targetGridIsStatic;

            var worldAabb = localAabb.Transform(ref worldMatrix);

            bool entityOverlap = false;
            MyVoxelBase overlappedVoxelMap = null;
            bool touchingStaticGrid = false;
            foreach (var entity in m_tmpResultList)
            {
                if (ignoredEntity != null && (entity == ignoredEntity || entity.GetTopMostParent() == ignoredEntity))
                    continue;

                var body = entity.Physics;
                if (body == null)
                    continue;

                var voxelMap = entity as MyVoxelBase;
                if (voxelMap != null)
                {
                    overlappedVoxelMap = voxelMap;
                    continue;
                }

                var grid = entity as MyCubeGrid;
                if (grid != null)
                {
                    // Small on large (or large on small) always possible
                    if (isStatic == grid.IsStatic && gridSize != grid.GridSize)
                        continue;

                    TestGridPlacement(ref settings, ref worldMatrix, ref touchingGrid, gridSize, isStatic, ref localAabb, null, null, ref entityOverlap, ref touchingStaticGrid, grid);

                    if (entityOverlap)
                    {
                        break;
                    }
                }
                else
                {
                    var character = entity as MyCharacter;
                    if (character != null && character.PositionComp.WorldAABB.Intersects(targetGrid.PositionComp.WorldAABB))
                    {
                        entityOverlap = true;
                        break;
                    }
                }
            }

            m_tmpResultList.Clear();
            ProfilerShort.End();

            if (entityOverlap)
                return false;

            if (targetGrid.IsStatic)
            {
                return true;
            }

            ProfilerShort.Begin("TestVoxelOverlap");
            bool result = TestVoxelOverlap(ref settings, ref localAabb, ref worldMatrix, ref worldAabb, ref overlappedVoxelMap, touchingStaticGrid);
            ProfilerShort.End();

            return result;
        }
        private static void TestGridPlacement(ref MyGridPlacementSettings settings, ref MatrixD worldMatrix, ref MyCubeGrid touchingGrid, float gridSize, bool isStatic, ref BoundingBoxD localAABB, MyCubeBlockDefinition blockDefinition,
           MyBlockOrientation? blockOrientation, ref bool entityOverlap, ref bool touchingStaticGrid, MyCubeGrid grid)
        {
            var worldAabb = localAABB.Transform(ref worldMatrix);
            var invWorldMatrix = grid.PositionComp.WorldMatrixNormalizedInv;
            var otherLocalAabb = worldAabb.Transform(ref invWorldMatrix);

            Vector3D minToWorld = Vector3D.Transform(localAABB.Min, worldMatrix);
            Vector3D maxToWorld = Vector3D.Transform(localAABB.Max, worldMatrix);
            Vector3D tempMinLocal = Vector3D.Transform(minToWorld, invWorldMatrix);
            Vector3D tempMaxLocal = Vector3D.Transform(maxToWorld, invWorldMatrix);

            Vector3D otherMinLocal = Vector3D.Min(tempMinLocal, tempMaxLocal);
            Vector3D otherMaxLocal = Vector3D.Max(tempMinLocal, tempMaxLocal);

            var scaledMin = (otherMinLocal + gridSize / 2) / grid.GridSize;
            var scaledMax = (otherMaxLocal - gridSize / 2) / grid.GridSize;
            var tempMin = Vector3I.Round(scaledMin);
            var tempMax = Vector3I.Round(scaledMax);
            var min = Vector3I.Min(tempMin, tempMax);
            var max = Vector3I.Max(tempMin, tempMax);

            MyBlockOrientation? gridBlockOrientation = null;
            if (MyFakes.ENABLE_COMPOUND_BLOCKS && isStatic && grid.IsStatic && blockOrientation != null)
            {
                Matrix blockRotation;
                blockOrientation.Value.GetMatrix(out blockRotation);
                Matrix rotationInGrid = blockRotation * worldMatrix;
                rotationInGrid = rotationInGrid * invWorldMatrix;
                rotationInGrid.Translation = Vector3.Zero;

                Base6Directions.Direction forwardDir = Base6Directions.GetForward(ref rotationInGrid);
                Base6Directions.Direction upDir = Base6Directions.GetUp(ref rotationInGrid);
                if (Base6Directions.IsValidBlockOrientation(forwardDir, upDir))
                    gridBlockOrientation = new MyBlockOrientation(forwardDir, upDir);
            }

            if (!grid.CanAddCubes(min, max, gridBlockOrientation, blockDefinition))
            {
                entityOverlap = true;
                return;
            }

            if (settings.CanAnchorToStaticGrid && grid.IsTouchingAnyNeighbor(min, max))
            {
                touchingStaticGrid = true;
                if (touchingGrid == null)
                    touchingGrid = grid;
            }
        }
예제 #19
0
 public void GetWorldBoundingBox(out BoundingBoxD aabb)
 {
     if (FatBlock != null)
     {
         aabb = FatBlock.PositionComp.WorldAABB;
     }
     else
     {
         float gridSize = CubeGrid.GridSize;
         aabb = new BoundingBoxD(Min * gridSize - gridSize / 2, Max * gridSize + gridSize / 2);
         aabb = aabb.Transform(CubeGrid.WorldMatrix);
     }
 }
예제 #20
0
        public override void UpdateWorldAABB()
        {
            m_aabb = m_localAABB.Transform(ref m_worldMatrix);

            base.UpdateWorldAABB();
        }
        /// <summary>
        /// Writes all surrounding blocks around the given one with the given size.
        /// </summary>
        private void GetSurroundingBlocksFromStaticGrids(MySlimBlock block, MyCubeSize cubeSizeEnum, HashSet <MySlimBlock> outBlocks)
        {
            outBlocks.Clear();

            BoundingBoxD aabbForNeighbors = new BoundingBoxD(block.Min * block.CubeGrid.GridSize, block.Max * block.CubeGrid.GridSize);
            BoundingBoxD aabb             = new BoundingBoxD(block.Min * block.CubeGrid.GridSize - block.CubeGrid.GridSize / 2, block.Max * block.CubeGrid.GridSize + block.CubeGrid.GridSize / 2);

            if (block.FatBlock != null)
            {
                var aabbCenter = aabb.Center;
                aabb = (BoundingBoxD)block.FatBlock.Model.BoundingBox;
                Matrix m;
                block.FatBlock.Orientation.GetMatrix(out m);
                aabb = aabb.Transform(m);
                aabb.Translate(aabbCenter);
            }

            aabb.Inflate(0.125);
            var aabbWorld = aabb.Transform(block.CubeGrid.WorldMatrix);

            List <MyEntity> boxOverlapList = new List <MyEntity>();

            MyEntities.GetElementsInBox(ref aabbWorld, boxOverlapList);

            for (int i = 0; i < boxOverlapList.Count; i++)
            {
                var cubeGrid = boxOverlapList[i] as MyCubeGrid;
                if (cubeGrid != null)
                {
                    if (cubeGrid.IsStatic && cubeGrid != block.CubeGrid && cubeGrid.EnableSmallToLargeConnections && cubeGrid.SmallToLargeConnectionsInitialized &&
                        cubeGrid.GridSizeEnum == cubeSizeEnum)
                    {
                        Debug.Assert(m_tmpSlimBlocksList.Count == 0);
                        m_tmpSlimBlocksList.Clear();
                        cubeGrid.GetBlocksIntersectingOBB(aabb, block.CubeGrid.WorldMatrix, m_tmpSlimBlocksList);

                        CheckNeighborBlocks(block, aabbForNeighbors, cubeGrid, m_tmpSlimBlocksList);

                        foreach (var slimBlock in m_tmpSlimBlocksList)
                        {
                            if (slimBlock.FatBlock != null)
                            {
                                if (slimBlock.FatBlock is MyFracturedBlock)
                                {
                                    continue;
                                }

                                if (slimBlock.FatBlock.Components.Has <MyFractureComponentBase>())
                                {
                                    continue;
                                }

                                if (slimBlock.FatBlock is MyCompoundCubeBlock)
                                {
                                    foreach (var blockInCompound in (slimBlock.FatBlock as MyCompoundCubeBlock).GetBlocks())
                                    {
                                        if (!(blockInCompound.FatBlock.Components.Has <MyFractureComponentBase>()))
                                        {
                                            outBlocks.Add(blockInCompound);
                                        }
                                    }
                                }
                                else
                                {
                                    outBlocks.Add(slimBlock);
                                }
                            }
                            else
                            {
                                outBlocks.Add(slimBlock);
                            }
                        }

                        m_tmpSlimBlocksList.Clear();
                    }
                }
            }

            boxOverlapList.Clear();
        }
예제 #22
0
        private static bool TestPlacementAreaInternal(MyCubeGrid targetGrid,
           bool targetGridIsStatic,
           ref MyGridPlacementSettings settings,
           MyCubeBlockDefinition blockDefinition,
           MyBlockOrientation? blockOrientation,
           ref BoundingBoxD localAabb,
           MyEntity ignoredEntity,
           ref MatrixD worldMatrix,
           out MyCubeGrid touchingGrid,
           bool dynamicBuildMode = false)
        {
            ProfilerShort.Begin("TestPlacementAreaInternal");

            touchingGrid = null;

            float gridSize = targetGrid != null ? targetGrid.GridSize : (blockDefinition != null ? MyDefinitionManager.Static.GetCubeSize(blockDefinition.CubeSize) : MyDefinitionManager.Static.GetCubeSize(MyCubeSize.Large));
            bool isStatic = targetGridIsStatic;

            var worldAabb = localAabb.Transform(ref worldMatrix);

            bool entityOverlap = false;
            MyVoxelBase overlappedVoxelMap = null;
            bool touchingStaticGrid = false;
            foreach (var rigidBody in m_physicsBoxQueryList)
            {
                var entity = rigidBody.GetEntity();
                if (entity == null)
                    continue;

                if (ignoredEntity != null && (entity == ignoredEntity || entity.GetTopMostParent() == ignoredEntity))
                    continue;

                var body = rigidBody.GetBody();
                if (body != null && body.IsPhantom)
                    continue;

                var voxelMap = entity as MyVoxelMap;
                if (voxelMap != null)
                {
                    overlappedVoxelMap = voxelMap;
                    continue;
                }

                var grid = entity as MyCubeGrid;
                if (grid != null && ((isStatic && grid.IsStatic)
                    || (MyFakes.ENABLE_DYNAMIC_SMALL_GRID_MERGING && !isStatic && !grid.IsStatic && blockDefinition!= null && blockDefinition.CubeSize == grid.GridSizeEnum)
                    || (MyFakes.ENABLE_BLOCK_PLACEMENT_ON_VOXEL && isStatic && grid.IsStatic && blockDefinition != null && blockDefinition.CubeSize == grid.GridSizeEnum)))
                {
                    // Small on large (or large on small) always possible
                    if (isStatic == grid.IsStatic && gridSize != grid.GridSize)
                        continue;

                    var invWorldMatrix = grid.PositionComp.WorldMatrixNormalizedInv;
                    var otherLocalAabb = worldAabb.Transform(ref invWorldMatrix);

					var scaledMin = otherLocalAabb.Min / grid.GridSize;
                    var scaledMax = otherLocalAabb.Max / grid.GridSize;
                    var min = Vector3I.Round(scaledMin);
                    var max = Vector3I.Round(scaledMax);

                    MyBlockOrientation? gridBlockOrientation = null;
                    if (MyFakes.ENABLE_COMPOUND_BLOCKS && isStatic && grid.IsStatic && blockOrientation != null)
                    {
                        Matrix blockRotation;
                        blockOrientation.Value.GetMatrix(out blockRotation);
                        Matrix rotationInGrid = blockRotation * worldMatrix;
                        rotationInGrid = rotationInGrid * invWorldMatrix;
                        rotationInGrid.Translation = Vector3.Zero;

                        gridBlockOrientation = new MyBlockOrientation(ref rotationInGrid);
                    }

                    if (!grid.CanAddCubes(min, max, gridBlockOrientation, blockDefinition))
                    {
                        entityOverlap = true;
                        break;
                    }

                    if (settings.CanAnchorToStaticGrid && grid.IsTouchingAnyNeighbor(min, max))
                    {
                        touchingStaticGrid = true;
                        if (touchingGrid == null)
                            touchingGrid = grid;
                    }

                    continue;
                }


                entityOverlap = true;
                break;
            }
            m_physicsBoxQueryList.Clear();
            ProfilerShort.End();

            if (entityOverlap)
                return false;

            ProfilerShort.Begin("VoxelOverlap");
            if (overlappedVoxelMap == null)
            { // Havok only detects overlap with voxel map surface. This test will detect a voxel map even if we're fully inside it.
                //BoundingSphere sphere = new BoundingSphere(Vector3.Transform(targetGrid.LocalAABB.Center, worldMatrix), targetGrid.LocalAABB.Size.AbsMin() / 2.0f);
                //overlappedVoxelMap = MySession.Static.VoxelMaps.GetOverlappingWithSphere(ref sphere);
                overlappedVoxelMap = MySession.Static.VoxelMaps.GetVoxelMapWhoseBoundingBoxIntersectsBox(ref worldAabb, null);
                if (overlappedVoxelMap != null)
                {
                    float cellCount = 0;
                    var res = overlappedVoxelMap.GetVoxelContentInBoundingBox(worldAabb, out cellCount);
                    if (res < 0.01f)
                        overlappedVoxelMap = null;
                }
                //using (m_tmpResultList.GetClearToken())
                //{
                //    MyGamePruningStructure.GetAllEntitiesInBox(ref worldAabb, m_tmpResultList);

                //    foreach (var entity in m_tmpResultList)
                //    {
                //        MyVoxelMap voxelMap = entity as MyVoxelMap;
                //        if (voxelMap != null)
                //        {
                //            if (voxelMap.DoOverlapSphereTest((float)localAabb.Size.AbsMin() / 2.0f, worldAabb.Center))
                //            {
                //                overlappedVoxelMap = voxelMap;
                //                break;
                //            }
                //        }
                //    }
                //}
            }
            ProfilerShort.End();
            

            return TestPlacementVoxelMapPenetration(overlappedVoxelMap, ref settings, ref localAabb, ref worldMatrix, touchingStaticGrid: touchingStaticGrid);
        }
        //public static bool TestPlacementAreaWithEntities(MyCubeGrid targetGrid, bool targetGridIsStatic, ref MyGridPlacementSettings settings, BoundingBoxD localAabb, bool dynamicBuildMode, MyEntity ignoredEntity = null)
        //{
        //    ProfilerShort.Begin("Test start with entities");
        //    var worldMatrix = targetGrid.WorldMatrix;

        //    Vector3 halfExtents = localAabb.HalfExtents;
        //    halfExtents += settings.SearchHalfExtentsDeltaAbsolute; //this works for SE
        //    if (MyFakes.ENABLE_BLOCK_PLACING_IN_OCCUPIED_AREA)
        //        halfExtents -= new Vector3D(GRID_PLACING_AREA_FIX_VALUE);
        //    Vector3D translation = localAabb.Transform(ref worldMatrix).Center;
        //    Quaternion quaternion = Quaternion.CreateFromRotationMatrix(worldMatrix);
        //    quaternion.Normalize();
        //    ProfilerShort.End();

        //    ProfilerShort.Begin("get top most entities");

        //    m_tmpResultList.Clear();
        //    BoundingBoxD box = targetGrid.PositionComp.WorldAABB;
        //    MyGamePruningStructure.GetTopMostEntitiesInBox(ref box, m_tmpResultList);
        //    ProfilerShort.End();

        //    return TestPlacementAreaInternalWithEntities(targetGrid, targetGridIsStatic, ref settings, ref localAabb, ignoredEntity, ref worldMatrix, dynamicBuildMode: dynamicBuildMode);
        //}

        public static bool TestPlacementArea(MyCubeGrid targetGrid, bool targetGridIsStatic, ref MyGridPlacementSettings settings, BoundingBoxD localAabb, bool dynamicBuildMode, MyEntity ignoredEntity = null, bool testVoxel = true)
        {
            ProfilerShort.Begin("TestStart");
            var worldMatrix = targetGrid.WorldMatrix;

            Vector3 halfExtents = localAabb.HalfExtents;
            halfExtents += settings.SearchHalfExtentsDeltaAbsolute; //this works for SE
            if (MyFakes.ENABLE_BLOCK_PLACING_IN_OCCUPIED_AREA)
                halfExtents -= new Vector3D(GRID_PLACING_AREA_FIX_VALUE);
            Vector3D translation = localAabb.Transform(ref worldMatrix).Center;
            Quaternion quaternion = Quaternion.CreateFromRotationMatrix(worldMatrix);
            quaternion.Normalize();
            ProfilerShort.End();

            ProfilerShort.Begin("VoxelOverlap");

            if (testVoxel && settings.VoxelPlacement.Value.PlacementMode != VoxelPlacementMode.Both)
            {
                bool result = IsAabbInsideVoxel(worldMatrix, localAabb, settings);

                if (settings.VoxelPlacement.Value.PlacementMode == VoxelPlacementMode.InVoxel)
                    result = !result;

                if (result)
                {
                    ProfilerShort.End();
                    return false;
                }
            }

            ProfilerShort.End();

            ProfilerShort.Begin("Havok.GetPenetrationsBox");

            Debug.Assert(m_physicsBoxQueryList.Count == 0, "List not cleared");
            MyPhysics.GetPenetrationsBox(ref halfExtents, ref translation, ref quaternion, m_physicsBoxQueryList, MyPhysics.CollisionLayers.NoVoxelCollisionLayer);
            m_lastQueryBox.HalfExtents = halfExtents;
            m_lastQueryTransform = MatrixD.CreateFromQuaternion(quaternion);
            m_lastQueryTransform.Translation = translation;
            ProfilerShort.End();

            MyCubeGrid touchingGrid;
            return TestPlacementAreaInternal(targetGrid, targetGridIsStatic, ref settings, null, null, ref localAabb, ignoredEntity, ref worldMatrix, out touchingGrid, dynamicBuildMode);
        }
예제 #24
0
        public override BoundingBoxD GetWorldBoundaries()
        {
            var bbox = new BoundingBoxD(A - Radius, B + Radius);

            return(bbox.Transform(Transformation));
        }
예제 #25
0
 public override BoundingBoxD GetWorldBoundaries()
 {
     return(Boundaries.Transform(Transformation));
 }
예제 #26
0
        /// <summary>
        /// Calculates amount of volume of a bounding box in voxels.
        /// </summary>
        /// <param name="localAabb">Local bounding box to query for.</param>
        /// <param name="worldMatrix">World matrix of the bounding box.</param>
        /// <returns>Pair of floats where 1st value is Volume amount and 2nd value is ratio of Volume amount to Whole volume.</returns>
        public MyTuple<float,float> GetVoxelContentInBoundingBox_Fast(BoundingBoxD localAabb, MatrixD worldMatrix)
        {
            MatrixD toVoxel = worldMatrix * PositionComp.WorldMatrixNormalizedInv;
            MatrixD toGrid; MatrixD.Invert(ref toVoxel, out toGrid);

            BoundingBoxD transAABB = localAabb.Transform(toVoxel);
            transAABB.Translate(SizeInMetresHalf + StorageMin);
            Vector3I minI = Vector3I.Floor(transAABB.Min);
            Vector3I maxI = Vector3I.Ceiling(transAABB.Max);

            double vol = localAabb.Volume / MyVoxelConstants.VOXEL_VOLUME_IN_METERS;
            int K = Math.Max((MathHelper.Log2Ceiling((int)vol) - MathHelper.Log2Ceiling(100)) / 3, 0);
            float voxelSizeAtLod = MyVoxelConstants.VOXEL_SIZE_IN_METRES * (1 << K);
            float voxelVolumeAtLod = voxelSizeAtLod * voxelSizeAtLod * voxelSizeAtLod;
            minI >>= K;
            maxI >>= K;

           // localAabb.Inflate(1 * voxelSizeAtLod);

            var offset = ((Size >> 1) + StorageMin) >> K;

            m_tempStorage.Resize(maxI - minI + 1);
            Storage.ReadRange(m_tempStorage, MyStorageDataTypeFlags.Content, K, minI, maxI);

            float resultVolume = 0;
            float resultPercent = 0;
            int hitVolumeBoxes = 0;

            MyOrientedBoundingBoxD worldbbox = new MyOrientedBoundingBoxD(localAabb, worldMatrix);

            Vector3I coord, cache;
            for (coord.Z = minI.Z, cache.Z = 0; coord.Z <= maxI.Z; coord.Z++, cache.Z++)
            {
                for (coord.Y = minI.Y, cache.Y = 0; coord.Y <= maxI.Y; coord.Y++, cache.Y++)
                {
                    for (coord.X = minI.X, cache.X = 0; coord.X <= maxI.X; coord.X++, cache.X++)
                    {
                        Vector3D voxelPos = (coord - offset) * voxelSizeAtLod;

                        Vector3D gridPoint;
                        Vector3D.Transform(ref voxelPos, ref toGrid, out gridPoint);

                        ContainmentType cont;
                        //localAabb.Contains(ref gridPoint, out cont);

                        var voxelToWorld = WorldMatrix;
                        voxelToWorld.Translation -= (Vector3D)StorageMin + SizeInMetresHalf;

                        BoundingBoxD voxelBox = new BoundingBoxD();
                        voxelBox.Min = ((Vector3D)(coord) - .5) * voxelSizeAtLod;
                        voxelBox.Max = ((Vector3D)(coord) + .5) * voxelSizeAtLod;

                        MyOrientedBoundingBoxD voxelBbox = new MyOrientedBoundingBoxD(voxelBox, voxelToWorld);

                        cont = worldbbox.Contains(ref voxelBbox);

                        if (cont == ContainmentType.Disjoint)
                        {
                            //VRageRender.MyRenderProxy.DebugDrawOBB(
                            //new MyOrientedBoundingBoxD(voxelBox, voxelToWorld), Color.Red, 0.1f,
                            //true, false);
                            continue;
                        }

                        float content = m_tempStorage.Content(ref cache) / MyVoxelConstants.VOXEL_CONTENT_FULL_FLOAT;



                        //VRageRender.MyRenderProxy.DebugDrawOBB(voxelBbox, Color.Aqua, content,
                        //   true, false);

                        resultVolume += content * voxelVolumeAtLod;
                        resultPercent += content;
                        hitVolumeBoxes++;
                    }
                }
            }

            resultPercent /= hitVolumeBoxes; 
            //float localAABBVol = (float)localAabb.Volume;
            //if (localAABBVol < resultVolume)
            //    resultPercent *= (float)localAabb.Volume / resultVolume;


            //VRageRender.MyRenderProxy.DebugDrawOBB(worldbbox, Color.Yellow, 0,
            //                true, false);
            //VRageRender.MyRenderProxy.DebugWaitForFrameFinish();


            return new MyTuple<float, float>(resultVolume, resultPercent);
        }
예제 #27
0
        private static bool TestPlacementAreaInternal(MyCubeGrid targetGrid,
           bool targetGridIsStatic,
           ref MyGridPlacementSettings settings,
           MyCubeBlockDefinition blockDefinition,
           MyBlockOrientation? blockOrientation,
           ref BoundingBoxD localAabb,
           MyEntity ignoredEntity,
           ref MatrixD worldMatrix,
           out MyCubeGrid touchingGrid,
           bool dynamicBuildMode = false)
        {
            ProfilerShort.Begin("TestPlacementAreaInternal");

            touchingGrid = null;

            float gridSize = targetGrid != null ? targetGrid.GridSize : (blockDefinition != null ? MyDefinitionManager.Static.GetCubeSize(blockDefinition.CubeSize) : MyDefinitionManager.Static.GetCubeSize(MyCubeSize.Large));
            bool isStatic = targetGridIsStatic;

            var worldAabb = localAabb.Transform(ref worldMatrix);

            bool entityOverlap = false;
            MyVoxelBase overlappedVoxelMap = null;
            bool touchingStaticGrid = false;
            foreach (var rigidBody in m_physicsBoxQueryList)
            {
                var entity = rigidBody.GetEntity();
                if (entity == null)
                    continue;

                if (ignoredEntity != null && (entity == ignoredEntity || entity.GetTopMostParent() == ignoredEntity))
                    continue;

                var body = rigidBody.GetBody();
                if (body != null && body.IsPhantom)
                    continue;

                var voxelMap = entity as MyVoxelBase;
                if (voxelMap != null)
                {
                    overlappedVoxelMap = voxelMap;
                    continue;
                }

                var grid = entity as MyCubeGrid;
                if (grid != null && ((isStatic && grid.IsStatic)
                    || (MyFakes.ENABLE_DYNAMIC_SMALL_GRID_MERGING && !isStatic && !grid.IsStatic && blockDefinition != null && blockDefinition.CubeSize == grid.GridSizeEnum)
                    || (MyFakes.ENABLE_BLOCK_PLACEMENT_ON_VOXEL && isStatic && grid.IsStatic && blockDefinition != null && blockDefinition.CubeSize == grid.GridSizeEnum)))
                {
                    // Small on large (or large on small) always possible
                    if (isStatic == grid.IsStatic && gridSize != grid.GridSize)
                        continue;

                    TestGridPlacement(ref settings, ref worldMatrix, ref touchingGrid, gridSize, isStatic, ref worldAabb, blockDefinition, blockOrientation, ref entityOverlap, ref touchingStaticGrid, grid);
                    if (entityOverlap)
                    {
                        break;
                    }
                    continue;
                }


                entityOverlap = true;
                break;
            }
            m_tmpResultList.Clear();
            m_physicsBoxQueryList.Clear();
            ProfilerShort.End();

            if (entityOverlap)
                return false;

            return TestVoxelOverlap(ref settings, ref localAabb, ref worldMatrix, ref worldAabb, ref overlappedVoxelMap, touchingStaticGrid);
        }
예제 #28
0
 public override BoundingBoxD GetWorldBoundaries()
 {
     var bbox = new BoundingBoxD(A - Radius, B + Radius);
     return bbox.Transform(Transformation);
 }
        public static bool TestPlacementAreaWithEntities(MyCubeGrid targetGrid, bool targetGridIsStatic, ref MyGridPlacementSettings settings, BoundingBoxD localAabb, bool dynamicBuildMode, MyEntity ignoredEntity = null)
        {
            ProfilerShort.Begin("Test start with entities");
            var worldMatrix = targetGrid.WorldMatrix;

            Vector3 halfExtents = localAabb.HalfExtents;
            halfExtents += settings.SearchHalfExtentsDeltaAbsolute; //this works for SE
            if (MyFakes.ENABLE_BLOCK_PLACING_IN_OCCUPIED_AREA)
                halfExtents -= new Vector3D(GRID_PLACING_AREA_FIX_VALUE);
            Vector3D translation = localAabb.Transform(ref worldMatrix).Center;
            Quaternion quaternion = Quaternion.CreateFromRotationMatrix(worldMatrix);
            quaternion.Normalize();
            ProfilerShort.End();

            ProfilerShort.Begin("get top most entities");

            m_tmpResultList.Clear();
            BoundingBoxD box = targetGrid.PositionComp.WorldAABB;
            MyGamePruningStructure.GetTopMostEntitiesInBox(ref box, m_tmpResultList);
            ProfilerShort.End();

            return TestPlacementAreaInternalWithEntities(targetGrid, targetGridIsStatic, ref settings, ref localAabb, ignoredEntity, ref worldMatrix, dynamicBuildMode: dynamicBuildMode);
        }
예제 #30
0
 public override BoundingBoxD PeekWorldBoundaries(ref Vector3D targetPosition)
 {
     MatrixD newTransformation = Transformation;
     newTransformation.Translation = targetPosition;
     var bbox = new BoundingBoxD(A - Radius, B + Radius);
     return bbox.Transform(newTransformation);
 }
        public static bool TestBlockPlacementArea(MyCubeBlockDefinition blockDefinition, MyBlockOrientation? blockOrientation, MatrixD worldMatrix, ref MyGridPlacementSettings settings, BoundingBoxD localAabb, bool dynamicBuildMode,
            MyEntity ignoredEntity = null)
        {
            ProfilerShort.Begin("TestStart");
            Vector3 halfExtents = localAabb.HalfExtents;
            halfExtents += settings.SearchHalfExtentsDeltaAbsolute; //this works for SE
            if (MyFakes.ENABLE_BLOCK_PLACING_IN_OCCUPIED_AREA)
                halfExtents -= new Vector3D(GRID_PLACING_AREA_FIX_VALUE);
            Vector3D translation = localAabb.Transform(ref worldMatrix).Center;
            Quaternion quaternion = Quaternion.CreateFromRotationMatrix(worldMatrix);
            quaternion.Normalize();
            ProfilerShort.End();

            ProfilerShort.Begin("Havok.GetPenetrationsBox");
            Debug.Assert(m_physicsBoxQueryList.Count == 0, "List not cleared");
            MyPhysics.GetPenetrationsBox(ref halfExtents, ref translation, ref quaternion, m_physicsBoxQueryList, MyPhysics.CollisionLayers.CharacterCollisionLayer);
            m_lastQueryBox.HalfExtents = halfExtents;
            m_lastQueryTransform = MatrixD.CreateFromQuaternion(quaternion);
            m_lastQueryTransform.Translation = translation;
            ProfilerShort.End();

            MyCubeGrid touchingGrid;
            return TestPlacementAreaInternal(null, ref settings, blockDefinition, blockOrientation, ref localAabb, ignoredEntity, ref worldMatrix, out touchingGrid, dynamicBuildMode: dynamicBuildMode);
        }
        /// <summary>
        /// Checks if aabb is in voxel. If settings provided it will return false if penetration settings allow for it.
        /// </summary>
        /// <param name="worldMatrix">World matrix of the aabb.</param>
        /// <param name="localAabb">Local aabb</param>
        /// <param name="settings">Game settings</param>
        /// <returns></returns>
        public static bool IsAabbInsideVoxel(MatrixD worldMatrix, BoundingBoxD localAabb, MyGridPlacementSettings settings)
        {

            var worldAabb = localAabb.Transform(ref worldMatrix);

            List<MyVoxelBase> voxels = new List<MyVoxelBase>();
            MyGamePruningStructure.GetAllVoxelMapsInBox(ref worldAabb, voxels);

            foreach (MyVoxelBase voxel in voxels)
            {
                if (settings.VoxelPlacement.Value.PlacementMode != VoxelPlacementMode.Volumetric && voxel.IsAnyAabbCornerInside(ref worldMatrix, localAabb))
                    return true;

                if (settings.VoxelPlacement.Value.PlacementMode == VoxelPlacementMode.Volumetric && !TestPlacementVoxelMapPenetration(voxel, settings, ref localAabb, ref worldMatrix))
                    return true;
            }

            return false;
        }
 public override BoundingBoxD GetWorldBoundaries()
 {
     //return new BoundingBoxD(Center - Radius, Center + Radius);
     var bbox = new BoundingBoxD(Center - Radius, Center + Radius);
     return bbox.Transform(Transformation);
 }
예제 #34
0
        /// <summary>
        /// Calculates amount of volume of a bounding box in voxels.
        /// </summary>
        /// <param name="localAabb">Local bounding box to query for.</param>
        /// <param name="worldMatrix">World matrix of the bounding box.</param>
        /// <returns>Pair of floats where 1st value is Volume amount and 2nd value is ratio of Volume amount to Whole volume.</returns>
        public MyTuple <float, float> GetVoxelContentInBoundingBox_Fast(BoundingBoxD localAabb, MatrixD worldMatrix)
        {
            MatrixD toVoxel = worldMatrix * PositionComp.WorldMatrixNormalizedInv;
            MatrixD toGrid; MatrixD.Invert(ref toVoxel, out toGrid);

            BoundingBoxD transAABB = localAabb.Transform(toVoxel);

            transAABB.Translate(SizeInMetresHalf + StorageMin);
            Vector3I minI = Vector3I.Floor(transAABB.Min);
            Vector3I maxI = Vector3I.Ceiling(transAABB.Max);

            double vol              = localAabb.Volume / MyVoxelConstants.VOXEL_VOLUME_IN_METERS;
            int    K                = Math.Max((MathHelper.Log2Ceiling((int)vol) - MathHelper.Log2Ceiling(100)) / 3, 0);
            float  voxelSizeAtLod   = MyVoxelConstants.VOXEL_SIZE_IN_METRES * (1 << K);
            float  voxelVolumeAtLod = voxelSizeAtLod * voxelSizeAtLod * voxelSizeAtLod;

            minI >>= K;
            maxI >>= K;

            // localAabb.Inflate(1 * voxelSizeAtLod);

            var offset = ((Size >> 1) + StorageMin) >> K;

            m_tempStorage.Resize(maxI - minI + 1);
            Storage.ReadRange(m_tempStorage, MyStorageDataTypeFlags.Content, K, minI, maxI);

            float resultVolume   = 0;
            float resultPercent  = 0;
            int   hitVolumeBoxes = 0;

            MyOrientedBoundingBoxD worldbbox = new MyOrientedBoundingBoxD(localAabb, worldMatrix);

            Vector3I coord, cache;

            for (coord.Z = minI.Z, cache.Z = 0; coord.Z <= maxI.Z; coord.Z++, cache.Z++)
            {
                for (coord.Y = minI.Y, cache.Y = 0; coord.Y <= maxI.Y; coord.Y++, cache.Y++)
                {
                    for (coord.X = minI.X, cache.X = 0; coord.X <= maxI.X; coord.X++, cache.X++)
                    {
                        Vector3D voxelPos = (coord - offset) * voxelSizeAtLod;

                        Vector3D gridPoint;
                        Vector3D.Transform(ref voxelPos, ref toGrid, out gridPoint);

                        ContainmentType cont;
                        //localAabb.Contains(ref gridPoint, out cont);

                        var voxelToWorld = WorldMatrix;
                        voxelToWorld.Translation -= (Vector3D)StorageMin + SizeInMetresHalf;

                        BoundingBoxD voxelBox = new BoundingBoxD();
                        voxelBox.Min = ((Vector3D)(coord) - .5) * voxelSizeAtLod;
                        voxelBox.Max = ((Vector3D)(coord) + .5) * voxelSizeAtLod;

                        MyOrientedBoundingBoxD voxelBbox = new MyOrientedBoundingBoxD(voxelBox, voxelToWorld);

                        cont = worldbbox.Contains(ref voxelBbox);

                        if (cont == ContainmentType.Disjoint)
                        {
                            //VRageRender.MyRenderProxy.DebugDrawOBB(
                            //new MyOrientedBoundingBoxD(voxelBox, voxelToWorld), Color.Red, 0.1f,
                            //true, false);
                            continue;
                        }

                        float content = m_tempStorage.Content(ref cache) / MyVoxelConstants.VOXEL_CONTENT_FULL_FLOAT;



                        //VRageRender.MyRenderProxy.DebugDrawOBB(voxelBbox, Color.Aqua, content,
                        //   true, false);

                        resultVolume  += content * voxelVolumeAtLod;
                        resultPercent += content;
                        hitVolumeBoxes++;
                    }
                }
            }

            resultPercent /= hitVolumeBoxes;
            //float localAABBVol = (float)localAabb.Volume;
            //if (localAABBVol < resultVolume)
            //    resultPercent *= (float)localAabb.Volume / resultVolume;


            //VRageRender.MyRenderProxy.DebugDrawOBB(worldbbox, Color.Yellow, 0,
            //                true, false);
            //VRageRender.MyRenderProxy.DebugWaitForFrameFinish();


            return(new MyTuple <float, float>(resultVolume, resultPercent));
        }
예제 #35
0
 public void UpdateWorldAABB(out BoundingBoxD worldAabb)
 {
     worldAabb   = m_localAABB.Transform(ref m_worldMatrix);
     m_worldAABB = worldAabb;
 }
        private bool QueryEmptyOrFull(int minX, int minY, int minZ, int maxX, int maxY, int maxZ)
        {
            ////return false;
            var bb = new BoundingBox(new Vector3(minX, minY, minZ), new Vector3(maxX, maxY, maxZ));
            if (bb.Volume() < 100)
                return false;

            //bb.Translate(m_voxelMap.StorageMin);
            var result = m_voxelMap.Storage.Intersect(ref bb, false) != ContainmentType.Intersects;
            {
                var bbd = new BoundingBoxD(new Vector3(minX, minY, minZ) * 8, new Vector3(maxX, maxY, maxZ) * 8);
                bbd.Transform(Entity.WorldMatrix);
                var obb = new MyOrientedBoundingBoxD(bbd, Entity.WorldMatrix);
                MyRenderProxy.DebugDrawAABB(bbd, result ? Color.Green : Color.Red, 1, 1, false);
            }
            return result;
        }
예제 #37
0
        private void UpdateGizmo_VoxelMap(MyCubeBuilderGizmo.MyGizmoSpaceProperties gizmoSpace, bool add, bool remove, bool draw)
        {
            Color green = new Color(Color.Green * 0.6f, 1f);
            Color red = new Color(Color.Red * 0.8f, 1);
            Color yellow = Color.Yellow;
            Color blue = Color.Blue;
            //Vector4 black = Color.Black.ToVector4();
            Color gray = Color.Gray;

            float gridSize = MyDefinitionManager.Static.GetCubeSize(CurrentBlockDefinition.CubeSize);

            Vector3 temp;
            Vector3D worldCenter = Vector3D.Zero;
            Vector3D worldPos = gizmoSpace.m_worldMatrixAdd.Translation;
            MatrixD drawMatrix = gizmoSpace.m_worldMatrixAdd;

            Color color = green;

            UpdateShowGizmoCube(gizmoSpace, gridSize);

            int posIndex = 0;
            for (temp.X = 0; temp.X < CurrentBlockDefinition.Size.X; temp.X++)
                for (temp.Y = 0; temp.Y < CurrentBlockDefinition.Size.Y; temp.Y++)
                    for (temp.Z = 0; temp.Z < CurrentBlockDefinition.Size.Z; temp.Z++)
                    {
                        color = gizmoSpace.m_buildAllowed ? green : gray;

                        Vector3I gridPosition = gizmoSpace.m_positions[posIndex++];
                        Vector3D tempWorldPos = gridPosition * gridSize;
                        if (!MyPerGameSettings.BuildingSettings.StaticGridAlignToCenter)
                            tempWorldPos -= 0.5 * gridSize;

                        worldCenter += tempWorldPos;

                        drawMatrix.Translation = tempWorldPos;

                        MyCubeGrid.GetCubeParts(CurrentBlockDefinition, gridPosition, gizmoSpace.m_localMatrixAdd.GetOrientation(), gridSize, gizmoSpace.m_cubeModelsTemp, gizmoSpace.m_cubeMatricesTemp, gizmoSpace.m_cubeNormals, gizmoSpace.m_patternOffsets);

                        if (gizmoSpace.m_showGizmoCube)
                        {
                            for (int i = 0; i < gizmoSpace.m_cubeMatricesTemp.Count; i++)
                            {
                                MatrixD modelMatrix = gizmoSpace.m_cubeMatricesTemp[i];
                                modelMatrix.Translation = tempWorldPos;
                                gizmoSpace.m_cubeMatricesTemp[i] = modelMatrix;
                            }

                            m_gizmo.AddFastBuildParts(gizmoSpace, CurrentBlockDefinition, null);
                            m_gizmo.UpdateGizmoCubeParts(gizmoSpace, m_renderData, ref MatrixD.Identity);
                        }
                    }


            //calculate world center for block model
            worldCenter /= CurrentBlockDefinition.Size.Size;
            drawMatrix.Translation = worldCenter;

            BoundingBoxD localAABB = new BoundingBoxD(-CurrentBlockDefinition.Size * gridSize * 0.5f, CurrentBlockDefinition.Size * gridSize * 0.5f);

            var settings = CurrentBlockDefinition.CubeSize == MyCubeSize.Large ? MyPerGameSettings.BuildingSettings.LargeStaticGrid : MyPerGameSettings.BuildingSettings.SmallStaticGrid;
            MyBlockOrientation blockOrientation = new MyBlockOrientation(ref Quaternion.Identity);
            bool placementTest = CheckValidBlockRotation(gizmoSpace.m_worldMatrixAdd, CurrentBlockDefinition.Direction, CurrentBlockDefinition.Rotation)
                && MyCubeGrid.TestBlockPlacementArea(CurrentBlockDefinition, blockOrientation, drawMatrix, ref settings, localAABB, false);
            gizmoSpace.m_buildAllowed &= placementTest;
            gizmoSpace.m_buildAllowed &= gizmoSpace.m_showGizmoCube;
            gizmoSpace.m_worldMatrixAdd = drawMatrix;

            if (MySession.Static.SurvivalMode && !DeveloperSpectatorIsBuilding)
            {
                BoundingBoxD gizmoBox = localAABB.Transform(ref drawMatrix);

                if (!MyCubeBuilderGizmo.DefaultGizmoCloseEnough(ref MatrixD.Identity, gizmoBox, gridSize, IntersectionDistance) || MySession.GetCameraControllerEnum() == MyCameraControllerEnum.Spectator)
                {
                    gizmoSpace.m_buildAllowed = false;
                    gizmoSpace.m_showGizmoCube = false;
                    gizmoSpace.m_removeBlock = null;
                    return;
                }

                if (!MySession.Static.SimpleSurvival && MySession.ControlledEntity is MyCharacter)
                {
                    gizmoSpace.m_buildAllowed &= (MySession.ControlledEntity as MyCharacter).CanStartConstruction(CurrentBlockDefinition);
                }

                if (MySession.Static.SimpleSurvival)
                {
                    gizmoSpace.m_buildAllowed &= CanBuildBlockSurvivalTime();
                }
            }


            //color = gizmoSpace.m_buildAllowed ? green : gray;
            color = Color.White;
            string lineMaterial = gizmoSpace.m_buildAllowed ? "GizmoDrawLine" : "GizmoDrawLineRed";

            if (gizmoSpace.SymmetryPlane == MySymmetrySettingModeEnum.Disabled)
            {
                MySimpleObjectDraw.DrawTransparentBox(ref drawMatrix,
                    ref localAABB, ref color, MySimpleObjectRasterizer.Wireframe, 1, 0.04f, lineMaterial: lineMaterial);

                m_rotationHints.CalculateRotationHints(drawMatrix, localAABB, !MyHud.MinimalHud && MySandboxGame.Config.RotationHints && draw && MyFakes.ENABLE_ROTATION_HINTS);
            }

            gizmoSpace.m_cubeMatricesTemp.Clear();
            gizmoSpace.m_cubeModelsTemp.Clear();

            if (gizmoSpace.m_showGizmoCube)
            {
                // Draw mount points of added cube block as yellow squares in neighboring cells.
                if (MyDebugDrawSettings.ENABLE_DEBUG_DRAW && MyDebugDrawSettings.DEBUG_DRAW_MOUNT_POINTS)
                {
                    DrawMountPoints(gridSize, CurrentBlockDefinition, ref drawMatrix);
                }

                AddFastBuildModels(gizmoSpace, MatrixD.Identity, gizmoSpace.m_cubeMatricesTemp, gizmoSpace.m_cubeModelsTemp, gizmoSpace.m_blockDefinition);

                Debug.Assert(gizmoSpace.m_cubeMatricesTemp.Count == gizmoSpace.m_cubeModelsTemp.Count);
                for (int i = 0; i < gizmoSpace.m_cubeMatricesTemp.Count; ++i)
                {
                    string model = gizmoSpace.m_cubeModelsTemp[i];
                    if (!string.IsNullOrEmpty(model))
                        m_renderData.AddInstance(MyModel.GetId(model), gizmoSpace.m_cubeMatricesTemp[i], ref MatrixD.Identity);
                }
            }

        }