protected override bool Attach(MyAttachableTopBlockBase topBlock, bool updateGroup = true)
        {
            Debug.Assert(topBlock != null, "Top block cannot be null!");

            MyPistonTop pistonTop = topBlock as MyPistonTop;

            if (pistonTop != null && base.Attach(topBlock, updateGroup))
            {
                Debug.Assert(m_constraint == null, "Contraint already attached, call detach first!");
                Debug.Assert(m_connectionState.Value.TopBlockId.HasValue && (m_connectionState.Value.TopBlockId.Value == 0 || m_connectionState.Value.TopBlockId.Value == topBlock.EntityId), "m_topBlockId must be set prior calling Attach!");

                UpdateAnimation();

                CreateConstraint(topBlock);

                if (updateGroup)
                {
                    m_conveyorEndpoint.Attach(pistonTop.ConveyorEndpoint as MyAttachableConveyorEndpoint);
                }

                UpdateText();
                return(true);
            }
            return(false);
        }
Exemple #2
0
        public override void UpdateOnceBeforeFrame()
        {
            if (m_topBlockId != 0 && m_topBlock == null)
            {
                MyPistonTop top;
                if (MyEntities.TryGetEntityById <MyPistonTop>(m_topBlockId, out top) && top.InScene)
                {
                    m_topBlock = top;
                    Attach(m_topBlock, Sync.IsServer);
                    m_retryAttach = false;
                }
                else
                {
                    if (Sync.IsServer)
                    {
                        m_topBlockId = 0;
                    }
                    if (m_retryAttach)
                    {
                        RetryAttach(m_topBlockId);
                    }
                }
            }

            LoadSubparts();

            UpdateAnimation();
            UpdatePosition(true);
            UpdatePhysicsShape();
            base.UpdateOnceBeforeFrame();
        }
Exemple #3
0
        internal void Detach()
        {
            if (m_constraint == null)
            {
                return;
            }

            Debug.Assert(m_constraint != null);
            Debug.Assert(m_topGrid != null);
            Debug.Assert(m_topBlock != null);

            var tmpTopGrid = m_topGrid;

            m_fixedData.Dispose();
            m_fixedData = null;
            CubeGrid.Physics.RemoveConstraint(m_constraint);
            m_constraint.Dispose();
            m_constraint = null;
            m_topGrid    = null;
            if (m_topBlock != null)
            {
                m_topBlock.Detach();
            }
            tmpTopGrid.OnPhysicsChanged -= CubeGrid_OnPhysicsChanged;
            m_conveyorEndpoint.Detach(m_topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint);

            m_topBlock = null;

            OnConstraintRemoved(GridLinkTypeEnum.Physical, tmpTopGrid);
            OnConstraintRemoved(GridLinkTypeEnum.Logical, tmpTopGrid);
            UpdateText();
        }
        protected override bool Attach(MyAttachableTopBlockBase topBlock, bool updateGroup = true)
        {
            Debug.Assert(topBlock != null, "Top block cannot be null!");

            if (CubeGrid == topBlock.CubeGrid)
            {
                return(false);
            }

            MyPistonTop pistonTop = topBlock as MyPistonTop;

            if (pistonTop != null && base.Attach(topBlock, updateGroup))
            {
                Debug.Assert(m_constraint == null, "Contraint already attached, call detach first!");
                Debug.Assert(m_connectionState.Value.TopBlockId.HasValue && (m_connectionState.Value.TopBlockId.Value == 0 || m_connectionState.Value.TopBlockId.Value == topBlock.EntityId), "m_topBlockId must be set prior calling Attach!");

                UpdateAnimation();

                var matAD = MatrixD.CreateWorld(Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up);
                var matBD = MatrixD.CreateWorld(m_topBlock.Position * m_topBlock.CubeGrid.GridSize, m_topBlock.PositionComp.LocalMatrix.Forward, m_topBlock.PositionComp.LocalMatrix.Up);
                var matA  = (Matrix)matAD;
                var matB  = (Matrix)matBD;
                m_fixedData = new HkFixedConstraintData();
                m_fixedData.SetInertiaStabilizationFactor(10);
                m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized);
                m_fixedData.SetInBodySpace(matA, matB, CubeGrid.Physics, m_topGrid.Physics);

                //Dont dispose the fixed data or we wont have access to them

                m_constraint             = new HkConstraint(CubeGrid.Physics.RigidBody, topBlock.CubeGrid.Physics.RigidBody, m_fixedData);
                m_constraint.WantRuntime = true;

                CubeGrid.Physics.AddConstraint(m_constraint);
                if (!m_constraint.InWorld)
                {
                    Debug.Fail("Constraint was not added to world");
                    CubeGrid.Physics.RemoveConstraint(m_constraint);
                    m_constraint.Dispose();
                    m_constraint = null;
                    m_fixedData  = null;
                    return(false);
                }
                m_constraint.Enabled = true;

                m_topBlock = topBlock;
                m_topGrid  = topBlock.CubeGrid;
                topBlock.Attach(this);
                m_isAttached = true;

                if (updateGroup)
                {
                    m_conveyorEndpoint.Attach(pistonTop.ConveyorEndpoint as MyAttachableConveyorEndpoint);
                }

                UpdateText();
                return(true);
            }
            return(false);
        }
