private static void DrawConnections(HkdBreakableShape breakableShape, MatrixD worldMatrix, float alpha, ref int shapeIndex, string customText = null, bool isPhantom = false)
        {
            List <HkdConnection> connections = new List <HkdConnection>();

            breakableShape.GetConnectionList(connections);

            List <HkdShapeInstanceInfo> children = new List <HkdShapeInstanceInfo>();

            breakableShape.GetChildren(children);

            foreach (var conn in connections)
            {
                var posA = DebugShapesPositions[conn.ShapeAName];
                var posB = DebugShapesPositions[conn.ShapeBName];

                bool cont = false;
                foreach (var child in children)
                {
                    if ((child.ShapeName == conn.ShapeAName) || (child.ShapeName == conn.ShapeBName))
                    {
                        cont = true;
                    }
                }

                if (cont)
                {
                    VRageRender.MyRenderProxy.DebugDrawLine3D(posA, posB, Color.White, Color.White, false);
                }
            }
        }
        private static void ConvertAllShapesToFractureComponentShapeBuilder(HkdBreakableShape shape, ref Matrix shapeRotation, MyBlockOrientation blockOrientation, HashSet <string> names, MyObjectBuilder_FractureComponentCubeBlock fractureComponentBuilder)
        {
            var name = shape.Name;

            if (names.Contains(name))
            {
                MyBlockOrientation shapeOrientation = new MyBlockOrientation(ref shapeRotation);
                if (shapeOrientation == blockOrientation)
                {
                    MyObjectBuilder_FractureComponentCubeBlock.FracturedShape builderShape = new MyObjectBuilder_FractureComponentBase.FracturedShape();
                    builderShape.Name  = name;
                    builderShape.Fixed = MyDestructionHelper.IsFixed(shape);

                    fractureComponentBuilder.Shapes.Add(builderShape);
                }
            }

            if (shape.GetChildrenCount() > 0)
            {
                List <HkdShapeInstanceInfo> children = new List <HkdShapeInstanceInfo>();
                shape.GetChildren(children);
                foreach (var child in children)
                {
                    var childShapeRotation = child.GetTransform();
                    ConvertAllShapesToFractureComponentShapeBuilder(child.Shape, ref childShapeRotation, blockOrientation, names, fractureComponentBuilder);
                }
            }
        }
        private static void DrawBreakableShape(HkdBreakableShape breakableShape, MatrixD worldMatrix, float alpha, ref int shapeIndex, string customText = null, bool isPhantom = false)
        {
            //VRageRender.MyRenderProxy.DebugDrawText3D(worldMatrix.Translation, , Color.White, 1, false);
            DrawCollisionShape(breakableShape.GetShape(), worldMatrix, alpha, ref shapeIndex, breakableShape.Name + " Strength: " + breakableShape.GetStrenght() + " Static:" + breakableShape.IsFixed());

            if (!string.IsNullOrEmpty(breakableShape.Name) && breakableShape.Name != "PineTree175m_v2_001" && breakableShape.IsFixed())
            {
            }

            DebugShapesPositions[breakableShape.Name] = worldMatrix.Translation;

            List <HkdShapeInstanceInfo> children = new List <HkdShapeInstanceInfo>();

            breakableShape.GetChildren(children);

            Vector3 parentCom = breakableShape.CoM;

            foreach (var shapeInst in children)
            {
                Matrix transform = shapeInst.GetTransform();
                //  transform.Translation += (shapeInst.Shape.CoM - parentCom);
                Matrix trWorld = transform * worldMatrix * Matrix.CreateTranslation(Vector3.Right * 2);
                DrawBreakableShape(shapeInst.Shape, trWorld, alpha, ref shapeIndex);
            }
        }
        protected void SerializeInternal(MyObjectBuilder_FractureComponentBase ob)
        {
            Debug.Assert(m_tmpChildren.Count == 0);

            if (string.IsNullOrEmpty(Shape.Name) || Shape.IsCompound() || Shape.GetChildrenCount() > 0)
            {
                Shape.GetChildren(m_tmpChildren);
                foreach (var child in m_tmpChildren)
                {
                    var shape = new MyObjectBuilder_FractureComponentCubeBlock.FracturedShape()
                    {
                        Name  = child.ShapeName,
                        Fixed = MyDestructionHelper.IsFixed(child.Shape)
                    };
                    ob.Shapes.Add(shape);
                }
                m_tmpChildren.Clear();
            }
            else
            {
                ob.Shapes.Add(new MyObjectBuilder_FractureComponentCubeBlock.FracturedShape()
                {
                    Name = Shape.Name
                });
            }
        }
Example #5
0
        /// <summary>
        /// Sets model from havok to render component of this entity.
        /// </summary>
        public void SetDataFromHavok(HkdBreakableShape shape)
        {
            ProfilerShort.Begin("FP.SetDataFromHavok");
            Shape = shape;
            Shape.AddReference();
            if (Render != null)
            {
                if (shape.IsCompound() || string.IsNullOrEmpty(shape.Name))
                {
                    shape.GetChildren(m_shapeInfos);
                    Debug.Assert(m_shapeInfos.Count > 0);
                    foreach (var shapeInstanceInfo in m_shapeInfos)
                    {
                        //System.Diagnostics.Debug.Assert(shapeInstanceInfo.IsValid(), "Invalid shapeInstanceInfo!");
                        if (shapeInstanceInfo.IsValid())
                        {
                            Render.AddPiece(shapeInstanceInfo.ShapeName, shapeInstanceInfo.GetTransform());
                        }
                    }

                    m_shapeInfos.Clear();
                }
                else
                {
                    Render.AddPiece(shape.Name, Matrix.Identity);
                }
            }
            ProfilerShort.End();

            m_hitPoints = Shape.Volume * 100;
        }
        private void CreatePieceData(MyModel model, HkdBreakableShape breakableShape)
        {
            //Root shape for fractured compound blocks
            {
                var msg = VRageRender.MyRenderProxy.PrepareAddRuntimeModel();
                ProfilerShort.Begin("GetDataFromShape");
                m_tmpMesh.Data = msg.ModelData;
                MyDestructionData.Static.Storage.GetDataFromShape(breakableShape, m_tmpMesh);
                System.Diagnostics.Debug.Assert(msg.ModelData.Sections.Count > 0, "Invalid data");
                if (msg.ModelData.Sections.Count > 0)
                {
                    if (MyFakes.USE_HAVOK_MODELS)
                    {
                        msg.ReplacedModel = model.AssetName;
                    }
                    VRageRender.MyRenderProxy.AddRuntimeModel(breakableShape.ShapeName, msg);
                }
                ProfilerShort.End();
            }

            using (m_tmpChildrenList.GetClearToken())
            {
                breakableShape.GetChildren(m_tmpChildrenList);
                LoadChildrenShapes(m_tmpChildrenList);
            }
        }
