diff --git a/crates/avian3d/Cargo.toml b/crates/avian3d/Cargo.toml index 6d7e2c61..a89c95c7 100644 --- a/crates/avian3d/Cargo.toml +++ b/crates/avian3d/Cargo.toml @@ -105,6 +105,10 @@ bevy_heavy = { git = "https://github.com/Jondolf/bevy_heavy", features = [ ] } approx = "0.5" criterion = { version = "0.5", features = ["html_reports"] } +big_space = { git = "https://github.com/aevyrie/big_space", branch = "bevy-0.16.0", features = [ + "debug", +] } + # TODO: Update to the latest version when it's available. # bevy_mod_debugdump = { version = "0.12" } diff --git a/crates/avian3d/examples/floating_origin.rs b/crates/avian3d/examples/floating_origin.rs new file mode 100644 index 00000000..4840665b --- /dev/null +++ b/crates/avian3d/examples/floating_origin.rs @@ -0,0 +1,126 @@ +use avian3d::prelude::*; +use bevy::prelude::*; +use big_space::prelude::*; +use examples_common_3d::ExampleCommonPlugin; + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_plugins(( + BigSpacePlugin::default(), + FloatingOriginDebugPlugin::default(), + PhysicsPlugins::default(), + PhysicsDebugPlugin::default(), + ExampleCommonPlugin, + )) + .insert_resource(Gravity(Vec3::ZERO)) + .add_systems(Startup, spawn_big_space) + .add_systems(Update, move_cube) + .add_systems( + PostUpdate, + move_camera.before(TransformSystem::TransformPropagate), + ) + .run(); +} + +#[derive(Component)] +struct WasdControlled; + +#[derive(Component)] +struct MainCamera; + +fn spawn_big_space( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + commands.spawn_big_space(Grid::new(5.0, 0.0), |root| { + // Camera with no physics components + root.spawn_spatial(( + FloatingOrigin, + Transform::from_xyz(0.0, 0.0, 25.0).looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y), + Camera::default(), + Camera3d::default(), + Projection::Perspective(PerspectiveProjection::default()), + MainCamera, + )); + + let cube_mesh = meshes.add(Cuboid::default()); + + // WASD controlled cube + root.spawn_spatial(( + Mesh3d(cube_mesh.clone()), + MeshMaterial3d(materials.add(Color::WHITE)), + Transform::IDENTITY, + RigidBody::Dynamic, + Collider::cuboid(1.0, 1.0, 1.0), + WasdControlled, + LinearVelocity::default(), + LinearDamping::default(), + TransformInterpolation, + )); + }); +} + +fn move_cube( + keyboard_input: Res>, + mut query: Query<(&mut LinearVelocity, &mut LinearDamping), With>, +) { + let mut velocity = Vec3::ZERO; + if keyboard_input.pressed(KeyCode::KeyW) { + velocity += Vec3::Y * 0.1; + } + if keyboard_input.pressed(KeyCode::KeyS) { + velocity -= Vec3::Y * 0.1; + } + if keyboard_input.pressed(KeyCode::KeyA) { + velocity -= Vec3::X * 0.1; + } + if keyboard_input.pressed(KeyCode::KeyD) { + velocity += Vec3::X * 0.1; + } + + for (mut linear_velocity, _) in &mut query { + linear_velocity.0 += velocity; + } + + let linear_damping_value = if keyboard_input.pressed(KeyCode::Space) { + 10.0 + } else { + 0.0 + }; + + for (_, mut linear_damping) in &mut query { + linear_damping.0 = linear_damping_value; + } +} + +fn move_camera( + keyboard_input: Res>, + mut query: Query<&mut Transform, With>, + time: Res