Exemple #5
0
        public void Attach(MyPistonTop topBlock)
        {
            if (CubeGrid == topBlock.CubeGrid)
            {
                return;
            }
            Debug.Assert(topBlock != null, "Top block cannot be null!");
            Debug.Assert(m_constraint == null, "Contraint already attached, call detach first!");
            Debug.Assert(m_topBlockId.Value == topBlock.EntityId, "m_topBlockId must be set prior calling Attach!");

            UpdateAnimation();

            if (CubeGrid.Physics != null && CubeGrid.Physics.Enabled && topBlock.CubeGrid.Physics != null)
            {
                m_topBlock = topBlock;
                m_topGrid  = m_topBlock.CubeGrid;
                var rotorBody = m_topGrid.Physics.RigidBody;

                var matAD = MatrixD.CreateWorld(Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up);
                var matBD = MatrixD.CreateWorld(m_topBlock.Position * m_topBlock.CubeGrid.GridSize, m_topBlock.PositionComp.LocalMatrix.Forward, m_topBlock.PositionComp.LocalMatrix.Up);
                var matA  = (Matrix)matAD;
                var matB  = (Matrix)matBD;
                m_fixedData = new HkFixedConstraintData();
                m_fixedData.SetInertiaStabilizationFactor(10);
                m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized);
                m_fixedData.SetInBodySpace(matA, matB, CubeGrid.Physics, m_topGrid.Physics);
                var breakData = new HkBreakableConstraintData(m_fixedData);
                //Dont dispose the fixed data or we wont have access to them

                breakData.Threshold = BreakOffTreshold;
                breakData.ReapplyVelocityOnBreak = true;
                breakData.RemoveFromWorldOnBrake = false;
                m_constraint             = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, breakData);
                m_constraint.WantRuntime = true;

                CubeGrid.Physics.AddConstraint(m_constraint);
                if (!m_constraint.InWorld)
                {
                    Debug.Fail("Constraint was not added to world");
                    CubeGrid.Physics.RemoveConstraint(m_constraint);
                    m_constraint.Dispose();
                    m_constraint = null;
                    m_fixedData.Dispose();
                    m_fixedData = null;
                    return;
                }
                m_constraint.Enabled = true;

                m_topBlock.Attach(this);
                m_topGrid.OnPhysicsChanged += CubeGrid_OnPhysicsChanged;

                OnConstraintAdded(GridLinkTypeEnum.Physical, m_topGrid);
                OnConstraintAdded(GridLinkTypeEnum.Logical, m_topGrid);

                m_conveyorEndpoint.Attach(topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint);
                UpdateText();
            }
        }
        protected override void CustomUnweld()
        {
            base.CustomUnweld();

            MyPistonTop pistonTop = m_topBlock as MyPistonTop;

            if (pistonTop != null)
            {
                m_conveyorEndpoint.Detach(pistonTop.ConveyorEndpoint as MyAttachableConveyorEndpoint);
            }
        }
