void GenerateSticks() { sticks = new VerletStick[sizeY * sizeX * 2 - (sizeX + sizeX - 1)]; int stickCounter = 0; for (int i = 0, y = 0; y < sizeY; y++) { for (int x = 0; x < sizeX; x++, i++) { // for each point, we connect to the one to the right, and the one above if (x < sizeX - 1) { int p0 = x + sizeX * y; int p1 = x + 1 + sizeX * y; float length = FastMath.Distance(ref points[p0].pos, ref points[p1].pos); sticks[stickCounter++] = new VerletStick(p0, p1, length); } if (y < sizeY - 1) { int p0 = x + sizeX * y; int p1 = x + sizeX * (y + 1); float length = FastMath.Distance(ref points[p0].pos, ref points[p1].pos); sticks[stickCounter++] = new VerletStick(p0, p1, length); } } } }
void UpdateSticks() { foreach (var s in sticks) { Vector3 d = points[s.p1].pos - points[s.p0].pos; float distance = FastMath.Distance(ref points[s.p0].pos, ref points[s.p1].pos); float differenceToTargetLength = s.length - distance; Vector3 offset = d * (differenceToTargetLength / distance / 2); points[s.p0].pos -= offset; points[s.p1].pos += offset; } }