Example #7
0
        private static void DrawConnections(HkdBreakableShape breakableShape, MatrixD worldMatrix, float alpha, ref int shapeIndex, string customText = null, bool isPhantom = false)
        {
            List <HkdConnection> resultList = new List <HkdConnection>();

            breakableShape.GetConnectionList(resultList);
            List <HkdShapeInstanceInfo> list = new List <HkdShapeInstanceInfo>();

            breakableShape.GetChildren(list);
            foreach (HkdConnection connection in resultList)
            {
                Vector3D pointFrom = DebugShapesPositions[connection.ShapeAName];
                Vector3D pointTo   = DebugShapesPositions[connection.ShapeBName];
                bool     flag      = false;
                foreach (HkdShapeInstanceInfo info in list)
                {
                    if ((info.ShapeName == connection.ShapeAName) || (info.ShapeName == connection.ShapeBName))
                    {
                        flag = true;
                    }
                }
                if (flag)
                {
                    MyRenderProxy.DebugDrawLine3D(pointFrom, pointTo, Color.White, Color.White, false, false);
                }
            }
        }
 public static bool IsFixed(HkdBreakableShape breakableShape)
 {
     System.Diagnostics.Debug.Assert(m_tmpInfos.Count == 0, "");
     if (!breakableShape.IsValid())
     {
         return(false);
     }
     if ((breakableShape.UserObject & (uint)HkdBreakableShape.Flags.IS_FIXED) != 0)
     {
         return(true);
     }
     else
     {
         Debug.Assert(m_tmpInfos.Count == 0);
         breakableShape.GetChildren(m_tmpInfos);
         foreach (var child in m_tmpInfos)
         {
             if ((child.Shape.UserObject & (uint)HkdBreakableShape.Flags.IS_FIXED) != 0)
             {
                 m_tmpInfos.Clear();
                 return(true);
             }
         }
         m_tmpInfos.Clear();
     }
     return(false);
 }
Example #9
0
 public static bool IsFixed(HkdBreakableShape breakableShape)
 {
     if (breakableShape.IsValid())
     {
         if ((breakableShape.UserObject & 4) != 0)
         {
             return(true);
         }
         breakableShape.GetChildren(m_tmpInfos);
         using (List <HkdShapeInstanceInfo> .Enumerator enumerator = m_tmpInfos.GetEnumerator())
         {
             while (true)
             {
                 if (!enumerator.MoveNext())
                 {
                     break;
                 }
                 HkdShapeInstanceInfo current = enumerator.Current;
                 HkdBreakableShape    shape   = current.Shape;
                 if ((shape.UserObject & 4) != 0)
                 {
                     m_tmpInfos.Clear();
                     return(true);
                 }
             }
         }
         m_tmpInfos.Clear();
     }
     return(false);
 }
 public static bool IsFixed(HkdBreakableShape breakableShape)
 {
     System.Diagnostics.Debug.Assert(m_tmpInfos.Count == 0, "");
     if (!breakableShape.IsValid())
         return false;
     if ((breakableShape.UserObject & (uint)HkdBreakableShape.Flags.IS_FIXED) != 0)
     {
         return true;
     }
     else
     {
         Debug.Assert(m_tmpInfos.Count == 0);
         breakableShape.GetChildren(m_tmpInfos);
         foreach (var child in m_tmpInfos)
         {
             if ((child.Shape.UserObject & (uint)HkdBreakableShape.Flags.IS_FIXED) != 0)
             {
                 m_tmpInfos.Clear();
                 return true;
             }
         }
         m_tmpInfos.Clear();
     }
     return false;
 }
