Hey everyone,
I’ve spent hours researching “cross-platform determinism” in Unity and concluded that achieving this isn’t feasible without significant modifications to Unity Physics.
However, I have one last question in mind.
Since we can build a game using IL2CPP to WebAssembly and WebAssembly is cross-platform deterministic (if we disable features that may cause nondeterminism, as explained here: WebAssembly Deterministic Execution), would it be possible to achieve a deterministic game using WASM?
Thanks in advance for your insights!
Not “can” but “do”. There is no option here. Web builds are IL2CPP builds for/with WebAssembly.
That wouldn’t change anything because WebAssembly still runs the same Unity Physics engine.
However, you still have two options:
- Do not use Physics. Depending on the type of features you require implementing a simple, deterministic physics engine is not that difficult.
- Use Physics but run additional code that fixes any non-determinism, if possible (depends on the features and the issue at hand). There’s also the project settings flag for “enhanced determinism” whatever that means.
Example case for #2: if you run into small deviations of physics forces pushing bodies to slightly different positions, you could define a threshold within which the position is regarded as “synchronized”. If it’s beyond the threshold you can take corrective action. This would work with an authoritative server but might not be feasible for something like a playback of game actions recorded on a different platform.
1 Like
Thanks for the quick answer!
So what about the “classical” Unity 3D Engine PhysX 
I read here that:
PhysX is guaranteed deterministic on same machine, same builds with Enable Enhanced Determinism checked. It is not deterministic between different CPU vendors, like Intel vs. AMD (likely due to SIMD differences), and definitely not deterministic between platforms (x86 vs ARM)
If we are running PhysX inside WebAssembly does it stay consistent (assuming the same start state and game objects ordering) ?
PS: You guessed it, we don’t want any server at all. We wanted to playback a set of inputs recorded on a different platform.
The PhysX code is still the same, WebAssembly can’t magically fix any non-determinism caused by it.
But I haven’t considered one thing: WebAssembly in itself is a platform, so the game is web only and anything cross-platform is actually happening on the “outside” from the game’s perspective, therefore it shouldn’t matter if the browser is on ARM Windows or x64 Android or Intel Mac. Buuuuuut I wouldn’t trust that one bit. Primarily because every browser is different and I wouldn’t trust Firefox to run a WebAssembly in precisely the same way as Chrome even on the same platform.
Also, it isn’t physics that’s not fully deterministic across difference CPU/devices, it’s floating-point usage itself which applies to absolutely everything including your scripts. Everyone mentions physics because it’s good at showing differences but if you were to modify Transform poses yourself with your own algorithm, you’d see they wouldn’t be in an identical pose on all machines.
It’s important to know this, assuming you didn’t already.
3 Likes
Thank you both very much for your answers.
It’s nice for once to see a very responsive community forum 
Hey guys,
Following our previous conversation, do you think it’s possible (not out-of-the-box) to compile a “Dedicated Server” build into WASM? I tried to do it using Ecmascripten on the Il2CppOutputProject but to be honest I’m kinda lost with all the parameters 
I’ve also tried to see how it was done inside Bee.Toolchain.Emscripten but withtout more success.
Thanks in advance!
No idea, but it may be pointless to begin with. 
WebAssembly runs in a browser, so WebSockets is required, which does not support incoming connections because the browsers don’t allow incoming connections for security reasons.
Unless there’s ways to run WebAssembly outside a browser environment/sandbox …
Yes it’s possible, there is a lot of Wasm runtimes (GitHub - appcypher/awesome-wasm-runtimes: A list of webassemby runtimes)
We don’t need networking at all, we just want to run a “Dedicated Server” to take advantage of the headless mode (i.e No WebGL dependency for example)