Пример #1
0
    //Appli the force of the spring to the particles that
    //take part of it
    public void ApplyForce()
    {
        //First I make a correction to avoid superelasticity.
        Vector3 direction1 = a.Position - b.Position;
        var     dirnor     = direction1.magnitude;
        var     f          = (dirnor - restLength) / restLength;

        /*if(a.isActive) a.Position -= 0.5f * direction1 * f;
        *  if(b.isActive) b.Position += 0.5f * direction1 * f;*/

        //Calculation of damping and elastic forces of the spring.
        Vector3 direction = a.Position - b.Position;
        float   dist      = direction.magnitude;

        direction = direction.normalized;

        float springForce = -elast * (dist - restLength);

        Vector3 deltaVelocity = a.Velocity - b.Velocity;
        float   dampingForce  = -dampi *Vector3.Dot(deltaVelocity, direction);

        Vector3 force = (springForce + dampingForce) * direction;

        //Add the force to the particle (is it's needed, if it's not ancored)
        if (float.IsNaN(dampingForce))
        {
            Debug.Log("nan-ForceDamping");
        }
        if (float.IsNaN(a.Velocity.x))
        {
            Debug.Log("nan-ForceDam");
        }
        if (a.isActive)
        {
            a.AddForce(force);
        }
        if (b.isActive)
        {
            b.AddForce(-force);
        }
    }