Exemple #7
0
        public void Attach(MyPistonTop topBlock, bool updateSync = false)
        {
            if (m_topBlock != null)
            {
                Detach();
            }
            if (CubeGrid.Physics == null || SafeConstraint != null)
            {
                return;
            }
            UpdateAnimation();
            Debug.Assert(SafeConstraint == null);

            if (CubeGrid.Physics.Enabled && topBlock != null)
            {
                m_topBlock   = topBlock;
                m_topBlockId = topBlock.EntityId;

                if (updateSync)
                {
                    SyncObject.AttachTop(m_topBlock);
                }

                m_topGrid = m_topBlock.CubeGrid;
                var rotorBody = m_topGrid.Physics.RigidBody;

                var matAD = MatrixD.CreateWorld(Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up);
                var matBD = MatrixD.CreateWorld(m_topBlock.Position * m_topBlock.CubeGrid.GridSize, m_topBlock.PositionComp.LocalMatrix.Forward, m_topBlock.PositionComp.LocalMatrix.Up);
                var matA  = (Matrix)matAD;
                var matB  = (Matrix)matBD;
                m_fixedData = new HkFixedConstraintData();
                m_fixedData.SetInertiaStabilizationFactor(10);
                m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized);
                m_fixedData.SetInBodySpace(ref matA, ref matB);
                var breakData = new HkBreakableConstraintData(m_fixedData);
                //Dont dispose the fixed data or we wont have access to them

                breakData.Threshold = BreakOffTreshold;
                breakData.ReapplyVelocityOnBreak = true;
                breakData.RemoveFromWorldOnBrake = false;
                m_constraint             = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, breakData);
                m_constraint.WantRuntime = true;

                CubeGrid.Physics.AddConstraint(m_constraint);
                m_constraint.Enabled = true;

                m_topBlock.Attach(this);

                OnConstraintAdded(GridLinkTypeEnum.Physical, m_topGrid);
                OnConstraintAdded(GridLinkTypeEnum.Logical, m_topGrid);

                m_conveyorEndpoint.Attach(topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint);
            }
        }
 protected override void Detach(bool updateGroup = true)
 {
     if (TopBlock != null && updateGroup)
     {
         MyPistonTop pistonTop = TopBlock as MyPistonTop;
         if (pistonTop != null)
         {
             m_conveyorEndpoint.Detach(pistonTop.ConveyorEndpoint as MyAttachableConveyorEndpoint);
         }
     }
     base.Detach(updateGroup);
 }
        public override bool Detach(bool updateGroup = true)
        {
            if (m_topBlock != null && updateGroup)
            {
                MyPistonTop pistonTop = m_topBlock as MyPistonTop;
                if (pistonTop != null)
                {
                    m_conveyorEndpoint.Detach(pistonTop.ConveyorEndpoint as MyAttachableConveyorEndpoint);
                }
            }
            var ret = base.Detach(updateGroup);

            return(ret);
        }
Exemple #10
0
        internal void Detach(bool updateGroups = true)
        {
            if (m_constraint == null)
            {
                return;
            }

            Debug.Assert(m_constraint != null);
            Debug.Assert(m_topGrid != null);
            Debug.Assert(m_topBlock != null);

            var tmpTopGrid = m_topGrid;

            if (m_constraint.InWorld && (m_constraint.ConstraintData as HkBreakableConstraintData).getIsBroken(m_constraint))
            {
                m_topBlockId = 0;
            }
            m_fixedData.Dispose();
            m_fixedData = null;
            CubeGrid.Physics.RemoveConstraint(m_constraint);
            m_constraint.Dispose();
            m_constraint = null;
            m_topGrid    = null;
            if (m_topBlock != null)
            {
                m_topBlock.Detach();
            }

            m_conveyorEndpoint.Detach(m_topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint);

            m_topBlock = null;

            if (updateGroups)
            {
                OnConstraintRemoved(GridLinkTypeEnum.Physical, tmpTopGrid);
                OnConstraintRemoved(GridLinkTypeEnum.Logical, tmpTopGrid);
            }


            // Try to reattach, if the block will still live next frame. This fixes missing attachments when splitting grids
        }
