-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpring.pde
55 lines (44 loc) · 1.09 KB
/
Spring.pde
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
// Class Spring
class Spring
{
PVector wide;
PVector fA, fB;
float k = 80;
float am = 10;
float len, Epe, elongation;
Extreme a;
Extreme b;
Spring (Extreme a_, Extreme b_, float l)
{
a = a_;
b = b_;
len = l;
wide = new PVector(b.s.x - a.s.x, b.s.y - a.s.y);
fA = new PVector();
fB = new PVector();
}
// Update spring forces in different directions
void update()
{
wide.x = b.s.x - a.s.x;
wide.y = b.s.y - a.s.y;
// Get the current elongation
elongation = wide.mag() - len;
wide.normalize();
// Addition of the forces exerted on the springs
// Elastic force - Damping force
fA.x = k* wide.x* elongation - (a.v.x - b.v.x)*am;
fA.y = k* wide.y* elongation - (a.v.y - b.v.y)*am;
// This force must be applied to both ends
a.applyForce(fA);
fB.x = -k* wide.x* elongation - (b.v.x - a.v.x)*am;
fB.y = -k* wide.y* elongation - (b.v.y - a.v.y)*am;
b.applyForce(fB);
}
void display()
{
strokeWeight(2);
stroke(0);
line(a.s.x, a.s.y, b.s.x, b.s.y);
}
}