DEFINITION  
    output = "pix/chain";

    xleft = 0; xright = 323;
    ylow  = 0; yhigh  = 242;

    aalevelmin  = 1;
    aalevelmax  = 2;
    aawidth     = 1;
    aathreshold = 100;

    up = yaxis;
    grid_cache = 4000;

	triple CameraView(0, 0, 0);
    scalar _print_ret_val(0);
    scalar a(0);
    scalar atn(0);
    scalar ax(0);
    scalar ay(0);
    scalar az(0);
    scalar b(0);
    scalar bend(0);
    scalar bend_damping(0);
    scalar bond(0);

    scalar d(0);
    scalar damping(0);
    scalar dt(0);
    scalar dt2(0);

    scalar dvx(0);
    scalar dvy(0);
    scalar dvz(0);

    scalar dx(0);
    scalar dy(0);
    scalar dz(0);

    scalar dxb(0);
    scalar dyb(0);
    scalar dzb(0);

    scalar elasticity(0);
    scalar f(0);
    scalar floor_damping(0);
    scalar floor_thickness(0);

    array forcex[64];
    array forcey[64];
    array forcez[64];

    scalar gravity(0);
    scalar last(0);
    scalar length(0);

    array mass[64];
    scalar mass_v(0);

    scalar next(0);
    scalar numint(0);
    scalar numpart(0);
    scalar other(0);
    scalar part(0);

    scalar r(0);
    scalar r2(0);
    scalar r3(0);
    scalar r5(0);
    scalar rad2(0);
    scalar radius(0);

    scalar scf(0);
    scalar sub(0);
    scalar timescale(0);
    scalar velocity(0);

    array vx[64];
    array vy[64];
    array vz[64];

    scalar vxmean(0);
    scalar vymean(0);
    scalar vzmean(0);

    array x[64];
    array y[64];
    array z[64];

    scalar xmax(0);
    scalar ymax(0);

    scalar ymin(0);
    scalar xmin(0);

    scalar xmean(0);
    scalar ymean(0);
    scalar zmean(0);

    scalar zangle(0);

startframe = 1.0;
endframe   = 300.0;
byframe    = 1.0;


shader Shader2 (
    model    = lambert,
    diffuse  = 0.7,
    color    = (255, 0, 0),
    transparency = 0.0,
    reflection_limit   = 0,
    refraction_limit   = 0,
    shadow_level_limit = -1,
    transparency_shade = 0.0
);

shader Shader1 (
    model    = lambert,
    diffuse  = 0.7,
    color    = (255, 255, 255),
    transparency = 0.0,
    reflection_limit   = 0,
    refraction_limit   = 0,
    shadow_level_limit = -1,
    transparency_shade = 0.0
);


ENVIRONMENT
	background( color = (0, 50, 10) );

MODEL

{
    translate(0, 0, 16);
    light (model = ambient, color = (130, 130, 130),
        ambient_shade = 0.0);
}

{
    camera (
        eye   = (0, 0, 16),
        view  = CameraView,
        twist = 0.0,
        fov   = (100.0),
        viewport = (xleft, xright, ylow, yhigh),
        aspect   = 1.31627,
        near = 0.1,
        far = 10000
    );
}

/* Caution: making mass_v much larger or gravity much stronger will */
/* cause odd effects unless you make bond larger. */

/* Define the control parameters for the particles and the springs */
timescale = 2.1;
mass_v    = 1.0;

/* NOTE: Playing with this value will */
/* simulate chains of differing size. */
/* The smaller gravity gets, the larger the chain will */
/* seem to be. */
gravity   = (0.125 / 30.0);

velocity  = 0.005;
ymax      = 60.0;
ymin      = -15.4;
xmax      = 60.0;
xmin      = -60.0;
elasticity = 0.95;
radius    = 1.0;
rad2      = (radius * radius);
floor_damping = 0.1;
floor_thickness = (radius / 2.0);
bond      = 4.0;
damping   = 4.0;
bend      = 0.15;
bend_damping = 0.15;

a = bond;
b = (bond / (radius * radius));

numpart = 20;
numint = 12;

