Skip to content

Commit

Permalink
update rapier integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobhellermann committed May 7, 2022
1 parent b755afa commit b11e13d
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 308 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ name = "world"
[patch.crates-io]

[workspace]
members = [".", "bevy-inspector-egui-derive"]
# members = [".", "bevy-inspector-egui-derive", "integrations/*"]
# members = [".", "bevy-inspector-egui-derive"]
members = [".", "bevy-inspector-egui-derive", "integrations/*"]

[profile.dev.package."*"]
opt-level = 2
6 changes: 6 additions & 0 deletions integrations/bevy-inspector-egui-rapier/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rust-analyzer.cargo.features": [
"rapier2d",
"rapier3d"
]
}
6 changes: 3 additions & 3 deletions integrations/bevy-inspector-egui-rapier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ rapier3d = ["bevy_rapier3d"]
rapier2d = ["bevy_rapier2d"]

[dependencies]
bevy-inspector-egui = { version = "0.10", path = "../..", features = ["nalgebra030"] }
bevy_rapier3d = { version = "0.12.0", optional = true }
bevy_rapier2d = { version = "0.12.0", optional = true }
bevy-inspector-egui = { version = "0.10", path = "../..", features = ["nalgebra031"] }
bevy_rapier3d = { version = "0.13.0", optional = true }
bevy_rapier2d = { version = "0.13.0", optional = true }
bevy = { version = "0.7", default-features = false }

[dev-dependencies]
Expand Down
54 changes: 23 additions & 31 deletions integrations/bevy-inspector-egui-rapier/examples/rapier2d.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
use bevy::prelude::*;
use bevy_inspector_egui::WorldInspectorPlugin;
use bevy_inspector_egui_rapier::InspectableRapierPlugin;
use bevy_rapier2d::prelude::*;
use bevy_rapier2d::rapier::na::Vector2;

fn main() {
App::new()
.insert_resource(WindowDescriptor {
title: "Player Movement Example".to_string(),
..Default::default()
})
.add_plugins(DefaultPlugins)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.add_plugin(WorldInspectorPlugin::new())
.add_plugin(bevy_inspector_egui_rapier::InspectableRapierPlugin)
.add_startup_system(spawn_player)
.add_system(player_movement)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0))
.add_plugin(RapierDebugRenderPlugin::default())
.add_plugin(WorldInspectorPlugin::default())
.add_plugin(InspectableRapierPlugin)
.run();
}

Expand All @@ -20,46 +25,35 @@ struct Player(f32);

fn spawn_player(mut commands: Commands, mut rapier_config: ResMut<RapierConfiguration>) {
// Set gravity to 0.0 and spawn camera.
rapier_config.gravity = Vector2::zeros();
rapier_config.gravity = Vec2::ZERO;
commands
.spawn()
.insert_bundle(OrthographicCameraBundle::new_2d());

let sprite_size_x = 40.0;
let sprite_size_y = 40.0;

// While we want our sprite to look ~40 px square, we want to keep the physics units smaller
// to prevent float rounding problems. To do this, we set the scale factor in RapierConfiguration
// and divide our sprite_size by the scale.
rapier_config.scale = 20.0;
let collider_size_x = sprite_size_x / rapier_config.scale;
let collider_size_y = sprite_size_y / rapier_config.scale;
let sprite_size = 100.0;

// Spawn entity with `Player` struct as a component for access in movement query.
commands
.spawn()
.insert_bundle(SpriteBundle {
sprite: Sprite {
color: Color::BLACK,
custom_size: Some(Vec2::new(sprite_size_x, sprite_size_y)),
color: Color::rgb(0.0, 0.0, 0.0),
custom_size: Some(Vec2::new(sprite_size, sprite_size)),
..Default::default()
},
..Default::default()
})
.insert_bundle(RigidBodyBundle::default())
.insert_bundle(ColliderBundle {
position: [collider_size_x / 2.0, collider_size_y / 2.0].into(),
..Default::default()
})
.insert(ColliderPositionSync::Discrete)
.insert(ColliderDebugRender::with_id(0))
.insert(Player(300.0));
.insert(RigidBody::Dynamic)
.insert(Velocity::zero())
.insert(Collider::ball(sprite_size / 2.0))
.insert(ColliderMassProperties::default())
.insert(Player(100.0))
.insert(Name::new("Player"));
}

