forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysics_Sandbox.js
635 lines (558 loc) · 18.5 KB
/
Physics_Sandbox.js
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
@title: Physics Sandbox
@author: Henry Bass
@tags: ['simulation']
@addedOn: 2023-03-27
Controls:
WASD to move pointer
When in game:
I to select
L to grab
K for action
J for options
s
In options menu:
I to go up
K to go down
L to change option
J to exit
Options explained:
Action:
Select create to spawn more objects
Select delete to remove objects
Select freeze to prevent an object from moving
Select link to connect two objects
Select reset to remove all objects
Size:
Dictates how large the next object created will be
Spring F:
Changes how much force links have
Gravity:
0.001 - 0.5:
Downwards force
Waves:
Causes objects to sway along the X axis
Loops:
Causes objects to sway along the X and Y axis
Pointer Speed:
Adjusts how many pixels the pointer moves each time it's moved
Drag:
Adjusts how quickly objects loose velocity
Note:
Feel free to reuse my settings manager - it was designed with reuse in mind!
*/
const player = "p";
let swidth = 64;
let sheight = 64;
let mouseX = 0;
let mouseY = 0;
let dt = 0.001;
let t = 0;
let k = 1;
let gx = 0.1
let gy = 0
let inputSpeed = 3;
let grabbing = false;
const dgrey = "d";
const grey = "g";
const white = "w";
const red = "r";
const green = "v";
let purple = "p";
let black = "b";
let screen = map`
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................
................................................................`.replaceAll(".", "d");
let freeze = false;
let del = false;
setLegend(
[white, bitmap`
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222
2222222222222222`],
[red, bitmap`
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333
3333333333333333`],
[green, bitmap`
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD
DDDDDDDDDDDDDDDD`],
[grey, bitmap`
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111
1111111111111111`],
[black, bitmap`
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000
0000000000000000`],
[purple, bitmap`
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH
HHHHHHHHHHHHHHHH`],
[dgrey, bitmap`
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL
LLLLLLLLLLLLLLLL`],
);
setMap(screen);
function dist(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2))
}
function findatloc(x, y) {
for (let i = 0; i < entities.length; i++) {
if (dist(x, y, entities[i].x, entities[i].y) < entities[i].radius) {
return entities[i]
}
}
return "Fail"
}
function inBounds(x, y) {
return ((x > 0 && x < swidth) && (y > 0 && y < sheight));
}
class Entity {
constructor(x, y, radius) {
this.x = x;
this.y = y;
this.lx = x;
this.ly = y;
this.accx = 0;
this.accy = 0;
this.radius = radius;
this.links = [];
this.fill = "g";
this.fixed = false;
}
renderEntity() {
let pointerX = Math.max(0, this.x - this.radius);
let pointerY = Math.max(0, this.y - this.radius);
while (pointerX < Math.min(swidth, this.x + this.radius + 1)) {
while (pointerY < Math.min(sheight, this.y + this.radius + 1)) {
let objDist = dist(pointerX, pointerY, this.x, this.y)
if ((this.links.length > 0) && (objDist < this.radius) && (objDist > this.radius - 1.5)) {
addSprite(Math.floor(pointerX), Math.floor(pointerY), "p");
}
else if ((this.fixed) && (objDist < this.radius) && (objDist > this.radius - 3)) {
addSprite(Math.floor(pointerX), Math.floor(pointerY), "g");
} else if (((objDist) < this.radius) && (objDist > this.radius - 1.1)) {
addSprite(Math.floor(pointerX), Math.floor(pointerY), "b");
}else if (objDist < this.radius) {
addSprite(Math.floor(pointerX), Math.floor(pointerY), this.fill);
}
pointerY += 1;
}
pointerY = Math.max(0, this.y - this.radius);
pointerX += 1;
}
}
renderLink(linked) {
let steps = Math.floor(dist(this.x, this.y, linked.x, linked.y))
let xdif = (linked.x - this.x) / steps;
let ydif = (linked.y - this.y) / steps;
for (let i = 0; i < steps; i ++) {
let drawX = Math.floor(this.x + (xdif * i));
let drawY = Math.floor(this.y + (ydif * i));
if (inBounds(drawX, drawY)) addSprite(drawX, drawY, "p");
}
}
render() {
this.renderEntity();
if (this.links.length > 0) {
for(let i = 0; i < this.links.length; i++) {
this.renderLink(this.links[i])
}
}
}
link(other) {
if (other !== undefined) {
this.links.push(other);
other.links.push(this);
}
}
substep() {
if (this.x + this.radius > swidth) {
this.x -= (this.x + this.radius - swidth);
}
if (this.y + this.radius > sheight) {
this.y -= (this.y + this.radius - sheight);
}
if (this.x - this.radius < 0) {
this.x -= (this.x - this.radius);
}
if (this.y - this.radius < 0) {
this.y -= (this.y - this.radius);
}
for (let i = 0; i < entities.length; i++) {
if (entities[i] !== this && this.fixed == false) {
let odist = dist(this.x, this.y, entities[i].x, entities[i].y);
let overlap = odist - (this.radius + entities[i].radius);
if (overlap < 0) {
let d = this.radius + entities[i].radius - odist;
this.x += ((this.x - entities[i].x) / odist) * d * 0.5;
this.y += ((this.y - entities[i].y) / odist) * d * 0.5;
if (entities[i].fixed == false) {
entities[i].x -= ((this.x - entities[i].x) / odist) * d * 0.5;
entities[i].y -= ((this.y - entities[i].y) / odist) * d * 0.5;
}
}
}
}
}
update() {
if (this.fixed == false) {
for (let i = 0; i < this.links.length; i++) {
if (this.links[i] !== undefined) {
let other = this.links[i];
let d = dist(this.x, this.y, other.x, other.y)
if (!other.fixed) {
other.x -= (((other.x - this.x) * d) / 1000) * k;
other.y -= (((other.y - this.y) * d) / 1000) * k;
}
this.x += (((other.x - this.x) * d) / 1000) * k;
this.y += (((other.y - this.y) * d) / 1000) * k;
}
}
this.accelerate(gx, gy);
for (let i = 0; i < 4; i++) {
this.substep();
}
let velx = (this.x - this.lx) * (1 - settingsManager.getValue("Drag"));
let vely = (this.y - this.ly) * (1 - settingsManager.getValue("Drag"));
this.lx = this.x;
this.ly = this.y;
this.x += velx + this.accx;
this.y += vely + this.accy;
this.accx = 0;
this.accy = 0;
} else {
this.fill = "g";
}
}
accelerate(x, y) {
this.accx += x;
this.accy += y;
}
}
// The settings manager was designed to be reused - feel free to use this in your own projects!
class SettingsManager {
constructor() {
this.enabled = false;
this.pointerIndex = 0;
this.options = [
{name : "Action", states : ["Create", "Delete", "Freeze", "Link", "Reset"], stateIndex : 0},
{name : "Size", states : [2, 4, 6, 8, "Rand"], stateIndex : 4},
{name : "Spring F", states : [0.1, 1, 10, 50], stateIndex : 1},
{name : "Gravity", states : [0, 0.1, 1, 5, "Waves", "Loops"], stateIndex : 2},
{name : "Pointer Speed", states : [1, 2, 3, 4, 5], stateIndex : 2},
{name : "Drag", states : [0, 0.01, 0.05, 0.1, 0.5], stateIndex : 1},
]
}
render() {
clearText();
if (this.enabled) {
addText("Options", {x: 2, y: 0, color: color`3`})
for (let i = 0; i < this.options.length; i++) {
let option = this.options[i];
addText(
option.name + ": " + option.states[option.stateIndex],
{x: 2, y: 2 * (i + 1),
color: ((i == this.pointerIndex) ? color`5` : color`3`)}
)
}
} else {
addText("", { x: 2, y: 0, color: color`3`})
}
}
toggle() {
this.enabled = !this.enabled;
}
handleInput(key) {
if (key == "k") this.pointerIndex = (this.pointerIndex + 1) % (this.options.length);
if (key == "i") this.pointerIndex = (this.pointerIndex + (this.options.length - 1)) % (this.options.length);
if (key == "l")
{
let pState = this.options[this.pointerIndex]
this.options[this.pointerIndex].stateIndex = Math.abs((pState.stateIndex + 1) % (pState.states.length));
}
}
getValue(key) {
let retVal = 0
for (let i = 0; i < this.options.length; i++) {
if (this.options[i].name == key) retVal = this.options[i].states[this.options[i].stateIndex];
}
return retVal
}
}
let settingsManager = new SettingsManager();
let mousedown = false;
let selected = new Entity(16, 16, 4);
let entities = [];
entities.push(selected)
onInput("k", () => {
if (settingsManager.enabled) {
settingsManager.handleInput("k")
} else {
if (settingsManager.getValue("Action") == "Create") {
let scale = 1;
if (settingsManager.getValue("Size") == "Rand") {
scale = ((Math.random() * 6) + 2);
} else {
scale = settingsManager.getValue("Size");
}
entities.push(new Entity(mouseX, mouseY, scale));
} else if ((settingsManager.getValue("Action") == "Delete") && entities.length > 1) {
let oentry = findatloc(mouseX, mouseY);
if (oentry !== "Fail") {
for (let i = 0; i < entities.length; i++) {
entities[i].links = entities[i].links.filter(x => x !== oentry);
}
entities.splice(entities.indexOf(oentry), 1);
}
} else if ((settingsManager.getValue("Action") == "Freeze") && entities.length > 1) {
let loc = findatloc(mouseX, mouseY);
if (loc != "Fail") {
loc.fixed = !loc.fixed;
}
} else if ((settingsManager.getValue("Action") == "Reset") && entities.length > 1) {
entities = [new Entity(16, 16, 4)]
} else if ((settingsManager.getValue("Action") == "Link") && entities.length > 1) {
if (entities.length > 1) {
let oentry = findatloc(mouseX, mouseY);
if (oentry !== "Fail" && oentry !== selected && (entities.indexOf(selected) !== -1)) {
oentry.link(selected);
selected = oentry;
}
}
}
}
})
onInput("i", () => {
if (settingsManager.enabled) {
settingsManager.handleInput("i")
} else {
mousedown = true;
let loc = findatloc(mouseX, mouseY)
if (loc != "Fail") {
selected = loc;
}
}
})
onInput("j", () => {
settingsManager.toggle();
})
onInput("l", () => {
if (settingsManager.enabled) {
settingsManager.handleInput("l")
} else {
grabbing = !grabbing;
}
})
onInput("w", () => {if ((mouseY - inputSpeed) >= 0 && (mouseY - inputSpeed) < sheight) mouseY -= inputSpeed;})
onInput("a", () => {if ((mouseX - inputSpeed) >= 0 && (mouseX - inputSpeed) < sheight) mouseX -= inputSpeed;})
onInput("s", () => {if ((mouseY + inputSpeed) >= 0 && (mouseY + inputSpeed) < sheight) mouseY += inputSpeed;})
onInput("d", () => {if ((mouseX + inputSpeed) >= 0 && (mouseX + inputSpeed) < sheight) mouseX += inputSpeed;})
setInterval(() => {
setMap(screen);
settingsManager.render();
t++;
// console.log(entities);
inputSpeed = settingsManager.getValue("Pointer Speed");
// console.log(settingsManager.getValue("Pointer Speed"));
let setGrav = settingsManager.getValue("Gravity");
if (typeof setGrav == "string") {
if (setGrav == "Waves") {
gx = Math.sin(t / 50) / 3.14 / 10;
gy = 0.05;
} else if (setGrav == "Loops") {
gx = Math.sin(t / 100) / 3.14 / 10;
gy = Math.cos(t / 100) / 3.14 / 10;
}
} else {
gx = 0;
gy = setGrav / 10;
}
k = settingsManager.getValue("Spring F") / 10;
if (mousedown) {
if (findatloc(mouseX, mouseY) == selected) {
selected.x = mouseX;
selected.y = mouseY;
}
}
if (grabbing) {
selected.x = mouseX
selected.y = mouseY
selected.lx = mouseX
selected.ly = mouseY
}
for (let i = 0; i < entities.length; i++) {
let e = entities[i];
if (e == selected) {
entities[i].fill = "r";
} else if (e == findatloc(mouseX, mouseY)) {
entities[i].fill = "v";
} else {
entities[i].fill = "w";
}
entities[i].render();
if (!settingsManager.enabled) {
entities[i].update();
}
}
addSprite(mouseX, mouseY, white);
}, 10);
afterInput(() => {
mousedown = false;
})