|
| 1 | +use bevy::prelude::*; |
| 2 | +use bevy_rapier3d::prelude::*; |
| 3 | + |
| 4 | +const N_WORLDS: usize = 2; |
| 5 | + |
| 6 | +fn main() { |
| 7 | + App::new() |
| 8 | + .insert_resource(ClearColor(Color::rgb( |
| 9 | + 0xF9 as f32 / 255.0, |
| 10 | + 0xF9 as f32 / 255.0, |
| 11 | + 0xFF as f32 / 255.0, |
| 12 | + ))) |
| 13 | + .add_plugins(( |
| 14 | + DefaultPlugins, |
| 15 | + RapierPhysicsPlugin::<NoUserData>::default().with_default_world(None), |
| 16 | + RapierDebugRenderPlugin::default(), |
| 17 | + )) |
| 18 | + .add_systems( |
| 19 | + Startup, |
| 20 | + ((create_worlds, setup_physics).chain(), setup_graphics), |
| 21 | + ) |
| 22 | + .add_systems(Update, move_platforms) |
| 23 | + // .add_systems(Update, change_world) |
| 24 | + // .add_systems(Update, despawn_last) |
| 25 | + .run(); |
| 26 | +} |
| 27 | + |
| 28 | +fn create_worlds(mut commands: Commands) { |
| 29 | + for i in 0..N_WORLDS { |
| 30 | + commands.spawn((RapierContext::default(), WorldId(i))); |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +fn setup_graphics(mut commands: Commands) { |
| 35 | + commands.spawn(Camera3dBundle { |
| 36 | + transform: Transform::from_xyz(0.0, 3.0, -10.0) |
| 37 | + .looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y), |
| 38 | + ..Default::default() |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Component)] |
| 43 | +pub struct WorldId(pub usize); |
| 44 | + |
| 45 | +#[derive(Component)] |
| 46 | +struct Platform { |
| 47 | + starting_y: f32, |
| 48 | +} |
| 49 | + |
| 50 | +fn move_platforms(time: Res<Time>, mut query: Query<(&mut Transform, &Platform)>) { |
| 51 | + for (mut transform, platform) in query.iter_mut() { |
| 52 | + transform.translation.y = platform.starting_y + -time.elapsed_seconds().sin(); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Demonstrates despawning an entity removing it from its world |
| 57 | +// fn despawn_last(query: Query<(&PhysicsWorld, Entity)>, mut commands: Commands) { |
| 58 | +// for (bw, entity) in query.iter() { |
| 59 | +// if bw.world_id == N_WORLDS - 1 { |
| 60 | +// commands.entity(entity).despawn_recursive(); |
| 61 | +// } |
| 62 | +// } |
| 63 | +// } |
| 64 | + |
| 65 | +/// Demonstrates how easy it is to move one entity to another world. |
| 66 | +// fn change_world(mut query: Query<&mut PhysicsWorld>) { |
| 67 | +// for mut bw in query.iter_mut() { |
| 68 | +// if bw.world_id == 1 { |
| 69 | +// bw.world_id = 0; |
| 70 | +// } |
| 71 | +// } |
| 72 | +// } |
| 73 | + |
| 74 | +pub fn setup_physics( |
| 75 | + context: Query<(Entity, &WorldId), With<RapierContext>>, |
| 76 | + mut commands: Commands, |
| 77 | +) { |
| 78 | + for (context_entity, id) in context.iter() { |
| 79 | + let id = id.0; |
| 80 | + |
| 81 | + let color = [ |
| 82 | + Hsla::hsl(220.0, 1.0, 0.3), |
| 83 | + Hsla::hsl(180.0, 1.0, 0.3), |
| 84 | + Hsla::hsl(260.0, 1.0, 0.7), |
| 85 | + ][id % 3]; |
| 86 | + |
| 87 | + /* |
| 88 | + * Ground |
| 89 | + */ |
| 90 | + let ground_size = 5.1; |
| 91 | + let ground_height = 0.1; |
| 92 | + |
| 93 | + let starting_y = (id as f32) * -0.5 - ground_height; |
| 94 | + |
| 95 | + let mut platforms = commands.spawn(( |
| 96 | + TransformBundle::from(Transform::from_xyz(0.0, starting_y, 0.0)), |
| 97 | + Collider::cuboid(ground_size, ground_height, ground_size), |
| 98 | + ColliderDebugColor(color), |
| 99 | + RapierContextEntityLink(context_entity), |
| 100 | + )); |
| 101 | + if id == 1 { |
| 102 | + platforms.insert(Platform { starting_y }); |
| 103 | + } |
| 104 | + |
| 105 | + /* |
| 106 | + * Create the cube |
| 107 | + */ |
| 108 | + |
| 109 | + commands.spawn(( |
| 110 | + TransformBundle::from(Transform::from_xyz(0.0, 1.0 + id as f32 * 5.0, 0.0)), |
| 111 | + RigidBody::Dynamic, |
| 112 | + Collider::cuboid(0.5, 0.5, 0.5), |
| 113 | + ColliderDebugColor(color), |
| 114 | + RapierContextEntityLink(context_entity), |
| 115 | + )); |
| 116 | + } |
| 117 | +} |
0 commit comments