fn player_movement(
keyboard_input: Res<Input<KeyCode>>,
rapier_parameters: Res<RapierConfiguration>,
mut player_info: Query<(&Player, &mut RigidBodyVelocityComponent)>,
mut player_info: Query<(&Player, &mut Velocity)>,
) {
for (player, mut rb_vels) in player_info.iter_mut() {
let up = keyboard_input.pressed(KeyCode::W) || keyboard_input.pressed(KeyCode::Up);
Expand All @@ -70,11 +64,9 @@ fn player_movement(
let x_axis = -(left as i8) + right as i8;
let y_axis = -(down as i8) + up as i8;

let mut move_delta = Vector2::new(x_axis as f32, y_axis as f32);
if move_delta != Vector2::zeros() {
// Note that the RapierConfiguration::Scale factor is also used here to transform
// the move_delta from: 'pixels/second' to 'physics_units/second'
move_delta /= move_delta.magnitude() * rapier_parameters.scale;
let mut move_delta = Vec2::new(x_axis as f32, y_axis as f32);
if move_delta != Vec2::ZERO {
move_delta /= move_delta.length();
}

// Update the velocity on the rigid_body_component,
Expand Down
36 changes: 11 additions & 25 deletions integrations/bevy-inspector-egui-rapier/examples/rapier3d.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use bevy::prelude::*;
use bevy::render::mesh::shape;
use bevy_inspector_egui::{widgets::InspectorQuerySingle, InspectorPlugin, WorldInspectorParams};
use bevy_inspector_egui::{widgets::InspectorQuerySingle, InspectorPlugin};
use bevy_inspector_egui_rapier::InspectableRapierPlugin;
use bevy_rapier3d::prelude::*;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugin(RapierRenderPlugin)
.add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
.insert_resource(WorldInspectorParams {
sort_components: true,
..Default::default()
})
.add_plugin(RapierDebugRenderPlugin::default())
.add_plugin(InspectorPlugin::<InspectorQuerySingle<Entity, With<Cube>>>::new())
.add_plugin(InspectableRapierPlugin)
.add_startup_system(setup)
Expand All @@ -39,16 +35,9 @@ fn setup(
commands
// .spawn_bundle(floor)
.spawn()
.insert(ColliderDebugRender::with_id(0))
.insert(Name::new("Floor"))
.insert_bundle(RigidBodyBundle {
body_type: RigidBodyType::Static.into(),
..Default::default()
})
.insert_bundle(ColliderBundle {
shape: ColliderShape::cuboid(floor_size / 2.0, 0.1, floor_size / 2.0).into(),
..Default::default()
});
.insert(RigidBody::Fixed)
.insert(Collider::cuboid(floor_size / 2.0, 0.1, floor_size / 2.0));

let _cube = PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: cube_size })),
Expand All @@ -59,18 +48,15 @@ fn setup(
commands
// .spawn_bundle(cube)
.spawn()
.insert(ColliderDebugRender::with_id(1))
.insert(Cube)
.insert(Name::new("Cube"))
.insert_bundle(ColliderBundle {
shape: ColliderShape::cuboid(cube_size / 2.0, cube_size / 2.0, cube_size / 2.0).into(),
..Default::default()
})
.insert_bundle(RigidBodyBundle {
position: Vec3::new(0.0, 2.0, 0.0).into(),
..Default::default()
})
.insert(ColliderPositionSync::Discrete);
.insert(Collider::cuboid(
cube_size / 2.0,
cube_size / 2.0,
cube_size / 2.0,
))
.insert(RigidBody::Dynamic)
.insert(Transform::from_xyz(0.0, 2.0, 0.0));

commands.spawn_bundle(PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
Expand Down
34 changes: 17 additions & 17 deletions integrations/bevy-inspector-egui-rapier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,34 @@
//! bevy-inspector-egui-rapier = { version = "0.1", features = ["rapier3d"] }
//! ```
//!
//! ```rust
//! ```rust,no_run
//! use bevy::prelude::*;
//! use bevy_inspector_egui::WorldInspectorPlugin;
//! use bevy_inspector_egui_rapier::InspectableRapierPlugin;
//! use bevy_rapier3d::prelude::*;
//!
//! fn main() {
//! App::new()
//! .add_plugins(DefaultPlugins)
//! .add_plugin(RapierRenderPlugin)
//! .add_plugin(RapierDebugRenderPlugin::default())
//! .add_plugin(RapierPhysicsPlugin::<NoUserData>::default())
//! .add_plugin(InspectableRapierPlugin) // <--- register the inspectable UI functions for rapier types
//! .add_plugin(WorldInpsectorPlugin)
//! .add_plugin(WorldInspectorPlugin::default())
//! .run();
//! }
//! ```
mod macros;

use bevy::prelude::{App, Plugin};
use bevy_inspector_egui::{InspectableRegistry, WorldInspectorParams};
use bevy_inspector_egui::InspectableRegistry;

/// Plugin that will add register rapier components on the [`InspectableRegistry`]
pub struct InspectableRapierPlugin;

#[cfg(all(not(feature = "rapier2d"), not(feature = "rapier3d")))]
//compile_error!("please select either the rapier2d or the rapier3d feature of the crate bevy-inspector-egui-rapier");
compile_error!("please select either the rapier2d or the rapier3d feature of the crate bevy-inspector-egui-rapier");

impl Plugin for InspectableRapierPlugin {
fn build(&self, app: &mut App) {
#[allow(unused_mut)]
Expand All @@ -39,29 +43,25 @@ impl Plugin for InspectableRapierPlugin {
rapier_2d::register(&mut inspectable_registry);
#[cfg(feature = "rapier3d")]
rapier_3d::register(&mut inspectable_registry);

#[allow(unused_mut)]
let mut world_inspector_params = app
.world
.get_resource_or_insert_with(WorldInspectorParams::default);

#[cfg(feature = "rapier2d")]
rapier_2d::register_params(&mut world_inspector_params);
#[cfg(feature = "rapier3d")]
rapier_3d::register_params(&mut world_inspector_params);
}
}

#[cfg(feature = "rapier2d")]
mod rapier_2d {
use bevy_rapier2d::prelude::*;
use bevy_rapier2d as bevy_rapier;

type Vect = bevy::math::Vec2;
type VectAttributes = bevy_inspector_egui::options::Vec2dAttributes;

include!("./rapier_impl.rs");
}

#[cfg(feature = "rapier3d")]
mod rapier_3d {
use bevy_rapier3d::prelude::*;
use bevy_rapier3d as bevy_rapier;

type Vect = bevy::math::Vec3;
type VectAttributes = bevy_inspector_egui::options::NumberAttributes<Vect>;

include!("./rapier_impl.rs");
}
54 changes: 40 additions & 14 deletions integrations/bevy-inspector-egui-rapier/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ macro_rules! grid {
ui.label(stringify!($field));
let field = &mut val.$field;
let context = &mut $context.with_id(i);
changed |= grid!(@field ui context field; $(by $ui_fn)? $(with $attrs)?);
changed |= $crate::macros::grid!(@field ui context field; $(by $ui_fn)? $(with $attrs)?);
ui.end_row();
i += 1;
)*
Expand Down Expand Up @@ -64,27 +64,53 @@ macro_rules! simple_enum {
}};
}

macro_rules! enum_one {
($ui:ident $context:ident $val:ident; $ty:ident: $($variant:ident {$attributes:expr})|*) => {{
let selected = match $val {
$($ty::$variant(_) => stringify!($variant),)*
};
ComboBox::from_id_source($context.id())
.selected_text(selected)
.show_ui($ui, |ui| {
$(if ui
.selectable_label(matches!($val, $ty::$variant(_)), stringify!($variant))
.clicked()
{
*$val = $ty::$variant(Default::default());
})*
});
match $val {
$($ty::$variant(val) => val.ui($ui, $attributes, $context),)*
}
}};
}

macro_rules! inspectable {
(grid $name:ident $ty:ty as $wrapper:ty: $($tt:tt)*) => {
fn $name(val: &mut $wrapper, ui: &mut egui::Ui, context: &mut Context<'_>) -> bool {
grid!(ui context &mut val.0; $($tt)*)
(grid $name:ident $ty:ty: $($tt:tt)*) => {
fn $name(val: &mut $ty, ui: &mut egui::Ui, context: &mut Context<'_>) -> bool {
$crate::macros::grid!(ui context val; $($tt)*)
}
};
(flags $name:ident $ty:ty: $($tt:tt)*) => {
fn $name(val: &mut $ty, ui: &mut egui::Ui, context: &mut Context<'_>) -> bool {
$crate::macros::flags!(ui context val; $ty: $($tt)*)
}
};
(flags $name:ident $ty:ty as $wrapper:ty: $($tt:tt)*) => {
fn $name(val: &mut $wrapper, ui: &mut egui::Ui, context: &mut Context<'_>) -> bool {
flags!(ui context &mut val.0; $ty: $($tt)*)
(enum $name:ident $ty:ty: $($tt:tt)*) => {
fn $name(val: &mut $ty, ui: &mut egui::Ui, context: &mut Context<'_>) -> bool {
$crate::macros::simple_enum!(ui context val; $ty: $($tt)*)
}
};
(enum $name:ident $ty:ty as $wrapper:ty: $($tt:tt)*) => {
fn $name(val: &mut $wrapper, ui: &mut egui::Ui, context: &mut Context<'_>) -> bool {
simple_enum!(ui context &mut val.0; $ty: $($tt)*)
(enum_one $name:ident $ty:ident: $($tt:tt)*) => {
fn $name(val: &mut $ty, ui: &mut egui::Ui, context: &mut Context<'_>) -> bool {
$crate::macros::enum_one!(ui context val; $ty: $($tt)*)
}
};
(defer $name:ident $ty:ty as $wrapper:ty: $val:tt) => {
fn $name(val: &mut $wrapper, ui: &mut egui::Ui, context: &mut Context<'_>) -> bool {
val.0.$val.ui(ui, Default::default(), context)
(defer $name:ident $ty:ty: $val:tt) => {
fn $name(val: &mut $ty, ui: &mut egui::Ui, context: &mut Context<'_>) -> bool {
val.$val.ui(ui, Default::default(), context)
}
}
}

pub(crate) use {flags, grid, inspectable, simple_enum};
pub(crate) use {enum_one, flags, grid, inspectable, simple_enum};
Loading

0 comments on commit b11e13d

Please sign in to comment.