dt = (timescale / numint);
dt2 = ((0.5 * dt) * dt);

/* Define the initial conditions */
if ((frame == startframe))
{
    _print_ret_val = srand( 0.0 );
    for (part = 0; (part < 64); part = (part + 1); )
    {
        x[part] = (-0.0 + (radius * part));
        y[part] = 9.0;
        z[part] = 0.0;
        vx[part] = (velocity * rand());
        vy[part] = (velocity * rand());
        vz[part] = 0.0;
        mass[part] = mass_v;
        mass[part] = mass_v;
    }
}

/* Loop for each subinterval of time */
for (sub = 0; (sub < numint); sub = (sub + 1); )
{
    for (part = 0; (part < 64); part = (part + 1); )
    {
        forcex[part] = 0.0;
        forcey[part] = 0.0;
        forcez[part] = 0.0;
    }

    /* Loop for each part to compute the effect of the neighbour forces */
    for (part = 0; (part < (numpart - 1)); part = (part + 1); )
    {
        other = (part + 1);

        /* Compute the interparticle attraction */
        dx = (x[part] - x[other]);
        dy = (y[part] - y[other]);
        dz = (z[part] - z[other]);

        r2 = (((dx * dx) + (dy * dy)) + (dz * dz));
        if ((r2 < (4.0 * rad2)))
        {
            r = sqrt( r2 );
            if ((r < 0.0001))
                r = 0.0001;

            r3 = (r * r2);
            r5 = (r3 * r2);

            scf = ((radius - (0.5 * r)) / radius);

            dvx = (vx[part] - vx[other]);
            dvy = (vy[part] - vy[other]);
            dvz = (vz[part] - vz[other]);

            f = ((((a / r5) - (b / r3)) - ((damping
                * (((dvx * dx) + (dvy * dy)) + (dvz * dz))) / r2)) * scf);

            forcex[part] = (forcex[part] + (f * dx));
            forcey[part] = (forcey[part] + (f * dy));
            forcez[part] = (forcez[part] + (f * dz));

            forcex[other] = (forcex[other] - (f * dx));
            forcey[other] = (forcey[other] - (f * dy));
            forcez[other] = (forcez[other] - (f * dz));

            /* Compute the floor drag-factor term */
            if ((y[part] < (ymin + floor_thickness)))
            {
                atn = ((floor_damping * ((ymin + floor_thickness)
                    - y[part])) / floor_thickness);
                forcex[part] = (forcex[part] - (atn * vx[part]));
                forcey[part] = (forcey[part] - (atn * vy[part]));
                forcez[part] = (forcez[part] - (atn * vz[part]));
            }
        }
    }

    /* Loop for each particle computing the torques */
    for (part = 1; (part < (numpart - 1)); part = (part + 1); )
    {
        last = (part - 1.0);
        next = (part + 1.0);
        xmean = (((x[((last))] + x[part]) + x[((next))]) / 3.0);
        ymean = (((y[((last))] + y[part]) + y[((next))]) / 3.0);
        zmean = (((z[((last))] + z[part]) + z[((next))]) / 3.0);

        dx = (x[part] - xmean);
        dy = (y[part] - ymean);
        dz = (z[part] - zmean);

        d = sqrt( (((dx * dx) + (dy * dy)) + (dz * dz)) );
        dxb = (dx * bend);
        dyb = (dy * bend);
        dzb = (dz * bend);

        vxmean = (((vx[((last))] + vx[part]) + vx[((next))]) / 3.0);
        vymean = (((vy[((last))] + vy[part]) + vy[((next))]) / 3.0);
        vzmean = (((vz[((last))] + vz[part]) + vz[((next))]) / 3.0);

        dvx = (vx[part] - vxmean);
        dvy = (vy[part] - vymean);
        dvz = (vz[part] - vzmean);

        dxb = (dxb + (bend_damping * dvx));
        dyb = (dyb + (bend_damping * dvy));
        dzb = (dzb + (bend_damping * dvz));

        forcex[part] = (forcex[part] - dxb);
        forcey[part] = (forcey[part] - dyb);
        forcez[part] = (forcez[part] - dzb);

        dxb = (dxb * 0.5);
        dyb = (dyb * 0.5);
        dzb = (dzb * 0.5);

        forcex[((last))] = (forcex[((last))] + dxb);
        forcey[((last))] = (forcey[((last))] + dyb);
        forcez[((last))] = (forcez[((last))] + dzb);

        forcex[((next))] = (forcex[((next))] + dxb);
        forcey[((next))] = (forcey[((next))] + dyb);
        forcez[((next))] = (forcez[((next))] + dzb);
    }

    /* Loop for each particle applying the forces to the position */
    for (part = 0; (part < numpart); part = (part + 1); )
    {
        ax = (forcex[part] / mass[part]);
        ay = (-gravity + (forcey[part] / mass[part]));
        az = (forcez[part] / mass[part]);

        if (((part > 0) || (frame > 900)))
        {
            x[part] = ((x[part] + (vx[part] * dt)) + (ax * dt2));
            y[part] = ((y[part] + (vy[part] * dt)) + (ay * dt2));
            z[part] = ((z[part] + (vz[part] * dt)) + (az * dt2));

            vx[part] = (vx[part] + (ax * dt));
            vy[part] = (vy[part] + (ay * dt));
            vz[part] = (vz[part] + (az * dt));
        }

        /* Bounce off the floor and ceiling */
        if ((y[part] > ymax))
        {

            y[part] = (ymax + ((ymax - y[part]) * elasticity));
            vy[part] = (-vy[part] * elasticity);
        }

        if ((y[part] < ymin))
        {

            y[part] = (ymin + ((ymin - y[part]) * elasticity));
            vy[part] = (-vy[part] * elasticity);
        }

        if ((x[part] > xmax))
        {
            x[part] = (xmax + ((xmax - x[part]) * elasticity));
            vx[part] = (-vx[part] * elasticity);
        }

        if ((x[part] < xmin))
        {
            x[part] = (xmin + ((xmin - x[part]) * elasticity));
            vx[part] = (-vx[part] * elasticity);
        }
    }
}


