void InitFromShape(MegaShape shape) { float len = shape.splines[0].length; // adjust by and pre stretch //Debug.Log("Length " + len + "m"); float volume = len * 2.0f * Mathf.PI * radius; //Debug.Log("Volume " + volume + "m3"); float totalmass = Density * volume; // Option for fill or set count, or count per unit len *= 0.75f; int nummasses = (int)(len / radius) + 1; //Debug.Log("Num Masses " + nummasses); float m = totalmass / (float)nummasses; if (DampingRatio > 1.0f) { DampingRatio = 1.0f; } damp = (DampingRatio * 0.45f) * (2.0f * Mathf.Sqrt(m * spring)); // The Max spring rate is based on m //float dmpratio = damp / (2.0f * Mathf.Sqrt(m * spring)); //Debug.Log("Current Damp Ratio " + dmpratio); //float dmp = DampingRatio * (2.0f * Mathf.Sqrt(m * spring)); //Debug.Log("TotalMass " + totalmass + "kg element mass " + m + "kg damp " + damp); // Mmm or should me move along iters by radius * 2 RopeLength = 0.0f; if (masses == null) { masses = new List <MegaRopeMass>(); } transform.position = Vector3.zero; masses.Clear(); //float ms = Mass / (float)(points + 1); float rlen = 0.0f; Vector3 lastpos = Vector3.zero; for (int i = 0; i <= nummasses; i++) { float alpha = (float)i / (float)nummasses; //points; //Vector3 pos = shape.transform.localToWorldMatrix.MultiplyPoint(shape.InterpCurve3D(0, alpha, true)); Vector3 pos = shape.transform.TransformPoint(shape.InterpCurve3D(0, alpha, true)); if (i != 0) { rlen += Vector3.Distance(lastpos, pos); lastpos = pos; } MegaRopeMass rm = new MegaRopeMass(m, pos); masses.Add(rm); } if (springs == null) { springs = new List <MegaRopeSpring>(); } springs.Clear(); if (constraints == null) { constraints = new List <MegaRopeConstraint>(); } constraints.Clear(); for (int i = 0; i < masses.Count - 1; i++) { MegaRopeSpring spr = new MegaRopeSpring(i, i + 1, spring, damp, this); springs.Add(spr); //spr.restlen = (rlen / masses.Count); // * 1.1f; RopeLength += spr.restlen; LengthConstraint lcon = new LengthConstraint(i, i + 1, spr.restlen); constraints.Add(lcon); } if (stiffsprings) { int gap = 2; for (int i = 0; i < masses.Count - gap; i++) { float alpha = (float)i / (float)masses.Count; // BUG: For a curve shape, len for stuff springs should be sum of springs we span MegaRopeSpring spr = new MegaRopeSpring(i, i + gap, stiffspring * stiffnessCrv.Evaluate(alpha), stiffdamp * stiffnessCrv.Evaluate(alpha), this); //spr.restlen = (springs[i].restlen + springs[i + 1].restlen); // * 1.1f; //spr.restlen = (RopeLength / masses.Count) * 2.0f; //(springs[i].restlen + springs[i + gap].restlen) * 1.0f; // TODO: Add these for rope, not needed for chain springs.Add(spr); LengthConstraint lcon = new LengthConstraint(i, i + gap, spr.restlen); //(RopeLength / masses.Count) * 2.0f); //spr.restlen); constraints.Add(lcon); } } if (top) { top.position = masses[0].pos; float ln = (masses[0].pos - masses[1].pos).magnitude; NewPointConstraint pconn = new NewPointConstraint(0, 1, ln, top.transform); //PointConstraint pconn = new PointConstraint(0, top.transform); constraints.Add(pconn); } if (bottom) { bottom.position = masses[masses.Count - 1].pos; float ln = (masses[masses.Count - 1].pos - masses[masses.Count - 2].pos).magnitude; NewPointConstraint pconn = new NewPointConstraint(masses.Count - 1, masses.Count - 2, ln, bottom.transform); //PointConstraint pconn = new PointConstraint(masses.Count - 1, bottom.transform); constraints.Add(pconn); } // Apply fixed end constraints //PointConstraint pcon = new PointConstraint(0, top.transform); //constraints.Add(pcon); //pcon = new PointConstraint(masses.Count - 1, bottom.transform); //constraints.Add(pcon); //float ln = (masses[0].pos - masses[1].pos).magnitude; //NewPointConstraint pconn = new NewPointConstraint(0, 1, ln, top.transform); //constraints.Add(pconn); //ln = (masses[masses.Count - 1].pos - masses[masses.Count - 2].pos).magnitude; //pconn = new NewPointConstraint(masses.Count - 1, masses.Count - 2, ln, bottom.transform); //constraints.Add(pconn); //ln = (masses[masses.Count - 1].pos - masses[masses.Count - 2].pos).magnitude; //NewPointConstraint pc = new NewPointConstraint(masses.Count / 2, (masses.Count / 2) + 1, ln, middle.transform); //constraints.Add(pc); if (top) { PointConstraint1 pcon1 = new PointConstraint1(); pcon1.p1 = 1; pcon1.off = new Vector3(0.0f, springs[0].restlen, 0.0f); pcon1.obj = top.transform; //.position; //constraints.Add(pcon1); //endcon = pcon1; } masspos = new Vector3[masses.Count + 2]; for (int i = 0; i < masses.Count; i++) { masspos[i + 1] = masses[i].pos; } masspos[0] = masspos[1]; masspos[masspos.Length - 1] = masspos[masspos.Length - 2]; }
void InitFromShape(MegaShape shape) { float len = shape.splines[0].length; // adjust by and pre stretch //Debug.Log("Length " + len + "m"); float volume = len * 2.0f * Mathf.PI * radius; //Debug.Log("Volume " + volume + "m3"); float totalmass = Density * volume; // Option for fill or set count, or count per unit len *= 0.75f; int nummasses = (int)(len / radius) + 1; //Debug.Log("Num Masses " + nummasses); float m = totalmass / (float)nummasses; if ( DampingRatio > 1.0f ) DampingRatio = 1.0f; damp = (DampingRatio * 0.45f) * (2.0f * Mathf.Sqrt(m * spring)); // The Max spring rate is based on m //float dmpratio = damp / (2.0f * Mathf.Sqrt(m * spring)); //Debug.Log("Current Damp Ratio " + dmpratio); //float dmp = DampingRatio * (2.0f * Mathf.Sqrt(m * spring)); //Debug.Log("TotalMass " + totalmass + "kg element mass " + m + "kg damp " + damp); // Mmm or should me move along iters by radius * 2 RopeLength = 0.0f; if ( masses == null ) masses = new List<MegaRopeMass>(); transform.position = Vector3.zero; masses.Clear(); //float ms = Mass / (float)(points + 1); float rlen = 0.0f; Vector3 lastpos = Vector3.zero; for ( int i = 0; i <= nummasses; i++ ) { float alpha = (float)i / (float)nummasses; //points; //Vector3 pos = shape.transform.localToWorldMatrix.MultiplyPoint(shape.InterpCurve3D(0, alpha, true)); Vector3 pos = shape.transform.TransformPoint(shape.InterpCurve3D(0, alpha, true)); if ( i != 0 ) { rlen += Vector3.Distance(lastpos, pos); lastpos = pos; } MegaRopeMass rm = new MegaRopeMass(m, pos); masses.Add(rm); } if ( springs == null ) springs = new List<MegaRopeSpring>(); springs.Clear(); if ( constraints == null ) constraints = new List<MegaRopeConstraint>(); constraints.Clear(); for ( int i = 0; i < masses.Count - 1; i++ ) { MegaRopeSpring spr = new MegaRopeSpring(i, i + 1, spring, damp, this); springs.Add(spr); //spr.restlen = (rlen / masses.Count); // * 1.1f; RopeLength += spr.restlen; LengthConstraint lcon = new LengthConstraint(i, i + 1, spr.restlen); constraints.Add(lcon); } if ( stiffsprings ) { int gap = 2; for ( int i = 0; i < masses.Count - gap; i++ ) { float alpha = (float)i / (float)masses.Count; // BUG: For a curve shape, len for stuff springs should be sum of springs we span MegaRopeSpring spr = new MegaRopeSpring(i, i + gap, stiffspring * stiffnessCrv.Evaluate(alpha), stiffdamp * stiffnessCrv.Evaluate(alpha), this); //spr.restlen = (springs[i].restlen + springs[i + 1].restlen); // * 1.1f; //spr.restlen = (RopeLength / masses.Count) * 2.0f; //(springs[i].restlen + springs[i + gap].restlen) * 1.0f; // TODO: Add these for rope, not needed for chain springs.Add(spr); LengthConstraint lcon = new LengthConstraint(i, i + gap, spr.restlen); //(RopeLength / masses.Count) * 2.0f); //spr.restlen); constraints.Add(lcon); } } if ( top ) { top.position = masses[0].pos; float ln = (masses[0].pos - masses[1].pos).magnitude; NewPointConstraint pconn = new NewPointConstraint(0, 1, ln, top.transform); //PointConstraint pconn = new PointConstraint(0, top.transform); constraints.Add(pconn); } if ( bottom ) { bottom.position = masses[masses.Count - 1].pos; float ln = (masses[masses.Count - 1].pos - masses[masses.Count - 2].pos).magnitude; NewPointConstraint pconn = new NewPointConstraint(masses.Count - 1, masses.Count - 2, ln, bottom.transform); //PointConstraint pconn = new PointConstraint(masses.Count - 1, bottom.transform); constraints.Add(pconn); } // Apply fixed end constraints //PointConstraint pcon = new PointConstraint(0, top.transform); //constraints.Add(pcon); //pcon = new PointConstraint(masses.Count - 1, bottom.transform); //constraints.Add(pcon); //float ln = (masses[0].pos - masses[1].pos).magnitude; //NewPointConstraint pconn = new NewPointConstraint(0, 1, ln, top.transform); //constraints.Add(pconn); //ln = (masses[masses.Count - 1].pos - masses[masses.Count - 2].pos).magnitude; //pconn = new NewPointConstraint(masses.Count - 1, masses.Count - 2, ln, bottom.transform); //constraints.Add(pconn); //ln = (masses[masses.Count - 1].pos - masses[masses.Count - 2].pos).magnitude; //NewPointConstraint pc = new NewPointConstraint(masses.Count / 2, (masses.Count / 2) + 1, ln, middle.transform); //constraints.Add(pc); if ( top ) { PointConstraint1 pcon1 = new PointConstraint1(); pcon1.p1 = 1; pcon1.off = new Vector3(0.0f, springs[0].restlen, 0.0f); pcon1.obj = top.transform; //.position; //constraints.Add(pcon1); //endcon = pcon1; } masspos = new Vector3[masses.Count + 2]; for ( int i = 0; i < masses.Count; i++ ) masspos[i + 1] = masses[i].pos; masspos[0] = masspos[1]; masspos[masspos.Length - 1] = masspos[masspos.Length - 2]; }
public void Init() { if (startShape != null) { InitFromShape(startShape); } else { if (top == null || bottom == null) { return; } Vector3 p1 = top.position; Vector3 p2 = bottom.position; RopeLength = (p1 - p2).magnitude; if (masses == null) { masses = new List <MegaRopeMass>(); } transform.position = Vector3.zero; masses.Clear(); float ms = Mass / (float)(points + 1); for (int i = 0; i <= points; i++) { float alpha = (float)i / (float)points; MegaRopeMass rm = new MegaRopeMass(ms, Vector3.Lerp(p1, p2, alpha)); masses.Add(rm); } if (springs == null) { springs = new List <MegaRopeSpring>(); } springs.Clear(); if (constraints == null) { constraints = new List <MegaRopeConstraint>(); } constraints.Clear(); for (int i = 0; i < masses.Count - 1; i++) { MegaRopeSpring spr = new MegaRopeSpring(i, i + 1, spring, damp, this); springs.Add(spr); LengthConstraint lcon = new LengthConstraint(i, i + 1, spr.restlen); constraints.Add(lcon); } int gap = 2; for (int i = 0; i < masses.Count - gap; i++) { float alpha = (float)i / (float)masses.Count; MegaRopeSpring spr = new MegaRopeSpring(i, i + gap, stiffspring * stiffnessCrv.Evaluate(alpha), stiffdamp * stiffnessCrv.Evaluate(alpha), this); springs.Add(spr); LengthConstraint lcon = new LengthConstraint(i, i + gap, spr.restlen); constraints.Add(lcon); } // Apply fixed end constraints PointConstraint pcon = new PointConstraint(0, top.transform); constraints.Add(pcon); pcon = new PointConstraint(masses.Count - 1, bottom.transform); constraints.Add(pcon); PointConstraint1 pcon1 = new PointConstraint1(); pcon1.p1 = 1; pcon1.off = new Vector3(0.0f, springs[0].restlen, 0.0f); pcon1.obj = top.transform; //.position; constraints.Add(pcon1); endcon = pcon1; masspos = new Vector3[masses.Count + 2]; for (int i = 0; i < masses.Count; i++) { masspos[i + 1] = masses[i].pos; } masspos[0] = masspos[1]; masspos[masspos.Length - 1] = masspos[masspos.Length - 2]; } }
public void Init() { if ( startShape != null ) InitFromShape(startShape); else { if ( top == null || bottom == null ) return; Vector3 p1 = top.position; Vector3 p2 = bottom.position; RopeLength = (p1 - p2).magnitude; if ( masses == null ) masses = new List<MegaRopeMass>(); transform.position = Vector3.zero; masses.Clear(); float ms = Mass / (float)(points + 1); for ( int i = 0; i <= points; i++ ) { float alpha = (float)i / (float)points; MegaRopeMass rm = new MegaRopeMass(ms, Vector3.Lerp(p1, p2, alpha)); masses.Add(rm); } if ( springs == null ) springs = new List<MegaRopeSpring>(); springs.Clear(); if ( constraints == null ) constraints = new List<MegaRopeConstraint>(); constraints.Clear(); for ( int i = 0; i < masses.Count - 1; i++ ) { MegaRopeSpring spr = new MegaRopeSpring(i, i + 1, spring, damp, this); springs.Add(spr); LengthConstraint lcon = new LengthConstraint(i, i + 1, spr.restlen); constraints.Add(lcon); } int gap = 2; for ( int i = 0; i < masses.Count - gap; i++ ) { float alpha = (float)i / (float)masses.Count; MegaRopeSpring spr = new MegaRopeSpring(i, i + gap, stiffspring * stiffnessCrv.Evaluate(alpha), stiffdamp * stiffnessCrv.Evaluate(alpha), this); springs.Add(spr); LengthConstraint lcon = new LengthConstraint(i, i + gap, spr.restlen); constraints.Add(lcon); } // Apply fixed end constraints PointConstraint pcon = new PointConstraint(0, top.transform); constraints.Add(pcon); pcon = new PointConstraint(masses.Count - 1, bottom.transform); constraints.Add(pcon); PointConstraint1 pcon1 = new PointConstraint1(); pcon1.p1 = 1; pcon1.off = new Vector3(0.0f, springs[0].restlen, 0.0f); pcon1.obj = top.transform; //.position; constraints.Add(pcon1); endcon = pcon1; masspos = new Vector3[masses.Count + 2]; for ( int i = 0; i < masses.Count; i++ ) masspos[i + 1] = masses[i].pos; masspos[0] = masspos[1]; masspos[masspos.Length - 1] = masspos[masspos.Length - 2]; } }