Esempio n. 1
0
        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);
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
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();
            }
        }