Loss of determinism when using collider.setRotationWrtParent or rigidBody.setRotation
- Dominant language
- Rust
- Stars
- 5.7k
- Forks
- 387
- Avg merge
- 4d 23h
- Merged PRs (30d)
- 6
Description
I have observed that the Rapier.js physics simulation loses determinism when using the `collider.setRotationWrtParent` and `rigidBody.setRotation` methods. Specifically, even when starting from the same initial state (restored via World.restoreSnapshot), the simulation outcomes differ every time these methods are applied. This behavior does not occur when only restoring the world snapshot without applying these methods.
#### Steps to Reproduce
1. Restore the world from a snapshot:
``` javascript
const world = RAPIER.World.restoreSnapshot(base64ToUint8Array(worldData.world));
```
2. Apply one of the following methods:
- For a rigid body:
``` javascript
const rigidBody = world.rigidBodies.get(rigidBodyHandle);
rigidBody.setRotation(0); // Breaks determinism
```
- For a collider:
``` javascript
const collider = world.colliders.get(colliderHandle);
collider.setRotationWrtParent(0); // also breaks determinism, even when the collider is already attached to a rigid body. This behavior contradicts the documentation, which states: "Does nothing if this collider isn't attached to a rigid-body." However, based on my observations, the method appears to have an effect even when the collider is attached, potentially introducing non-deterministic behavior.
```
3. Run the simulation multiple times and observe that the results differ each time.
#### Expected Behavior
The simulation should produce identical results every time it is run from the same initial state, regardless of whether `collider.setRotationWrtParent` or `rigidBody.setRotation` is used.
#### Actual Behavior
When `collider.setRotationWrtParent` or `rigidBody.setRotation` is called, the simulation outcomes vary between runs, breaking determinism.
#### Environment
- Rapier.js version: `@dimforge/rapier2d-compat` `0.14.0`
- Browser: Chrome 132.0.6834.162 (Official Build) (x86_64)
- Operating System: macOS
#### Additional Notes
It seems that these methods might introduce non-deterministic behavior due to floating-point precision issues or internal state changes that are not synchronized properly. This could be related to how the physics engine handles rotations internally.
Contributor guide
Assessment
This issue has not been assessed yet.