|
| 1 | +use bevy::prelude::*; |
| 2 | +use bevy_rapier2d::geometry::RapierColliderHandle; |
| 3 | +use bevy_rapier2d::prelude::*; |
| 4 | + |
| 5 | +fn main() { |
| 6 | + App::new() |
| 7 | + .insert_resource(ClearColor(Color::srgb( |
| 8 | + 0xF9 as f32 / 255.0, |
| 9 | + 0xF9 as f32 / 255.0, |
| 10 | + 0xFF as f32 / 255.0, |
| 11 | + ))) |
| 12 | + .add_plugins(( |
| 13 | + DefaultPlugins, |
| 14 | + RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(10.0), |
| 15 | + RapierDebugRenderPlugin::default(), |
| 16 | + )) |
| 17 | + .add_systems(Startup, setup) |
| 18 | + .add_systems(Update, (check_collisions, check_collider_enabled)) |
| 19 | + .run(); |
| 20 | +} |
| 21 | + |
| 22 | +fn setup(mut commands: Commands) { |
| 23 | + // 2D camera |
| 24 | + commands.spawn((Camera2d, Transform::from_xyz(0.0, 20.0, 0.0))); |
| 25 | + |
| 26 | + let scale = 15f32; |
| 27 | + |
| 28 | + let nx = 50; |
| 29 | + for i in 0..nx { |
| 30 | + for j in 0..10 { |
| 31 | + let falling_objects = (j + i) % 3; |
| 32 | + let ball_radius = 0.5 * scale; |
| 33 | + |
| 34 | + let collider = match falling_objects { |
| 35 | + 0 => Collider::ball(ball_radius), |
| 36 | + 1 => Collider::cuboid(ball_radius, ball_radius), |
| 37 | + 2 => Collider::capsule_y(ball_radius, ball_radius), |
| 38 | + _ => unreachable!(), |
| 39 | + }; |
| 40 | + |
| 41 | + commands |
| 42 | + .spawn(( |
| 43 | + Transform::from_xyz( |
| 44 | + (i as f32 * 2.0 - nx as f32 / 2.0) * scale, |
| 45 | + (20.0 + j as f32 * 2.0) * scale, |
| 46 | + 0.0, |
| 47 | + ), |
| 48 | + RigidBody::Dynamic, |
| 49 | + AdditionalMassProperties::Mass(1.0), |
| 50 | + )) |
| 51 | + .with_children(|parent| { |
| 52 | + parent.spawn((collider, ColliderDisabled)); |
| 53 | + }); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + let polyline = [ |
| 58 | + Vec2::new(0.0, 0.0), |
| 59 | + Vec2::new(0.0, 10.0), |
| 60 | + Vec2::new(7.0, 4.0), |
| 61 | + Vec2::new(14.0, 10.0), |
| 62 | + Vec2::new(14.0, 0.0), |
| 63 | + Vec2::new(13.0, 7.0), |
| 64 | + Vec2::new(7.0, 2.0), |
| 65 | + Vec2::new(1.0, 7.0), |
| 66 | + ] |
| 67 | + .iter() |
| 68 | + .map(|p| p * scale) |
| 69 | + .collect::<Vec<_>>(); |
| 70 | + let indices: Vec<_> = (0..polyline.len() as u32) |
| 71 | + .map(|i| [i, (i + 1) % polyline.len() as u32]) |
| 72 | + .collect(); |
| 73 | + |
| 74 | + commands.spawn(( |
| 75 | + Transform::from_xyz(-20.0 * scale, -10.0 * scale, 0.0), |
| 76 | + Collider::voxelized_mesh(&polyline, &indices, 0.2 * scale, FillMode::default()), |
| 77 | + ColliderDisabled, |
| 78 | + )); |
| 79 | + |
| 80 | + let voxel_size = Vec2::new(scale, scale); |
| 81 | + let voxels: Vec<_> = (0..300) |
| 82 | + .map(|i| { |
| 83 | + let y = (i as f32 / 20.0).sin().clamp(-0.5, 0.5) * 20.0; |
| 84 | + Vec2::new((i as f32 - 125.0) * voxel_size.x / 2.0, y * voxel_size.y) |
| 85 | + }) |
| 86 | + .collect(); |
| 87 | + commands.spawn(( |
| 88 | + Transform::from_xyz(0.0, 0.0, 0.0), |
| 89 | + Collider::voxels_from_points(voxel_size, &voxels), |
| 90 | + )); |
| 91 | +} |
| 92 | + |
| 93 | +fn check_collisions(mut collision_events: MessageReader<CollisionEvent>) { |
| 94 | + for event in collision_events.read() { |
| 95 | + info!("Collision event: {:?}", event); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +fn check_collider_enabled( |
| 100 | + rapier_context: ReadRapierContext, |
| 101 | + query: Query<&RapierColliderHandle, With<ColliderDisabled>>, |
| 102 | +) { |
| 103 | + let context = match rapier_context.single() { |
| 104 | + Ok(c) => c, |
| 105 | + Err(_) => return, |
| 106 | + }; |
| 107 | + |
| 108 | + for rapier_handle in &query { |
| 109 | + let handle = rapier_handle.0; |
| 110 | + if let Some(col) = context.colliders.colliders.get(handle) { |
| 111 | + if col.is_enabled() { |
| 112 | + info!( |
| 113 | + "FIX ME: Voxel collider should NOT be enabled: {}", |
| 114 | + col.is_enabled() |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments