public void generateSprings()
    {
        foreach (ApplyParticle i in allPoints)
        {
            int springIndex = FindIndex(allPoints, i);
            i.particle.allInstances = new List <Particle>();

            if ((springIndex + 1) % width > springIndex % width)
            {
                i.particle.allInstances.Add(allPoints[springIndex + 1].particle);
                SpringDamper sdRight = new SpringDamper(Spring, Damping, Rest, i.particle, allPoints[springIndex + 1].particle);
                allJoints.Add(sdRight);
            }

            if (springIndex + width < allPoints.Count)
            {
                i.particle.allInstances.Add(allPoints[springIndex + width].particle);
                SpringDamper sdDown = new SpringDamper(Spring, Damping, Rest, i.particle, allPoints[springIndex + width].particle);
                allJoints.Add(sdDown);
            }

            if ((springIndex + 1) % width > springIndex % width && springIndex + width + 1 < allPoints.Count)
            {
                i.particle.allInstances.Add(allPoints[springIndex + width + 1].particle);
                SpringDamper sdRD = new SpringDamper(Spring, Damping, Rest, i.particle, allPoints[springIndex + width + 1].particle);
                allJoints.Add(sdRD);
            }

            if (springIndex + width - 1 < allPoints.Count && springIndex - 1 >= 0 && (springIndex - 1) % width < springIndex % width)
            {
                i.particle.allInstances.Add(allPoints[springIndex + width - 1].particle);
                SpringDamper sdLD = new SpringDamper(Spring, Damping, Rest, i.particle, allPoints[springIndex + width - 1].particle);
                allJoints.Add(sdLD);
            }
        }

        foreach (SpringDamper i in allJoints)
        {
            int          sdIndex  = FindIndex(allJoints, i);
            GameObject   linkDraw = new GameObject();
            LineRenderer lr       = linkDraw.AddComponent <LineRenderer>();

            linkDraw.transform.position = (allJoints[sdIndex].partOne.Position + allJoints[sdIndex].partTwo.Position) / 2;

            lr.SetPosition(0, allJoints[sdIndex].partOne.Position);
            lr.SetPosition(1, allJoints[sdIndex].partTwo.Position);
            lr.material.color = Color.black;
            lr.SetWidth(0.25f, 0.25f);
            allLines.Add(lr);
            linkDraw.name       = "Link " + (allLines.Count).ToString();
            lr.transform.parent = transform;
        }
    }
    public int FindIndex(List <SpringDamper> SpringList, SpringDamper aSpringDamper)
    {
        int index = 0;

        for (int i = 0; i < SpringList.Count; i++)
        {
            if (SpringList[i] == aSpringDamper)
            {
                index = i;
                break;
            }
        }
        return(index);
    }
 public void clothTearing(SpringDamper torn)
 {
     if ((torn.partTwo.Position - torn.partOne.Position).magnitude > (Rest * tearPoint) / (0.03f * fSpring))
     {
         if ((torn.partTwo.allInstances.Contains(torn.partOne)))
         {
             torn.partTwo.allInstances.Remove(torn.partOne);
             Destroy(allLines[allJoints.IndexOf(torn)].gameObject);
             allLines.Remove(allLines[allJoints.IndexOf(torn)]);
             allJoints.Remove(torn);
         }
         if (torn.partOne.allInstances.Contains(torn.partTwo))
         {
             torn.partOne.allInstances.Remove(torn.partTwo);
             Destroy(allLines[allJoints.IndexOf(torn)].gameObject);
             allLines.Remove(allLines[allJoints.IndexOf(torn)]);
             allJoints.Remove(torn);
         }
     }
 }
    // Use this for initialization
    void Start()
    {
        p1 = new Particle(new Vector3(0, 0, 0));
        p2 = new Particle(new Vector3(0, 1, 0));
        SD = new SpringDamper(p1, p2);

        for (int y = 0; y < height; y++)
        {
            for (float x = 0; x < width; x++)
            {
                particleList.Add(new Particle(new Vector3(x, y, 0)));
            }
        }

        for (int u = 0; u < particleList.Count; u++)
        {
            //if (particleList[u].r.y == height - 1)
            //{
            //    particleList[u].anchor = true;
            //}

            if (particleList[u].r.y == height - 1)
            {
                particleList[height * (width - 1)].anchor = true;
                particleList[(height * width) - 1].anchor = true;
            }
        }

        for (int i = 0; i < particleList.Count; i++)
        {
            if (particleList[i].r.x < width - 1)
            {
                springdamper.Add(new SpringDamper(particleList[i], particleList[i + 1]));
                var l = Instantiate(Line);
                line.Add(l);
            }
            if (particleList[i].r.y < height - 1)
            {
                springdamper.Add(new SpringDamper(particleList[i], particleList[i + width]));
                var l = Instantiate(Line);
                line.Add(l);
            }
            if (particleList[i].r.x < width - 1 && particleList[i].r.y < height - 1)
            {
                springdamper.Add(new SpringDamper(particleList[i], particleList[i + 1 + height]));
                var l = Instantiate(Line);
                line.Add(l);
            }
            if (particleList[i].r.x > 0 && particleList[i].r.y != height - 1)
            {
                springdamper.Add(new SpringDamper(particleList[i], particleList[i - 1 + width]));
                var l = Instantiate(Line);
                line.Add(l);
            }
        }

        for (int i = 0; i < particleList.Count; i++)
        {
            if (particleList[i].r.x < width - 1 && particleList[i].r.y < height - 1)
            {
                forces.Add(new AerodynamicForce(particleList[i], particleList[i + 1], particleList[i + width]));
                forces.Add(new AerodynamicForce(particleList[i + 1], particleList[i + width], particleList[i + width + 1]));
            }
        }
    }
    /// <summary>
    /// Calculates the Spring Force of each spring in the simulation
    /// </summary>
    /// <param name="s"></param>
    void CalcSpringForce(SpringDamper s)
    {
        Vector3 disBetween = s.p2.transform.position - s.p1.transform.position;
        Vector3 disBetweenNorm = disBetween.normalized / s.l;

        float dis = CalcDis(s.p2.transform.position, s.p1.transform.position);
        float springForce = -k * (s.l - dis);

        float v1 = Vector3.Dot(disBetween, s.p1.m_Velocity);
        float v2 = Vector3.Dot(disBetween, s.p2.m_Velocity);

        float springDamp = -b * (v1 - v2);

        float Damper = springForce + springDamp;

        Vector3 f1 = Damper * disBetweenNorm;
        Vector3 f2 = -f1;

        s.p1.m_Force += f1;
        s.p2.m_Force += f2;
        s.DrawLines();
    }