/* Loop for each object outputting the geometry */
for (part = 0; (part < (numpart - 1)); part = (part + 1); )
{
    dx = (x[(((part + 1)))] - x[part]);
    dy = (y[(((part + 1)))] - y[part]);
    length = sqrt( ((dx * dx) + (dy * dy)) );
    zangle = atan2d( dy, dx );
    translate (x[part], y[part], z[part]);
    rotate    ( zaxis, zangle);
    translate ((-0.5 * radius), 0, 0);
    scale     ((radius + length), radius, 1);
    {
        translate (0.5, 0, 0);
        scale     (5, 5, 5);
        patch (
            shader = ( Shader1 ),
            doublesided = TRUE, 
            subdivide = uniform,
            divisions = (1, 1),
            udegree = 3, vdegree = 3,
            uknots = (0.0, 1.0, 2.0, 3.0, 4.0,
                     5.0),
            vknots = (0.0, 1.0, 2.0, 3.0, 4.0,
                     5.0),

            cvs = ((cv((-0.3, -0.3, 0.0), 1.0),
                    cv((-0.1, -0.3, 0.0), 1.0),
                    cv(( 0.1, -0.3, 0.0), 1.0),
                    cv(( 0.3, -0.3, 0.0), 1.0)
                ),
                (   cv((-0.3, -0.1, 0.0), 1.0),
                    cv((-0.1, -0.1, 0.0), 1.0),
                    cv(( 0.1, -0.1, 0.0), 1.0),
                    cv(( 0.3, -0.1, 0.0), 1.0)
                ),
                (   cv((-0.3, 0.1, 0.0), 1.0),
                    cv((-0.1, 0.1, 0.0), 1.0),
                    cv(( 0.1, 0.1, 0.0), 1.0),
                    cv(( 0.3, 0.1, 0.0), 1.0)
                ),
                (   cv((-0.3, 0.3, 0.0), 1.0),
                    cv((-0.1, 0.3, 0.0), 1.0),
                    cv(( 0.1, 0.3, 0.0), 1.0),
                    cv(( 0.3, 0.3, 0.0), 1.0)
                )
            )
        ); /* end patch */
    }
}
