Example #1
0
    public void Init(MegaWire wire)
    {
        if (start == null || end == null)
        {
            return;
        }

        Vector3 p1 = start.TransformPoint(outOffset);
        Vector3 p2 = end.TransformPoint(inOffset);

        WireLength = (p1 - p2).magnitude;

        if (masses == null)
        {
            masses = new List <MegaWireMass>();
        }

        masses.Clear();
        float ms = wire.Mass / (float)(wire.points + 1);

        for (int i = 0; i <= wire.points; i++)
        {
            float alpha = (float)i / (float)wire.points;

            float        rn = (Random.value - 0.5f) * 2.0f * (wire.massRand * ms);
            float        m  = rn + ms;
            MegaWireMass rm = new MegaWireMass(m, Vector3.Lerp(p1, p2, alpha));
            masses.Add(rm);
        }

        if (springs == null)
        {
            springs = new List <MegaWireSpring>();
        }

        springs.Clear();

        if (constraints == null)
        {
            constraints = new List <MegaWirePointConstraint>();
        }

        if (lenconstraints == null)
        {
            lenconstraints = new List <MegaWireLengthConstraint>();
        }

        constraints.Clear();
        lenconstraints.Clear();

        for (int i = 0; i < masses.Count - 1; i++)
        {
            MegaWireSpring spr = new MegaWireSpring(i, i + 1, wire.spring, wire.damp, this, wire.stretch);
            springs.Add(spr);

            if (wire.lengthConstraints)
            {
                MegaWireLengthConstraint lcon = new MegaWireLengthConstraint(i, i + 1, spr.restlen);
                lenconstraints.Add(lcon);
            }
        }

        if (wire.stiffnessSprings)
        {
            int gap = 2;
            for (int i = 0; i < masses.Count - gap; i++)
            {
                MegaWireSpring spr = new MegaWireSpring(i, i + 2, wire.stiffrate, wire.stiffdamp, this, wire.stretch);
                springs.Add(spr);
                //float alpha = (float)i / (float)masses.Count;
                //MegaWireSpring spr = new MegaWireSpring(i, i + gap, stiffspring * stiffnessCrv.Evaluate(alpha), stiffdamp * stiffnessCrv.Evaluate(alpha), this);	//, stretch);
                //springs.Add(spr);

                //WireLengthConstraint lcon = new WireLengthConstraint(i, i + gap, spr.restlen);
                //constraints.Add(lcon);
            }
        }

        // Apply fixed end constraints
        MegaWirePointConstraint pcon = new MegaWirePointConstraint(0, start.transform, outOffset);

        constraints.Add(pcon);

        pcon = new MegaWirePointConstraint(masses.Count - 1, end.transform, inOffset);
        constraints.Add(pcon);

        masspos = new Vector3[masses.Count + 2];

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

        masspos[0] = masses[0].pos - (masses[1].pos - masses[0].pos);
        masspos[masspos.Length - 1] = masses[masses.Count - 1].pos + (masses[masses.Count - 1].pos - masses[masses.Count - 2].pos);
    }
	void BuildSoftBody()
	{
		if ( masses == null )
			masses = new List<MegaWireMass>();

		masses.Clear();
		float ms = wire.Mass / (float)(wire.points + 1);

		for ( int i = 0; i <= wire.points; i++ )
		{
			float alpha = (float)i / (float)wire.points;

			float rn = (Random.value - 0.5f) * 2.0f * (wire.massRand * ms);
			float m = rn + ms;
			MegaWireMass rm = new MegaWireMass(m, Vector3.Lerp(p1, p2, alpha));
			masses.Add(rm);
		}

		if ( springs == null )
			springs = new List<MegaWireSpring>();

		springs.Clear();

		if ( constraints == null )
			constraints = new List<MegaWirePointConstraint>();

		if ( lenconstraints == null )
			lenconstraints = new List<MegaWireLengthConstraint>();

		constraints.Clear();
		lenconstraints.Clear();

		for ( int i = 0; i < masses.Count - 1; i++ )
		{
			MegaWireSpring spr = new MegaWireSpring(i, i + 1, wire.spring, wire.damp, this, wire.stretch);
			springs.Add(spr);

			if ( wire.lengthConstraints )
			{
				MegaWireLengthConstraint lcon = new MegaWireLengthConstraint(i, i + 1, spr.restlen);
				lenconstraints.Add(lcon);
			}
		}

		if ( wire.stiffnessSprings )
		{
			int gap = 2;
			for ( int i = 0; i < masses.Count - gap; i++ )
			{
				MegaWireSpring spr = new MegaWireSpring(i, i + 2, wire.stiffrate, wire.stiffdamp, this, wire.stretch);
				springs.Add(spr);
				//float alpha = (float)i / (float)masses.Count;
				//MegaWireSpring spr = new MegaWireSpring(i, i + gap, stiffspring * stiffnessCrv.Evaluate(alpha), stiffdamp * stiffnessCrv.Evaluate(alpha), this);	//, stretch);
				//springs.Add(spr);

				//WireLengthConstraint lcon = new WireLengthConstraint(i, i + gap, spr.restlen);
				//constraints.Add(lcon);
			}
		}

		// Apply fixed end constraints
		MegaWirePointConstraint pcon = new MegaWirePointConstraint(0, start.transform, outOffset);
		constraints.Add(pcon);

		pcon = new MegaWirePointConstraint(masses.Count - 1, end.transform, inOffset);
		constraints.Add(pcon);

		masspos = new Vector3[masses.Count + 2];

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

		masspos[0] = masses[0].pos - (masses[1].pos - masses[0].pos);
		masspos[masspos.Length - 1] = masses[masses.Count - 1].pos + (masses[masses.Count - 1].pos - masses[masses.Count - 2].pos);

	}