Phaser 4 Arcade Physics
Add movement and collision to a Phaser game with the lightweight Arcade
Physics engine (AABB rectangles and circles only). Targets Phaser 4.2 for
new projects; inspect the installed major before editing an existing project.
When to use
- Use for top-down or platformer movement, velocity/acceleration/gravity, bouncing,
world bounds, and collision/overlap resolution between sprites, groups, and tiles.
- Use when the scene enables
physics: { default: 'arcade' } and code calls
this.physics.add.*, body.setVelocity, or this.physics.add.collider.
When not to use: the Game config, scene structure, asset loading, or
cameras → use phaser-core. Hinges, springs, complex polygons, or stacking
rigid bodies → use Matter physics (a different engine; Arcade and Matter bodies
do not interact). For engine-agnostic feel tuning see physics-tuning.
Core workflow
- Enable the world. Set
physics: { default: 'arcade', arcade: { gravity: {...}, debug: true } } in the game or scene config. Turn debug on while
building to see body outlines and velocity vectors.
- Give a sprite a body. Create it with
this.physics.add.sprite(...) (dynamic)
or this.physics.add.staticImage(...) (static), or attach to an existing object
with this.physics.add.existing(obj).
- Drive it through the body, not by setting
x/y. Use setVelocity,
setAcceleration, gravity, setBounce, and setCollideWorldBounds. The engine
integrates position from velocity each step (already frame-rate independent).
- Resolve interactions.
this.physics.add.collider(a, b) separates bodies;
this.physics.add.overlap(a, b, cb) detects without separating (pickups,
triggers). Pass a callback to react.
- Group many objects. Use
this.physics.add.group() (dynamic) or
staticGroup() (platforms) so one collider call handles all members.
- Check ground contact with
body.onFloor() / body.blocked.down before
jumping. Run with debug: true and confirm bodies, contacts, and bounds.
Patterns
1. Enable Arcade Physics (game config)
const config = {
type: Phaser.AUTO,
width: 800, height: 600,
physics: {
default: 'arcade',
arcade: {
gravity: { x: 0, y: 600 }, // top-down? use { x: 0, y: 0 }
debug: false // true to draw bodies + velocity while building
}
},
scene: [PlayScene]
};
new Phaser.Game(config);
2. Top-down movement (velocity from input)
create() {
this.player = this.physics.add.sprite(400, 300, 'player');
this.player.setCollideWorldBounds(true);
this.cursors = this.input.keyboard.createCursorKeys();
}
update() {
const speed = 220;
const body = this.player.body;
body.setVelocity(0); // reset each frame
if (this.cursors.left.isDown) body.setVelocityX(-speed);
if (this.cursors.right.isDown) body.setVelocityX(speed);
if (this.cursors.up.isDown) body.setVelocityY(-speed);
if (this.cursors.down.isDown) body.setVelocityY(speed);
body.velocity.normalize().scale(speed); // keep diagonals same speed
}
3. Platformer jump (gravity + ground check)
create() {
this.player = this.physics.add.sprite(100, 450, 'player');
this.player.setCollideWorldBounds(true);
// Static platforms: one body each, never moved by collisions.
this.platforms = this.physics.add.staticGroup();
this.platforms.create(400, 568, 'ground');
this.physics.add.collider(this.player, this.platforms);
this.cursors = this.input.keyboard.createCursorKeys();
}
update() {
const // or this.player.body.onFloor()
if (this.cursors.left.isDown) this.player.setVelocityX(-160);
else if (this.cursors.right.isDown) this.player.setVelocityX(160);
else this.player.setVelocityX(0);
if (this.cursors.up.isDown && onGround) this.player.setVelocityY(-450);
}
4. Colliders vs overlaps (separate vs detect)
// Push apart and react: player vs enemies.
this.physics.add.collider(this.player, this.enemies, (player, enemy) => {
this.handleHit(player, enemy);
});
// Detect without pushing: collect coins. The 4th arg is an optional
// process callback returning a boolean to filter pairs before the main callback.
this.physics.add.overlap(this.player, this.coins, (player, coin) => {
coin.disableBody(true, true); // deactivate + hide
this.registry.inc('score', 10);
});
5. A group of moving objects
this.bullets = this.physics.add.group({
defaultKey: 'bullet',
maxSize: 30 // pool size; reuse instead of allocating
});
fire(x, y) {
const bullet = this.bullets.get(x, y); // reuses a dead bullet if available
if (!bullet) return;
bullet.enableBody(true, x, y, true, true);
bullet.setVelocityY(-500);
}
Pitfalls
- Sprite ignores physics → it was added with
this.add.sprite instead of
this.physics.add.sprite (or this.physics.add.existing(obj)), so it has no body.
- Setting
sprite.x directly fights the engine → move dynamic bodies with
setVelocity/setAcceleration. Direct position writes can tunnel through colliders.
- Diagonal movement is faster → independent X and Y velocities add up; normalise
the velocity vector and rescale to the intended speed.
- Platforms get pushed by the player → use a
staticGroup, or set
body.setImmovable(true) on a dynamic platform.
onFloor() is always false → the body needs something to collide with; add the
collider against the ground/platforms before checking, and ensure gravity is on.
- Moved a static body but collisions are stale → static bodies don't auto-sync;
call
body.updateFromGameObject() (or refreshBody() on the game object).
- Collider added every frame → register
collider/overlap once in create,
not in update.
References
- For body anatomy and tuning (drag, bounce, max velocity, custom
setSize/
setCircle/setOffset hitboxes, collision categories/masks, and worldbounds
events), read references/bodies-and-collision.md.
Related skills
phaser-core — game config, scenes, loader, cameras (the prerequisite setup).
physics-tuning — engine-agnostic feel (fixed timestep, tunneling, jitter).
platformer / tower-defense — genres that compose this skill.
level-design — laying out tile/platform geometry these bodies collide with.
1---2name: phaser-arcade-physics3description: Use Phaser 4 Arcade Physics: enable the world, give sprites bodies, set velocity/acceleration/gravity, and resolve collisions with colliders, overlaps, groups, and world bounds. Use when a Phaser game needs movement or collisions — when the user mentions Arcade Physics, this.physics, setVelocity, collider, overlap, gravity, onFloor, or a platformer/top-down controller. For game config, scenes, and the loader use phaser-core.4---5
6# Phaser 4 Arcade Physics
7
8Add movement and collision to a Phaser game with the lightweight **Arcade
9Physics** engine (AABB rectangles and circles only). Targets **Phaser 4.2** for
10new projects; inspect the installed major before editing an existing project.
11
12## When to use
13
14- Use for top-down or platformer movement, velocity/acceleration/gravity, bouncing,
15 world bounds, and collision/overlap resolution between sprites, groups, and tiles.
16- Use when the scene enables `physics: { default: 'arcade' }` and code calls
17 `this.physics.add.*`, `body.setVelocity`, or `this.physics.add.collider`.
18
19**When *not* to use:** the `Game` config, scene structure, asset loading, or
20cameras → use `phaser-core`. Hinges, springs, complex polygons, or stacking
21rigid bodies → use Matter physics (a different engine; Arcade and Matter bodies
22do not interact). For engine-agnostic feel tuning see `physics-tuning`.
23
24## Core workflow
25
261. **Enable the world.** Set `physics: { default: 'arcade', arcade: { gravity:
27 {...}, debug: true } }` in the game or scene config. Turn `debug` on while
28 building to see body outlines and velocity vectors.
292. **Give a sprite a body.** Create it with `this.physics.add.sprite(...)` (dynamic)
30 or `this.physics.add.staticImage(...)` (static), or attach to an existing object
31 with `this.physics.add.existing(obj)`.
323. **Drive it through the body, not by setting `x`/`y`.** Use `setVelocity`,
33 `setAcceleration`, gravity, `setBounce`, and `setCollideWorldBounds`. The engine
34 integrates position from velocity each step (already frame-rate independent).
354. **Resolve interactions.** `this.physics.add.collider(a, b)` separates bodies;
36 `this.physics.add.overlap(a, b, cb)` detects without separating (pickups,
37 triggers). Pass a callback to react.
385. **Group many objects.** Use `this.physics.add.group()` (dynamic) or
39 `staticGroup()` (platforms) so one collider call handles all members.
406. **Check ground contact** with `body.onFloor()` / `body.blocked.down` before
41 jumping. Run with `debug: true` and confirm bodies, contacts, and bounds.
42
43## Patterns
44
45### 1. Enable Arcade Physics (game config)
46
47```js
48const config = {
49 type: Phaser.AUTO,
50 width: 800, height: 600,
51 physics: {
52 default: 'arcade',
53 arcade: {
54 gravity: { x: 0, y: 600 }, // top-down? use { x: 0, y: 0 }
55 debug: false // true to draw bodies + velocity while building
56 }
57 },
58 scene: [PlayScene]
59};
60new Phaser.Game(config);
61```
62
63### 2. Top-down movement (velocity from input)
64
65```js
66create() {
67 this.player = this.physics.add.sprite(400, 300, 'player');
68 this.player.setCollideWorldBounds(true);
69 this.cursors = this.input.keyboard.createCursorKeys();
70}
71
72update() {
73 const speed = 220;
74 const body = this.player.body;
75 body.setVelocity(0); // reset each frame
76 if (this.cursors.left.isDown) body.setVelocityX(-speed);
77 if (this.cursors.right.isDown) body.setVelocityX(speed);
78 if (this.cursors.up.isDown) body.setVelocityY(-speed);
79 if (this.cursors.down.isDown) body.setVelocityY(speed);
80 body.velocity.normalize().scale(speed); // keep diagonals same speed
81}
82```
83
84### 3. Platformer jump (gravity + ground check)
85
86```js
87create() {
88 this.player = this.physics.add.sprite(100, 450, 'player');
89 this.player.setCollideWorldBounds(true);
90
91 // Static platforms: one body each, never moved by collisions.
92 this.platforms = this.physics.add.staticGroup();
93 this.platforms.create(400, 568, 'ground');
94 this.physics.add.collider(this.player, this.platforms);
95
96 this.cursors = this.input.keyboard.createCursorKeys();
97}
98
99update() {
100 const onGround = this.player.body.blocked.down; // or this.player.body.onFloor()
101 if (this.cursors.left.isDown) this.player.setVelocityX(-160);
102 else if (this.cursors.right.isDown) this.player.setVelocityX(160);
103 else this.player.setVelocityX(0);
104
105 if (this.cursors.up.isDown && onGround) this.player.setVelocityY(-450);
106}
107```
108
109### 4. Colliders vs overlaps (separate vs detect)
110
111```js
112// Push apart and react: player vs enemies.
113this.physics.add.collider(this.player, this.enemies, (player, enemy) => {
114 this.handleHit(player, enemy);
115});
116
117// Detect without pushing: collect coins. The 4th arg is an optional
118// process callback returning a boolean to filter pairs before the main callback.
119this.physics.add.overlap(this.player, this.coins, (player, coin) => {
120 coin.disableBody(true, true); // deactivate + hide
121 this.registry.inc('score', 10);
122});
123```
124
125### 5. A group of moving objects
126
127```js
128this.bullets = this.physics.add.group({
129 defaultKey: 'bullet',
130 maxSize: 30 // pool size; reuse instead of allocating
131});
132
133fire(x, y) {
134 const bullet = this.bullets.get(x, y); // reuses a dead bullet if available
135 if (!bullet) return;
136 bullet.enableBody(true, x, y, true, true);
137 bullet.setVelocityY(-500);
138}
139```
140
141## Pitfalls
142
143- **Sprite ignores physics** → it was added with `this.add.sprite` instead of
144 `this.physics.add.sprite` (or `this.physics.add.existing(obj)`), so it has no body.
145- **Setting `sprite.x` directly fights the engine** → move dynamic bodies with
146 `setVelocity`/`setAcceleration`. Direct position writes can tunnel through colliders.
147- **Diagonal movement is faster** → independent X and Y velocities add up; normalise
148 the velocity vector and rescale to the intended speed.
149- **Platforms get pushed by the player** → use a `staticGroup`, or set
150 `body.setImmovable(true)` on a dynamic platform.
151- **`onFloor()` is always false** → the body needs something to collide with; add the
152 `collider` against the ground/platforms before checking, and ensure gravity is on.
153- **Moved a static body but collisions are stale** → static bodies don't auto-sync;
154 call `body.updateFromGameObject()` (or `refreshBody()` on the game object).
155- **Collider added every frame** → register `collider`/`overlap` once in `create`,
156 not in `update`.
157
158## References
159
160- For body anatomy and tuning (drag, bounce, max velocity, custom `setSize`/
161 `setCircle`/`setOffset` hitboxes, collision categories/masks, and `worldbounds`
162 events), read `references/bodies-and-collision.md`.
163
164## Related skills
165
166- `phaser-core` — game config, scenes, loader, cameras (the prerequisite setup).
167- `physics-tuning` — engine-agnostic feel (fixed timestep, tunneling, jitter).
168- `platformer` / `tower-defense` — genres that compose this skill.
169- `level-design` — laying out tile/platform geometry these bodies collide with.