public static void SetInBodySpace(this HkHingeConstraintData data, Vector3 posA, Vector3 posB, Vector3 axisA, Vector3 axisB, MyPhysicsBody bodyA, MyPhysicsBody bodyB)
        {
            if (bodyA.IsWelded)
            {
                posA  = Vector3.Transform(posA, bodyA.WeldInfo.Transform);
                axisA = Vector3.TransformNormal(axisA, bodyA.WeldInfo.Transform);
            }
            if (bodyB.IsWelded)
            {
                posB  = Vector3.Transform(posB, bodyB.WeldInfo.Transform);
                axisB = Vector3.TransformNormal(axisB, bodyB.WeldInfo.Transform);
            }

            data.SetInBodySpaceInternal(ref posA, ref posB, ref axisA, ref axisB);
        }
        private void CreateConstraintNosync(MyShipConnector otherConnector, ref Vector3 posA, ref Vector3 posB, ref Vector3 axisA, ref Vector3 axisB)
        {
            var data = new HkHingeConstraintData();
            data.SetInBodySpace(ref posA, ref posB, ref axisA, ref axisB);
            var data2 = new HkMalleableConstraintData();
            data2.SetData(data);
            data.ClearHandle();
            data = null;
            data2.Strength = 0.0003f;

            var newConstraint = new HkConstraint(CubeGrid.Physics.RigidBody, otherConnector.CubeGrid.Physics.RigidBody, data2);
            this.Master = true;
            otherConnector.Master = false;
            SetConstraint(otherConnector, newConstraint);
            otherConnector.SetConstraint(this, newConstraint);

            AddConstraint(newConstraint);
        }
        void CubeGrid_OnPhysicsChanged(MyEntity obj)
        {
            if (Sync.IsServer && m_welding == false && InConstraint && m_welded == false)
            {
                if (m_hasConstraint)
                {
                    if (MyPhysicsBody.IsConstraintValid(m_constraint) == false && m_constraint.IsDisposed == false)
                    {
                        RemoveConstraint(m_other, m_constraint);
                        this.m_constraint = null;
                        m_other.m_constraint = null;
                        m_hasConstraint = false;
                        m_other.m_hasConstraint = false;
                        
                        if (m_connectionState.Value.MasterToSlave.HasValue == false && CubeGrid.Physics != null && m_other.CubeGrid.Physics != null)
                        {
                            var posA = ConstraintPositionInGridSpace();
                            var posB = m_other.ConstraintPositionInGridSpace();
                            var axisA = ConstraintAxisGridSpace();
                            var axisB = -m_other.ConstraintAxisGridSpace();

                            var data = new HkHingeConstraintData();
                            data.SetInBodySpace(posA, posB, axisA, axisB, CubeGrid.Physics, m_other.CubeGrid.Physics);
                            var data2 = new HkMalleableConstraintData();
                            data2.SetData(data);
                            data.ClearHandle();
                            data = null;
                            data2.Strength = GetEffectiveStrength(m_other);

                            var newConstraint = new HkConstraint(CubeGrid.Physics.RigidBody, m_other.CubeGrid.Physics.RigidBody, data2);
                            this.SetConstraint(m_other, newConstraint);
                            m_other.SetConstraint(this, newConstraint);

                            AddConstraint(newConstraint);
                        }
                    }
                }
            }
        }
        private void CreateConstraintNosync(MyShipConnector otherConnector)
        {
            Debug.Assert(IsMaster, "Constraints should be created only master (entity with higher EntityId)");

            var posA = ConstraintPositionInGridSpace();
            var posB = otherConnector.ConstraintPositionInGridSpace();
            var axisA = ConstraintAxisGridSpace();
            var axisB = -otherConnector.ConstraintAxisGridSpace();

            var data = new HkHingeConstraintData();
            data.SetInBodySpace(posA, posB, axisA, axisB, CubeGrid.Physics, otherConnector.CubeGrid.Physics);
            var data2 = new HkMalleableConstraintData();
            data2.SetData(data);
            data.ClearHandle();
            data = null;
            data2.Strength = GetEffectiveStrength(otherConnector);

            var newConstraint = new HkConstraint(CubeGrid.Physics.RigidBody, otherConnector.CubeGrid.Physics.RigidBody, data2);
            SetConstraint(otherConnector, newConstraint);
            otherConnector.SetConstraint(this, newConstraint);

            AddConstraint(newConstraint);
        }
        void CubeGrid_OnPhysicsChanged(MyEntity obj)
        {
            if (m_hasConstraint)
            {
                if (MyPhysicsBody.IsConstraintValid(m_constraint) == false && m_constraint.IsDisposed == false)
                {
                    RemoveConstraint(m_other, m_constraint);
               
                    if (m_connectionState.Value.MasterToSlave.HasValue)
                    {
                            Matrix localSpaceA = Matrix.CreateTranslation(m_connectionPosition);
                            Matrix localSpaceB = localSpaceA * m_connectionState.Value.MasterToSlave.Value;

                            localSpaceA = localSpaceA * this.PositionComp.LocalMatrix;
                            localSpaceB = localSpaceB * m_other.PositionComp.LocalMatrix;

                            var data = new HkFixedConstraintData();
                            data.SetInBodySpace(localSpaceA, localSpaceB, CubeGrid.Physics, m_other.CubeGrid.Physics);
                            var newConstraint = new HkConstraint(CubeGrid.Physics.RigidBody, m_other.CubeGrid.Physics.RigidBody, data);
                            this.SetConstraint(m_other, newConstraint);
                            m_other.SetConstraint(this, newConstraint);

                            AddConstraint(newConstraint);
                    }
                    else
                    {
                        var posA = ConstraintPositionInGridSpace();
                        var posB = m_other.ConstraintPositionInGridSpace();
                        var axisA = ConstraintAxisGridSpace();
                        var axisB = -m_other.ConstraintAxisGridSpace();

                        var data = new HkHingeConstraintData();
                        data.SetInBodySpace(posA, posB, axisA, axisB, CubeGrid.Physics, m_other.CubeGrid.Physics);
                        var data2 = new HkMalleableConstraintData();
                        data2.SetData(data);
                        data.ClearHandle();
                        data = null;
                        data2.Strength = GetEffectiveStrength(m_other);

                        var newConstraint = new HkConstraint(CubeGrid.Physics.RigidBody, m_other.CubeGrid.Physics.RigidBody, data2);
                        this.SetConstraint(m_other, newConstraint);
                        m_other.SetConstraint(this, newConstraint);

                        AddConstraint(newConstraint);
                    }
                }
            }
        }