質問編集履歴
2
ソースコードの編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -36,173 +36,493 @@
|
|
36
36
|
|
37
37
|
```
|
38
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
|
-
|
39
|
+
```Javascript
|
40
|
+
|
41
|
+
//box2dクラスの短縮変数
|
42
|
+
|
43
|
+
var b2Vec2 = Box2D.Common.Math.b2Vec2, b2AABB = Box2D.Collision.b2AABB, b2BodyDef = Box2D.Dynamics.b2BodyDef, b2Body = Box2D.Dynamics.b2Body, b2FixtureDef = Box2D.Dynamics.b2FixtureDef, b2Fixture = Box2D.Dynamics.b2Fixture, b2World = Box2D.Dynamics.b2World, b2MassData = Box2D.Collision.Shapes.b2MassData, b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape, b2CircleShape = Box2D.Collision.Shapes.b2CircleShape, b2DebugDraw = Box2D.Dynamics.b2DebugDraw, b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef;
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
//クッキーの画像URLと頂点の座標
|
48
|
+
|
49
|
+
var cookieImages = {
|
50
|
+
|
51
|
+
'logo': {
|
52
|
+
|
53
|
+
'width': 420,
|
54
|
+
|
55
|
+
'height': 211,
|
56
|
+
|
57
|
+
'src': './images/cookies/logo.png',
|
58
|
+
|
59
|
+
'vertices': [
|
60
|
+
|
61
|
+
[0, -106],
|
62
|
+
|
63
|
+
[160, -71],
|
64
|
+
|
65
|
+
[211, 0],
|
66
|
+
|
67
|
+
[122, 83],
|
68
|
+
|
69
|
+
[0, 106],
|
70
|
+
|
71
|
+
[-156, 73],
|
72
|
+
|
73
|
+
[-209, 0],
|
74
|
+
|
75
|
+
[-155, -73]
|
76
|
+
|
77
|
+
]
|
78
|
+
|
79
|
+
},
|
80
|
+
|
81
|
+
'cookies': [
|
82
|
+
|
83
|
+
{
|
84
|
+
|
85
|
+
'width': 83,
|
86
|
+
|
87
|
+
'height': 48,
|
88
|
+
|
89
|
+
'src': './images/cookies/01.png',
|
90
|
+
|
91
|
+
'vertices': [
|
92
|
+
|
93
|
+
[42, -11],
|
94
|
+
|
95
|
+
[31, 17],
|
96
|
+
|
97
|
+
[-37, 24],
|
98
|
+
|
99
|
+
[-37, 24],
|
100
|
+
|
101
|
+
[-41, -12],
|
102
|
+
|
103
|
+
[0, -24],
|
104
|
+
|
105
|
+
[26, -21]
|
106
|
+
|
107
|
+
]
|
108
|
+
|
109
|
+
},
|
110
|
+
|
111
|
+
{
|
112
|
+
|
113
|
+
'width': 86,
|
114
|
+
|
115
|
+
'height': 109,
|
116
|
+
|
117
|
+
'src': './images/cookies/02.png',
|
118
|
+
|
119
|
+
'vertices': [
|
120
|
+
|
121
|
+
[0, -55],
|
122
|
+
|
123
|
+
[23, -30],
|
124
|
+
|
125
|
+
[43, 39],
|
126
|
+
|
127
|
+
[0, 54],
|
128
|
+
|
129
|
+
[-42, 44],
|
130
|
+
|
131
|
+
[-28, 5]
|
132
|
+
|
133
|
+
]
|
134
|
+
|
135
|
+
},
|
136
|
+
|
137
|
+
{
|
138
|
+
|
139
|
+
'width': 79,
|
140
|
+
|
141
|
+
'height': 55,
|
142
|
+
|
143
|
+
'src': './images/cookies/03.png',
|
144
|
+
|
145
|
+
'vertices': [
|
146
|
+
|
147
|
+
[32, -24],
|
148
|
+
|
149
|
+
[37, 23],
|
150
|
+
|
151
|
+
[-38, 26],
|
152
|
+
|
153
|
+
[-25, -20],
|
154
|
+
|
155
|
+
[-7, -25]
|
156
|
+
|
157
|
+
]
|
158
|
+
|
159
|
+
},
|
160
|
+
|
161
|
+
{
|
162
|
+
|
163
|
+
'width': 65,
|
164
|
+
|
165
|
+
'height': 71,
|
166
|
+
|
167
|
+
'src': './images/cookies/04.png',
|
168
|
+
|
169
|
+
'vertices': [
|
170
|
+
|
171
|
+
[-6, -34],
|
172
|
+
|
173
|
+
[29, 10],
|
174
|
+
|
175
|
+
[32, 29],
|
176
|
+
|
177
|
+
[3, 36],
|
178
|
+
|
179
|
+
[-31, 23],
|
180
|
+
|
181
|
+
[-23, -8]
|
182
|
+
|
183
|
+
]
|
184
|
+
|
185
|
+
},
|
186
|
+
|
187
|
+
{
|
188
|
+
|
189
|
+
'width': 151,
|
190
|
+
|
191
|
+
'height': 94,
|
192
|
+
|
193
|
+
'src': './images/cookies/05.png',
|
194
|
+
|
195
|
+
'vertices': [
|
196
|
+
|
197
|
+
[-74, -29],
|
198
|
+
|
199
|
+
[-17, -46],
|
200
|
+
|
201
|
+
[48, -17],
|
202
|
+
|
203
|
+
[74, 37],
|
204
|
+
|
205
|
+
[-49, 41],
|
206
|
+
|
207
|
+
[-69, 0]
|
208
|
+
|
209
|
+
]
|
210
|
+
|
211
|
+
},
|
212
|
+
|
213
|
+
{
|
214
|
+
|
215
|
+
'width': 107,
|
216
|
+
|
217
|
+
'height': 60,
|
218
|
+
|
219
|
+
'src': './images/cookies/06.png',
|
220
|
+
|
221
|
+
'vertices': [
|
222
|
+
|
223
|
+
[54, -12],
|
224
|
+
|
225
|
+
[37, 24],
|
226
|
+
|
227
|
+
[-18, 27],
|
228
|
+
|
229
|
+
[-52, -6],
|
230
|
+
|
231
|
+
[7, -29],
|
232
|
+
|
233
|
+
[31, -25]
|
234
|
+
|
235
|
+
]
|
236
|
+
|
237
|
+
},
|
238
|
+
|
239
|
+
{
|
240
|
+
|
241
|
+
'width': 181,
|
242
|
+
|
243
|
+
'height': 82,
|
244
|
+
|
245
|
+
'src': './images/cookies/07.png',
|
246
|
+
|
247
|
+
'vertices': [
|
248
|
+
|
249
|
+
[0, -40],
|
250
|
+
|
251
|
+
[56, -23],
|
252
|
+
|
253
|
+
[91, 29],
|
254
|
+
|
255
|
+
[-27, 41],
|
256
|
+
|
257
|
+
[-90, 24],
|
258
|
+
|
259
|
+
[-54, -24]
|
260
|
+
|
261
|
+
]
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
]
|
266
|
+
|
267
|
+
};
|
268
|
+
|
269
|
+
//ロゴを再生成
|
270
|
+
|
271
|
+
Physics.prototype.recreateLogo = function () {
|
272
|
+
|
273
|
+
if (this.logoExist) {
|
274
|
+
|
275
|
+
this.destroyLogo();
|
276
|
+
|
277
|
+
this.createLogo();
|
278
|
+
|
279
|
+
}
|
280
|
+
|
281
|
+
};
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
//クッキーを創る
|
286
|
+
|
287
|
+
Physics.prototype.createCookie = function () {
|
288
|
+
|
289
|
+
var random = (Math.random() + Math.random()) / 2;
|
290
|
+
|
291
|
+
var cookieNum = Math.floor(random * (cookieImages['cookies'].length - 0.000001));
|
292
|
+
|
293
|
+
var cookie = cookieImages['cookies'][cookieNum];
|
294
|
+
|
295
|
+
var canvasWidth = this.canvas.width;
|
296
|
+
|
297
|
+
var applyForce;
|
298
|
+
|
299
|
+
if (this.canvas.width < 769) {
|
300
|
+
|
301
|
+
applyForce = (10 * Math.random()) - 5;
|
302
|
+
|
303
|
+
} else {
|
304
|
+
|
305
|
+
applyForce = (30 * Math.random()) - 15;
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
this.createBody({ type: 'dynamic', x: (canvasWidth * Math.random()), y: -30, width: cookie['width'], height: cookie['height'], angle: 360 * Math.random(), force: [applyForce, 0], image: cookie });
|
310
|
+
|
311
|
+
};
|
312
|
+
|
313
|
+
//すべてのオブジェクトを消す
|
314
|
+
|
315
|
+
Physics.prototype.destroyAll = function () {
|
316
|
+
|
317
|
+
for (var bodyItem = this.world.GetBodyList(); bodyItem; bodyItem = bodyItem.GetNext()) {
|
318
|
+
|
319
|
+
this.world.DestroyBody(bodyItem);
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
};
|
324
|
+
|
325
|
+
|
326
|
+
|
327
|
+
//box2dのbodyを生成
|
328
|
+
|
329
|
+
Physics.prototype.createBody = function (opts) {
|
330
|
+
|
331
|
+
//オプションのデフォルト値
|
332
|
+
|
333
|
+
var defaults = {
|
334
|
+
|
335
|
+
type: 'static',
|
336
|
+
|
337
|
+
x: 0,
|
338
|
+
|
339
|
+
y: 0,
|
340
|
+
|
341
|
+
width: 10,
|
342
|
+
|
343
|
+
height: 10,
|
344
|
+
|
345
|
+
angle: 0,
|
346
|
+
|
347
|
+
restitution: 0.2,
|
348
|
+
|
349
|
+
friction: 0.7,
|
350
|
+
|
351
|
+
density: 2.65,
|
352
|
+
|
353
|
+
force: [0, 0],
|
354
|
+
|
355
|
+
image: null
|
356
|
+
|
357
|
+
};
|
358
|
+
|
359
|
+
|
360
|
+
|
361
|
+
//デフォルト値を上書き
|
362
|
+
|
363
|
+
$.extend(defaults, opts);
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
var type;
|
368
|
+
|
369
|
+
switch (defaults['type']) {
|
370
|
+
|
371
|
+
case 'static':
|
372
|
+
|
373
|
+
type = b2Body.b2_staticBody;
|
374
|
+
|
375
|
+
break;
|
376
|
+
|
377
|
+
case 'dynamic':
|
378
|
+
|
379
|
+
type = b2Body.b2_dynamicBody;
|
380
|
+
|
381
|
+
break;
|
382
|
+
|
383
|
+
default:
|
384
|
+
|
385
|
+
type = b2Body.b2_staticBody;
|
386
|
+
|
387
|
+
break;
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
var x = this.pixelToMeter(defaults['x']);
|
392
|
+
|
393
|
+
var y = this.pixelToMeter(defaults['y']);
|
394
|
+
|
395
|
+
var width = this.pixelToMeter(defaults['width']);
|
396
|
+
|
397
|
+
var height = this.pixelToMeter(defaults['height']);
|
398
|
+
|
399
|
+
var angle = this.degreeToRadians(defaults['angle']);
|
400
|
+
|
401
|
+
var restitution = defaults['restitution'];
|
402
|
+
|
403
|
+
var friction = defaults['friction'];
|
404
|
+
|
405
|
+
var density = defaults['density'];
|
406
|
+
|
407
|
+
var force = defaults['force'];
|
408
|
+
|
409
|
+
var image = defaults['image'];
|
410
|
+
|
411
|
+
|
412
|
+
|
413
|
+
//bodyを生成
|
414
|
+
|
415
|
+
var body = this.world.CreateBody(bodyDef);
|
416
|
+
|
417
|
+
body.CreateFixture(fixDef);
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
//物体に力を作用させる
|
422
|
+
|
423
|
+
body.ApplyForce(new b2Vec2(force[0], force[1]), body.GetWorldCenter());
|
424
|
+
|
425
|
+
|
426
|
+
|
427
|
+
return body;
|
428
|
+
|
429
|
+
};
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
//描く
|
434
|
+
|
435
|
+
Physics.prototype.draw = function () {
|
436
|
+
|
437
|
+
for (var bodyItem = this.world.GetBodyList(); bodyItem; bodyItem = bodyItem.GetNext()) {
|
438
|
+
|
439
|
+
//if ( bodyItem.GetType() == b2Body.b2_dynamicBody ) {
|
440
|
+
|
441
|
+
var position = bodyItem.GetPosition();
|
442
|
+
|
443
|
+
var userData = bodyItem.GetUserData();
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
this.context.save();
|
448
|
+
|
449
|
+
if (userData && userData.img && userData.img.complete) {
|
450
|
+
|
451
|
+
if (userData.shape_type && userData.shape_type == 'circle') {
|
452
|
+
|
453
|
+
var slideX = position.x * this.worldScale;
|
454
|
+
|
455
|
+
var slideY = position.y * this.worldScale;
|
456
|
+
|
457
|
+
|
458
|
+
|
459
|
+
this.context.translate(slideX, slideY);
|
460
|
+
|
461
|
+
this.context.rotate(bodyItem.GetAngle());
|
462
|
+
|
463
|
+
this.context.drawImage(userData.img, -userData.radius, -userData.radius);
|
464
|
+
|
465
|
+
}
|
466
|
+
|
467
|
+
if (userData.shape_type && userData.shape_type == 'box') {
|
468
|
+
|
469
|
+
var slideX = position.x * this.worldScale;
|
470
|
+
|
471
|
+
var slideY = position.y * this.worldScale;
|
472
|
+
|
473
|
+
|
474
|
+
|
475
|
+
this.context.translate(slideX, slideY);
|
476
|
+
|
477
|
+
this.context.rotate(bodyItem.GetAngle());
|
478
|
+
|
479
|
+
this.context.drawImage(userData.img, -userData.width / 2.0, -userData.height / 2.0, userData.width, userData.height);
|
480
|
+
|
481
|
+
}
|
482
|
+
|
483
|
+
}
|
484
|
+
|
485
|
+
this.context.restore();
|
486
|
+
|
487
|
+
//}
|
488
|
+
|
489
|
+
}
|
490
|
+
|
491
|
+
};
|
492
|
+
|
493
|
+
|
494
|
+
|
495
|
+
//重力加速度を変える
|
496
|
+
|
497
|
+
Physics.prototype.setGravity = function (gravity) {
|
498
|
+
|
499
|
+
if (typeof gravity === "undefined") { gravity = [0, 9.8]; }
|
500
|
+
|
501
|
+
this.world.SetGravity(new b2Vec2(gravity[0], gravity[1]));
|
502
|
+
|
503
|
+
};
|
504
|
+
|
505
|
+
|
506
|
+
|
507
|
+
//座標の配列をb2Vec2の配列で出力
|
508
|
+
|
509
|
+
Physics.prototype.getB2Vec2 = function (array) {
|
510
|
+
|
511
|
+
var b2Vec2Array = new Array(array.length);
|
512
|
+
|
513
|
+
for (var i = 0; i < array.length; i++) {
|
514
|
+
|
515
|
+
b2Vec2Array[i] = new b2Vec2(this.pixelToMeter(array[i][0]) * this.getViewScale(), this.pixelToMeter(array[i][1]) * this.getViewScale());
|
516
|
+
|
517
|
+
}
|
518
|
+
|
519
|
+
return b2Vec2Array;
|
520
|
+
|
521
|
+
};
|
522
|
+
|
523
|
+
|
524
|
+
|
525
|
+
|
206
526
|
|
207
527
|
|
208
528
|
|
1
申し訳ありません。長すぎて投稿できず分割いたしました。https://teratail.com/questions/102553?modal=q-comp
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|