-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSHMPendulum.js
More file actions
139 lines (119 loc) · 2.47 KB
/
SHMPendulum.js
File metadata and controls
139 lines (119 loc) · 2.47 KB
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
131
132
133
134
135
136
137
138
139
class SHMPendulum
{
static options =
{
"length (m):":
{
get: "getLength",
set: "setLength"
},
"amplitude (m):":
{
get: "getAmplitude",
set: "setAmplitude"
},
"period (s):":
{
get: "getPeriod",
set: "setPeriod"
},
"time (s)":
{
get: "getTime",
set: "setTime"
}
};
constructor(length, amplitude, base)
{
this.type = 2;
this.length = length;
this.period = Math.sqrt(length/gravity);
this.inversePeriod = 1/this.period;
this.period *= Math.PI * 2;
this.A = amplitude;
this.t = 0;
this.base = base;
this.x = this.A;
this.selectionBox = new box(0, 0, 0, 0);
this.selected = false;
}
getTime()
{
return this.t;
}
setTime(s)
{
this.t = Math.max(s, 0);
}
getPeriod()
{
return this.period;
}
setPeriod(newT)
{
if (newT < 0.1) return;
const newLen = Math.pow(newT/(Math.PI * 2), 2) * gravity;
if (newLen < this.A) return;
this.length = newLen;
this.period = Math.sqrt(this.length/gravity);
this.inversePeriod = 1/this.period;
this.period *= Math.PI * 2;
this.x = this.A;
this.t = 0;
}
getLength()
{
return this.length;
}
setLength(length)
{
this.length = Math.max(length, 0.1);
this.A = Math.min(this.length, this.A);
this.x = this.A;
}
getAmplitude()
{
return this.A;
}
setAmplitude(a)
{
this.A = Math.min(a, this.length);
}
tick(deltaTime)
{
this.t += deltaTime;
this.period = Math.sqrt(this.length/gravity);
this.x = this.A * Math.cos(this.t/this.period);
this.period *= 2 * Math.PI;
}
draw()
{
ctx.strokeStyle = "#FF7276";
ctx.lineWidth = 5;
ctx.lineCap = "round";
//determine if you even need to draw
let hitBox = new box(this.base.x - this.length, this.base.y - this.length, 2 * this.length, 2 * this.length);
if (!boxInCamera(hitBox)) return false;
const yOff = Math.sqrt(this.length * this.length - this.x * this.x);
this.selectionBox = new selectionBox(this.base.x, this.base.y, this.x, yOff);
const end = new vector2d(this.x, yOff).add(this.base);
ctx.beginPath();
line(this.base, end);
ctx.stroke();
//calculate angle hanging mass needs to be drawn at
let angle = Math.atan2(yOff, this.x);
//draw mass hanging at bottom
ctx.save();
translate(end);
ctx.rotate(angle);
ctx.fillStyle = "#FF7276";
ctx.fillRect(-15, -15, 30, 30);
ctx.restore();
if (this.selected) this.selectionBox.show();
return true;
}
}
function SHMFromAngle(length, angle, base)
{
return new SHMPendulum(length, length * Math.sin(angle), base);
}