DetPhysDocs日本語

Troubleshooting desyncs (desync debugger)

Desyncs are repaired automatically first

When the two peers' states differ, the hash check finds it (in real time on every confirmed tick, in turn-based play at the end of every turn). The host's (peer 0) state is then sent to the guest and replaces its state (resync), so the match goes on.

  • Real time: detphys_rt_frame.flags has DETPHYS_RT_FRAME_DESYNC (found) and DETPHYS_RT_FRAME_RESYNCED (the guest was replaced). Counts are in detphys_rt_get_stats: hashes_mismatched, desyncs, resyncs.
  • Turn-based: the turn's result has DETPHYS_TURN_FLAG_DESYNC and DETPHYS_TURN_FLAG_RESYNCED in flags.

A desync is a sign that a rule (4. Determinism rules) is broken somewhere. The desync debugger points at the place in one go. (If the peers do not connect at all, the problem comes before any desync; see When peers do not connect.)

Turning the desync debugger on

It is off by default. When off, nothing is recorded or sent (the bytes on the wire, the frame time and the hashes are the same as without it).

detphys_debug_session_config dbg;
detphys_debug_session_config_default(&dbg);
dbg.enabled = 1;                 // turn it on on both peers
dbg.ticks_before = 30;           // range to exchange: from 30 ticks before the desync tick T up to T (default)
detphys_rt_set_debug(session, &dbg);      // turn-based: detphys_turn_set_debug(session, &dbg)
Setting (detphys_debug_session_config)DefaultMeaning
enabled01 = record, and exchange recordings on a desync
flagsDETPHYS_DEBUG_REC_DEFAULTAlso record Box2D's deep hash (REC_BOX2D_HASH; Box3D's state hash in 3D) and body types, shapes and tags (REC_META). Without them recording costs about half
ticks_before30How many ticks before the desync tick T to exchange
ring_ticks0 (auto)Length of the recording ring. Auto: real time = ticks_before + prediction window + 1 + 2 seconds; turn-based = ticks_before + turn limit + 2
max_bodies0 (auto)Bodies per tick that fit in a recording. When too small it doubles and starts over (earlier recordings are lost)
max_reports4Maximum number of exchanges (later desyncs are only counted)
timeout_ms10000How long to wait for the other peer's recording (FAILED here if the other peer has it off)
scene_id / label0 / emptyMarks on the recording (0 = hash of the session settings, empty = "host" / "guest")
  • Turn it on on both peers. If the other peer has it off, its recording never arrives and the report fails after timeout_ms.
  • It can be called at any time (called at tick 0 it records from the state right after creation).
  • Engine layers: in Unreal, use the sessions' SetDebug, GetDesyncReportCount and GetDesyncReport (from Blueprint too; FDetPhysDebugSettings, FDetPhysDesyncReport). Unity uses the session's SetDebug, TryGetDebugReport and DebugBundle; Godot uses the real-time session's set_debug, get_debug_report and get_debug_bundle (7. API reference).
  • 3D: in 3D sessions (detphys_rt_create3d, detphys_turn_create_rule3d) turn it on the same way with detphys_rt_set_debug / detphys_turn_set_debug (with a core where detphys_debug_abi_version() is 3 or later). What differs (fields, the meaning of the deep hash, the recording format) is in 3D worlds and sessions.

Cost (when on)

ConditionRecordingOne frame
Real time, 77 bodies (PC, i7-12700F)5 µs / tick+10–15%
Real time, 200 bodies, up to 10 re-simulated ticks (PC)12 µs / tick0.13 → 0.15 ms
Same (Galaxy S9)20 µs / tick0.19 → 0.22 ms
Turn-based1.2 µs / tick–
3D real time, about 115 bodies (PC)7.4–7.8 µs / tick–
3D turn-based, up to 12 bodies (PC)1.5 µs / tick–
3D real time, about 70 bodies, two processes PC ↔ S9 (PC side)15–19 µs / tick–
Same (Galaxy S9 side)about 175 µs / tickp99 7.1–9.3 → 8.9–10.6 ms

We recommend on during development and testing, off in production (or on for testers only).

