melonjs-physics
できること
このガイドは、melonJSに組み込まれた衝突判定のシステムと、2つの任意の物理エンジンを説明します。それぞれがどのような場合に合うかも扱います。プラットフォーマー、見下ろし型のゲーム、物理の多い場面を作るゲーム開発者向けです。
使う場面
- ✓開発者が、組み込みの衝突判定システムと本格的な物理エンジンのどちらを選ぶか決める必要があるとき。
- ✓開発者の回転するオブジェクトが、まったく回転していないかのように衝突しているとき。
- ✓ゲームのワールドでオブジェクトを見つけるために、レイキャストや範囲のクエリが必要なとき。
スキルのリンク
https://github.com/melonjs/melonJS/blob/master/packages/melonjs/skills/melonjs-physics/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。
貼り付け用メッセージ
Claude Code、Codex、Cursorの場合:プロジェクトのフォルダで実行するとスキルがインストールされます。
SKILL.mdをダウンロードして、エージェントのスキル用フォルダに入れてください。
AIへの頼み方の例
“私のmelonJSのプラットフォーマーは、組み込みの衝突判定と物理エンジンのどちらを使うべきですか。衝突判定の設定方法も示してください。”
毎週5つの新しいスキル。1分で読めます。
仕組み
このスキルは、組み込みのシステムはほとんどのアーケードゲームに合う一方、回転・衝突・ジョイントには物理のアダプターが必要だと説明します。そのうえで、衝突の種類、マスク、コールバックを扱います。
技術的な内容(元のSKILL.md)
作者による元のテキスト(英語)をGitHubからそのまま表示しています。
Physics and collision
melonJS has a built-in SAT collision world and two optional rigid-body adapters. Movement, collision callbacks and spatial queries all behave differently depending on which is active — and several of the differences fail silently.
Choosing a world
| when | |
|---|---|
| built-in (default) | Arcade-style: platformers, top-down games. Fast, position-based, not Newtonian. |
@melonjs/planck-adapter |
Full rigid-body dynamics — stacking, joints, realistic restitution. |
@melonjs/matter-adapter |
Same class of thing, backed by matter-js. |
Picking one
Start with the built-in. It is the default for a reason: no dependency, the smallest bundle, and its position-based model is what most 2D games actually want — a platformer's jump arc is a designed curve, not a simulated one, and tuning gravity against a solver you are fighting is a bad afternoon.
Move to an adapter when the game needs something the builtin cannot express, and these are the usual triggers:
- Rotation that collides. The clearest signal. The builtin's SAT never reads
body.angle, so a spinning shape collides as though it never turned. See the note in Built-in world quirks below. - Torque, angular velocity, or spin from an impact. Anything that should start turning because it was hit.
- Joints and constraints — ragdolls, chains, ropes, vehicles, hinged doors. The builtin has no concept of them.
- Stacking and resting contacts. Boxes that pile up and stay put need an iterative solver. The builtin resolves each contact once per step with no iteration pass, which splits three ways. A dynamic body resting on STATIC geometry is exact, to the pixel, which is the platformer case it exists for. A body squeezed between a dynamic body and static geometry is exact too: the immovable side gets the casting vote and the other body absorbs the whole correction, so a crate shoved against a wall stops at the wall. But a body squeezed between two DYNAMIC bodies has no immovable side to defer to, so those per-pair corrections still compete and it comes to rest slightly overlapping. Three stacked 44px boxes rest flush on the floor with about 2px of overlap inside the pile, and stay there: a stable equilibrium, not jitter you can wait out or tune away.
- Believable restitution and friction, where a ball's bounce height and roll should follow from its material rather than from code you wrote per case.
Stay on the built-in when the game is grid or tile based, movement is
authored rather than simulated, collision is "did these two boxes touch" plus a
push-out, or you are shipping to a tight bundle budget. Tiled maps, Trigger
and Collectable all work on every backend, so this is not a fork in the road
for level content.
planck or matter? Both are real rigid-body engines and either will do; the adapters expose the same portable API, so a game can switch with an import change and a gravity re-tune.
- planck (a Box2D port) is the more accurate and the more predictable under
stress: better stacking, better joint behaviour, a continuous-collision
bulletmode for fast movers. It works in metres, so it has apixelsPerMeterscale to think about, and its tuning vocabulary is Box2D's. - matter-js works directly in pixels, which makes it the gentler
introduction, and its compound bodies and constraints are pleasant to author.
It is the softer solver: stacks settle less crisply and fast bodies need
subSteps.
If you have no preference and the game leans on stacking, joints or fast projectiles, take planck. If you want to be up and running with the least conversion to think about, take matter.
An adapter is an Application setting, not a plugin:
import { PlanckAdapter } from "@melonjs/planck-adapter";
const app = new Application(800, 600, {
parent: "screen",
physic: new PlanckAdapter({
gravity: { x: 0, y: 320 }, // pixels/s², default {x: 0, y: 320}
pixelsPerMeter: 32, // default 32
subSteps: 2, // default 1
}),
});
await app.init();
PlanckAdapter also takes velocityIterations (default 8) and
positionIterations (default 3). MatterAdapter takes a different set —
gravity (default {x: 0, y: 1}, matter's own convention), subSteps and
matterEngineOptions; it has no pixelsPerMeter because it already works in
pixels. subSteps is the a
よくある質問
組み込みの衝突判定システムで十分なのはどのような場合ですか?
スキルによると、高速で位置に基づいているため、ほとんどのプラットフォーマーや見下ろし型のゲームに合います。
回転するオブジェクトが正しく衝突しないのはなぜですか?
組み込みのシステムは回転を読まないため、そのためには物理のアダプターが必要だとスキルは述べています。
どのようなクエリを実行できますか?
オブジェクトを見つけるためのレイキャスト、範囲のクエリ、円のクエリを扱います。
詳細
作者による元の説明
Use this skill for collision, physics bodies, movement, and spatial queries in melonJS — the built-in SAT world and the planck/matter adapters. Covers bodyDef vs Body, collision types and masks, the collision callback family and their firing rules, raycast/queryAABB/querySphere, the behaviour differences between adapters, and which one to pick for a given game. Triggers on: which physics engine, choosing an adapter, rotation collision, rotating body, torque, joints, stacking, Body, bodyDef, collision, collisionType, collisionMask, onCollision, onCollisionStart, onCollisionActive, raycast, quer
repo: melonjs/melonJS
人気
- GitHubのスター
- 6.4k
- ランキング
- #14210
- ライセンス
- MIT
- 更新日
- 2026/11/2
コメント
- …
このリポジトリの他のスキル
melonjs-20-migration
6.4kエージェントが、バージョン20のmelonJSゲームのコードを書き、必要な非同期の起動など、古いバージョンの古いメソッドを避けられるよう支援します。
melonjs-3d-assets
6.4kエージェントが、glTF、GLB、OBJなどの3DモデルをmelonJSのブラウザゲームに読み込み、スケールとライティングのオプションを使えるよう支援します。
melonjs-3d
6.4kエージェントが、エンジン独自の軸のルールを使い、カメラ、メッシュ、ライト、奥行きを含めて、melonJSのブラウザゲームで3Dまたは2.5Dのシーンを作れるよう支援します。
melonjs-audio
6.4kエージェントが、音声の設定、背景の音楽、音量の制御、空間的な音響を含め、melonJSのゲームに効果音と音楽を追加できるよう支援します。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。