Example #11
0
        private void CreateBreakableShape(MyEnvironmentItemDefinition itemDefinition, ref MyEnvironmentItems.MyEnvironmentItemData itemData, ref Vector3D hitWorldPosition, Vector3 hitNormal, float forceMultiplier, string fallSound = "")
        {
            HkdBreakableShape oldBreakableShape = MyModels.GetModelOnlyData(itemDefinition.Model).HavokBreakableShapes[0].Clone();
            MatrixD           transformMatrix   = itemData.Transform.TransformMatrix;

            oldBreakableShape.SetMassRecursively(500f);
            oldBreakableShape.SetStrenghtRecursively(5000f, 0.7f);
            oldBreakableShape.GetChildren(base.m_childrenTmp);
            HkdBreakableShape[] havokBreakableShapes = MyModels.GetModelOnlyData(itemDefinition.Model).HavokBreakableShapes;
            Vector3D.Transform(hitWorldPosition, MatrixD.Normalize(MatrixD.Invert(transformMatrix)));
            float num = (float)(hitWorldPosition.Y - itemData.Transform.Position.Y);
            List <HkdShapeInstanceInfo> shapeList = new List <HkdShapeInstanceInfo>();
            List <HkdShapeInstanceInfo> list2     = new List <HkdShapeInstanceInfo>();
            HkdShapeInstanceInfo?       nullable  = null;

            foreach (HkdShapeInstanceInfo info in base.m_childrenTmp)
            {
                if ((nullable == null) || (info.CoM.Y < nullable.Value.CoM.Y))
                {
                    nullable = new HkdShapeInstanceInfo?(info);
                }
                if (info.CoM.Y > num)
                {
                    list2.Add(info);
                }
                else
                {
                    shapeList.Add(info);
                }
            }
            if (shapeList.Count != 2)
            {
                if ((shapeList.Count == 0) && list2.Remove(nullable.Value))
                {
                    shapeList.Add(nullable.Value);
                }
            }
            else if ((shapeList[0].CoM.Y < shapeList[1].CoM.Y) && (num < (shapeList[1].CoM.Y + 1.25f)))
            {
                list2.Insert(0, shapeList[1]);
                shapeList.RemoveAt(1);
            }
            else if ((shapeList[0].CoM.Y > shapeList[1].CoM.Y) && (num < (shapeList[0].CoM.Y + 1.25f)))
            {
                list2.Insert(0, shapeList[0]);
                shapeList.RemoveAt(0);
            }
            if (shapeList.Count > 0)
            {
                CreateFracturePiece(itemDefinition, oldBreakableShape, transformMatrix, hitNormal, shapeList, forceMultiplier, true, "");
            }
            if (list2.Count > 0)
            {
                CreateFracturePiece(itemDefinition, oldBreakableShape, transformMatrix, hitNormal, list2, forceMultiplier, false, fallSound);
            }
            base.m_childrenTmp.Clear();
        }
        private bool GetCurrentFracturedShapeList(HkdBreakableShape breakableShape, List <MyObjectBuilder_FractureComponentBase.FracturedShape> shapeList, string[] excludeShapeNames = null)
        {
            var  shapeName      = breakableShape.Name;
            bool shapeNameEmpty = string.IsNullOrEmpty(shapeName);

            if (excludeShapeNames != null && !shapeNameEmpty)
            {
                foreach (var shapeNameToRemove in excludeShapeNames)
                {
                    if (shapeName == shapeNameToRemove)
                    {
                        return(false);
                    }
                }
            }

            if (breakableShape.GetChildrenCount() > 0)
            {
                List <HkdShapeInstanceInfo> shapeInst = new List <HkdShapeInstanceInfo>();
                breakableShape.GetChildren(shapeInst);

                bool allChildrenAdded = true;
                foreach (var inst in shapeInst)
                {
                    allChildrenAdded &= GetCurrentFracturedShapeList(inst.Shape, shapeList, excludeShapeNames: excludeShapeNames);
                }

                if (!shapeNameEmpty && allChildrenAdded)
                {
                    foreach (var inst in shapeInst)
                    {
                        shapeList.RemoveAll(s => s.Name == inst.ShapeName);
                    }

                    shapeList.Add(new MyObjectBuilder_FractureComponentBase.FracturedShape()
                    {
                        Name = shapeName, Fixed = breakableShape.IsFixed()
                    });
                }

                return(allChildrenAdded);
            }
            else
            {
                if (!shapeNameEmpty)
                {
                    shapeList.Add(new MyObjectBuilder_FractureComponentBase.FracturedShape()
                    {
                        Name = shapeName, Fixed = breakableShape.IsFixed()
                    });
                    return(true);
                }
            }

            return(false);
        }
        private void DisableRefCountRec(HkdBreakableShape bShape)
        {
            bShape.DisableRefCount();
            var lst = new List <HkdShapeInstanceInfo>();

            bShape.GetChildren(lst);
            foreach (var child in lst)
            {
                DisableRefCountRec(child.Shape);
            }
        }
 private static void RenameShapesRecursive(HkdBreakableShape shape, string shapeSuffix)
 {
     shape.Name += shapeSuffix;
     using (PoolManager.Get(out List <HkdShapeInstanceInfo> shapes))
     {
         shape.GetChildren(shapes);
         foreach (var child in shapes)
         {
             RenameShapesRecursive(child.Shape, shapeSuffix);
         }
     }
 }
 private static void CollectShapeNamesRecursive(HkdBreakableShape shape, HashSet <string> shapeName)
 {
     shapeName.Add(shape.Name);
     using (PoolManager.Get(out List <HkdShapeInstanceInfo> shapes))
     {
         shape.GetChildren(shapes);
         foreach (var child in shapes)
         {
             CollectShapeNamesRecursive(child.Shape, shapeName);
         }
     }
 }
        private static void ConvertAllShapesToFractureComponentShapeBuilder(HkdBreakableShape shape, ref Matrix shapeRotation, MyBlockOrientation blockOrientation,
                                                                            HashSet <Tuple <string, float> > namesAndBuildProgress, MyObjectBuilder_FractureComponentCubeBlock fractureComponentBuilder, out float buildProgress)
        {
            buildProgress = 1f;

            var name = shape.Name;
            Tuple <string, float> foundTuple = null;

            foreach (var tuple in namesAndBuildProgress)
            {
                if (tuple.Item1 == name)
                {
                    foundTuple = tuple;
                    break;
                }
            }

            if (foundTuple != null)
            {
                MyBlockOrientation shapeOrientation = new MyBlockOrientation(ref shapeRotation);
                if (shapeOrientation == blockOrientation)
                {
                    MyObjectBuilder_FractureComponentCubeBlock.FracturedShape builderShape = new MyObjectBuilder_FractureComponentBase.FracturedShape();
                    builderShape.Name  = name;
                    builderShape.Fixed = MyDestructionHelper.IsFixed(shape);

                    fractureComponentBuilder.Shapes.Add(builderShape);
                    buildProgress = foundTuple.Item2;
                }
            }

            if (shape.GetChildrenCount() > 0)
            {
                List <HkdShapeInstanceInfo> children = new List <HkdShapeInstanceInfo>();
                shape.GetChildren(children);
                foreach (var child in children)
                {
                    var   childShapeRotation = child.GetTransform();
                    float localBuildProgress;
                    ConvertAllShapesToFractureComponentShapeBuilder(child.Shape, ref childShapeRotation, blockOrientation, namesAndBuildProgress,
                                                                    fractureComponentBuilder, out localBuildProgress);

                    if (foundTuple == null)
                    {
                        buildProgress = localBuildProgress;
                    }
                }
            }
        }