What happens on a desync

  1. When the hash check finds a mismatch, the host picks the desync tick T and the range [T − ticks_before, T] (only after the previous resync).
  2. Both peers send their own recordings (in real time, confirmed ticks only) to each other in small pieces, so play traffic is not held up. The resync runs at the same time as usual.
  3. When both recordings are there, both peers compare them with A = host and B = guest and hold the same report. In that frame DETPHYS_RT_FRAME_DEBUG is set (turn-based: DETPHYS_TURN_FLAG_DEBUG in that turn's result). Between a PC and a Galaxy S9 the report was ready about 0.3 s after the desync was found (3D, two peers in one process with 20–40 ms one way and 5% loss: 0.4–0.6 s).
  4. If a peer rejoins or restarts during an exchange, it fails (DETPHYS_DEBUG_FAIL_LINK).

Reading the report

uint32_t n = detphys_rt_debug_count(session);           // number of reports (including WAITING)
detphys_debug_session_report rep = { sizeof rep };
detphys_debug_diff diffs[16];
detphys_rt_debug_get(session, n - 1, &rep, diffs, 16, NULL, 0);
if (rep.status == DETPHYS_DEBUG_EXCHANGE_READY) {
    // rep.report.cause / first_tick, rep.first_diff.body / field / a / b / ulp
}
  • status: WAITING (exchanging), READY (report done) or FAILED (fail_reason: TIMEOUT, LINK, RECORD, COMPARE).
  • report.first_tick: the first tick where anything differs. input_first_tick, state_first_tick, body_first_tick, box2d_first_tick and hash_first_tick tell which part differed first (DETPHYS_DEBUG_NONE if it never did).
  • first_diff: the first differing body ID, field (x y cos sin vx vy w, …; in 3D x y z qx–qw vx vy vz wx–wz), both raw values and the distance in ULPs.
  • report.diff_bodies, diff_values: how many bodies and fields differ at that tick. Pass spread to get "number of differing bodies" per tick and watch the desync spread.

Causes (report.cause) and what to suspect

CauseNameMeaningSuspect
DETPHYS_DEBUG_CAUSE_INPUTinputThe inputs differ (before the physics)How inputs are passed and quantized (quantizing again on the receiving side?), lost button presses
DETPHYS_DEBUG_CAUSE_STATEstateSame inputs and physics, but the game state words differRule code (random numbers, iteration order, clocks, values kept on the rule object; rules 5–8 in 4. Determinism rules)
DETPHYS_DEBUG_CAUSE_SETUPsetupBody count, types, shapes or tags differHow bodies are created and removed (order, conditions, shape registration order). At tick 0: the initial setup
DETPHYS_DEBUG_CAUSE_PHYSICSphysicsSame inputs and shapes, but the physics values differFloat math in rules, build settings (fast-math, FMA), a core you built yourself
DETPHYS_DEBUG_CAUSE_HIDDENhiddenAll visible values are equal, only Box2D's deep hash (Box3D's state hash in 3D) differsHidden state such as contacts; it shows in the values from the next tick. Suspect the same as physics
DETPHYS_DEBUG_SAME / NO_OVERLAPsame / no_overlapEverything equal / no common ticksWiden the range (ticks_before)

Example: against a peer whose core was built with fast-math, both peers' reports pointed at "tick 0, body 1, x, 1 ULP, physics" (a mm → m division by 1000 had become a multiplication by 0.001).

Saving recordings to a file (bundle)

uint32_t size = 0;
detphys_rt_debug_bundle(session, index, NULL, 0, &size);       // returns DETPHYS_ERR_CAPACITY with the size
void* buf = malloc(size);
detphys_rt_debug_bundle(session, index, buf, size, &size);
detphys_debug_write_file("desync_0.dndb", buf, size);          // UTF-8 path (non-ASCII paths work on Windows too)

A bundle (DNDB) holds both peers' recordings (DNDR) and the report. It can be made even when WAITING or FAILED, from what has arrived. Handy for testers to send you.

Comparing recordings again

