-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
390 lines (311 loc) · 16 KB
/
index.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
<!doctype html>
<html>
<head>
<title>Projectile Motion</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
<script src="https://pixijs.download/release/pixi.min.js">
</script>
</head>
<body>
<div class="simulation">
<script type="module">
// Step 1: [Initialize framework: create the application helper and add its render target to the page]
const app = new PIXI.Application();
await app.init({ width: 0.8 * window.innerWidth, height: window.innerHeight, background: '#1099bb' })
document.querySelector('.simulation').appendChild(app.canvas);
let vw = 0.80 * (window.innerWidth / 100);
let vh = window.innerHeight / 100;
// Step 2: [Define helper functions and physics constants]
const calc_horizontal_pos = (x0, v0, t) => x0 + v0 * t
const calc_vertical_pos = (y0, v0, t, g) => y0 + v0 * t + 0.5 * g * (t * t);
const calc_horizontal_velocity = (vx, t) => vx;
const calc_vertical_velocity = (vy, t, g) => vy + g * t
// Step 3: [Define state, inputs, and stats-recording objects]
const state = {
// state is for the frequently updating information about the projectile
moving: false,
xVelocity: 0,
yVelocity: 0,
speed: 0,
flightTime: 0,
distance: 5,
pathDots: []
}
const inputs = {
// inputs are for user inputs used in the physics calculations
initYVelocity: 0,
initXVelocity: 0,
angle: 45,
gravityConstant: -9.81,
mass: 10,
initialHeight: 0,
}
const stats = {
// stats are to hold records for max / highest etc along trajectory
maxHeight: 0,
maxSpeed: 0,
netForce: 0 //positive if upwards, negative if downwards
}
// Step 4: [Init sprites and input boxes]
// Init cannon
await PIXI.Assets.load('assets/images/cannon.png');
let cannon = PIXI.Sprite.from('assets/images/cannon.png');
cannon.anchor.set(0.5)
cannon.width = 8 * vw
cannon.height = 8 * vw
cannon.x = vw * 5;
cannon.y = vh * (100 - 4.8 - inputs.initialHeight);
app.stage.addChild(cannon);
// Init cannonball
await PIXI.Assets.load('assets/images/cannonball.webp');
let projectile = PIXI.Sprite.from('assets/images/cannonball.webp');
projectile.anchor.set(0.5)
projectile.width = 5 * vw
projectile.height = 5 * vw
projectile.x = vw * 5;
projectile.y = vh * (100 - 8.5 - inputs.initialHeight);
projectile.alpha = 0;
app.stage.addChild(projectile);
// Init ground
let ground = new PIXI.Graphics()
.rect(0, vh * 98, 100 * vw, 2 * vh)
.fill(0x000000)
app.stage.addChild(ground)
// Step 5: [Define runtime functions]
const setDocElementContents = (id, value) => {
document.getElementById(id).innerText = value;
}
const formatNumber = (number) => {
return number.toLocaleString('en-US', { maximumFractionDigits: 2, minimumFractionDigits: 2 });
}
const clearPathDots = () => {
state.pathDots.forEach(dot => app.stage.removeChild(dot));
state.pathDots = [];
}
const resetSimulation = () => {
clearPathDots();
state.moving = false;
projectile.x = vw * 5;
projectile.y = vh * (100 - 8.5 - inputs.initialHeight);
projectile.alpha = 0;
stats.maxHeight = 0;
stats.maxSpeed = 0;
setDocElementContents('max-height', 0 + ' m');
setDocElementContents('max-speed', 0 + ' m/s');
setInputs(25, 45, 1, 0, -9.81);
setDocElementContents('max-height', '0.00 m');
setDocElementContents('max-speed', '0.00 m/s');
setDocElementContents('flight-time', '0.00 s');
setDocElementContents('distance', '0.00 m');
document.getElementById('height').value = 0.00;
document.getElementById('speed').value = 25.00;
document.getElementById('angle').value = 45;
document.getElementById('mass').value = 1.00;
document.getElementById('gravity').value = -9.81;
}
const setInputs = (velocity, angle, mass, height, gravity) => {
// resetSimulation()
clearPathDots()
height = height + 1 // minimum height 1
inputs.angle = angle
inputs.initYVelocity = velocity * Math.sin(angle * Math.PI / 180);
inputs.initXVelocity = velocity * Math.cos(angle * Math.PI / 180);
inputs.initialHeight = height;
inputs.mass = mass;
inputs.gravityConstant = gravity;
stats.netForce = inputs.mass * inputs.gravityConstant;
cannon.rotation = -angle * Math.PI / 180;
cannon.y = vh * (100 - 4.8 - height); //update cannon height
// Adjust force diagram
const netForce = stats.netForce; // Assuming stats.netForce is already defined
const arrow = document.getElementById('arrow');
const cannonball = document.getElementById('cannonball');
const netForceValue = document.getElementById('netForceValue');
const forceDiagramContainer = document.querySelector('.force-diagram');
if (netForce > 0) {
// Position arrow above the cannonball
arrow.style.transform = 'rotate(-180deg)';
forceDiagramContainer.insertBefore(arrow, cannonball);
} else {
forceDiagramContainer.insertBefore(cannonball, arrow);
}
// Set the net force value
netForceValue.textContent = "Net Force: " + Math.abs(netForce.toFixed(2)) + ' N';
}
// const calculateKineticEnergy = (mass, speed) => 0.5 * mass * speed * speed;
// const calculatePotentialEnergy = (mass, height, gravity) => mass * Math.abs(gravity) * height;
// const calculateTotalEnergy = (kineticEnergy, potentialEnergy) => kineticEnergy + potentialEnergy;
// Save reference to ticker function to cancel and reset when shoot is called again
let tickerFunction;
const shoot = () => {
// resetSimulation()
if (tickerFunction) {
app.ticker.remove(tickerFunction);
}
clearPathDots();
projectile.alpha = 1;
state.moving = true;
const ticksPerFrame = 20; // How frequently to update frames
let elapsed = 0.0;
tickerFunction = (ticker) => {
if (state.moving) {
state.flightTime = elapsed;
elapsed += ticker.deltaTime / ticksPerFrame;
setDocElementContents('flight-time', formatNumber(elapsed) + ' s');
// Calculate new coordinates
let newX = vw * calc_horizontal_pos(5, inputs.initXVelocity, elapsed);
let newY = vh * (100 - Math.round(calc_vertical_pos(inputs.initialHeight + 8, inputs.initYVelocity, elapsed, inputs.gravityConstant)))
let height = ((vh * 100) - newY) / ticksPerFrame;
let distance = newX - (vw * 5);
state.distance = distance / ticksPerFrame;
setDocElementContents('distance', formatNumber(distance / ticksPerFrame) + ' m');
if (height > stats.maxHeight) {
stats.maxHeight = height;
setDocElementContents('max-height', formatNumber(height) + ' m');
}
// Put tracking dot for projectile path
let dot = new PIXI.Graphics();
dot.beginFill(0xFF0000); // Red color
dot.drawCircle(0, 0, 2); // Radius of 2
dot.endFill();
dot.x = newX;
dot.y = newY;
app.stage.addChild(dot);
state.pathDots.push(dot);
// Check for out of screen and stopping conditions
if (newX <= vw * 97.5 && newX >= vw * 2.5) {
projectile.x = newX;
} else {
projectile.alpha = 0.5;
}
if (height > stats.maxHeight) {
stats.maxHeight = height;
}
if (newY < vh * 97.5 && newY >= vh * 0.5) {
projectile.y = newY;
} else {
projectile.alpha = 0.5;
}
if (newY >= vh * 97.5) {
state.moving = false;
}
const xVelocity = calc_horizontal_velocity(inputs.initXVelocity, elapsed);
const yVelocity = calc_vertical_velocity(inputs.initYVelocity, elapsed, inputs.gravityConstant);
setDocElementContents('x-velocity', formatNumber(xVelocity) + ' m/s');
setDocElementContents('y-velocity', formatNumber(yVelocity) + ' m/s');
const speed = Math.sqrt(xVelocity * xVelocity + yVelocity * yVelocity);
setDocElementContents('magnitude-velocity', formatNumber(speed) + ' m/s');
if (speed > stats.maxSpeed) {
stats.maxSpeed = speed;
setDocElementContents('max-speed', formatNumber(speed) + ' m/s');
}
// let PE = Math.round(inputs.mass * Math.abs(inputs.gravityConstant) * (height));
// let KE = Math.round(0.5 * inputs.mass * speed * speed);
// const kineticEnergy = calculateKineticEnergy(inputs.mass, speed);
// const potentialEnergy = calculatePotentialEnergy(inputs.mass, height, inputs.gravityConstant);
// const totalEnergy = calculateTotalEnergy(kineticEnergy, potentialEnergy);
// setDocElementContents('kinetic-energy', formatNumber(calculateKineticEnergy) + ' J');
// setDocElementContents('potential-energy', formatNumber(potentialEnergy) + ' J');
// setDocElementContents('total-energy', formatNumber(totalEnergy) + ' J');
state.xVelocity = xVelocity;
state.yVelocity = yVelocity;
state.speed = speed;
if (speed > stats.maxSpeed) {
stats.maxSpeed = speed;
}
} else {
app.ticker.remove(tickerFunction);
state.xVelocity = 0;
state.yVelocity = 0;
state.speed = 0;
setDocElementContents('x-velocity', 0 + ' m/s');
setDocElementContents('y-velocity', 0 + ' m/s');
setDocElementContents('magnitude-velocity', 0 + ' m/s');
}
};
app.ticker.add(tickerFunction);
};
// Step 6: [Event Listeners and defaults]
setInputs(25, 45, 1, 0, -9.81);
document.getElementById('setInputsBtn').addEventListener('click', () => {
const velocity = parseFloat(document.getElementById('speed').value);
const angle = parseFloat(document.getElementById('angle').value);
const mass = parseFloat(document.getElementById('mass').value);
const height = parseFloat(document.getElementById('height').value);
const gravity = parseFloat(document.getElementById('gravity').value);
setInputs(velocity, angle, mass, height, gravity);
})
document.getElementById('shootButton').addEventListener('click', () => {
shoot();
})
document.getElementById('reset').addEventListener('click', () => {
resetSimulation();
})
</script>
</div>
<div class="text-area">
<label for="parameters" class="bold-label">Parameters</label>
<div class="input-group">
<label for="height" style="font-weight: bold;">Initial Height (meters):</label>
<input type="number" min="0" max="90" id="height" name="height" value="0.00">
</div>
<div class="input-group">
<label for="speed" style="font-weight: bold;"> Initial speed (m/s):</label>
<input type="number" min="0" id="speed" name="speed" value="25.00">
</div>
<div class="input-group">
<label for="angle" style="font-weight: bold;">Angle (degrees):</label>
<input type="number" min="-359" max="359" id="angle" name="angle" value="45">
</div>
<div class="input-group">
<label for="mass" style="font-weight: bold;">Mass (kg):</label>
<input type="number" min="0" id="mass" name="mass" value="1.00">
</div>
<div class="input-group">
<label for="gravity" style="font-weight: bold;">Gravitational Acceleration (m/s^2):</label>
<input type="number" id="gravity" name="gravity" value="-9.810">
</div>
<button id="setInputsBtn">Set Inputs</button>
<button id="shootButton">Shoot</button>
<button id="reset">Reset</button>
<label for="calculation" class="bold-label">Calculations</label>
<div class="net-force">
<div class="force-diagram">
<img id="cannonball" src="assets/images/cannonball.webp" alt="cannonball">
<img id="arrow" src="assets/images/arrow.png" alt="arrow">
</div>
<span id="netForceValue"></span>
</div>
<div class="input-group">
<div class="stats-box">
<label for="position>" class="title-label">Position</label>
<label for="distance">Horizontal Distance:
<span id="distance">0.00 m</span>
</label>
<label for="max-height">Max Height:
<span id="max-height">0.00 m</span>
</label>
<label for="flight-time">Flight Time:
<span id="flight-time">0.00 s</span>
</div>
</div>
<div class="input-group">
<div class="stats-box">
<label for="Velocity" class="title-label">Velocity</label>
<label for="y-velocity">Y Velocity:
<span id="y-velocity">0.00 m/s</span>
</label>
<label for="x-velocity">X Velocity:
<span id="x-velocity">0.00 m/s</span>
</label>
<label for="magnitude-velocity">Speed:
<span id="magnitude-velocity">0.00 m/s</span>
</label>
<label for="max-speed">Max Speed:
<span id="max-speed">0.00 m/s</span>
</label>
</div>
</div>
</div>
</body>
</html>