diff --git a/Cargo.toml b/Cargo.toml index c2a1e44..0a0dbf0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ wgpu-hal = "0.17.1" [dev-dependencies] bevy = { git = "https://github.com/bevyengine/bevy.git" } color-eyre = "0.6.2" +bevy_rapier3d = { git = "https://github.com/alexichepura/bevy_rapier", version = "0.22.0", branch = "bevy-012"} [[example]] name = "xr" diff --git a/examples/demo/src/main.rs b/examples/demo/src/main.rs index f3c4438..3e53a10 100644 --- a/examples/demo/src/main.rs +++ b/examples/demo/src/main.rs @@ -1,17 +1,26 @@ use bevy::{ diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin}, - prelude::{info, App, Startup, Commands, SpatialBundle}, + prelude::{info, App, Commands, SpatialBundle, Startup, PostUpdate, IntoSystemSetConfigs, IntoSystemConfigs, Transform, Color}, transform::{TransformSystem, TransformBundle}, +}; +use bevy_openxr::{ + xr_input::{ + debug_gizmos::OpenXrDebugRenderer, + trackers::{OpenXRController, OpenXRLeftController, OpenXRRightController, OpenXRTracker}, + }, + DefaultXrPlugins, }; -use bevy_openxr::{xr_input::{debug_gizmos::OpenXrDebugRenderer, trackers::{OpenXRLeftController, OpenXRController, OpenXRTracker, OpenXRRightController}}, DefaultXrPlugins}; mod setup; use crate::setup::setup_scene; +use bevy_rapier3d::prelude::*; fn main() { color_eyre::install().unwrap(); info!("Running bevy_openxr demo"); - App::new() + let mut app = App::new(); + + app //lets get the usual diagnostic stuff added .add_plugins(LogDiagnosticsPlugin::default()) .add_plugins(FrameTimeDiagnosticsPlugin) @@ -19,10 +28,44 @@ fn main() { .add_plugins(DefaultXrPlugins) //lets add the debug renderer for the controllers .add_plugins(OpenXrDebugRenderer) + //rapier goes here + .add_plugins(RapierPhysicsPlugin::::default().with_default_system_setup(false)) + .add_plugins(RapierDebugRenderPlugin::default()) //lets setup the starting scene .add_systems(Startup, setup_scene) .add_systems(Startup, spawn_controllers_example) //you need to spawn controllers or it crashes TODO:: Fix this - .run(); + //spawn rapier test physics + .add_systems(Startup, setup_physics) + ; + + //configure rapier sets + app.configure_sets( + PostUpdate, + ( + PhysicsSet::SyncBackend, + PhysicsSet::StepSimulation, + PhysicsSet::Writeback, + ) + .chain() + .before(TransformSystem::TransformPropagate), + ); + //add rapier systems + app.add_systems( + PostUpdate, + ( + RapierPhysicsPlugin::::get_systems(PhysicsSet::SyncBackend) + .in_set(PhysicsSet::SyncBackend), + ( + RapierPhysicsPlugin::::get_systems(PhysicsSet::StepSimulation), + // despawn_one_box, + ) + .in_set(PhysicsSet::StepSimulation), + RapierPhysicsPlugin::::get_systems(PhysicsSet::Writeback) + .in_set(PhysicsSet::Writeback), + ), + ); + + app.run(); } fn spawn_controllers_example(mut commands: Commands) { @@ -47,3 +90,54 @@ fn spawn_controllers_example(mut commands: Commands) { )); } +pub fn setup_physics(mut commands: Commands) { + /* + * Ground + */ + let ground_size = 200.1; + let ground_height = 0.1; + + commands.spawn(( + TransformBundle::from(Transform::from_xyz(0.0, -ground_height, 0.0)), + Collider::cuboid(ground_size, ground_height, ground_size), + )); + + /* + * Create the cubes + */ + let num = 8; + let rad = 1.0; + + let shift = rad * 2.0 + rad; + let centerx = shift * (num / 2) as f32; + let centery = shift / 2.0; + let centerz = shift * (num / 2) as f32; + + let mut offset = -(num as f32) * (rad * 2.0 + rad) * 0.5; + let mut color = 0; + let colors = [ + Color::hsl(220.0, 1.0, 0.3), + Color::hsl(180.0, 1.0, 0.3), + Color::hsl(260.0, 1.0, 0.7), + ]; + + for j in 0usize..20 { + for i in 0..num { + for k in 0usize..num { + let x = i as f32 * shift - centerx + offset; + let y = j as f32 * shift + centery + 3.0; + let z = k as f32 * shift - centerz + offset; + color += 1; + + commands.spawn(( + TransformBundle::from(Transform::from_xyz(x, y, z)), + RigidBody::Dynamic, + Collider::cuboid(rad, rad, rad), + ColliderDebugColor(colors[color % 3]), + )); + } + } + + offset -= 0.05 * rad * (num as f32 - 1.0); + } +} \ No newline at end of file