Game Development Reference
In-Depth Information
int getNeighbors( int vertex, int
neighborList)
{
ee = vertices[vertex].edge;
neighborList[0] = vertex;
neighborList[1] = edges[ee].vertex;
n=2;
e = edges[ee].prev; e = edges[e].neighbor;
while (e != ee) {
neighborList[n] = edges[e].vertex;
n++;
e = edges[e].prev; e = edges[e].neighbor;
return n;
}
Of course, we have to deal with the case where one of these references does
not exist—for example, if the triangle has no neighboring triangle. Those refer-
ences can be set to
1, and the implementation has to handle them with some
care.
14.3.1 Viscous Damping
The neighborhood information can be used to construct more sophisticated forces.
In a viscous material, two particles in the vicinity of each other feel a pairwise in-
fluence if they have a nonzero relative velocity. We use this fact to formulate
another form of damping. A vertex should experience resistance when its ve-
locity differs from its neighbors. We sum over all our neighbors and add up the
velocity differences between the vertices. In the end, we divide by the amount of
neighbors:
1
f neigh
i
=
v j
v i ,
h
|
nbr( i )
|
j∈ nbr( i )
or in pseudocode,
for each vertex v in v i.neighbors {
sumVel += v.vel;
averageVel = sumVel / v i.numNeighbors;
neighborForce = (averageVel v i.vel) / timeStep;
This damping model combined with a bit of global damping should be enough to
keep the system stable.