public static SoftBody CreateRope(SoftBodyWorldInfo worldInfo, Vector3 from, Vector3 to, int res, int fixeds) { // Create nodes int r = res + 2; Vector3[] x = new Vector3[r]; float[] m = new float[r]; for (int i = 0; i < r; i++) { Vector3.Lerp(ref from, ref to, i / (float)(r - 1), out x[i]); m[i] = 1; } SoftBody psb = new SoftBody(worldInfo, r, x, m); if ((fixeds & 1) != 0) psb.SetMass(0, 0); if ((fixeds & 2) != 0) psb.SetMass(r - 1, 0); // Create links for (int i = 1; i < r; i++) { psb.AppendLink(i - 1, i); } return psb; }
public static SoftBody CreatePatch(SoftBodyWorldInfo worldInfo, Vector3 corner00, Vector3 corner10, Vector3 corner01, Vector3 corner11, int resx, int resy, int fixeds, bool gendiags) { // Create nodes if ((resx < 2) || (resy < 2)) return null; int rx = resx; int ry = resy; int tot = rx * ry; Vector3[] x = new Vector3[tot]; float[] m = new float[tot]; for (int iy = 0; iy < ry; iy++) { float ty = iy / (float)(ry - 1); Vector3 py0, py1; Vector3.Lerp(ref corner00, ref corner01, ty, out py0); Vector3.Lerp(ref corner10, ref corner11, ty, out py1); for (int ix = 0; ix < rx; ix++) { float tx = ix / (float)(rx - 1); int index = rx * iy + ix; Vector3.Lerp(ref py0, ref py1, tx, out x[index]); m[index] = 1; } } SoftBody psb = new SoftBody(worldInfo, tot, x, m); if ((fixeds & 1) != 0) psb.SetMass(0, 0); if ((fixeds & 2) != 0) psb.SetMass(rx - 1, 0); if ((fixeds & 4) != 0) psb.SetMass(rx * (ry - 1), 0); if ((fixeds & 8) != 0) psb.SetMass(rx * (ry - 1) + rx - 1, 0); // Create links and faces for (int iy = 0; iy < ry; ++iy) { for (int ix = 0; ix < rx; ++ix) { int ixy = rx * iy + ix; int ix1y = ixy + 1; int ixy1 = rx * (iy + 1) + ix; bool mdx = (ix + 1) < rx; bool mdy = (iy + 1) < ry; if (mdx) psb.AppendLink(ixy, ix1y); if (mdy) psb.AppendLink(ixy, ixy1); if (mdx && mdy) { int ix1y1 = ixy1 + 1; if (((ix + iy) & 1) != 0) { psb.AppendFace(ixy, ix1y, ix1y1); psb.AppendFace(ixy, ix1y1, ixy1); if (gendiags) { psb.AppendLink(ixy, ix1y1); } } else { psb.AppendFace(ixy1, ixy, ix1y); psb.AppendFace(ixy1, ix1y, ix1y1); if (gendiags) { psb.AppendLink(ix1y, ixy1); } } } } } return psb; }