Exemple #11
0
        private void CreateTopGrid(out MyCubeGrid topGrid, out MyPistonTop topBlock, MyCubeBlockDefinitionGroup topGroup)
        {
            if (topGroup == null)
            {
                topGrid  = null;
                topBlock = null;
                return;
            }

            var gridSize = CubeGrid.GridSizeEnum;

            float size   = MyDefinitionManager.Static.GetCubeSize(gridSize);
            var   matrix = MatrixD.CreateWorld(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), WorldMatrix.Forward, WorldMatrix.Up);

            var definition = topGroup[gridSize];

            Debug.Assert(definition != null);

            var block = MyCubeGrid.CreateBlockObjectBuilder(definition, Vector3I.Zero, MyBlockOrientation.Identity, MyEntityIdentifier.AllocateId(), OwnerId, fullyBuilt: MySession.Static.CreativeMode);

            var gridBuilder = Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.CreateNewObject <MyObjectBuilder_CubeGrid>();

            gridBuilder.GridSizeEnum           = gridSize;
            gridBuilder.IsStatic               = false;
            gridBuilder.PositionAndOrientation = new MyPositionAndOrientation(matrix);
            gridBuilder.CubeBlocks.Add(block);

            var grid = MyEntityFactory.CreateEntity <MyCubeGrid>(gridBuilder);

            grid.Init(gridBuilder);

            topGrid  = grid;
            topBlock = (MyPistonTop)topGrid.GetCubeBlock(Vector3I.Zero).FatBlock;
            //topGrid.SetPosition(topGrid.WorldMatrix.Translation - (topBlock.WorldMatrix.Translation/*Vector3.Transform(topBlock.DummyPosLoc, topGrid.WorldMatrix) - topGrid.WorldMatrix.Translation*/));

            MyEntities.Add(grid);

            MySyncCreate.SendEntityCreated(grid.GetObjectBuilder());
            m_topBlockId = topBlock.EntityId;
        }
        internal void AttachTop(MyPistonTop topBlock)
        {
            var msg = new AttachMsg();
            Debug.Assert(Entity != null, "null entity when attaching piston top !");
            Debug.Assert(topBlock != null, "null topBlock when attaching piston top !");

            if (Entity != null && topBlock != null)
            {
                msg.EntityId = Entity.EntityId;
                msg.TopEntityId = topBlock.EntityId;

                Sync.Layer.SendMessageToAll(ref msg);
            }
            else
            {
                MySandboxGame.Log.WriteLine(string.Format("Failed to attach piston top ! Entity value :  {0} top block value : {1}",Entity,topBlock));
            }
        }
Exemple #13
0
        public override void UpdateOnceBeforeFrame()
        {
            if (m_topBlockId != 0 && m_topBlock == null)
            {
                MyPistonTop top;
                if (MyEntities.TryGetEntityById<MyPistonTop>(m_topBlockId, out top) && top.InScene)
                {
                    m_topBlock = top;
                    Attach(m_topBlock, Sync.IsServer);
                    m_retryAttach = false;
                }
                else
                {
                    if (Sync.IsServer)
                    {
                        m_topBlockId = 0;
                    }
                    if(m_retryAttach)
                    {
                        RetryAttach(m_topBlockId);
                    }
                }
            }

            LoadSubparts();

            UpdateAnimation();
            UpdatePosition(true);
            UpdatePhysicsShape();
            base.UpdateOnceBeforeFrame();
        }
Exemple #14
0
        public override void UpdateOnceBeforeFrame()
        {
            if (m_topBlockId != 0 && m_topBlock == null)
            {
                MyPistonTop top;
                if (MyEntities.TryGetEntityById<MyPistonTop>(m_topBlockId, out top))
                {
                    m_topBlock = top;
                    Attach(m_topBlock, true);
                }
                else
                    if (Sync.IsServer)
                        m_topBlockId = 0;
            }

            LoadSubparts();

            UpdateAnimation();
            UpdatePosition(true);
            UpdatePhysicsShape();
            base.UpdateOnceBeforeFrame();
        }
Exemple #15
0
        internal void Detach(bool updateGroups = true)
        {
            if (m_constraint == null)
                return;

            Debug.Assert(m_constraint != null);
            Debug.Assert(m_topGrid != null);
            Debug.Assert(m_topBlock != null);

            var tmpTopGrid = m_topGrid;
            if (m_constraint.InWorld && (m_constraint.ConstraintData as HkBreakableConstraintData).getIsBroken(m_constraint))
                m_topBlockId = 0;
            m_fixedData.Dispose();
            m_fixedData = null;
            CubeGrid.Physics.RemoveConstraint(m_constraint);
            m_constraint.Dispose();
            m_constraint = null;
            m_topGrid = null;
            if (m_topBlock != null)
                m_topBlock.Detach();

            m_conveyorEndpoint.Detach(m_topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint);

            m_topBlock = null;

            if (updateGroups)
            {
                OnConstraintRemoved(GridLinkTypeEnum.Physical, tmpTopGrid);
                OnConstraintRemoved(GridLinkTypeEnum.Logical, tmpTopGrid);
            }


            // Try to reattach, if the block will still live next frame. This fixes missing attachments when splitting grids
        }