Example #17
0
        public void SetDataFromCompound(HkdBreakableShape compound)
        {
            MyRenderComponentFracturedPiece render = this.Render;

            if (render != null)
            {
                compound.GetChildren(m_shapeInfos);
                foreach (HkdShapeInstanceInfo info in m_shapeInfos)
                {
                    if (info.IsValid())
                    {
                        render.AddPiece(info.ShapeName, info.GetTransform());
                    }
                }
                m_shapeInfos.Clear();
            }
        }
Example #18
0
        public static void GetAllBlockBreakableShapeNames(HkdBreakableShape shape, HashSet <Tuple <string, float> > outNamesAndBuildProgress, float buildProgress)
        {
            string name = shape.Name;

            if (!string.IsNullOrEmpty(name))
            {
                outNamesAndBuildProgress.Add(new Tuple <string, float>(name, buildProgress));
            }
            if (shape.GetChildrenCount() > 0)
            {
                List <HkdShapeInstanceInfo> list = new List <HkdShapeInstanceInfo>();
                shape.GetChildren(list);
                foreach (HkdShapeInstanceInfo info in list)
                {
                    GetAllBlockBreakableShapeNames(info.Shape, outNamesAndBuildProgress, buildProgress);
                }
            }
        }
        public virtual void SetShape(HkdBreakableShape shape, bool compound)
        {
            Debug.Assert(shape.IsValid());

            if (Shape.IsValid())
            {
                Shape.RemoveReference();
            }

            Shape = shape;

            var render = Entity.Render as MyRenderComponentFracturedPiece;

            Debug.Assert(render != null);

            if (render != null)
            {
                render.ClearModels();

                if (compound)
                {
                    Debug.Assert(m_tmpChildren.Count == 0);

                    shape.GetChildren(m_tmpChildren);

                    foreach (var shapeInstanceInfo in m_tmpChildren)
                    {
                        System.Diagnostics.Debug.Assert(shapeInstanceInfo.IsValid(), "Invalid shapeInstanceInfo!");
                        if (shapeInstanceInfo.IsValid())
                        {
                            render.AddPiece(shapeInstanceInfo.ShapeName, Matrix.Identity);
                        }
                    }

                    m_tmpChildren.Clear();
                }
                else
                {
                    render.AddPiece(shape.Name, Matrix.Identity);
                }

                render.UpdateRenderObject(true);
            }
        }
Example #20
0
        private static void GetAllBlockBreakableShapeNames(HkdBreakableShape shape, HashSet <string> outNames)
        {
            var name = shape.Name;

            if (!string.IsNullOrEmpty(name))
            {
                outNames.Add(name);
            }

            if (shape.GetChildrenCount() > 0)
            {
                List <HkdShapeInstanceInfo> children = new List <HkdShapeInstanceInfo>();
                shape.GetChildren(children);
                foreach (var child in children)
                {
                    GetAllBlockBreakableShapeNames(child.Shape, outNames);
                }
            }
        }
Example #21
0
        public void SetDataFromCompound(HkdBreakableShape compound)
        {
            var render = this.Render as MyRenderComponentFracturedPiece;

            if (render != null)
            {
                compound.GetChildren(m_shapeInfos);

                foreach (var shapeInstanceInfo in m_shapeInfos)
                {
                    System.Diagnostics.Debug.Assert(shapeInstanceInfo.IsValid(), "Invalid shapeInstanceInfo!");
                    if (shapeInstanceInfo.IsValid())
                    {
                        render.AddPiece(shapeInstanceInfo.ShapeName, shapeInstanceInfo.GetTransform());
                    }
                }

                m_shapeInfos.Clear();
            }
        }
Example #22
0
        private static void DrawBreakableShape(HkdBreakableShape breakableShape, MatrixD worldMatrix, float alpha, ref int shapeIndex, string customText = null, bool isPhantom = false)
        {
            object[] objArray1 = new object[] { breakableShape.Name, " Strength: ", breakableShape.GetStrenght(), " Static:", breakableShape.IsFixed().ToString() };
            DrawCollisionShape(breakableShape.GetShape(), worldMatrix, alpha, ref shapeIndex, string.Concat(objArray1), false);
            if (!string.IsNullOrEmpty(breakableShape.Name) && (breakableShape.Name != "PineTree175m_v2_001"))
            {
                breakableShape.IsFixed();
            }
            DebugShapesPositions[breakableShape.Name] = worldMatrix.Translation;
            List <HkdShapeInstanceInfo> list = new List <HkdShapeInstanceInfo>();

            breakableShape.GetChildren(list);
            Vector3 coM = breakableShape.CoM;

            foreach (HkdShapeInstanceInfo info in list)
            {
                Matrix matrix = (info.GetTransform() * worldMatrix) * Matrix.CreateTranslation(Vector3.Right * 2f);
                DrawBreakableShape(info.Shape, matrix, alpha, ref shapeIndex, null, false);
            }
        }
