Example #1
0
//----------------------------------------------------------------------------
void Rope::CreateSprings ()
{
    const int numParticles = 8;
    const float step = 0.1f;
    Vector3f gravity(0.0f, 0.0f, -1.0f);
    Vector3f wind(0.0f, -0.25f, 0.0f);
    float windChangeAmplitude = 0.01f;
    float viscosity = 10.0f;
    mModule = new0 PhysicsModule(numParticles, step, gravity, wind,
        windChangeAmplitude, viscosity);

    // Constant mass at interior points (endpoints are immovable).
    mModule->SetMass(0, Mathf::MAX_REAL);
    mModule->SetMass(numParticles - 1, Mathf::MAX_REAL);
    int i;
    for (i = 1; i < numParticles - 1; ++i)
    {
        mModule->SetMass(i, 1.0f);
    }

    // Initial position on a horizontal line segment.
    float factor = 1.0f/(float)(numParticles - 1);
    for (i = 0; i < numParticles; ++i)
    {
        mModule->Position(i) = Vector3f(i*factor, 0.0f, 1.0f);
    }

    // Initial velocities are all zero.
    for (i = 0; i < numParticles; ++i)
    {
        mModule->Velocity(i) = Vector3f::ZERO;
    }

    // Springs are at rest in the initial horizontal configuration.
    int numSprings = numParticles - 1;
    float restLength = 1.0f/(float)numSprings;
    for (i = 0; i < numSprings; ++i)
    {
        mModule->Constant(i) = 10.0f;
        mModule->Length(i) = restLength;
    }
}
//----------------------------------------------------------------------------
void GelatinCube::CreateSprings ()
{
    // The inner 4-by-4-by-4 particles are used as the control points of a
    // B-spline volume.  The outer layer of particles are immovable to
    // prevent the cuboid from collapsing into itself.
    int numSlices = 6;
    int numRows = 6;
    int numCols = 6;

    // Viscous forces applied.  If you set viscosity to zero, the cuboid
    // wiggles indefinitely since there is no dissipation of energy.  If
    // the viscosity is set to a positive value, the oscillations eventually
    // stop.  The length of time to steady state is inversely proportional
    // to the viscosity.
#ifdef _DEBUG
    float step = 0.1f;
#else
    float step = 0.01f;  // simulation needs to run slower in release mode
#endif
    float viscosity = 0.01f;
    mModule = new0 PhysicsModule(numSlices, numRows, numCols, step,
        viscosity);

    // The initial cuboid is axis-aligned.  The outer shell is immovable.
    // All other masses are constant.
    float sFactor = 1.0f/(float)(numSlices - 1);
    float rFactor = 1.0f/(float)(numRows - 1);
    float cFactor = 1.0f/(float)(numCols - 1);
    int s, r, c;
    for (s = 0; s < numSlices; ++s)
    {
        for (r = 0; r < numRows; ++r)
        {
            for (c = 0; c < numCols; ++c)
            {
                mModule->Position(s, r, c) =
                    Vector3f(c*cFactor, r*rFactor, s*sFactor);

                if (1 <= s && s < numSlices - 1
                &&  1 <= r && r < numRows - 1
                &&  1 <= c && c < numCols - 1)
                {
                    mModule->SetMass(s, r, c, 1.0f);
                    mModule->Velocity(s, r, c) = 0.1f*Vector3f(
                        Mathf::SymmetricRandom(), Mathf::SymmetricRandom(),
                        Mathf::SymmetricRandom());
                }
                else
                {
                    mModule->SetMass(s, r, c, Mathf::MAX_REAL);
                    mModule->Velocity(s, r, c) = Vector3f::ZERO;
                }
            }
        }
    }

    // Springs are at rest in the initial configuration.
    const float constant = 10.0f;
    Vector3f diff;

    for (s = 0; s < numSlices - 1; ++s)
    {
        for (r = 0; r < numRows; ++r)
        {
            for (c = 0; c < numCols; ++c)
            {
                mModule->ConstantS(s, r, c) = constant;
                diff = mModule->Position(s + 1, r, c) -
                    mModule->Position(s, r, c);
                mModule->LengthS(s, r, c) = diff.Length();
            }
        }
    }

    for (s = 0; s < numSlices; ++s)
    {
        for (r = 0; r < numRows - 1; ++r)
        {
            for (c = 0; c < numCols; ++c)
            {
                mModule->ConstantR(s, r, c) = constant;
                diff = mModule->Position(s, r + 1, c) -
                    mModule->Position(s, r, c);
                mModule->LengthR(s, r, c) = diff.Length();
            }
        }
    }

    for (s = 0; s < numSlices; ++s)
    {
        for (r = 0; r < numRows; ++r)
        {
            for (c = 0; c < numCols - 1; ++c)
            {
                mModule->ConstantC(s, r, c) = constant;
                diff = mModule->Position(s, r, c + 1) -
                    mModule->Position(s, r, c);
                mModule->LengthC(s, r, c) = diff.Length();
            }
        }
    }
}