Game Development Reference
In-Depth Information
{
Objects[i].vSprings.x = 0;
Objects[i].vSprings.y = 0;
Objects[i].vSprings.z = 0;
}
// Calculate all spring forces based on positions of connected objects.
for(i=0; i<_NUM_SPRINGS; i++)
{
j = Springs[i].End1;
pt1 = Objects[j].vPosition;
v1 = Objects[j].vVelocity;
j = Springs[i].End2;
pt2 = Objects[j].vPosition;
v2 = Objects[j].vVelocity;
vr = v2 - v1;
r = pt2 - pt1;
dl = r.Magnitude() - Springs[i].InitialLength;
f = Springs[i].k * dl; // - means compression, + means tension
r.Normalize();
F = (r*f) + (Springs[i].d*(vr*r))*r;
j = Springs[i].End1;
Objects[j].vSprings += F;
j = Springs[i].End2;
Objects[j].vSprings -= F;
}
.
.
.
// Integrate equations of motion as usual.
.
.
.
// Render the scene as usual.
.
.
.
}
As you can see, there are several local variables here. We'll explain each one as we get
to the code where it's used. After the local variable declarations, this function's first task
is to reset the aggregate spring forces on each particle to 0. Each particle stores the
aggregate spring force in the property vSprings , which is a vector. In this example, each
particle will have up to two springs acting on it at any given time.