回答編集履歴

5

修正しました。

2019/01/23 09:51

投稿

退会済みユーザー
test CHANGED
@@ -30,13 +30,9 @@
30
30
 
31
31
  GAIN = 0.99,
32
32
 
33
- PI2 = Math.PI * 2;
33
+ PI2 = Math.PI * 2,
34
-
35
-
36
-
37
- const
34
+
38
-
39
- square = (n) => n * n;
35
+ DEF_COLOR = 'rgba(255,0,0,.5)';
40
36
 
41
37
 
42
38
 
@@ -74,7 +70,7 @@
74
70
 
75
71
  class Ball extends Point {
76
72
 
77
- constructor (name, x, y, r = 100, c = 'rgba(255,0,0,.5)', direction = new Vector) {
73
+ constructor (name, x, y, r = 100, color = DEF_COLOR, direction = new Vector) {
78
74
 
79
75
  super (x, y);
80
76
 
@@ -82,7 +78,7 @@
82
78
 
83
79
  this.r = r;
84
80
 
85
- this.color = c;
81
+ this.color = color;
86
82
 
87
83
  this.direction = direction;
88
84
 
@@ -94,25 +90,29 @@
94
90
 
95
91
 
96
92
 
97
- setTouch (p) {
93
+ setTouch ({x: px, y: py}) {
98
-
94
+
99
- let {x, y, r} = this, rst;
95
+ let {x, y, r} = this;
100
-
96
+
101
- if (rst = square (p.x - x) + square (p.y - y) < square (r)) {
97
+ if ((px - x)**2 + (py - y)**2 < r**2) {
102
-
98
+
103
- this.touched = rst;
99
+ this.touched = true;
104
-
100
+
105
- this.offset = new Point (x - p.x, y - p.y);
101
+ this.offset = new Point (x - px, y - py);
102
+
103
+ this.direction = new Vector;
106
104
 
107
105
  }
108
106
 
109
107
  }
110
108
 
111
-
112
-
109
+
110
+
113
- isCollision ({ x, y, r }) {
111
+ isCollision ({x: px, y: py, r: pr}) {
112
+
114
-
113
+ let {x, y, r} = this;
114
+
115
- return square (this.x - x) + square (this.y - y) <= square (this.r+r);
115
+ return (x - px)**2 + (y - py)**2 <= (r + pr)**2;
116
116
 
117
117
  }
118
118
 
@@ -166,25 +166,21 @@
166
166
 
167
167
  mousemove (event) {
168
168
 
169
- this.mx = this.x;
170
-
171
- this.my = this.y;
169
+ let {x, y, ary} = this;
172
-
173
-
174
-
175
- let x = event.clientX, y = event.clientY;
170
+
176
-
177
-
178
-
171
+
172
+
179
- this.ary.forEach (b => {
173
+ ary.forEach (b => {
180
174
 
181
175
  if (b.touched) {
182
176
 
177
+ let o = b.offset;
178
+
183
- b.x = this.x + b.offset.x;
179
+ b.x = x + o.x;
184
-
180
+
185
- b.y = this.y + b.offset.y;
181
+ b.y = y + o.y;
186
-
182
+
187
- b.direction = new Point;
183
+ b.direction = new Vector;
188
184
 
189
185
  }
190
186
 
@@ -192,7 +188,13 @@
192
188
 
193
189
 
194
190
 
191
+ this.mx = x;
192
+
193
+ this.my = y;
194
+
195
- this.x = x; this.y = y;
195
+ this.x = event.clientX;
196
+
197
+ this.y = event.clientY;
196
198
 
197
199
  }
198
200
 
@@ -200,9 +202,15 @@
200
202
 
201
203
  mouseup (event) {
202
204
 
205
+ let
206
+
207
+ {mx, my, ary} = this,
208
+
203
- let v = new Vector (event.clientX - this.mx, event.clientY - this.my);
209
+ v = new Vector (event.clientX - mx, event.clientY - my);
204
-
210
+
211
+
212
+
205
- this.ary.forEach (b => {
213
+ ary.forEach (b => {
206
214
 
207
215
  if (b.touched) {
208
216
 
@@ -230,11 +238,11 @@
230
238
 
231
239
  class Field {
232
240
 
233
- constructor (target, objs = [ ]) {
241
+ constructor (target, ary = [ ]) {
234
242
 
235
243
  this.target = target;
236
244
 
237
- this.ary = objs;
245
+ this.ary = ary;
238
246
 
239
247
  this.w = target.width;
240
248
 
@@ -254,7 +262,11 @@
254
262
 
255
263
  progress () {
256
264
 
265
+ let { w, h, ary } = this;
266
+
267
+
268
+
257
- this.ary.forEach (b => {
269
+ ary.forEach (b => {
258
270
 
259
271
  let
260
272
 
@@ -264,9 +276,9 @@
264
276
 
265
277
 
266
278
 
267
- x += d.x;
279
+ x += d.x *= GAIN;
268
-
280
+
269
- y += d.y;
281
+ y += d.y *= GAIN;
270
282
 
271
283
 
272
284
 
@@ -278,9 +290,9 @@
278
290
 
279
291
  }
280
292
 
281
- else if (this.w - r < x) {
293
+ else if (w - r < x) {
282
-
294
+
283
- x = this.w - r;
295
+ x = w - r;
284
296
 
285
297
  d.x *= -1;
286
298
 
@@ -296,9 +308,9 @@
296
308
 
297
309
  }
298
310
 
299
- else if (this.h - r < y) {
311
+ else if (h - r < y) {
300
-
312
+
301
- y = this.h - r;
313
+ y = h - r;
302
314
 
303
315
  d.y *= -1;
304
316
 
@@ -306,13 +318,11 @@
306
318
 
307
319
 
308
320
 
309
- //___
310
-
311
321
  //当り判定後が雑すぎる
312
322
 
313
323
  let tmp = new Ball ('tmp', x, y, r);
314
324
 
315
- this.ary.forEach (tb => {
325
+ ary.forEach (tb => {
316
326
 
317
327
  if (tb != b) {
318
328
 
@@ -334,20 +344,10 @@
334
344
 
335
345
 
336
346
 
337
- //__
338
-
339
-
340
-
341
347
  b.x = x;
342
348
 
343
349
  b.y = y;
344
350
 
345
-
346
-
347
- d.x *= GAIN;
348
-
349
- d.y *= GAIN;
350
-
351
351
  });
352
352
 
353
353
  }
@@ -388,11 +388,13 @@
388
388
 
389
389
  draw (balls) {
390
390
 
391
+ let ctx = this.ctx;
392
+
393
+
394
+
391
395
  balls.forEach (b => {
392
396
 
393
- let {name, x, y, r, color } = b;
397
+ let {name, x, y, r, color} = b;
394
-
395
- let ctx = this.ctx;
396
398
 
397
399
 
398
400
 
@@ -402,7 +404,7 @@
402
404
 
403
405
  ctx.arc (x, y, r, 0, PI2, false);
404
406
 
405
- ctx.fill ();
407
+ ctx.fill ();
406
408
 
407
409
 
408
410
 
@@ -420,8 +422,6 @@
420
422
 
421
423
  }
422
424
 
423
-
424
-
425
425
  }
426
426
 
427
427
 

4

update

2019/01/23 09:51

投稿

退会済みユーザー
test CHANGED
@@ -462,7 +462,7 @@
462
462
 
463
463
  new Ball ('こんにちは', 500, 300, 80),
464
464
 
465
- new Ball ('当り判定無視', 700, 500, 70, 'rgba(0,255,255,.5)')
465
+ new Ball ('当り判定が雑', 700, 500, 70, 'rgba(0,255,255,.5)')
466
466
 
467
467
  ],
468
468
 

3

update

2019/01/22 00:36

投稿

退会済みユーザー
test CHANGED
@@ -2,18 +2,20 @@
2
2
 
3
3
  <!DOCTYPE html>
4
4
 
5
- <html>
6
-
7
5
  <title></title>
8
6
 
9
7
  <meta charset="utf-8">
10
8
 
9
+
10
+
11
11
  <style>
12
12
 
13
13
  canvas { background: #fd0; }
14
14
 
15
15
  </style>
16
16
 
17
+
18
+
17
19
  <body>
18
20
 
19
21
  <canvas width="700" height="700"></canvas>
@@ -90,19 +92,27 @@
90
92
 
91
93
  }
92
94
 
95
+
96
+
97
+ setTouch (p) {
98
+
99
+ let {x, y, r} = this, rst;
100
+
101
+ if (rst = square (p.x - x) + square (p.y - y) < square (r)) {
102
+
103
+ this.touched = rst;
104
+
105
+ this.offset = new Point (x - p.x, y - p.y);
106
+
107
+ }
108
+
109
+ }
110
+
93
111
 
94
112
 
95
- setTouch (p) {
96
-
97
- let {x, y, r} = this, rst;
113
+ isCollision ({ x, y, r }) {
98
-
114
+
99
- if (rst = square (p.x - x) + square (p.y - y) < square (r)) {
115
+ return square (this.x - x) + square (this.y - y) <= square (this.r+r);
100
-
101
- this.touched = rst;
102
-
103
- this.offset = new Point (x - p.x, y - p.y);
104
-
105
- }
106
116
 
107
117
  }
108
118
 
@@ -128,7 +138,7 @@
128
138
 
129
139
  this.my = 0;
130
140
 
131
-
141
+
132
142
 
133
143
  ['mousedown', 'mousemove', 'mouseup']
134
144
 
@@ -136,7 +146,7 @@
136
146
 
137
147
  }
138
148
 
139
-
149
+
140
150
 
141
151
  handleEvent (event) {
142
152
 
@@ -144,7 +154,7 @@
144
154
 
145
155
  }
146
156
 
147
-
157
+
148
158
 
149
159
  mousedown (event) {
150
160
 
@@ -152,7 +162,7 @@
152
162
 
153
163
  }
154
164
 
155
-
165
+
156
166
 
157
167
  mousemove (event) {
158
168
 
@@ -160,11 +170,11 @@
160
170
 
161
171
  this.my = this.y;
162
172
 
163
-
173
+
164
174
 
165
175
  let x = event.clientX, y = event.clientY;
166
176
 
167
-
177
+
168
178
 
169
179
  this.ary.forEach (b => {
170
180
 
@@ -180,7 +190,7 @@
180
190
 
181
191
  });
182
192
 
183
-
193
+
184
194
 
185
195
  this.x = x; this.y = y;
186
196
 
@@ -232,7 +242,7 @@
232
242
 
233
243
  }
234
244
 
235
-
245
+
236
246
 
237
247
  add (obj) {
238
248
 
@@ -240,7 +250,7 @@
240
250
 
241
251
  }
242
252
 
243
-
253
+
244
254
 
245
255
  progress () {
246
256
 
@@ -258,7 +268,7 @@
258
268
 
259
269
  y += d.y;
260
270
 
261
-
271
+
262
272
 
263
273
  if (x < r) {
264
274
 
@@ -276,7 +286,7 @@
276
286
 
277
287
  }
278
288
 
279
-
289
+
280
290
 
281
291
  if (y < r) {
282
292
 
@@ -294,15 +304,35 @@
294
304
 
295
305
  }
296
306
 
297
-
307
+
298
308
 
299
309
  //___
300
310
 
301
- //丸同士の当り判定
311
+ //当り判定後が雑すぎる
312
+
302
-
313
+ let tmp = new Ball ('tmp', x, y, r);
314
+
303
-
315
+ this.ary.forEach (tb => {
316
+
304
-
317
+ if (tb != b) {
318
+
305
-
319
+ if (tmp.isCollision (tb)) {
320
+
321
+ d.x *= -1;
322
+
323
+ d.y *= -1;
324
+
325
+ x = b.x;
326
+
327
+ y = b.y;
328
+
329
+ }
330
+
331
+ }
332
+
333
+ })
334
+
335
+
306
336
 
307
337
  //__
308
338
 
@@ -322,11 +352,11 @@
322
352
 
323
353
  }
324
354
 
325
-
355
+
326
356
 
327
357
  }
328
358
 
329
-
359
+
330
360
 
331
361
 
332
362
 
@@ -354,7 +384,7 @@
354
384
 
355
385
  }
356
386
 
357
-
387
+
358
388
 
359
389
  draw (balls) {
360
390
 
@@ -390,7 +420,7 @@
390
420
 
391
421
  }
392
422
 
393
-
423
+
394
424
 
395
425
  }
396
426
 

2

update

2019/01/22 00:34

投稿

退会済みユーザー
test CHANGED
@@ -18,7 +18,7 @@
18
18
 
19
19
  <canvas width="700" height="700"></canvas>
20
20
 
21
- </body>
21
+
22
22
 
23
23
  <script>
24
24
 

1

update

2019/01/21 22:16

投稿

退会済みユーザー
test CHANGED
@@ -468,6 +468,4 @@
468
468
 
469
469
 
470
470
 
471
-
472
-
473
471
  ```