-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spring.m
60 lines (47 loc) · 1.5 KB
/
Spring.m
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
classdef Spring
%SPRING that can be connected to masses with dynamics
properties (Constant)
a = 0.1 %breathing altitude
k = 5000 %spring constant
end
properties
vertex1 % instance vertex position 1
vertex2 % instance vertex position 2
L % instance rest length
L0 % rest length with no breath
m1index % index of connected mass 1
m2index % index of connected mass 2
index
b
end
methods
function obj = Spring(Mass1, Mass2, b)
%SPRING Construct an instance of this class
obj.m1index = Mass1.index;
obj.m2index = Mass2.index;
obj.vertex1 = Mass1.P;
obj.vertex2 = Mass2.P;
obj.L = norm(Mass1.P-Mass2.P);
obj.L0 = norm(Mass1.P-Mass2.P);
obj.b = b * 2;
end
function obj = updateVertex(obj, Mass1, Mass2)
obj.vertex1 = Mass1.P;
obj.vertex2 = Mass2.P;
end
function obj = updateL(obj, T)
obj.L = obj.L0 + obj.a * sin(obj.b * T);
end
function F = sprF(obj,whichMass)
F1 = obj.k * (norm(obj.vertex1 - obj.vertex2) - obj.L) * ...
(obj.vertex2 - obj.vertex1) / norm(obj.vertex2 - obj.vertex1);
F2 = -F1;
if whichMass == 1
F = F1;
end
if whichMass == 2
F = F2;
end
end
end
end