Skip to content

Commit

Permalink
basic serialization example
Browse files Browse the repository at this point in the history
  • Loading branch information
Vrixyz committed Aug 8, 2024
1 parent 74611c9 commit aa0ee14
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions bevy_rapier2d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ rapier2d = { version = "0.21", features = ["serde-serialize"] }
serde_json = "1.0"
bevy-inspector-egui = "0.25.1"
bevy_egui = "0.28.0"
rapier2d = { version = "0.22", features = ["serde-serialize"] }
serde_json = "1.0"

[package.metadata.docs.rs]
# Enable all the features when building the docs on docs.rs
Expand Down
36 changes: 36 additions & 0 deletions bevy_rapier2d/examples/serialization.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//! Example for RapierContext serialization, run with `--features serde-serialize`.

use bevy::prelude::*;
use bevy_rapier2d::prelude::*;

/// Note: This will end up in duplication for testbed, but that's more simple.
mod joints2;

fn main() {
App::new()
.insert_resource(ClearColor(Color::srgb(
0xF9 as f32 / 255.0,
0xF9 as f32 / 255.0,
0xFF as f32 / 255.0,
)))
.add_plugins((
DefaultPlugins,
RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0),
RapierDebugRenderPlugin::default(),
))
.add_systems(Startup, (joints2::setup_graphics, joints2::setup_physics))
.add_systems(PostUpdate, print_physics)
.add_systems(Last, quit)
.run();
}

pub fn print_physics(context: Res<RapierContext>) {
println!(
"{}",
serde_json::to_string_pretty(&(*context)).expect("Unable to serialize `RapierContext`")
);
}

fn quit(mut exit_event: EventWriter<AppExit>) {
exit_event.send(AppExit::Success);
}
2 changes: 2 additions & 0 deletions src/plugin/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct RapierContext {
/// The island manager, which detects what object is sleeping
/// (not moving much) to reduce computations.
pub islands: IslandManager,
// FIXME: This shoulnd not be skipped.
#[cfg_attr(feature = "serde-serialize", serde(skip))]
/// The broad-phase, which detects potential contact pairs.
pub broad_phase: DefaultBroadPhase,
/// The narrow-phase, which computes contact points, tests intersections,
Expand Down

0 comments on commit aa0ee14

Please sign in to comment.