-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathphysics.js
126 lines (112 loc) · 2.85 KB
/
physics.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
// @ts-check
kaplay();
loadSprite("bean", "sprites/bean.png");
loadSprite("bag", "sprites/bag.png");
setGravity(300);
const trajectoryText = add([
pos(20, 20),
text(`0`),
]);
function ballistics(pos, vel, t) {
return pos.add(vel.scale(t)).add(
vec2(0, 1).scale(getGravity() * t * t * 0.5),
);
}
let y;
onDraw(() => {
drawCurve(t => ballistics(vec2(50, 100), vec2(200, -100), t * 2), {
width: 2,
color: RED,
});
});
onClick(() => {
const startTime = time();
let results = [];
const bean = add([
sprite("bean"),
anchor("center"),
pos(50, 100),
body(),
offscreen({ destroy: true }),
{
draw() {
drawLine({
p1: vec2(-40, 0),
p2: vec2(40, 0),
width: 2,
color: GREEN,
});
drawLine({
p1: vec2(0, -40),
p2: vec2(0, 40),
width: 2,
color: GREEN,
});
},
update() {
const t = time() - startTime;
if (t >= 2) return;
results.push([
t,
this.pos.y,
ballistics(vec2(50, 100), vec2(200, -100), t).y,
]);
},
destroy() {
const a = results.map(d =>
Math.sqrt((d[1] - d[2]) * (d[1] - d[2]))
).reduce((s, v) => s + v, 0) / results.length;
trajectoryText.text = `${a.toFixed(2)}`;
},
},
]);
bean.vel = vec2(200, -100);
});
function highestPoint(pos, vel) {
return pos.y - vel.y * vel.y / (2 * getGravity());
}
const heightGoal = highestPoint(vec2(50, 300), vec2(0, -200));
let heightResult = 0;
onDraw(() => {
y = highestPoint(vec2(50, 300), vec2(0, -200));
drawLine({
p1: vec2(10, y),
p2: vec2(90, y),
width: 2,
color: RED,
});
});
const heightText = add([
pos(100, heightGoal),
text(`0%`),
]);
onUpdate(() => {
heightText.text =
`${((100 * (heightResult - heightGoal) / heightGoal).toFixed(2))}%`;
});
onClick(() => {
y = highestPoint(vec2(50, 300), vec2(0, -200));
const bean = add([
sprite("bag"),
anchor("center"),
pos(50, 300),
body(),
offscreen({ destroy: true }),
{
draw() {
drawLine({
p1: vec2(-40, 0),
p2: vec2(40, 0),
width: 2,
color: GREEN,
});
},
update() {
if (this.vel.y <= 0) {
heightResult = this.pos.y;
}
},
},
]);
bean.vel = vec2(0, -200);
});