Example #23
0
        private static void ConvertAllShapesToFractureComponentShapeBuilder(HkdBreakableShape shape, ref Matrix shapeRotation, MyBlockOrientation blockOrientation, HashSet <Tuple <string, float> > namesAndBuildProgress, MyObjectBuilder_FractureComponentCubeBlock fractureComponentBuilder, out float buildProgress)
        {
            buildProgress = 1f;
            string name = shape.Name;
            Tuple <string, float> tuple = null;

            foreach (Tuple <string, float> tuple2 in namesAndBuildProgress)
            {
                if (tuple2.Item1 == name)
                {
                    tuple = tuple2;
                    break;
                }
            }
            if ((tuple != null) && (new MyBlockOrientation(ref shapeRotation) == blockOrientation))
            {
                MyObjectBuilder_FractureComponentBase.FracturedShape item = new MyObjectBuilder_FractureComponentBase.FracturedShape {
                    Name  = name,
                    Fixed = MyDestructionHelper.IsFixed(shape)
                };
                fractureComponentBuilder.Shapes.Add(item);
                buildProgress = tuple.Item2;
            }
            if (shape.GetChildrenCount() > 0)
            {
                List <HkdShapeInstanceInfo> list = new List <HkdShapeInstanceInfo>();
                shape.GetChildren(list);
                foreach (HkdShapeInstanceInfo info in list)
                {
                    float  num;
                    Matrix transform = info.GetTransform();
                    ConvertAllShapesToFractureComponentShapeBuilder(info.Shape, ref transform, blockOrientation, namesAndBuildProgress, fractureComponentBuilder, out num);
                    if (tuple == null)
                    {
                        buildProgress = num;
                    }
                }
            }
        }
        private void DrawConnections(HkdBreakableShape breakableShape, MatrixD worldMatrix, float alpha, ref int shapeIndex, string customText = null, bool isPhantom = false)
        {
            List<HkdConnection> connections = new List<HkdConnection>();
            breakableShape.GetConnectionList(connections);

            List<HkdShapeInstanceInfo> children = new List<HkdShapeInstanceInfo>();
            breakableShape.GetChildren(children);

            foreach (var conn in connections)
            {
                var posA = DebugShapesPositions[conn.ShapeAName];
                var posB = DebugShapesPositions[conn.ShapeBName];

                bool cont = false;
                foreach (var child in children)
                {
                    if ((child.ShapeName == conn.ShapeAName) || (child.ShapeName == conn.ShapeBName))
                        cont = true;
                }

                if (cont)
                    VRageRender.MyRenderProxy.DebugDrawLine3D(posA, posB, Color.White, Color.White, false);
            }
        }
        private void DrawBreakableShape(HkdBreakableShape breakableShape, MatrixD worldMatrix, float alpha, ref int shapeIndex, string customText = null, bool isPhantom = false)
        {
            //VRageRender.MyRenderProxy.DebugDrawText3D(worldMatrix.Translation, , Color.White, 1, false);
            DrawCollisionShape(breakableShape.GetShape(), worldMatrix, alpha, ref shapeIndex, breakableShape.Name + " Strength: " + breakableShape.GetStrenght() + " Static:" + breakableShape.IsFixed());

            if (!string.IsNullOrEmpty(breakableShape.Name) && breakableShape.Name != "PineTree175m_v2_001" && breakableShape.IsFixed())
            {
            }

            DebugShapesPositions[breakableShape.Name] = worldMatrix.Translation;

            List<HkdShapeInstanceInfo> children = new List<HkdShapeInstanceInfo>();
            breakableShape.GetChildren(children);

            Vector3 parentCom =  breakableShape.CoM;

            foreach (var shapeInst in children)
            {
                Matrix transform = shapeInst.GetTransform();
              //  transform.Translation += (shapeInst.Shape.CoM - parentCom);
                Matrix trWorld = transform * worldMatrix * Matrix.CreateTranslation(Vector3.Right * 2);
                DrawBreakableShape(shapeInst.Shape, trWorld, alpha, ref shapeIndex);
            }
        }
        private void CreatePieceData(MyModel model, HkdBreakableShape breakableShape)
        {
            //Root shape for fractured compound blocks
            {
                var msg = VRageRender.MyRenderProxy.PrepareAddRuntimeModel();
                ProfilerShort.Begin("GetDataFromShape");
                m_tmpMesh.Data = msg.ModelData;
                MyDestructionData.Static.Storage.GetDataFromShape(breakableShape, m_tmpMesh);
                System.Diagnostics.Debug.Assert(msg.ModelData.Sections.Count > 0, "Invalid data");
                if (msg.ModelData.Sections.Count > 0)
                {
                    if(MyFakes.USE_HAVOK_MODELS)
                        msg.ReplacedModel = model.AssetName;
                    VRageRender.MyRenderProxy.AddRuntimeModel(breakableShape.ShapeName, msg);
                }
                ProfilerShort.End();
            }

            using (m_tmpChildrenList.GetClearToken())
            {
                breakableShape.GetChildren(m_tmpChildrenList);
                LoadChildrenShapes(m_tmpChildrenList);
            }
        }
 private void DisableRefCountRec(HkdBreakableShape bShape)
 {
     bShape.DisableRefCount();
     var lst = new List<HkdShapeInstanceInfo>();
     bShape.GetChildren(lst);
     foreach (var child in lst)
         DisableRefCountRec(child.Shape);
 }
Example #28
0
 public static void ConnectShapesWithChildren(HkdBreakableShape parent, HkdBreakableShape shapeA, HkdBreakableShape shapeB)
 {
     var c = CreateConnection(shapeA, shapeB, shapeA.CoM, shapeB.CoM);
     //parent.AddConnection(ref c);
     c.AddToCommonParent();
     c.RemoveReference();
     shapeB.GetChildren(m_shapeInfosList3);
     foreach (var child in m_shapeInfosList3)
     {
         var c2 = CreateConnection(shapeA, child.Shape, shapeA.CoM, shapeB.CoM);
         //parent.AddConnection(ref c2);
         c2.AddToCommonParent();
         c2.RemoveReference();
     }
     m_shapeInfosList3.Clear();
 }
