箱とボールがぶつかる
前の節では箱は「絵」だけでした。この節では壁と床に当たり判定を付けて、ボールが箱の中に 溜まるようにします。
今回さわる
game/:main.jsを書き換え
前の節を終えていない人は、ここから始められます。
壁と床を「動かない物理ボディ」にする
Section titled “壁と床を「動かない物理ボディ」にする”箱を描いた graphics のすぐ後ろに、壁と床の物理ボディを 3 つ足します。
this.matter.add.rectangle(中心x, 中心y, 幅, 高さ, オプション) で四角い当たり判定を作れます。
大事なのは { isStatic: true } です。これは「重力の影響を受けず、その場に固定される」という指定で、
壁や床のように動かないものに使います。
main.js を変更する
Section titled “main.js を変更する”2727g.fillStyle(0x3498db, 1);2828g.fillRect(box.left - box.wall, box.bottom, box.right - box.left + box.wall * 2, box.wall);2929
30const radius = 2530const wallH = box.bottom - box.top;31const wallCY = (box.top + box.bottom) / 2;32this.matter.add.rectangle(box.left - box.wall / 2, wallCY, box.wall, wallH, { isStatic: true });33this.matter.add.rectangle(box.right + box.wall / 2, wallCY, box.wall, wallH, { isStatic: true });34const floorW = box.right - box.left + box.wall * 2;35this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });36
37const radius = 25;3138const ball = this.add.circle(200, 80, radius, 0x333333);3239
3340this.matter.add.gameObject(ball, {3542 type: 'circle',3643 radius: radius3744 },45 restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる)3846 });3947 }4048},rectangle は中心の座標を指定する点に注意してください(fillRect は左上起点でした)。
そのため、絵と当たり判定がちょうど重なるように box.wall / 2 などで中心をずらしています。
見た目(
fillRect)と物理(rectangle)は別物です。この節では「絵と同じ位置に、見えない当たり判定を 置く」ことをしています。数値がぴったり一致していると、絵の壁でボールが止まって見えます。
跳ね返りの強さを決める
Section titled “跳ね返りの強さを決める”床に当たったときにボールがどれくらい跳ね返るかは、ボールを物理ボディにするときの
restitution(反発係数)で決めます。ボールの gameObject にオプションを 1 つ足します。
restitution は 0〜1 の値で、大きいほどよく跳ねます。目安は次の通りです。
| 値 | 挙動 |
|---|---|
0 | まったく跳ねない(着地したらそのまま止まる) |
0.2 | 少しだけ跳ねる(このハンズオンで使う値) |
0.5 | ボヨンと跳ね返る |
1 | ほぼエネルギーを失わずに跳ね続ける |
スイカゲームのように「積み上げる」遊びでは、跳ねすぎると積みにくくなります。ここでは
軽く跳ねる 0.2 にしておきます。この設定はこれ以降の節でもそのまま引き継ぎます。
値を
0や0.8に変えて、跳ね方がどう変わるか試してみると感覚がつかめます。
ボールが床に当たって止まり、箱の中に溜まるようになりました。まだ 1 個ずつしか出ませんが、 次の節でタップするたびにボールを増やせるようにします。
この節を終えたときのディレクトリ
Section titled “この節を終えたときのディレクトリ”<!doctype html><html lang="ja"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>04 箱とボールがぶつかる/箱にたまる</title> <style> body { margin: 0; display: grid; place-items: center; min-height: 100vh; background: #222; } canvas { display: block; } </style> <script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.90.0/phaser.min.js"></script> <script src="main.js" defer></script> </head> <body></body></html>new Phaser.Game({ type: Phaser.AUTO, width: 400, height: 600, backgroundColor: '#fdf6e3', physics: { default: 'matter', matter: { gravity: { y: 1 } }, }, scene: { create: function () { const box = { left: 60, right: 340, top: 150, bottom: 540, wall: 12 };
const g = this.add.graphics(); g.fillStyle(0xe74c3c, 1); g.fillRect(box.left - box.wall, box.top, box.wall, box.bottom - box.top);
g.fillStyle(0x2ecc71, 1); g.fillRect(box.right, box.top, box.wall, box.bottom - box.top);
g.fillStyle(0x3498db, 1); g.fillRect(box.left - box.wall, box.bottom, box.right - box.left + box.wall * 2, box.wall);
const wallH = box.bottom - box.top; const wallCY = (box.top + box.bottom) / 2; this.matter.add.rectangle(box.left - box.wall / 2, wallCY, box.wall, wallH, { isStatic: true }); this.matter.add.rectangle(box.right + box.wall / 2, wallCY, box.wall, wallH, { isStatic: true }); const floorW = box.right - box.left + box.wall * 2; this.matter.add.rectangle((box.left + box.right) / 2, box.bottom + box.wall / 2, floorW, box.wall, { isStatic: true });
const radius = 25; const ball = this.add.circle(200, 80, radius, 0x333333);
this.matter.add.gameObject(ball, { shape: { type: 'circle', radius: radius }, restitution: 0.2, // 跳ね返りの強さ(0=跳ねない 〜 1=よく跳ねる) }); } },});