AI CHOREOGRAPHY / STUDY / LEVEL 7 OF 7
Dancing
together.
Level 1 said every player sees the same pose because it depends only on the time. That is true only if every device agrees on the time. Real clocks disagree by milliseconds to seconds, so the game elects one headset as the clock owner and everyone else follows it. Then each player reports "I hit pose k", and each device decides whether the whole team hit it. This page simulates two devices and the network between them.
Two devices
A owns the clock; B follows itDevice A · first headset
Device B · second player
The network and the clocks
make them disagreeThe players
both dance perfectly, unless you say otherwiseThe shared clock
coachClock.jsThe page runs this exact file. When a new sample arrives, B remembers it together with its own monotonic clock (performance.now, which never jumps). Between samples, B's time is the sample plus the time that has passed since.
Where the scene uses it
coach.js① Pick the clock owner and broadcast the clock; ② report my result for each pose; ③ remember every report heard, and judge each finished pose once every active player's result for it has arrived. This page runs a copy of ② and ③; the waiting rule itself is activePlayers and teamResult in scoreRules.js, which the page imports.
What you should be able to explain
- One clock owner. Device clocks drift and can be set wrong. Instead of trusting each one, the game picks one headset (the first by id) and everyone follows its time. This is simple clock synchronization.
- Following a sample. B cannot receive a message every frame, so it keeps the last sample and adds the time passed on its own monotonic clock. B is then behind A by about the network latency, since each sample is already old when it arrives. At 80 ms that is a small fraction of a 600 ms beat.
- Reports and the team judgement. Each device sends
{k, ok}for every pose and remembers every report it hears. When all the players who have been taking part have a result for a pose, the device judges it: all good means the team combo grows; two or more good results mean a shared hit. - The bug, and the fix. The game used to judge a pose a fixed 0.3 beat (180 ms in Dance) into the next one, with whatever had arrived. But B's clock runs about one latency behind A's, and B's report then needs another latency to travel: at 400 ms that is 0.8 s, so A judged every pose on its own result alone, counted B's misses as hits, and the two combos drifted apart. Now a pose waits until every active player's result is in (
teamResultreturnsnullwhile waiting), and gives up after 1.8 beats, still inside the next pose, counting a result that never came as a miss. Tick "use the old rule" to see the bug again.
Exercises: ① Untick "B follows A's shared clock" with a 0.8 s clock error. What happens to the pose numbers? Tick it again. ② Set latency to 300 ms: how far is B's clock behind A's now? Is it still a problem for the pose? ③ Tick "B misses every third pose" and set 400 ms latency. With "use the old rule" ticked, compare the two combos and the "reports" count in each history: which device is right? Untick it: do the combos agree now? What does a player lose with the fixed rule? (Hint: when does the reward arrive?)