Example #29
0
        public override void Init(MyObjectBuilder_CubeBlock builder, MyCubeGrid cubeGrid)
        {
            base.Init(builder, cubeGrid);
            base.CheckConnectionAllowed = true;
            MyObjectBuilder_FracturedBlock block = builder as MyObjectBuilder_FracturedBlock;

            if (block.Shapes.Count == 0)
            {
                if (!block.CreatingFracturedBlock)
                {
                    throw new Exception("No relevant shape was found for fractured block. It was probably reexported and names changed.");
                }
            }
            else
            {
                this.OriginalBlocks = new List <MyDefinitionId>();
                this.Orientations   = new List <MyBlockOrientation>();
                List <HkdShapeInstanceInfo> list = new List <HkdShapeInstanceInfo>();
                foreach (SerializableDefinitionId id in block.BlockDefinitions)
                {
                    MyCubeBlockDefinition cubeBlockDefinition = MyDefinitionManager.Static.GetCubeBlockDefinition(id);
                    string model = cubeBlockDefinition.Model;
                    if (MyModels.GetModelOnlyData(model).HavokBreakableShapes == null)
                    {
                        MyDestructionData.Static.LoadModelDestruction(model, cubeBlockDefinition, Vector3.One, true, false);
                    }
                    HkdBreakableShape    shape       = MyModels.GetModelOnlyData(model).HavokBreakableShapes[0];
                    Quaternion?          rotation    = null;
                    Vector3?             translation = null;
                    HkdShapeInstanceInfo item        = new HkdShapeInstanceInfo(shape, rotation, translation);
                    list.Add(item);
                    m_children.Add(item);
                    shape.GetChildren(m_children);
                    if (cubeBlockDefinition.BuildProgressModels != null)
                    {
                        MyCubeBlockDefinition.BuildProgressModel[] buildProgressModels = cubeBlockDefinition.BuildProgressModels;
                        for (int j = 0; j < buildProgressModels.Length; j++)
                        {
                            model = buildProgressModels[j].File;
                            if (MyModels.GetModelOnlyData(model).HavokBreakableShapes == null)
                            {
                                MyDestructionData.Static.LoadModelDestruction(model, cubeBlockDefinition, Vector3.One, true, false);
                            }
                            shape       = MyModels.GetModelOnlyData(model).HavokBreakableShapes[0];
                            rotation    = null;
                            translation = null;
                            item        = new HkdShapeInstanceInfo(shape, rotation, translation);
                            list.Add(item);
                            m_children.Add(item);
                            shape.GetChildren(m_children);
                        }
                    }
                    this.OriginalBlocks.Add(id);
                }
                foreach (SerializableBlockOrientation orientation in block.BlockOrientations)
                {
                    this.Orientations.Add((MyBlockOrientation)orientation);
                }
                if (block.MultiBlocks.Count > 0)
                {
                    this.MultiBlocks = new List <MultiBlockPartInfo>();
                    foreach (MyObjectBuilder_FracturedBlock.MyMultiBlockPart part in block.MultiBlocks)
                    {
                        if (part == null)
                        {
                            this.MultiBlocks.Add(null);
                            continue;
                        }
                        MultiBlockPartInfo item = new MultiBlockPartInfo();
                        item.MultiBlockDefinition = part.MultiBlockDefinition;
                        item.MultiBlockId         = part.MultiBlockId;
                        this.MultiBlocks.Add(item);
                    }
                }
                this.m_shapes.AddRange(block.Shapes);
                for (int i = 0; i < m_children.Count; i++)
                {
                    HkdShapeInstanceInfo child = m_children[i];
                    Func <MyObjectBuilder_FracturedBlock.ShapeB, bool>  predicate = s => s.Name == child.ShapeName;
                    IEnumerable <MyObjectBuilder_FracturedBlock.ShapeB> source    = this.m_shapes.Where <MyObjectBuilder_FracturedBlock.ShapeB>(predicate);
                    if (source.Count <MyObjectBuilder_FracturedBlock.ShapeB>() <= 0)
                    {
                        child.GetChildren(m_children);
                    }
                    else
                    {
                        MyObjectBuilder_FracturedBlock.ShapeB item = source.First <MyObjectBuilder_FracturedBlock.ShapeB>();
                        Matrix transform = Matrix.CreateFromQuaternion((Quaternion)item.Orientation);
                        transform.Translation = child.GetTransform().Translation;
                        HkdShapeInstanceInfo info2 = new HkdShapeInstanceInfo(child.Shape.Clone(), transform);
                        if (item.Fixed)
                        {
                            info2.Shape.SetFlagRecursively(HkdBreakableShape.Flags.IS_FIXED);
                        }
                        list.Add(info2);
                        m_shapeInfos.Add(info2);
                        this.m_shapes.Remove(item);
                    }
                }
                if (m_shapeInfos.Count == 0)
                {
                    m_children.Clear();
                    throw new Exception("No relevant shape was found for fractured block. It was probably reexported and names changed.");
                }
                foreach (HkdShapeInstanceInfo info3 in m_shapeInfos)
                {
                    HkdBreakableShape shape = info3.Shape;
                    if (!string.IsNullOrEmpty(shape.Name))
                    {
                        this.Render.AddPiece(info3.Shape.Name, Matrix.CreateFromQuaternion(Quaternion.CreateFromRotationMatrix(info3.GetTransform().GetOrientation())));
                    }
                }
                if (base.CubeGrid.CreatePhysics)
                {
                    HkdBreakableShape?oldParent = null;
                    HkdBreakableShape shape3    = (HkdBreakableShape) new HkdCompoundBreakableShape(oldParent, m_shapeInfos);
                    shape3.RecalcMassPropsFromChildren();
                    this.Shape = shape3;
                    HkMassProperties massProperties = new HkMassProperties();
                    shape3.BuildMassProperties(ref massProperties);
                    this.Shape = new HkdBreakableShape(shape3.GetShape(), ref massProperties);
                    shape3.RemoveReference();
                    foreach (HkdShapeInstanceInfo info4 in m_shapeInfos)
                    {
                        this.Shape.AddShape(ref info4);
                    }
                    this.Shape.SetStrenght(MyDestructionConstants.STRENGTH);
                    this.CreateMountPoints();
                }
                m_children.Clear();
                foreach (HkdShapeInstanceInfo info5 in m_shapeInfos)
                {
                    info5.Shape.RemoveReference();
                }
                foreach (HkdShapeInstanceInfo info6 in list)
                {
                    info6.RemoveReference();
                }
                m_shapeInfos.Clear();
            }
        }
        public void SetDataFromCompound(HkdBreakableShape compound)
        {
            var render = this.Render as MyRenderComponentFracturedPiece;

            if (render != null)
            {
                compound.GetChildren(m_shapeInfos);

                foreach (var shapeInstanceInfo in m_shapeInfos)
                {
                    System.Diagnostics.Debug.Assert(shapeInstanceInfo.IsValid(), "Invalid shapeInstanceInfo!");
                    if (shapeInstanceInfo.IsValid())
                    {
                        render.AddPiece(shapeInstanceInfo.ShapeName, shapeInstanceInfo.GetTransform());
                    }
                }

                m_shapeInfos.Clear();
            }
        }
