Skip to content
This repository has been archived by the owner on Nov 10, 2024. It is now read-only.

Commit

Permalink
mention fixed timestep issues wrt replicon
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ committed Oct 6, 2023
1 parent 2d6d6a3 commit ddd8efe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
42 changes: 40 additions & 2 deletions REPLICON_INTEGRATION.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
## Integrating bevy_timewarp with bevy_replicon

I'm using [bevy_replicon](https://crates.io/crates/bevy_replicon) in my game, alongside bevy_timewarp.
You can use custom deserializers with replicon to write updates into the `ServerSnapshot` buffer like this:
You can use custom deserializers with replicon to write updates into the `ServerSnapshot` buffer.

### Custom timewarp deserializer example

This is for [bevy_xpbd](https://crates.io/crates/bevy_xpbd_2d)'s `Rotation`` component:
This is for [bevy_xpbd](https://crates.io/crates/bevy_xpbd_2d)'s `Rotation` component:

```rust
pub fn deserialize_rotation_timewarp(
Expand Down Expand Up @@ -35,3 +35,41 @@ pub fn serialize_rotation(
bincode::serialize_into(&mut cursor, &comp.cos())
}
```

### Fixed Timestep

You'll need to set replicon's TickPolicy to manual, and advance the replicon tick on the server in line with your tick counter in your fixed timestep.

```rust
#[cfg(feature = "is_server")]
{
app.add_plugins(
ReplicationPlugins
.build()
.disable::<ClientPlugin>()
.set(ServerPlugin::new(TickPolicy::Manual)),
);
// we set the tick policy to manual, and want to do sending AFTER physics every fixed step,
// so clients get the updated-by-physics-systems versions of pos, vel, etc.
app.configure_set(
FixedUpdate,
ServerSet::Send.after(SpacepitSet::AfterPhysics),
);
}
```

And scheduled in your fixed timestep:

```rust
pub fn frame_inc_and_replicon_tick_sync(
mut game_clock: ResMut<GameClock>, // your own tick counter
mut replicon_tick: ResMut<bevy_replicon::prelude::RepliconTick>,
) {
// advance your tick and replicon's tick in lockstep
game_clock.advance(1);
let delta = game_clock.frame().saturating_sub(replicon_tick.get());
replicon_tick.increment_by(delta);
}
```

With this setup, the `RepliconTick` you receive in the deserialize functions will match up with your game's fixed timestep tick.
7 changes: 7 additions & 0 deletions src/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ impl TimewarpConfig {
pub fn rollback_window(&self) -> FrameNumber {
self.rollback_window
}
pub fn is_within_rollback_window(
&self,
current_frame: FrameNumber,
target_frame: FrameNumber,
) -> bool {
target_frame > current_frame - self.rollback_window
}
}

/// Updated whenever we perform a rollback
Expand Down

0 comments on commit ddd8efe

Please sign in to comment.