Exemplo n.º 1
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);
            }
        }