Example #1
0
    void CreateConstraint()
    {
        m_startMatrix = m_baseBody ? m_baseBody.transform.worldToLocalMatrix * transform.localToWorldMatrix : transform.localToWorldMatrix;
        m_showSnaps   = !!m_showSnaps;
        TxSoftBody softBody = m_attachedBody = GetComponent <TxSoftBody>();

        m_jointID = TxNative.CreateJoint();
        TxNative.JointSetFlags(m_jointID, (m_disableCollision ? (1 << 0) : 0));
        foreach (Snap s in m_snaps)
        {
            int[] nodes = softBody.truss.FindNodeSet(s.node);
            if (s.type == SnapType.Point && nodes != null && nodes.Length > 1) // @@@
            {
                foreach (int node in nodes)
                {
                    Vector3 position = softBody.transform.TransformPoint(softBody.truss.nodePosition[node]);
                    if (m_baseBody)
                    {
                        position = m_baseBody.transform.InverseTransformPoint(position);
                    }
                    TxNative.JointSnapToPoint(m_jointID, node, s.minLimit, s.maxLimit, s.strength, s.flags, position);
                }
            }
            else
            {
                if (nodes != null && nodes.Length == 1)
                {
                    int node = nodes[0];
                    switch (s.type)
                    {
                    case SnapType.Point:
                        Vector3 position = softBody.transform.TransformPoint(softBody.truss.nodePosition[node]);
                        if (m_baseBody)
                        {
                            position = m_baseBody.transform.InverseTransformPoint(position);
                        }
                        TxNative.JointSnapToPoint(m_jointID, node, s.minLimit, s.maxLimit, s.strength, s.flags, position);
                        break;

                    case SnapType.Node:
                        if (!(m_baseBody is TxSoftBody))
                        {
                            Debug.LogError("TRUSS PHYSICS: Soft body '" + softBody.gameObject.name + "' constraint. Base body isn't soft body.");
                        }
                        else
                        {
                            int[] nodesB = (m_baseBody as TxSoftBody).truss.FindNodeSet(s.featureB);
                            if (nodesB == null)
                            {
                                Debug.LogError("TRUSS PHYSICS: Soft body '" + m_baseBody.gameObject.name + "'. Nodes set '" + s.featureB + "' not found.");
                            }
                            else if (nodesB.Length == 1)
                            {
                                TxNative.JointSnapToNode(m_jointID, node, s.minLimit, s.maxLimit, s.strength, s.flags, nodesB[0]);
                            }
                            else
                            {
                                Debug.LogError("TRUSS PHYSICS: Soft body '" + m_baseBody.gameObject.name + "'. Nodes set '" + s.featureB + "' should contain one node.");
                            }
                        }
                        break;

                    case SnapType.Edge:
                        if (!(m_baseBody is TxSoftBody))
                        {
                            Debug.LogError("TRUSS PHYSICS: Soft body '" + m_baseBody.gameObject.name + "' joint. Base body isn't soft body.");
                        }
                        else
                        {
                            int[] nodesB = (m_baseBody as TxSoftBody).truss.FindNodeSet(s.featureB);
                            if (nodesB == null)
                            {
                                Debug.LogError("TRUSS PHYSICS: Soft body '" + m_baseBody.gameObject.name + "'. Nodes set '" + s.featureB + "' not found.");
                            }
                            else if (nodesB.Length == 2)
                            {
                                TxNative.JointSnapToEdge(m_jointID, node, s.minLimit, s.maxLimit, s.strength, s.flags, nodesB[0], nodesB[1]);
                            }
                            else
                            {
                                Debug.LogError("TRUSS PHYSICS: Soft body '" + m_baseBody.gameObject.name + "'. Nodes set '" + s.featureB + "' should contain two nodes.");
                            }
                        }
                        break;
                    }
                }
            }
        }
        m_jointInstanceID = TxNative.WorldObjectAttachJoint(softBody.worldID, softBody.objectID, m_baseBody ? m_baseBody.objectID : -1, m_jointID);
        if (m_enableMotor)
        {
            m_motorID = TxNative.CreateMotor();
            int[] axis = softBody.truss.FindNodeSet(m_axisNodeSet);
            if (axis == null)
            {
                Debug.LogError("TRUSS PHYSICS: Soft body '" + softBody.gameObject.name + "'. Nodes set '" + m_axisNodeSet + "' not found.");
            }
            else if (axis.Length == 2)
            {
                TxNative.MotorSetAxis(m_motorID, axis[0], axis[1]);
            }
            else
            {
                Debug.LogError("TRUSS PHYSICS: Soft body '" + softBody.gameObject.name + "'. Nodes set '" + m_axisNodeSet + "' should contain two nodes.");
            }
            m_motorInstanceID = TxNative.WorldObjectAttachMotor(softBody.worldID, softBody.objectID, m_baseBody ? m_baseBody.objectID : -1, m_motorID);
            TxNative.MotorInstanceSetRate(softBody.worldID, m_motorInstanceID, m_targetRate);
            TxNative.MotorInstanceSetTorque(softBody.worldID, m_motorInstanceID, m_maxTorque);
        }
    }