private void reset() { //inits the vertices the particles in an even spaced grid for (int j = 0; j < gridSize; j++) { for (int i = 0; i < gridSize; i++) { float U = (i / (float)(gridSize - 1)) - 0.5f; float V = (j / (float)(gridSize - 1)) - 0.5f; int BallID = j * gridSize + i; _particles[BallID].currentPosition = new Vector3((float)clothScale * U, 8.5f, (float)clothScale * V); _particles[BallID].currentVelocity = Vector3.zero; _particles[BallID].inverseMass = 1.0f / mass; _particles[BallID].pinned = false; _particles[BallID].tension = Vector3.zero; } } float naturalLengthVec = Vector3.Distance(_particles[0].currentPosition, _particles[1].currentPosition); //Pinned the corners _particles[0].pinned = true; _particles[gridSize - 1].pinned = true; /* * _particles[gridSize * (gridSize -1)].pinned = true; * _particles[gridSize * gridSize - 1].pinned = true; * */ int currentSpring = 0; //Connecting the springs that are next to each other except the points on the edge of row for (int j = 0; j < gridSize; j++) { for (int i = 0; i < gridSize; i++) { _springs[currentSpring] = new ClothSpring(j * gridSize + i, j * gridSize + 1, naturalLengthVec, StretchStiffness); currentSpring++; } } }
private void reset() { Mesh myCloth = GetComponent <MeshFilter>().mesh; float naturalLengthVec = (_particles[0].currentPosition - _particles[1].currentPosition).magnitude; //Pinned the corners _particles[0].pinned = true; _particles[gridSize - 1].pinned = true; _particles[gridSize * (gridSize - 1)].pinned = true; _particles[gridSize * gridSize - 1].pinned = true; //Initialise the springs int currentSpring = 0; //The first (gridSize-1)*gridSize springs go from one ball to the next, excluding those on the right hand edge for (int j = 0; j < gridSize; j++) { for (int i = 0; i < gridSize - 1; i++) { _springs[currentSpring] = new ClothSpring(j * gridSize + i, j * gridSize + i + 1, naturalLengthVec, StretchStiffness); currentSpring++; } } //The next (gridSize-1)*gridSize springs go from one ball to the one below, excluding those on the bottom edge for (int j = 0; j < gridSize - 1; j++) { for (int i = 0; i < gridSize; i++) { _springs[currentSpring] = new ClothSpring(j * gridSize + i, (j + 1) * gridSize + i, naturalLengthVec, StretchStiffness); currentSpring++; } } //The next (gridSize-1)*(gridSize-1) go from a ball to the one below and right, excluding those on the bottom or right for (int j = 0; j < gridSize - 1; j++) { for (int i = 0; i < gridSize - 1; i++) { _springs[currentSpring] = new ClothSpring(j * gridSize + i, (j + 1) * gridSize + i + 1, naturalLengthVec * (float)Mathf.Sqrt(2.0f), BendStiffness); currentSpring++; } } //The next (gridSize-1)*(gridSize-1) go from a ball to the one below and left, excluding those on the bottom or right for (int j = 0; j < gridSize - 1; j++) { for (int i = 1; i < gridSize; i++) { _springs[currentSpring] = new ClothSpring(j * gridSize + i, (j + 1) * gridSize + i - 1, naturalLengthVec * (float)Mathf.Sqrt(2.0f), BendStiffness); currentSpring++; } } //The first (gridSize-2)*gridSize springs go from one ball to the next but one, excluding those on or next to the right hand edge for (int j = 0; j < gridSize; j++) { for (int i = 0; i < gridSize - 2; i++) { _springs[currentSpring] = new ClothSpring(j * gridSize + i, j * gridSize + i + 2, naturalLengthVec * 2, BendStiffness); currentSpring++; } } //The next (gridSize-2)*gridSize springs go from one ball to the next but one below, excluding those on or next to the bottom edge for (int j = 0; j < gridSize - 2; j++) { for (int i = 0; i < gridSize; i++) { _springs[currentSpring] = new ClothSpring(j * gridSize + i, (j + 2) * gridSize + i, naturalLengthVec * 2, BendStiffness); currentSpring++; } } UpdateMesh(); }
public void Reset() { constraints.Clear(); springs.Clear(); clothAvarage = Vector3.zero; gridSize = gridSizeInit; particles = new ClothParticle[gridSize, gridSize]; float x = 0; for (int i = 0; i < particles.GetLength(0); i++) { float z = 0; for (int j = 0; j < particles.GetLength(1); j++) { ClothParticle p = new ClothParticle(new Vector3(x, 0, z), particleMass, particleCollider); particles[i, j] = p; z = z + radius; clothAvarage += new Vector3(0, -(radius / (gridSize) / 2), radius / gridSize); } x = x + radius; clothAvarage += new Vector3(radius / 2, 0, 0); } for (int i = 0; i < particles.GetLength(0); i++) { for (int j = 0; j < particles.GetLength(1); j++) { ClothParticle mainParticle = particles[i, j]; ClothParticle subParticle; if (i < particles.GetLength(0) - 1) { subParticle = particles[i + 1, j]; ClothSpring s1 = new ClothSpring(mainParticle, subParticle, tenstion); springs.Add(s1); ClothConstraint c1 = new ClothConstraint(mainParticle, subParticle); constraints.Add(c1); mainParticle.constraints.Add(c1); subParticle.constraints.Add(c1); } if (j < particles.GetLength(1) - 1) { subParticle = particles[i, j + 1]; ClothSpring s2 = new ClothSpring(mainParticle, subParticle, tenstion); springs.Add(s2); ClothConstraint c2 = new ClothConstraint(mainParticle, subParticle); constraints.Add(c2); mainParticle.constraints.Add(c2); subParticle.constraints.Add(c2); } if (sheerGrid) { if (j < particles.GetLength(0) - 1 && i < particles.GetLength(1) - 1) { subParticle = particles[i + 1, j + 1]; ClothSpring s3 = new ClothSpring(mainParticle, subParticle, tenstion); springs.Add(s3); ClothConstraint c3 = new ClothConstraint(mainParticle, subParticle); constraints.Add(c3); mainParticle.constraints.Add(c3); subParticle.constraints.Add(c3); } if (j > 0 && i < particles.GetLength(1) - 1) { subParticle = particles[i + 1, j - 1]; ClothSpring s4 = new ClothSpring(mainParticle, subParticle, tenstion); springs.Add(s4); ClothConstraint c4 = new ClothConstraint(mainParticle, subParticle); constraints.Add(c4); mainParticle.constraints.Add(c4); subParticle.constraints.Add(c4); } } if (bendGrid) { if (i < particles.GetLength(0) - 2) { subParticle = particles[i + 2, j]; ClothSpring s5 = new ClothSpring(mainParticle, subParticle, tenstion); springs.Add(s5); ClothConstraint c5 = new ClothConstraint(mainParticle, subParticle); constraints.Add(c5); mainParticle.constraints.Add(c5); subParticle.constraints.Add(c5); } if (j < particles.GetLength(1) - 2) { subParticle = particles[i, j + 2]; ClothSpring s6 = new ClothSpring(mainParticle, subParticle, tenstion); springs.Add(s6); ClothConstraint c6 = new ClothConstraint(mainParticle, subParticle); constraints.Add(c6); mainParticle.constraints.Add(c6); subParticle.constraints.Add(c6); } if (j < particles.GetLength(0) - 2 && i < particles.GetLength(1) - 2) { subParticle = particles[i + 2, j + 2]; ClothSpring s7 = new ClothSpring(mainParticle, subParticle, tenstion); springs.Add(s7); ClothConstraint c7 = new ClothConstraint(mainParticle, subParticle); constraints.Add(c7); mainParticle.constraints.Add(c7); subParticle.constraints.Add(c7); } if (j > 1 && i < particles.GetLength(1) - 2) { subParticle = particles[i + 2, j - 2]; ClothSpring s8 = new ClothSpring(mainParticle, subParticle, tenstion); springs.Add(s8); ClothConstraint c8 = new ClothConstraint(mainParticle, subParticle); constraints.Add(c8); mainParticle.constraints.Add(c8); subParticle.constraints.Add(c8); } } } } particles[0, gridSize - 1].posLocked = true; particles[0, gridSize - 1].startPos = particles[0, gridSize - 1].pos; particles[gridSize - 1, gridSize - 1].posLocked = true; particles[gridSize - 1, gridSize - 1].startPos = particles[gridSize - 1, gridSize - 1].pos; selectedParticle = particles[gridSize - 1, gridSize - 1]; filter.mesh = GenerateMesh(); CreateWireframe(); }