//Makes sure the first point is always located at the base public void ConstrainStartingPosition() { RopePoint point = ropePoints[0]; point.posNow = startObject.position; ropePoints[0] = point; }
//Makes sure no two points can be any more than a certain distance from each other. public void ConstrainDistance() { for (int i = 0; i < pointCount - 1; i++) { RopePoint firstPoint = ropePoints[i]; RopePoint secondPoint = ropePoints[i + 1]; float dist = (firstPoint.posNow - secondPoint.posNow).magnitude; //distance between points float error = Mathf.Abs(dist - ropeSegmentLength); //how much larger or smaller the distance between points is vs. the current segment length Vector3 changeDirection = Vector3.zero; if (dist > ropeSegmentLength) { changeDirection = (firstPoint.posNow - secondPoint.posNow).normalized; } else if (dist < ropeSegmentLength) { changeDirection = (secondPoint.posNow - firstPoint.posNow).normalized; } Vector3 changeAmount = changeDirection * error; if (i != 0) { firstPoint.posNow -= changeAmount * 0.5f; ropePoints[i] = firstPoint; secondPoint.posNow += changeAmount * 0.5f; ropePoints[i + 1] = secondPoint; } else { secondPoint.posNow += changeAmount; ropePoints[i + 1] = secondPoint; } } }
//Makes sure the last point is always located at the hook public void ConstrainEndingPosition() { RopePoint point = ropePoints[pointCount - 1]; point.posNow = endObject.position; ropePoints[pointCount - 1] = point; }
private void Simulate() { for (int i = 1; i < this.numRopePoints; i++) { RopePoint point = ropePoints[i]; float displacement = decay * (float)Math.Sin(Math.PI * i / numRopePoints); point.posNow = point.posOrigin + displacement * this.orthonormal_vector * (float)Math.Cos(60 * Time.time); this.ropePoints[i] = point; this.decay *= this.decay_rate; } }
private void SimulateRope() { for (int i = 0; i < pointCount; i++) { RopePoint point = ropePoints[i]; Vector3 velocity = (point.posNow - point.posOld); //calculates velocity by subtracting old position from current position point.posOld = point.posNow; point.posNow += velocity; point.posNow += Physics.gravity * Time.deltaTime; ropePoints[i] = point; } for (int i = 0; i < 50; i++) { AddConstraints(); } }