Example #31
0
        private static void ConvertAllShapesToFractureComponentShapeBuilder(HkdBreakableShape shape, ref Matrix shapeRotation, MyBlockOrientation blockOrientation, HashSet<string> names, MyObjectBuilder_FractureComponentCubeBlock fractureComponentBuilder)
        {
            var name = shape.Name;
            if (names.Contains(name))
            {
                MyBlockOrientation shapeOrientation = new MyBlockOrientation(ref shapeRotation);
                if (shapeOrientation == blockOrientation)
                {
                    MyObjectBuilder_FractureComponentCubeBlock.FracturedShape builderShape = new MyObjectBuilder_FractureComponentBase.FracturedShape();
                    builderShape.Name = name;
                    builderShape.Fixed = MyDestructionHelper.IsFixed(shape);

                    fractureComponentBuilder.Shapes.Add(builderShape);
                }
            }

            if (shape.GetChildrenCount() > 0)
            {
                List<HkdShapeInstanceInfo> children = new List<HkdShapeInstanceInfo>();
                shape.GetChildren(children);
                foreach (var child in children) 
                {
                    var childShapeRotation = child.GetTransform();
                    ConvertAllShapesToFractureComponentShapeBuilder(child.Shape, ref childShapeRotation, blockOrientation, names, fractureComponentBuilder);
                }
            }

        }
Example #32
0
        private static void GetAllBlockBreakableShapeNames(HkdBreakableShape shape, HashSet<string> outNames)
        {
            var name = shape.Name;
            if (!string.IsNullOrEmpty(name))
                outNames.Add(name);

            if (shape.GetChildrenCount() > 0)
            {
                List<HkdShapeInstanceInfo> children = new List<HkdShapeInstanceInfo>();
                shape.GetChildren(children);
                foreach (var child in children)
                    GetAllBlockBreakableShapeNames(child.Shape, outNames);
            }
        }
        public virtual void SetShape(HkdBreakableShape shape, bool compound)
        {
            Debug.Assert(shape.IsValid());

            if (Shape.IsValid())
                Shape.RemoveReference();

            Shape = shape;

            var render = Entity.Render as MyRenderComponentFracturedPiece;
            Debug.Assert(render != null);

            if (render != null)
            {
                render.ClearModels();

                if (compound)
                {
                    Debug.Assert(m_tmpChildren.Count == 0);

                    shape.GetChildren(m_tmpChildren);

                    foreach (var shapeInstanceInfo in m_tmpChildren)
                    {
                        System.Diagnostics.Debug.Assert(shapeInstanceInfo.IsValid(), "Invalid shapeInstanceInfo!");
                        if (shapeInstanceInfo.IsValid())
                        {
                            render.AddPiece(shapeInstanceInfo.ShapeName, Matrix.Identity);
                        }
                    }

                    m_tmpChildren.Clear();
                }
                else
                {
                    render.AddPiece(shape.Name, Matrix.Identity);
                }

                render.UpdateRenderObject(true);
            }
        }
        private static bool GetCurrentFracturedShapeList(HkdBreakableShape breakableShape, List<MyObjectBuilder_FractureComponentBase.FracturedShape> shapeList, string[] excludeShapeNames = null)
        {
            Debug.Assert(breakableShape.IsValid());
            if (!breakableShape.IsValid())
                return false;

            var shapeName = breakableShape.Name;
            bool shapeNameEmpty = string.IsNullOrEmpty(shapeName);

            if (excludeShapeNames != null && !shapeNameEmpty)
            {
                foreach (var shapeNameToRemove in excludeShapeNames)
                {
                    if (shapeName == shapeNameToRemove)
                        return false;
                }
            }

            if (breakableShape.GetChildrenCount() > 0)
            {
                List<HkdShapeInstanceInfo> shapeInst = new List<HkdShapeInstanceInfo>();
                breakableShape.GetChildren(shapeInst);

                bool allChildrenAdded = true;
                foreach (var inst in shapeInst)
                {
                    allChildrenAdded &= GetCurrentFracturedShapeList(inst.Shape, shapeList, excludeShapeNames: excludeShapeNames);
                }

                if (!shapeNameEmpty && allChildrenAdded)
                {
                    foreach (var inst in shapeInst)
                    {
                        if (inst.Shape.IsValid())
                            shapeList.RemoveAll(s => s.Name == inst.ShapeName);
                    }

                    shapeList.Add(new MyObjectBuilder_FractureComponentBase.FracturedShape() { Name = shapeName, Fixed = breakableShape.IsFixed() });
                }

                return allChildrenAdded;
            }
            else
            {
                if (!shapeNameEmpty)
                {
                    shapeList.Add(new MyObjectBuilder_FractureComponentBase.FracturedShape() { Name = shapeName, Fixed = breakableShape.IsFixed() });
                    return true;
                }
            }

            return false;
        }
        public static void GetAllBlockBreakableShapeNames(HkdBreakableShape shape, HashSet<Tuple<string, float>> outNamesAndBuildProgress, float buildProgress)
        {
            var name = shape.Name;
            if (!string.IsNullOrEmpty(name))
                outNamesAndBuildProgress.Add(new Tuple<string, float>(name, buildProgress));

            if (shape.GetChildrenCount() > 0)
            {
                List<HkdShapeInstanceInfo> children = new List<HkdShapeInstanceInfo>();
                shape.GetChildren(children);
                foreach (var child in children)
                    GetAllBlockBreakableShapeNames(child.Shape, outNamesAndBuildProgress, buildProgress);
            }
        }
