Hi everyone, I’m working on a Phaser 3 game and I’m trying to build a deterministic enemy movement system based on patterns.
Each enemy follows a pattern like this:
interface PatternStep {
deltaX: number;
deltaY: number;
rotation: number;
duration: number; // how long this step should take
wait: number;
}
Each step moves the enemy by (deltaX, deltaY) tiles and must complete exactly in duration time.
I need multiple enemies to stay perfectly synchronized (like a train / formation), while also respecting the exact duration of each step.
Right now I have several enemies on the map running the same pattern.
They should move:
-
same path
-
same timing
-
same step transitions
-
no drift over time
The problem
I tried several approaches:
-
progress interpolation (
Linear(start, target, progress)) -
speed = distance / duration
-
fixed tick loop (40–60 FPS)
-
manual update loop
But I always get one of two problems:
-
They move for the correct duration, but desync over time
-
They stay synchronized, but don’t finish the movement in the exact duration
This is critical because some levels require perfect timing (train-like movement, rhythm-based dodging).
Example of my current approach
const targetX = this.startX + step.deltaX * PIXELS_MULTIPLIER;
const targetY = this.startY + step.deltaY * PIXELS_MULTIPLIER;
const speedX = step.deltaX / step.duration;
const speedY = step.deltaY / step.duration;
this.x += speedX;
this.y += speedY;
reachX = speedX >= 0 ? this.x >= targetX : this.x <= targetX;
reachY = speedY >= 0 ? this.y >= targetY : this.y <= targetY;
I also run updates using a fixed loop:
const loop = 1000 / 40;
this.time.addEvent({
delay: loop,
loop: true,
callback: () => {
this.enemies.children.entries.forEach(e => e.update());
}
});
What I want
A clean architecture for:
-
deterministic movement
-
exact duration per step
-
perfect sync between enemies
-
no drift over time
-
no tween desync
Basically something like a fixed-step simulation or lockstep movement.
Question
What is the correct way to implement deterministic, synchronized movement in Phaser 3?
Should I:
-
use my own fixed timestep loop?
-
ignore
deltaand rely on ticks? -
accumulate elapsed time per step?
-
use a global clock for all enemies?
Any architecture advice or examples would be extremely helpful.