Expand description

Bevy Component Mirrors

Bevy tracking Latest version MIT/Apache 2.0 Documentation

A third party crate to mirror Component values.

By default, it also provides a set of bevy Components mirroring the values of bevy_rapier Components. (Currently only bevy_rapier3d, PRs welcome!) Since some of bevy_rapier Components do not implement Reflect, they may be harder to work with. This crate is especially useful with bevy-inspector-egui, it will allow you to edit rapier values at run time, so you don’t have to restart your game repetitively to find the right physics parameters.

Usage

  1. Add this crate as a dependency to your Cargo.toml.
[dependencies]
bevy_mod_component_mirror = "<current version>"
  1. Add RapierMirrorsPlugins to your app
use bevy_mod_component_mirror::RapierMirrorsPlugins;

app
  // Notice  v the plural
  .add_plugins(RapierMirrorsPlugins);

That’s it! Now every Entity with the following rapier (3d) components will automatically have an equivalent XyzMirror component that automatically syncs its value with it.

  • ImpulseJoint
  • Collider (some collider shape are not implemented yet!)
  • ColliderMassProperties
  • AdditionalMassProperties

Implement your own mirrors

If you wish to mirror other components, you need to do the following:

  • Create a Component (eg: ForeignMirror)
  • Implement the Mirror trait for that component.
  • Implement From<&'a Foreign> for ForeignMirror
  • Add MirrorPlugin::<Foreign, ForeignMirror>::new() to your app
use bevy_mod_component_mirror::{Mirror, MirrorPlugin};
use bevy::prelude::*;

use foreign_crate::Foreign;

// Component: required because you want it to be a component
// Reflect: this let `MirrorPlugin` register the `Mirror` type itself
#[derive(Component, Reflect)]
pub struct ForeignMirror {
  inner: f32,
}

// Foreign → ForeignMirror
impl<'a> From<&'a Foreign> for ForeignMirror {
  fn from(value: &'a Foreign) -> Self {
    ForeignMirror {
      inner: value.length(),
    }
  }
}
// ForeignMirror → Foreign
impl Mirror<Foreign> for ForeignMirror {
  fn apply(&self, value: &mut Foreign) {
    value.set_length(self.inner);
  }
}

fn main() {
  let mut app = App::new();
  app.add_plugin(MirrorPlugin::<Foreign, ForeignMirror>::new());
}

Features

If you don’t need the definitions for the rapier components but still wish to use the mirror plugin, you can disable the rapier components with:

[dependencies]
bevy_mod_component_mirror = { version = "<fill in>", default-features = false }

Version matrix

bevybevy_rapier3dbevy_mod_component_mirror
0.90.20.0<current version>

License

Copyright © 2022 Nicola Papale

This software is licensed under either MIT or Apache 2.0 at your leisure. See licenses directory for details.

Re-exports

pub use rapier_mirrors::RapierMirrorsPlugins;

Modules

Rapier mirror definitions.

Structs

Update each frame [Component] U with the value of T and vis-versa.

Enums

Systems added by the MirrorPlugin.

Traits

Mirror T If you wish to mirror other components, you need to do the following: