-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotationalPendulum.js
More file actions
109 lines (86 loc) · 1.91 KB
/
RotationalPendulum.js
File metadata and controls
109 lines (86 loc) · 1.91 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
//Jacob H 2023
//this pendulum is simulated by considering the mass at the end to be a point mass, the rod to have no mass, and using newton's second law for rotational motion
class rotationalPendulum {
static options =
{
"length (m):":
{
get: "getLength",
set: "setLength"
},
"θ (deg):":
{
get: "getAngle",
set: "setAngle"
},
"ω (deg/s)":
{
get: "getOmega",
set: "setOmega"
}
};
constructor(length, angle, base) {
this.type = 1;
this.angle = -angle;
this.length = length;
this.omega = 0;
this.base = base;
this.selectionBox = new box(0, 0, 0, 0);
this.selected = false;
}
getLength()
{
return this.length;
}
setLength(length)
{
this.length = Math.max(length, 0.1);
}
getAngle()
{
return -rad2Deg(this.angle);
}
setAngle(deg)
{
this.angle = -deg2Rad(deg);
}
getOmega()
{
return -rad2Deg(this.omega);
}
setOmega(deg)
{
this.omega = -deg2Rad(deg);
}
tick(deltaTime) {
this.angle += this.omega * deltaTime;
this.angle %= 2 * Math.PI;
let torque = -gravity * Math.sin(this.angle);//gravity is at angle 90 degrees or PI/2
let a = torque / this.length;
this.omega += a * deltaTime;
}
draw() {
ctx.strokeStyle = "white";
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;
let vec = vector2dFromAngle(this.angle + Math.PI/2, this.length);
this.selectionBox = new selectionBox(this.base.x, this.base.y, vec.x, vec.y);
vec = vec.add(this.base);
ctx.beginPath();
line(this.base, vec);
ctx.stroke();
//draw mass hanging at bottom
ctx.save();
translate(vec);
ctx.rotate(this.angle);
ctx.fillStyle = "white";
ctx.fillRect(-15, -15, 30, 30);
ctx.restore();
if (this.selected)
this.selectionBox.show();
return true;
}
}