Example #36
0
        /// <summary>
        /// Sets model from havok to render component of this entity.
        /// </summary>
        public void SetDataFromHavok(HkdBreakableShape shape)
        {
            ProfilerShort.Begin("FP.SetDataFromHavok");
            Shape = shape;
            Shape.AddReference();
            if (Render != null)
            {
                if (shape.IsCompound() || string.IsNullOrEmpty(shape.Name))
                {
                    shape.GetChildren(m_shapeInfos);
                    Debug.Assert(m_shapeInfos.Count > 0);
                    foreach (var shapeInstanceInfo in m_shapeInfos)
                    {
                        //System.Diagnostics.Debug.Assert(shapeInstanceInfo.IsValid(), "Invalid shapeInstanceInfo!");
                        if (shapeInstanceInfo.IsValid())
                        {
                            Render.AddPiece(shapeInstanceInfo.ShapeName, shapeInstanceInfo.GetTransform());
                        }
                    }

                    m_shapeInfos.Clear();
                }
                else
                    Render.AddPiece(shape.Name, Matrix.Identity);
            }
            ProfilerShort.End();

            m_hitPoints = Shape.Volume * 100;
        }
        private static void ConvertAllShapesToFractureComponentShapeBuilder(HkdBreakableShape shape, ref Matrix shapeRotation, MyBlockOrientation blockOrientation,
            HashSet<Tuple<string, float>> namesAndBuildProgress, MyObjectBuilder_FractureComponentCubeBlock fractureComponentBuilder, out float buildProgress)
        {
            buildProgress = 1f;

            var name = shape.Name;
            Tuple<string, float> foundTuple = null;
            foreach (var tuple in namesAndBuildProgress)
            {
                if (tuple.Item1 == name)
                {
                    foundTuple = tuple;
                    break;
                }
            }

            if (foundTuple != null)
            {
                MyBlockOrientation shapeOrientation = new MyBlockOrientation(ref shapeRotation);
                if (shapeOrientation == blockOrientation)
                {
                    MyObjectBuilder_FractureComponentCubeBlock.FracturedShape builderShape = new MyObjectBuilder_FractureComponentBase.FracturedShape();
                    builderShape.Name = name;
                    builderShape.Fixed = MyDestructionHelper.IsFixed(shape);

                    fractureComponentBuilder.Shapes.Add(builderShape);
                    buildProgress = foundTuple.Item2;
                }
            }

            if (shape.GetChildrenCount() > 0)
            {
                List<HkdShapeInstanceInfo> children = new List<HkdShapeInstanceInfo>();
                shape.GetChildren(children);
                foreach (var child in children) 
                {
                    var childShapeRotation = child.GetTransform();
                    float localBuildProgress;
                    ConvertAllShapesToFractureComponentShapeBuilder(child.Shape, ref childShapeRotation, blockOrientation, namesAndBuildProgress, 
                        fractureComponentBuilder, out localBuildProgress);

                    if (foundTuple == null)
                        buildProgress = localBuildProgress;
                }
            }

        }
Example #38
0
        public static unsafe void FixPosition(MyFracturedPiece fp)
        {
            HkdBreakableShape breakableShape = fp.Physics.BreakableBody.BreakableShape;

            if (breakableShape.GetChildrenCount() != 0)
            {
                breakableShape.GetChildren(m_tmpInfos);
                Vector3 translation = m_tmpInfos[0].GetTransform().Translation;
                if (translation.LengthSquared() < 1f)
                {
                    m_tmpInfos.Clear();
                }
                else
                {
                    List <HkdConnection>        resultList = new List <HkdConnection>();
                    HashSet <HkdBreakableShape> set        = new HashSet <HkdBreakableShape>();
                    HashSet <HkdBreakableShape> set2       = new HashSet <HkdBreakableShape>();
                    set.Add(breakableShape);
                    breakableShape.GetConnectionList(resultList);
                    fp.PositionComp.SetPosition(Vector3D.Transform(translation, fp.PositionComp.WorldMatrix), null, false, true);
                    foreach (HkdShapeInstanceInfo info2 in m_tmpInfos)
                    {
                        Matrix  transform  = info2.GetTransform();
                        Matrix *matrixPtr1 = (Matrix *)ref transform;
                        matrixPtr1.Translation -= translation;
                        info2.SetTransform(ref transform);
                        m_tmpInfos2.Add(info2);
                        HkdBreakableShape shape = info2.Shape;
                        shape.GetConnectionList(resultList);
                        while (true)
                        {
                            if (!shape.HasParent)
                            {
                                set2.Add(info2.Shape);
                                break;
                            }
                            shape = shape.GetParent();
                            if (set.Add(shape))
                            {
                                shape.GetConnectionList(resultList);
                            }
                        }
                    }
                    m_tmpInfos.Clear();
                    HkdBreakableShape parent = (HkdBreakableShape) new HkdCompoundBreakableShape(new HkdBreakableShape?(breakableShape), m_tmpInfos2);
                    parent.RecalcMassPropsFromChildren();
                    ((HkdBreakableShape *)ref parent).SetChildrenParent(parent);
                    foreach (HkdConnection connection in resultList)
                    {
                        HkBaseSystem.EnableAssert(0x1745920b, true);
                        if (set2.Contains(connection.ShapeA) && set2.Contains(connection.ShapeB))
                        {
                            HkdConnection connection2 = connection;
                            parent.AddConnection(ref connection2);
                        }
                    }
                    fp.Physics.BreakableBody.BreakableShape = parent;
                    m_tmpInfos2.Clear();
                    parent.RecalcMassPropsFromChildren();
                }
            }
        }