Exemple #16
0
        private void CreateTopGrid(out MyCubeGrid topGrid, out MyPistonTop topBlock, MyCubeBlockDefinitionGroup topGroup)
        {
            if (topGroup == null)
            {
                topGrid = null;
                topBlock = null;
                return;
            }

            var gridSize = CubeGrid.GridSizeEnum;

            float size = MyDefinitionManager.Static.GetCubeSize(gridSize);
            var matrix = MatrixD.CreateWorld(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), WorldMatrix.Forward, WorldMatrix.Up);

            var definition = topGroup[gridSize];
            Debug.Assert(definition != null);

            var block = MyCubeGrid.CreateBlockObjectBuilder(definition, Vector3I.Zero, MyBlockOrientation.Identity, MyEntityIdentifier.AllocateId(), OwnerId, fullyBuilt: MySession.Static.CreativeMode);

            var gridBuilder = Sandbox.Common.ObjectBuilders.Serializer.MyObjectBuilderSerializer.CreateNewObject<MyObjectBuilder_CubeGrid>();
            gridBuilder.GridSizeEnum = gridSize;
            gridBuilder.IsStatic = false;
            gridBuilder.PositionAndOrientation = new MyPositionAndOrientation(matrix);
            gridBuilder.CubeBlocks.Add(block);

            var grid = MyEntityFactory.CreateEntity<MyCubeGrid>(gridBuilder);
            grid.Init(gridBuilder);

            topGrid = grid;
            topBlock = (MyPistonTop)topGrid.GetCubeBlock(Vector3I.Zero).FatBlock;
            //topGrid.SetPosition(topGrid.WorldMatrix.Translation - (topBlock.WorldMatrix.Translation/*Vector3.Transform(topBlock.DummyPosLoc, topGrid.WorldMatrix) - topGrid.WorldMatrix.Translation*/));

            MyEntities.Add(grid);

            MySyncCreate.SendEntityCreated(grid.GetObjectBuilder());
            m_topBlockId = topBlock.EntityId;
        }
Exemple #17
0
        public void Attach(MyPistonTop topBlock, bool updateSync = false)
        {
            if (m_topBlock != null)
                Detach();
            if (CubeGrid.Physics == null || SafeConstraint != null)
                return;
            UpdateAnimation();
            Debug.Assert(SafeConstraint == null);

            if (CubeGrid.Physics.Enabled && topBlock != null)
            {
                m_topBlock = topBlock;
                m_topBlockId = topBlock.EntityId;

                if (updateSync)
                    SyncObject.AttachTop(m_topBlock);

                m_topGrid = m_topBlock.CubeGrid;
                var rotorBody = m_topGrid.Physics.RigidBody;

                var matAD = MatrixD.CreateWorld(Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up);
                var matBD = MatrixD.CreateWorld(m_topBlock.Position * m_topBlock.CubeGrid.GridSize, m_topBlock.PositionComp.LocalMatrix.Forward, m_topBlock.PositionComp.LocalMatrix.Up);
                var matA = (Matrix)matAD;
                var matB = (Matrix)matBD;
                m_fixedData = new HkFixedConstraintData();
                m_fixedData.SetInertiaStabilizationFactor(10);
                m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized);
                m_fixedData.SetInBodySpace(ref matA, ref matB);
                var breakData = new HkBreakableConstraintData(m_fixedData);
                //Dont dispose the fixed data or we wont have access to them

                breakData.Threshold = BreakOffTreshold;
                breakData.ReapplyVelocityOnBreak = true;
                breakData.RemoveFromWorldOnBrake = false;
                m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, breakData);
                m_constraint.WantRuntime = true;

                CubeGrid.Physics.AddConstraint(m_constraint);
                m_constraint.Enabled = true;

                m_topBlock.Attach(this);

                OnConstraintAdded(GridLinkTypeEnum.Physical, m_topGrid);
                OnConstraintAdded(GridLinkTypeEnum.Logical, m_topGrid);

                m_conveyorEndpoint.Attach(topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint);

            }
        }
Exemple #18
0
 private void CreateTopGrid(out MyCubeGrid topGrid, out MyPistonTop topBlock)
 {
     CreateTopGrid(out topGrid, out topBlock, MyDefinitionManager.Static.TryGetDefinitionGroup(BlockDefinition.TopPart));
 }