To read a tester's bundle (or two recordings) again on your side, compare the two recordings in code:

  • C: detphys_debug_bundle_info_get gives where the host's and the guest's recordings are in a bundle; detphys_debug_compare compares two recordings. detphys_debug_file_info_get reads a recording's header and tick range.
  • Unity (C#): DetPhysDebug.Compare(a, b) (A = the reference, for example the host) returns a DetPhysDebugComparison with the same fields as the report: the first diverged tick, the body, the field, both values, the ULP distance and the cause. DetPhysDebug.TryGetFileInfo reads a recording's header.
  • Read the result the same way as the report (Reading the report, the cause table).
  • When you contact support about a desync, attach the bundle file (.dndb).
  • 3D recordings and bundles are compared the same way (see 3D worlds and sessions).

Recording without a session

If you run the ticks yourself, use the recording parts directly (detphys_debug.h): detphys_debug_recorder_size → allocate a buffer → detphys_debug_recorder_init → every tick detphys_debug_record(rec, world, inputs, state) → detphys_debug_export to DNDR bytes → compare with detphys_debug_compare (Unity: DetPhysDebug.Compare). Recording only reads the world and never changes the hash. Recording the same tick again (after a rollback) drops that tick and later ones and records them anew.

3D worlds and sessions

With 3D worlds (detphys3d.h), recording, exchanging, reports and bundles work the same way as in 2D. Check detphys_debug_abi_version() >= 3 to know that 3D sessions accept the debugger (the rt and turn versions did not change). What differs:

  • Fields: a 3D body has 13 fields: position x y z, orientation (quaternion) qx qy qz qw, velocity vx vy vz and angular velocity wx wy wz. DETPHYS_DEBUG_FIELD_X, Y, VX and VY keep their 2D numbers; Z to WZ are 12–20 (cos, sin and w are not used). Reports (first_diff.field) use these names.
  • Deep hash: in 3D, box2d_hash, box2d_first_tick, and DETPHYS_DEBUG_REC_BOX2D_HASH hold Box3D's state hash (the same value as detphys3d_world_engine_hash). The names are kept from 2D. Read the hidden cause as "only Box3D's hash differs".
  • Hash: the recorded hash is the same value as detphys3d_world_hash (the one used for the hash check).
  • Types and shapes: body types are DETPHYS3D_BODY_* and shapes are box, sphere, capsule and hull (DETPHYS3D_SHAPE_*); where 2D has a mesh index (detphys_debug_diff.mesh), 3D has the hull index.
  • Recording format: 3D recordings are DNDR format 2 and have DETPHYS_DEBUG_FILE_3D in detphys_debug_file_info.flags. 2D recordings stay in format 1. Comparing a 2D recording with a 3D one returns DETPHYS_ERR_FORMAT.
  • Size: in 3D real time (about 115 bodies, PC) one tick was about 7.5 KB and the exchanged recordings were 166–222 KB per peer (31 ticks). 3D turn-based (up to 12 bodies) was 384 bytes per tick.
  • Example: comparing a 3D recording from a core built with fast-math against the reference recording pointed at "tick 0, body 1 (a box), x, 1 ULP", as in 2D.
  • 3D recordings were byte-identical across builds (msvc, clang-cl, mingw, debug), and a recording written on a Galaxy S9 (android-arm64) was the same as the PC reference recording (61 ticks, no differences).
  • 3D matches over two processes (UDP; the 3D push scene with about 70 bodies [40 boxes, 8 spheres, 12 hulls and more], 600 ticks at 60 Hz, injected 20–40 ms one way, 5% loss and 1% duplicates, with a guest value nudged by 1 ULP at ticks 150 and 400): two processes on one PC and PC ↔ S9 (S9 as the guest and as the host; UDP over Wi-Fi) all gave the same report in both processes, pointing at both nudges in one go (tick 150, body 68, x, 1 ULP; tick 400, body 76, x, 1 ULP). Outside the nudged ranges, the confirmed tick hashes matched on all 600 ticks.
  • Against a core built with fast-math, both processes' first reports also said "tick 0, body 1, x, 1 ULP" (in a scene without hulls; see the note below).
  • Cost (table above): on an S9 recording takes about 175 µs per tick, much more than on a PC (15–19 µs). PC ↔ S9, the S9's one-frame p99 went from 7.1–9.3 ms with the debugger off to 8.9–10.6 ms with it on (the S9 was not pinned to its big cores; measured at a 60 Hz wall clock). Traffic goes from 34–39 B to 497–795 B per tick, but that includes the recording exchange on a desync (about 300,000 B); without a desync nothing is exchanged.
  • Note: 3D sessions include the hull contents (checksums) in the config check. A peer whose build computes hulls differently (fast-math and so on) does not connect, and both report "shapes, first differing shape 0" (When peers do not connect).

Recording 3D without a session

Create the recorder with dimensions = 3 in the recording settings (cfg.dimensions = 3 after detphys_debug_config_default) and call detphys_debug_record3d(rec, world3d, inputs, state) every tick. The rest is the same as in 2D: detphys_debug_export → detphys_debug_compare. Read bodies with detphys_debug_file_bodies3d (detphys_debug_body3d: 13 raw values and the hull index). Mixing up the 2D and 3D calls returns DETPHYS_ERR_ARGUMENT when recording and DETPHYS_ERR_FORMAT when reading.

With moving concave shapes

detphys_kinematic_check checks that the kinematic bodies in the world are bit-identical to the values rebuilt from their motion functions (0 = all equal). If it is not 0 after a rollback, a motion callback depends on something other than the tick (a clock, a variable outside the game state). Check 3D tables the same way with detphys3d_kinematic_check (raw bits of position, orientation, velocity and angular velocity). In 3D sessions, peers whose tables differ (motions, split settings) do not connect (the table is part of the config check). Table contents do not appear in recordings or reports. When they do not connect, diff_parts in detphys_tag_info shows kinematic (When peers do not connect).

When peers do not connect (config check mismatch)

If a session keeps waiting for its peer (rt: DETPHYS_RT_FRAME_CONNECTED never appears; turn-based: stays CONNECTING) even on the same LAN and port, first suspect that, before any determinism problem, the two peers are not running the same thing. Before connecting, peers compare a config check tag (built from the settings, rule, shapes, hulls, kinematic tables and core version; rt: the config check value, turn-based: the setup hash) and drop packets from a peer whose tag differs, because running different things would diverge from tick 0. Cores with rt version 7 / turn version 9 count such packets and exchange the tag contents (one hash per part) with that peer, so both peers learn which part differs. When both are new, this usually takes 1–2 round trips (tens of ms) from the first packet.

detphys_tag_info ti = { sizeof ti };
detphys_rt_get_tag_info(session, &ti);                  // turn-based: detphys_turn_get_tag_info. Reading every frame is fine
if (ti.flags & DETPHYS_TAG_INFO_ACTIVE) {               // packets come from a peer with a different tag, nothing yet from a matching one
    if (ti.flags & DETPHYS_TAG_INFO_REMOTE_KNOWN) {     // the peer's contents arrived = we know which part differs
        for (uint32_t p = 1; p < (1u << DETPHYS_TAG_PART_COUNT); p <<= 1)
            if (ti.diff_parts & p) printf("config differs from the peer: %s\n", detphys_tag_part_name(p));
        if (ti.diff_parts & DETPHYS_TAG_PART_SHAPES) printf("first differing shape: %d\n", ti.first_shape_diff);
    } else {
        printf("config differs from the peer (part unknown; the peer's core may be older)\n");
    }
}
  • Flags: rt sets DETPHYS_RT_FRAME_TAG_MISMATCH in detphys_rt_frame.flags (while nothing has arrived from a peer with a matching tag, and when a mismatching packet arrived during that call). Turn-based sessions: check DETPHYS_TAG_INFO_ACTIVE in detphys_tag_info.flags. The fields are described in 7. API reference.
  • Session behavior: rt sessions keep waiting (they never give up; the app decides whether to stop). Turn-based sessions still go to ERROR on a HELLO / WELCOME with a different setup hash, and once the peer's contents arrive the differing parts are appended to the detphys_turn_error text, like "(違う所: shapes (first shape 0))" (the error text is in Japanese; 違う所 = differing part).
  • Engine layers and samples: Unity reads the same contents with TryGetTagInfo, Godot with get_tag_info() and the tag_mismatch signal, Unreal with GetTagInfo (7. API reference). When the samples see ACTIVE before connecting, they wait for the peer's contents, show "Peer has different settings (differs: rule)" and stop connecting. Whether to stop is the game's choice (the core only reports).
  • Peers that differ only in session_id (turn-based): no ERROR; the session counts and reports it (the differing part is config) and keeps waiting, so stray old packets from another match on the same port do not break the current match.
  • An older core on the other side: cores up to rt version 6 / turn version 8 do not send their tag contents, so the differing part is unknown. If ACTIVE stays on without REMOTE_KNOWN, suspect the peer's core version.
  • REMOTE_KNOWN with diff_parts = 0: all part hashes are equal and only the whole tag differs (only when the 32-bit folded part hashes collide; practically never).
Differing partWhat differsHow to fix
versionThe core's check / protocol version, or 2D vs 3DRebuild both with the same core version (same package, plugin, APK). 2D and 3D sessions never connect to each other
configsession_id, tick rate, substeps, gravity, 3D contact tuning, turn-based first mover and tick limit per turnPass the same settings on both. Use the same session_id on both for each match
rulerule_id, input and state word counts, params (settings of the built-in push rule such as the box count are params too)Same rule, same version of the rule code and same settings on both (for the samples: the same sample and the same moving field)
shapes2D: contents, count and order of the registered shapes (.dnshape) / 3D: hull contents (checksums), vertex counts, count and orderIn 2D, register the same .dnshape files in the same order. In 3D, if the shape data is the same but this still differs, suspect a build that computes hulls differently (fast-math, FMA, a core you built yourself; 4. Determinism rules). first_shape_diff is the first differing shape
kinematicThe kinematic table (whether one was passed, flags, motions, split settings)Pass the same table on both (same motions in the same order, same split settings)

Measured (two processes on one PC, UDP, 20–40 ms delay, 5% loss): with only the guest changing the push box count in 2D rt (rule), only the guest moving hull 0 by 1 mm in 3D rt (shapes, shape 0), a different tick limit per turn in turn-based play (config; both go to ERROR with "違う所: config" in the text) and only the session_id differing in turn-based play (config, no ERROR), both processes learned the differing part within 0.1 s of starting (sending their contents 1–2 times per side). On a one-process loopback with 10 ms one way, both peers learned it in 30–31 ms (three one-way trips).

DetPhys is not affiliated with Box2D or Box3D and is not endorsed or recommended by Box2D, Box3D or their author. It uses Box2D v3 and Box3D, both under the MIT license, as its physics.

LicensesMatches the current versions (2026-11)

© 2026 PetafProductsContactPrivacy

This site uses no analytics or tracking.

↑↓ to choose, Enter to open, Esc to close