-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
160 lines (128 loc) · 4.04 KB
/
main.js
File metadata and controls
160 lines (128 loc) · 4.04 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//Jacob 2022
//Code for user input
var runButton = document.getElementById("startStopBtn");
//drawing
var simCanvas = document.getElementById("simCanvas");
var ctx = simCanvas.getContext("2d");
//if the hand is holding a ghost block
var hand = false;
//if simulation is currently supposed to be running
var running = false;
var selectedElement = null;
function pointInBox(x, y, box)
{
return (x >= box.x && x <= box.x + box.width && y >= box.y && y <= box.y + box.height);
}
//show editing buttons and hide add object button
function showEditButtons() {
return;
addObjectBtn.hidden = true;
for (var i = 0; i < editButtons.children.length; i++) {
editButtons.children[i].hidden = false;
}
}
//hide all buttons, exluding start/stop
function hideExtraButtons() {
return;
addObjectBtn.hidden = true;
for (var i = 0; i < editButtons.children.length; i++) {
editButtons.children[i].hidden = true;
}
}
//hide edit buttons and show add object button
function hideEditButtons() {
return;
addObjectBtn.hidden = false;
for (var i = 0; i < editButtons.children.length; i++) {
editButtons.children[i].hidden = true;
}
}
hideEditButtons();
function clearSelection()
{
selectedElement = null;
hideEditButtons();
}
function localize(x, y)
{
return [(x - simCanvas.width / 2) / pixelsPerMeter, (simCanvas.height - y) / pixelsPerMeter];
}
var mouseX;
var mouseY;
simCanvas.addEventListener("mousemove", function(event)
{
[mouseX, mouseY] = localize(event.offsetX, event.offsetY);
});
simCanvas.addEventListener("mouseup", function(event) {
//ignore user input when simulation is running
if (running)
return;
var selectedObject = selectedElement == null ? null : selectedElement.value;
//localize click coords to world coords in meters
var [x, y] = localize(event.offsetX, event.offsetY);
if (selectedObject != null)
selectedObject.borderWidth = 0;
//iterate over object list and find selected object
for (var currentElement = simWorld.objects.firstElement; currentElement != null; currentElement = currentElement.next) {
var currentObject = currentElement.value;
//check if mouse is inside object box
if (pointInBox(x, y, currentObject)) {
if (currentObject == selectedObject)
{
clearSelection();
return;
}
currentObject.borderWidth = 4;
selectedElement = currentElement;
showEditButtons();
return;
}
}
clearSelection();
});
runButton.addEventListener("click", function() {
var selectedObject = selectedElement == null ? null : selectedElement.value;
//change Start/Stop button text and store whether physics should be running
if (running) {
runButton.innerText = "Start";
running = false;
//addObjectBtn.hidden = false;
}
else {
runButton.innerText = "Stop";
running = true;
if (selectedObject != null)
selectedObject.borderWidth = 0;
selectedObject = null;
hand = null;
hideExtraButtons();
}
});
var lastFrameTime = (new Date()).getTime();
function controlLoop() {
//get time since last frame
var currentTime = (new Date()).getTime();
var deltaTime = (currentTime - lastFrameTime) / 1000;
lastFrameTime = currentTime;
//make canvas fill window
simCanvas.width = document.body.clientWidth;
//clear canvas
ctx.clearRect(0, 0, simCanvas.width, simCanvas.height);
if (running)
physicsLoop(deltaTime);
simWorld.draw(ctx);
var totalM = 0;
//calculate total momentum of all objects and show on screen
for (var currentObject = simWorld.objects.firstElement; currentObject != null; currentObject = currentObject.next) {
totalM += currentObject.value.mass * currentObject.value.velocity;
}
ctx.textAlign = "left";
ctx.fillStyle = "white";
ctx.font = "20px consolas";
ctx.fillText(`total p = ${totalM.toFixed(2)} Kg*m/s (hitting walls is considered an external force)`, 0, 20);
//to calculate fps take reciprocal of seconds between frames
var fps = 1 / deltaTime;
ctx.fillText(`${fps.toFixed(2)} fps`, 0, 40);
window.requestAnimationFrame(controlLoop);
}
controlLoop();