-
Notifications
You must be signed in to change notification settings - Fork 1
/
level-7-mouse-constraint-and-friction.html
158 lines (143 loc) · 5.61 KB
/
level-7-mouse-constraint-and-friction.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Level 7 - Mouse Constraint and Friction (physics-simulations-balls)</title>
<link rel="stylesheet" href="assets/style.css">
</head>
<body>
<canvas></canvas>
<script src="assets/util.js"></script>
<script src="assets/canvas.js"></script>
<script src="assets/timingManager.js"></script>
<script>
const defaultBall = {
x: canvas.w / 2,
y: canvas.h / 2,
v_x: 0,
v_y: 0,
r: 10,
m: 1,
color: 'red',
};
const balls = [
{
...defaultBall,
x: 30,
y: canvas.h / 2,
v_x: 8,
color: '#E91E63',
},
{
...defaultBall,
x: canvas.w - 50 - 61,
y: canvas.h / 2,
color: '#009688',
},
{
...defaultBall,
x: canvas.w - 50 - 44,
y: canvas.h / 2 - 11,
color: '#FF9800',
},
{
...defaultBall,
x: canvas.w - 50 - 44,
y: canvas.h / 2 + 11,
color: '#2196F3',
},
{
...defaultBall,
x: canvas.w - 50 - 27,
y: canvas.h / 2,
color: '#FFC107',
},
{
...defaultBall,
x: canvas.w - 50 - 10,
y: canvas.h / 2 - 11,
color: '#333333',
},
{
...defaultBall,
x: canvas.w - 50 - 10,
y: canvas.h / 2 + 11,
color: '#00BCD4',
},
{
isMouseConstrained: true,
x: -100,
y: -100,
r: 15,
color: 'red',
}
];
const k = 5; // spring stiffness
const mu = 0.0005; // drag friction coefficient
configureTimingManager({
callback_simulateOneStep: function (dt) {
for (let ball of balls) {
ball.F_x = ball.F_y = 0; // reset/initialize
}
util.forEachPair(balls, (i, j) => {
const d_x = i.x - j.x;
const d_y = i.y - j.y;
const d = Math.sqrt(d_x ** 2 + d_y ** 2) + 0.000001; // '+0.000001' prevents division with zero later
const s = i.r + j.r - d;
if (0 < s) {
i.F_x += s * k * (d_x / d);
i.F_y += s * k * (d_y / d);
j.F_x -= s * k * (d_x / d);
j.F_y -= s * k * (d_y / d);
}
});
for (let ball of balls) {
if (ball.x - ball.r < 0) {
ball.F_x -= (ball.x - ball.r) * k;
}
if (ball.x + ball.r - canvas.w > 0) {
ball.F_x -= (ball.x + ball.r - canvas.w) * k;
}
if (ball.y - ball.r < 0) {
ball.F_y -= (ball.y - ball.r) * k;
}
if (ball.y + ball.r - canvas.h > 0) {
ball.F_y -= (ball.y + ball.r - canvas.h) * k;
}
}
for (let ball of balls) {
if (ball.isMouseConstrained) {
continue;
}
const v = Math.sqrt(ball.v_x ** 2 + ball.v_y ** 2) + 0.000001; // '+0.000001' prevents division with zero later
const F_R = mu * v ** 2 * ball.m;
ball.F_x -= ball.v_x / v * F_R;
ball.F_y -= ball.v_y / v * F_R;
const a_x = ball.F_x / ball.m;
const a_y = ball.F_y / ball.m;
ball.v_x += dt * a_x;
ball.v_y += dt * a_y;
ball.x += dt * ball.v_x;
ball.y += dt * ball.v_y;
}
},
callback_renderFrame: function () {
canvas.clear();
for (let ball of balls) {
canvas.fillCircle(ball.x, ball.y, ball.r, ball.color);
}
},
subSteps_perFrame: 20, // sup-step-precision has the effect that collisions are detected earlier and more precisely
});
window.addEventListener('mousemove', (evt) => {
for (let ball of balls) {
if (ball.isMouseConstrained) {
ball.x = evt.clientX;
ball.y = evt.clientY;
}
}
});
</script>
</body>
</html>