-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRope.cs
130 lines (110 loc) · 3.89 KB
/
Rope.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Rope : MonoBehaviour
{
private LineRenderer lineRenderer;
private List<RopeSegment> ropeSegments = new List<RopeSegment>();
private float ropeSegLen = 0.25f;
public int segmentLength = 10;
private float lineWidth = 0.1f;
public float gravityForce = 0.05f;
public float ropeYPosition;
// Use this for initialization
void Start()
{
this.lineRenderer = this.GetComponent<LineRenderer>();
Vector3 ropeStartPoint = transform.TransformPoint(Vector2.down * ropeYPosition);
for (int i = 0; i < segmentLength; i++)
{
this.ropeSegments.Add(new RopeSegment(ropeStartPoint));
ropeStartPoint.y -= ropeSegLen;
}
}
// Update is called once per frame
void Update()
{
this.DrawRope();
}
private void FixedUpdate()
{
this.Simulate();
}
private void Simulate()
{
// SIMULATION
Vector2 gravity = new Vector2( gravityForce * Mathf.Sin( transform.eulerAngles.z / Mathf.Rad2Deg) , -gravityForce * Mathf.Cos( transform.eulerAngles.z / Mathf.Rad2Deg) ) ;
for (int i = 1; i < this.segmentLength; i++)
{
RopeSegment firstSegment = this.ropeSegments[i];
Vector2 velocity = firstSegment.posNow - firstSegment.posOld;
firstSegment.posOld = firstSegment.posNow;
firstSegment.posNow += velocity;
firstSegment.posNow += gravity * Time.fixedDeltaTime;
this.ropeSegments[i] = firstSegment;
}
//CONSTRAINTS
for (int i = 0; i < 50; i++)
{
this.ApplyConstraint();
}
}
private void ApplyConstraint()
{
//Constrant to Player
RopeSegment firstSegment = this.ropeSegments[0];
firstSegment.posNow = transform.TransformPoint(Vector2.down * 0.1f);
this.ropeSegments[0] = firstSegment;
for (int i = 0; i < this.segmentLength - 1; i++)
{
RopeSegment firstSeg = this.ropeSegments[i];
RopeSegment secondSeg = this.ropeSegments[i + 1];
float dist = (firstSeg.posNow - secondSeg.posNow).magnitude;
float error = Mathf.Abs(dist - this.ropeSegLen);
Vector2 changeDir = Vector2.zero;
if (dist > ropeSegLen)
{
changeDir = (firstSeg.posNow - secondSeg.posNow).normalized;
} else if (dist < ropeSegLen)
{
changeDir = (secondSeg.posNow - firstSeg.posNow).normalized;
}
Vector2 changeAmount = changeDir * error;
if (i != 0)
{
firstSeg.posNow -= changeAmount * 0.5f;
this.ropeSegments[i] = firstSeg;
secondSeg.posNow += changeAmount * 0.5f;
this.ropeSegments[i + 1] = secondSeg;
}
else
{
secondSeg.posNow += changeAmount;
this.ropeSegments[i + 1] = secondSeg;
}
}
}
private void DrawRope()
{
float lineWidth = this.lineWidth;
lineRenderer.startWidth = lineWidth;
lineRenderer.endWidth = lineWidth;
Vector3[] ropePositions = new Vector3[this.segmentLength];
for (int i = 0; i < this.segmentLength; i++)
{
ropePositions[i] = this.ropeSegments[i].posNow ;
}
lineRenderer.positionCount = ropePositions.Length;
lineRenderer.SetPositions(ropePositions);
}
public struct RopeSegment
{
public Vector2 posNow;
public Vector2 posOld;
public RopeSegment(Vector2 pos)
{
this.posNow = pos;
this.posOld = pos;
}
}
}