Esempio n. 1
0
    /// <summary>
    /// Remove a JelloJoint from this JelloBody.
    /// </summary>
    /// <param name="joint">The JelloJoint to remove.</param>
    public void RemoveJoint(JelloJoint joint)
    {
        bool contains = false;

        List<JelloJoint> tempJoints = new List<JelloJoint>();
        for(int i = 0; i < mJoints.Length; i++)
        {
            if(joint != mJoints[i])
            {
                tempJoints.Add (mJoints[i]);
            }
            else
            {
                //do something
                contains = true;
            }
        }

        if(!contains)
            return;

        mJoints = tempJoints.ToArray();

        bool rigidB = joint.bodyB == null && joint.rigidbodyB != null;
        bool rigidA = joint.bodyA == null && joint.rigidbodyA != null;
        //only check for similar if a rigidbody
        if(rigidB || rigidA)
        {
            bool similar = false;
            //add similar to a list?
            List<JelloJoint> similarJoints = new List<JelloJoint>();

            for(int i = 0; i < mJoints.Length; i++)
            {
                similar = false;

                if(rigidB)
                {
                    if(joint.TransformB == mJoints[i].TransformB)
                    {
                        similar = true;
                    }
                    else if(joint.TransformB == mJoints[i].TransformA)
                    {
                        similar = true;
                    }
                }
                else if(rigidA)
                {
                    if(joint.TransformA == mJoints[i].TransformA)
                    {
                        similar = true;
                    }
                    else if(joint.TransformA == mJoints[i].TransformB)
                    {
                        similar = true;
                    }
                }

                if(similar)
                    similarJoints.Add (mJoints[i]);
            }

            for(int i = 0; i < similarJoints.Count; i++)
            {
                similarJoints[i].numSimilar = similarJoints.Count;
            }

            joint.numSimilar = similarJoints.Count;
        }

        if(Application.isPlaying && !applicationIsQuitting)
            World.removeJoint(joint);
    }
Esempio n. 2
0
    //TODO make alternative method sigantures.
    /// <summary>
    /// Add a JelloJoint to this JelloBody.
    /// </summary>
    /// <param name="joint">The JelloJoint to add.</param>
    public void AddJoint(JelloJoint joint)
    {
        if(mJoints == null)
            mJoints = new JelloJoint[0];

        for(int i = 0; i < mJoints.Length; i++)
        {
            if(mJoints[i] == joint)
                return;
        }

        bool rigidB = joint.bodyB == null && joint.rigidbodyB != null;
        bool rigidA = joint.bodyA == null && joint.rigidbodyA != null;
        //only check for similar if a rigidbody
        if(rigidB || rigidA)
        {
            bool similar = false;
            //add similar to a list?
            List<JelloJoint> similarJoints = new List<JelloJoint>();

            for(int i = 0; i < mJoints.Length; i++)
            {
                similar = false;
                if(rigidB)
                {
                    if(joint.TransformB == mJoints[i].TransformB)
                    {
                        similar = true;
                    }
                    else if(joint.TransformB == mJoints[i].TransformA)
                    {
                        similar = true;
                    }
                }
                else if(rigidA)
                {
                    if(joint.TransformA == mJoints[i].TransformA)
                    {
                        similar = true;
                    }
                    else if(joint.TransformA == mJoints[i].TransformB)
                    {
                        similar = true;
                    }
                }

                if(similar)
                    similarJoints.Add (mJoints[i]);
            }

            for(int i = 0; i < similarJoints.Count; i++)
            {
                similarJoints[i].numSimilar = similarJoints.Count + 1;
            }

            joint.numSimilar = similarJoints.Count + 1;
        }

        JelloJoint[] oldJoints = mJoints;
        mJoints = new JelloJoint[mJoints.Length + 1];
        for(int i =0; i < oldJoints.Length; i++)
        {
            mJoints[i] = oldJoints[i];
        }
        mJoints[mJoints.Length - 1] = joint;

        if(Application.isPlaying)
            World.addJoint(joint);
    }
Esempio n. 3
0
    /// <summary>
    /// Clears any invalid JelloJoint from the JelloBody.
    /// </summary>
    public void ClearInvalidJoints()
    {
        if(mJoints != null)
        {
            bool skip = false;

            //Remove any invalid joints.
            List<JelloJoint> joints = new List<JelloJoint>();
            for(int i = 0; i < mJoints.Length; i++)
            {
                JelloJoint joint = mJoints[i];
                skip = false;

                if(joint == null)
                    continue;

                if(joint.bodyA != null)
                {
                    for(int a = 0; a < joint.affectedIndicesA.Length; a++)
                    {
                        if(joint.affectedIndicesA[a] > PointMassCount || joint.affectedIndicesA[a] < 0)
                        {
                            Debug.LogWarning(name + " JelloJoint  # " + i + " is invalid, removing.");
                            skip = true;
                            break;
                        }
                    }
                }

                if(joint.bodyB != null)
                {
                    for(int a = 0; a < joint.affectedIndicesB.Length; a++)
                    {
                        if(joint.affectedIndicesB[a] > PointMassCount || joint.affectedIndicesB[a] < 0)
                        {
                            Debug.LogWarning(name + " JelloJoint  # " + i + " is invalid, removing.");
                            skip = true;
                            break;
                        }
                    }
                }

                if(skip)
                    continue;

                joints.Add (joint);
            }

            mJoints = joints.ToArray();
        }
    }