Exemplo n.º 1
0
    public void Init()
    {
        if (soft.masses == null)
        {
            soft.masses = new List <Mass2D>();
        }

        soft.masses.Clear();
        float ms = Mass / (float)(NumMasses);

        int ax = (int)axis;

        Vector2 pos = Vector2.zero;

        //DampingRatio = Mathf.Clamp01(DampingRatio);

        damp = (DampingRatio * 0.45f) * (2.0f * Mathf.Sqrt(ms * spring));

        for (int i = 0; i < NumMasses; i++)
        {
            float alpha = (float)i / (float)(NumMasses - 1);

            pos.x = Mathf.Lerp(bbox.min[ax], bbox.max[ax], alpha);
            //Debug.Log("m[" + i + "] alpha " + alpha + " " + pos.x);

            Mass2D rm = new Mass2D(ms, pos);
            soft.masses.Add(rm);
        }

        masspos = new Vector2[soft.masses.Count + 2];

        for (int i = 0; i < soft.masses.Count; i++)
        {
            masspos[i + 1] = soft.masses[i].pos;
        }

        if (soft.springs == null)
        {
            soft.springs = new List <Spring2D>();
        }

        soft.springs.Clear();

        if (soft.constraints == null)
        {
            soft.constraints = new List <Constraint2D>();
        }

        soft.constraints.Clear();

        for (int i = 0; i < soft.masses.Count - 1; i++)
        {
            Spring2D spr = new Spring2D(i, i + 1, spring, damp, soft);

            //float len = spr.restLen;
            spr.restLen *= SpringCompress;
            soft.springs.Add(spr);

            if (Constraints)
            {
                // Do we use restLen or len here?
                Constraint2D lcon = Constraint2D.CreateLenCon(i, i + 1, spr.restLen);
                soft.constraints.Add(lcon);
            }
        }
#if true
        if (BendSprings)
        {
            int gap = 2;
            for (int i = 0; i < soft.masses.Count - gap; i++)
            {
                float    alpha = (float)i / (float)soft.masses.Count;
                Spring2D spr   = new Spring2D(i, i + gap, stiffspring * stiffnessCrv.Evaluate(alpha), stiffdamp * stiffnessCrv.Evaluate(alpha), soft);
                soft.springs.Add(spr);

                Constraint2D lcon = Constraint2D.CreateLenCon(i, i + gap, spr.restLen);
                soft.constraints.Add(lcon);
            }
        }
#endif
        // Apply fixed end constraints
        Constraint2D pcon;

        //if ( left )
        //{
        //	pcon = Constraint2D.CreatePointTargetCon(0, left);
        //}
        //else
        {
            pos.x = bbox.min[ax];
            pos.y = 0.0f;
            pcon  = Constraint2D.CreatePointCon(0, pos);
        }
        pconl = soft.constraints.Count;
        soft.constraints.Add(pcon);

        //if ( right )
        //{
        //	pcon = Constraint2D.CreatePointTargetCon(soft.masses.Count - 1, left);
        //}
        //else
        {
            pos.x = bbox.max[ax];
            pcon  = Constraint2D.CreatePointCon(soft.masses.Count - 1, pos);
        }
        pconr = soft.constraints.Count;
        soft.constraints.Add(pcon);

        soft.DoConstraints();
    }
Exemplo n.º 2
0
    public void Init()
    {
        if ( soft.masses == null )
            soft.masses = new List<Mass2D>();

        soft.masses.Clear();
        float ms = Mass / (float)(NumMasses);

        int ax = (int)axis;

        Vector2 pos = Vector2.zero;

        //DampingRatio = Mathf.Clamp01(DampingRatio);

        damp = (DampingRatio * 0.45f) * (2.0f * Mathf.Sqrt(ms * spring));

        for ( int i = 0; i < NumMasses; i++ )
        {
            float alpha = (float)i / (float)(NumMasses - 1);

            pos.x = Mathf.Lerp(bbox.min[ax], bbox.max[ax], alpha);
            //Debug.Log("m[" + i + "] alpha " + alpha + " " + pos.x);

            Mass2D rm = new Mass2D(ms, pos);
            soft.masses.Add(rm);
        }

        masspos = new Vector2[soft.masses.Count + 2];

        for ( int i = 0; i < soft.masses.Count; i++ )
            masspos[i + 1] = soft.masses[i].pos;

        if ( soft.springs == null )
            soft.springs = new List<Spring2D>();

        soft.springs.Clear();

        if ( soft.constraints == null )
            soft.constraints = new List<Constraint2D>();

        soft.constraints.Clear();

        for ( int i = 0; i < soft.masses.Count - 1; i++ )
        {
            Spring2D spr = new Spring2D(i, i + 1, spring, damp, soft);

            //float len = spr.restLen;
            spr.restLen *= SpringCompress;
            soft.springs.Add(spr);

            if ( Constraints )
            {
                // Do we use restLen or len here?
                Constraint2D lcon = Constraint2D.CreateLenCon(i, i + 1, spr.restLen);
                soft.constraints.Add(lcon);
            }
        }
        #if true
        if ( BendSprings )
        {
            int gap = 2;
            for ( int i = 0; i < soft.masses.Count - gap; i++ )
            {
                float alpha = (float)i / (float)soft.masses.Count;
                Spring2D spr = new Spring2D(i, i + gap, stiffspring * stiffnessCrv.Evaluate(alpha), stiffdamp * stiffnessCrv.Evaluate(alpha), soft);
                soft.springs.Add(spr);

                Constraint2D lcon = Constraint2D.CreateLenCon(i, i + gap, spr.restLen);
                soft.constraints.Add(lcon);
            }
        }
        #endif
        // Apply fixed end constraints
        Constraint2D pcon;

        //if ( left )
        //{
        //	pcon = Constraint2D.CreatePointTargetCon(0, left);
        //}
        //else
        {
            pos.x = bbox.min[ax];
            pos.y = 0.0f;
            pcon = Constraint2D.CreatePointCon(0, pos);
        }
        pconl = soft.constraints.Count;
        soft.constraints.Add(pcon);

        //if ( right )
        //{
        //	pcon = Constraint2D.CreatePointTargetCon(soft.masses.Count - 1, left);
        //}
        //else
        {
            pos.x = bbox.max[ax];
            pcon = Constraint2D.CreatePointCon(soft.masses.Count - 1, pos);
        }
        pconr = soft.constraints.Count;
        soft.constraints.Add(pcon);

        soft.DoConstraints();
    }