public Constraint Pin(Particle particle, Vector2 pos) { var pc = new PinConstraint(particle, pos); constraints.Add(pc); return(pc); }
public static Composite CreateBox(Vector2 position, bool pinRandomCorner = true) { Composite comp = new Composite(); Vector2 center = position; float stiffness = 0.25f; float size = 200f; Particle c1 = new Particle(center + new Vector2(-size, size)); Particle c2 = new Particle(center + new Vector2(size, size)); Particle c3 = new Particle(center + new Vector2(size, -size)); Particle c4 = new Particle(center + new Vector2(-size, -size)); Constraint e1 = new DistanceConstraint(c1, c2, stiffness); Constraint e2 = new DistanceConstraint(c2, c3, stiffness); Constraint e3 = new DistanceConstraint(c3, c4, stiffness); Constraint e4 = new DistanceConstraint(c4, c1, stiffness); Constraint e5 = new DistanceConstraint(c1, c3, stiffness); Constraint e6 = new DistanceConstraint(c2, c4, stiffness); List <Particle> p = new[] { c1, c2, c3, c4 }.ToList(); List <Constraint> c = new[] { e1, e2, e3, e4, e5, e6 }.ToList(); if (pinRandomCorner) { Constraint pin = new PinConstraint(p[Random.Range(0, p.Count())]); c.Add(pin); } comp.particles.AddRange(p); comp.constraints.AddRange(c); return(comp); }
void MakePlant(Vector2 basePosition, int joints, float baseAngle) { Particle root = new Particle(basePosition); particleIndices.Add(composite.elemNum); composite.AddSimElement(root); Particle prev = root; Particle prev2 = root; Particle current = root; PinConstraint firstPin = null; float angle = baseAngle; for (var i = 0; i <= joints; ++i) { angle += angleRange.random * Mathf.Deg2Rad; if (i == joints) { Particle leaf = new Particle(prev.pos + AngleToVector2(angle) * branchLengthRange.random); particleIndices.Add(composite.elemNum); composite.AddSimElement(leaf); SpringConstraint leafSpring = new SpringConstraint(prev, leaf, springStiffness); leafSpringIndices.Add(composite.elemNum); composite.AddSimElement(leafSpring); current = leaf; } else { Particle joint = new Particle(prev.pos + AngleToVector2(angle) * branchLengthRange.random); particleIndices.Add(composite.elemNum); composite.AddSimElement(joint); SpringConstraint spring = new SpringConstraint(prev, joint, springStiffness); branchSpringIndices.Add(composite.elemNum); composite.AddSimElement(spring); current = joint; if (i == 0) { firstPin = new PinConstraint(joint); } } if (i > 0) { AngleConstraint ac = new AngleConstraint(prev2, current, prev, angleStiffness); composite.AddSimElement(ac); } prev2 = prev; prev = current; } PinConstraint rootPin = new PinConstraint(root); composite.AddSimElement(rootPin); composite.AddSimElement(firstPin); }
Composite MakeTree(int depth, float branchLength) { composite = new Composite(); branchParticleIndices = new List <int>(); springIndices = new List <int>(); leafSpringIndices = new List <int>(); Particle root = new Particle(Vector2.zero, particleDamping); branchParticleIndices.Add(composite.elemNum); composite.AddSimElement(root); PinConstraint rootPin = new PinConstraint(root); Particle branch = MakeBranch(root, depth, branchLength, Mathf.PI * 0.5f, angleStiffness); PinConstraint branchPin = new PinConstraint(branch); composite.AddSimElement(rootPin); composite.AddSimElement(branchPin); composite.AddRenderingGroup(new SimRenderer.SimRenderingGroup(2, springIndices)); composite.AddRenderingGroup(new SimRenderer.SimRenderingGroup(0, branchParticleIndices)); composite.AddRenderingGroup(new SimRenderer.SimRenderingGroup(1, leafSpringIndices)); return(composite); }
IEnumerator VerletCo() { Debug.Break(); yield return(null); while (true) { for (int j = 0; j < verlet.stiffness; ++j) { for (int i = 0; i < composite.constraints.Count; i++) { IConstraint constraint = composite.constraints[i]; if (constraint is DistanceConstraint) { DistanceConstraint dc = constraint as DistanceConstraint; int i1 = composite.particles.IndexOf(dc.p1); int i2 = composite.particles.IndexOf(dc.p2); title.text = "Iteration:" + (j + 1) + "/" + verlet.stiffness + " Relax nodes: " + (i1 + 1) + " and " + (i2 + 1); links[i].GetComponent <Image>().color = Color.red; } else if (constraint is PinConstraint) { PinConstraint pc = constraint as PinConstraint; int index = composite.particles.IndexOf(pc.p); title.text = "Iteration:" + (j + 1) + "/" + verlet.stiffness + " Pin node: " + (index + 1); } else { title.text = ""; } constraint.Relax(); UpdateView(); UpdateLinks(); yield return(null); if (i < links.Count) { links[i].GetComponent <Image>().color = linkPrefab.GetComponent <Image>().color;; } } } for (int i = 0; i < verlet.composites[0].particles.Count; ++i) { title.text = "Move node: " + (i + 1); Particle p = verlet.composites[0].particles[i]; p.Move(verlet.friction, verlet.gravity); arrow.gameObject.SetActive(true); arrow.transform.position = p.pos; arrow.transform.rotation = Quaternion.LookRotation(Vector3.back, p.pos - p.lastPos); UpdateView(); UpdateLinks(); yield return(null); arrow.gameObject.SetActive(false); } } }
/* * Pin related */ public void MakePinMarker(PinConstraint p) { Vector3 pos = p.pos; pos.z = pinMarkerDepth; MakeMarker(ANGLE_ID, p, pos); }
/* * Methods */ public override SimElement MakeSimElement(AlignedEditableForm aef, List <SimElement> simElements) { var a = simElements[aef.uid2idxDic[_aUID]] as Particle; var pc = new PinConstraint(a, _pos); pc.OverrideUID(uid); return(pc); }
Composite MakeClothComposite(float rowSize, int row, float colSize, int col, float particleDamping, float springStiffness, Vector2 gravity, Vector2 rootPosition, Vector2 offset) { float rowCellSize = rowSize / row; float colCellSize = colSize / col; Particle[] rowParticles = new Particle[row]; var composite = new Composite(); List <int> particleIndices = new List <int>(); List <int> springIndices = new List <int>(); for (var y = 0; y < col; ++y) { if (y == 0) { for (var x = 0; x < row; ++x) { var p = new Particle(rootPosition + new Vector2(rowCellSize * x, -colCellSize * y) + offset, particleDamping); particleIndices.Add(composite.elemNum); composite.AddSimElement(p, 0); var pc = new PinConstraint(p); composite.AddSimElement(p, 10); if (x != 0) { var s = new SpringConstraint(rowParticles[x - 1], p, springStiffness); // var s = new DistanceConstraint(rowParticles[x - 1], p); springIndices.Add(composite.elemNum); composite.AddSimElement(s, 1); } rowParticles[x] = p; } } else { for (var x = 0; x < row; ++x) { var p = new Particle(rootPosition + new Vector2(rowCellSize * x, -colCellSize * y) + offset, particleDamping); particleIndices.Add(composite.elemNum); composite.AddSimElement(p, 0); var cfc = new ConstantForceConstraint(p, gravity); composite.AddSimElement(cfc, 5); var s = new SpringConstraint(rowParticles[x], p, springStiffness); // var s = new DistanceConstraint(rowParticles[x], p); springIndices.Add(composite.elemNum); composite.AddSimElement(s, 1); if (x != 0) { s = new SpringConstraint(rowParticles[x - 1], p, springStiffness); // s = new DistanceConstraint(rowParticles[x - 1], p); springIndices.Add(composite.elemNum); composite.AddSimElement(s, 1); } rowParticles[x] = p; } } } composite.AddRenderingGroup(new SimRenderer.SimRenderingGroup(1, springIndices)); composite.AddRenderingGroup(new SimRenderer.SimRenderingGroup(0, particleIndices)); return(composite); }
// Use this for initialization void Start() { var sd = GetComponent <RectTransform>().sizeDelta; for (int i = 0; i < numberOfParticles; ++i) { GameObject circle = Instantiate(circlePrefab); circle.GetComponentInChildren <Text>().text = (i + 1).ToString(); circle.GetComponent <RectTransform>().anchoredPosition = new Vector3(0, sd.y / 2 - i * sd.y / (numberOfParticles - 1), 0); circles.Add(circle); GameObject prevCircle = Instantiate(prevCirclePrefab); prevCircle.GetComponentInChildren <Text>().text = (i + 1).ToString(); prevCircle.GetComponent <RectTransform>().anchoredPosition = circle.GetComponent <RectTransform>().anchoredPosition; prevCircles.Add(prevCircle); if (i > 0) { GameObject link = Instantiate(linkPrefab); link.transform.position = circle.transform.position; links.Add(link); } } foreach (var link in links) { link.transform.SetParent(this.transform, false); } foreach (var prevCircle in prevCircles) { prevCircle.transform.SetParent(this.transform, false); } foreach (var circle in circles) { circle.transform.SetParent(this.transform, false); } composite = new Composite(); for (int i = 0; i < circles.Count; ++i) { Particle p = new Particle(circles[i].transform.position); composite.particles.Add(p); startingPositions.Add(p.pos); } for (int i = 1; i < composite.particles.Count; ++i) { DistanceConstraint dc = new DistanceConstraint(composite.particles[i - 1], composite.particles[i], 1f); composite.constraints.Add(dc); } PinConstraint pc = new PinConstraint(composite.particles[0]); composite.constraints.Add(pc); verlet.composites.Add(composite); startButton.onClick.AddListener(OnStart); }
public void DeletePinMarker(PinConstraint p) { DeleteMarker(PIN_ID, p); }