Exemple #19
0
 private void CreateTopGrid(out MyCubeGrid topGrid, out MyPistonTop topBlock)
 {
     CreateTopGrid(out topGrid, out topBlock, MyDefinitionManager.Static.TryGetDefinitionGroup(BlockDefinition.TopPart));
 }
        internal void Detach()
        {
            if (m_constraint == null)
                return;

            Debug.Assert(m_constraint != null);
            Debug.Assert(m_topGrid != null);
            Debug.Assert(m_topBlock != null);

            var tmpTopGrid = m_topGrid;
            m_fixedData.Dispose();
            m_fixedData = null;
            CubeGrid.Physics.RemoveConstraint(m_constraint);
            m_constraint.Dispose();
            m_constraint = null;
            m_topGrid = null;
            if (m_topBlock != null)
                m_topBlock.Detach();
            tmpTopGrid.OnPhysicsChanged -= CubeGrid_OnPhysicsChanged;
            m_conveyorEndpoint.Detach(m_topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint);

            m_topBlock = null;

            OnConstraintRemoved(GridLinkTypeEnum.Physical, tmpTopGrid);
            OnConstraintRemoved(GridLinkTypeEnum.Logical, tmpTopGrid);
            UpdateText();
        }
        public void Attach(MyPistonTop topBlock)
        {
            if (CubeGrid == topBlock.CubeGrid)
                return;
            Debug.Assert(topBlock != null, "Top block cannot be null!");
            Debug.Assert(m_constraint == null, "Contraint already attached, call detach first!");
            Debug.Assert(m_topBlockId.Value == topBlock.EntityId, "m_topBlockId must be set prior calling Attach!");

            UpdateAnimation();

            if (CubeGrid.Physics != null && CubeGrid.Physics.Enabled && topBlock.CubeGrid.Physics != null)
            {
                m_topBlock = topBlock;
                m_topGrid = m_topBlock.CubeGrid;
                var rotorBody = m_topGrid.Physics.RigidBody;

                var matAD = MatrixD.CreateWorld(Vector3D.Transform(Vector3D.Transform(m_constraintBasePos, Subpart3.WorldMatrix), CubeGrid.PositionComp.WorldMatrixNormalizedInv), PositionComp.LocalMatrix.Forward, PositionComp.LocalMatrix.Up);
                var matBD = MatrixD.CreateWorld(m_topBlock.Position * m_topBlock.CubeGrid.GridSize, m_topBlock.PositionComp.LocalMatrix.Forward, m_topBlock.PositionComp.LocalMatrix.Up);
                var matA = (Matrix)matAD;
                var matB = (Matrix)matBD;
                m_fixedData = new HkFixedConstraintData();
                m_fixedData.SetInertiaStabilizationFactor(10);
                m_fixedData.SetSolvingMethod(HkSolvingMethod.MethodStabilized);
                m_fixedData.SetInBodySpace(matA, matB, CubeGrid.Physics, m_topGrid.Physics);
                var breakData = new HkBreakableConstraintData(m_fixedData);
                //Dont dispose the fixed data or we wont have access to them

                breakData.Threshold = BreakOffTreshold;
                breakData.ReapplyVelocityOnBreak = true;
                breakData.RemoveFromWorldOnBrake = false;
                m_constraint = new HkConstraint(CubeGrid.Physics.RigidBody, rotorBody, breakData);
                m_constraint.WantRuntime = true;

                CubeGrid.Physics.AddConstraint(m_constraint);
                if(!m_constraint.InWorld)
                {
                    Debug.Fail("Constraint was not added to world");
                    CubeGrid.Physics.RemoveConstraint(m_constraint);
                    m_constraint.Dispose();
                    m_constraint = null;
                    m_fixedData.Dispose();
                    m_fixedData = null;
                    return;
                }
                m_constraint.Enabled = true;

                m_topBlock.Attach(this);
                m_topGrid.OnPhysicsChanged += CubeGrid_OnPhysicsChanged;

                OnConstraintAdded(GridLinkTypeEnum.Physical, m_topGrid);
                OnConstraintAdded(GridLinkTypeEnum.Logical, m_topGrid);

                m_conveyorEndpoint.Attach(topBlock.ConveyorEndpoint as MyAttachableConveyorEndpoint);
                UpdateText();
            }
        }