public override DCGConstraint NearestConstraint(Vector3 pos) { float nd2 = Mathf.Infinity; float nx = 0; int ni = 0; for (int i = 0; i < points.Count - 1; ++i) { Vector3 dir = points[i + 1].position - points[i].position; Vector3 diff = pos - points[i].position; Vector3 x = Vector3.Project(diff, dir); float dist2 = (diff - x).sqrMagnitude; if (dist2 < nd2) { nd2 = dist2; nx = x.magnitude / dir.magnitude; ni = i; } } DCGConstraint con = new DCGConstraint(); con.constrainerID = elementID; con.constraintData = new float[] { ni, nx }; return(con); }
//Constraints are index of subtriangle, and proportions of axes (b-a) and (c-a) public override DCGConstraint NearestConstraint(Vector3 pos) { float nz = Mathf.Infinity; float ni = 0; float nx = 0; float ny = 0; Vector3 nPos = Vector3.zero; for (int i = 0; i < subTriangles.Count; i += 3) { Vector3 a = subTriangles[i]; //Fetch vertices Vector3 b = subTriangles[i + 1]; Vector3 c = subTriangles[i + 2]; Vector3 diff = pos - a; //position of pos relative to this triangle Vector3 tx = (b - a); //X vector of this triangle's subspace Vector3 ty = (c - a); //Y vector of this triangle's subspace Vector3 tz = Vector3.Cross(tx, ty).normalized; //Normal of this triangle float Z = Vector3.Dot(diff, tz); float pX = Vector3.Dot(diff, tx) / tx.sqrMagnitude; float pY = Vector3.Dot(diff, ty) / ty.sqrMagnitude; if (Z < nz && pX > 0 && pY > 0 && pX + pY <= 1) { nz = Z; nPos = a + tx * pX + ty * pY; nx = pX; ny = pY; ni = i; } } DCGConstraint con = new DCGConstraint(); con.constrainerID = elementID; con.constraintData = new float[] { ni, nx, ny }; return(con); }
public void AddConstraint(DCGConstraint con) { //TODO: Network it constraints.Add(con); }