#![allow(clippy::unnecessary_cast)] use avian2d::{math::*, prelude::*}; use bevy::{prelude::*, sprite::MaterialMesh2dBundle}; use examples_common_2d::ExampleCommonPlugin; fn main() { App::new() .add_plugins(( DefaultPlugins, ExampleCommonPlugin, // Add physics plugins and specify a units-per-meter scaling factor, 1 meter = 20 pixels. // The unit allows the engine to tune its parameters for the scale of the world, improving stability. PhysicsPlugins::default().with_length_unit(20.0), )) .insert_resource(ClearColor(Color::srgb(0.05, 0.05, 0.1))) .insert_resource(Gravity(Vector::NEG_Y * 1000.0)) .add_systems(Startup, setup) .add_systems(Update, movement) .run(); } #[derive(Component)] struct Controllable; fn setup( mut commands: Commands, mut materials: ResMut>, mut meshes: ResMut>, ) { commands.spawn(Camera2dBundle::default()); let square_sprite = Sprite { color: Color::srgb(0.7, 0.7, 0.8), custom_size: Some(Vec2::splat(50.0)), ..default() }; // Ceiling commands.spawn(( SpriteBundle { sprite: square_sprite.clone(), transform: Transform::from_xyz(0.0, 50.0 * 6.0, 0.0) .with_scale(Vec3::new(20.0, 1.0, 1.0)), ..default() }, RigidBody::Static, Collider::rectangle(50.0, 50.0), )); // Floor commands.spawn(( SpriteBundle { sprite: square_sprite.clone(), transform: Transform::from_xyz(0.0, -50.0 * 6.0, 0.0) .with_scale(Vec3::new(20.0, 1.0, 1.0)), ..default() }, RigidBody::Static, Collider::rectangle(50.0, 50.0), )); // Left wall commands.spawn(( SpriteBundle { sprite: square_sprite.clone(), transform: Transform::from_xyz(-50.0 * 9.5, 0.0, 0.0) .with_scale(Vec3::new(1.0, 11.0, 1.0)), ..default() }, RigidBody::Static, Collider::rectangle(50.0, 50.0), )); // Right wall commands.spawn(( SpriteBundle { sprite: square_sprite, transform: Transform::from_xyz(50.0 * 9.5, 0.0, 0.0) .with_scale(Vec3::new(1.0, 11.0, 1.0)), ..default() }, RigidBody::Static, Collider::rectangle(50.0, 50.0), )); let circle = Circle::new(7.5); let rectangle = Rectangle::new(15.0, 15.0); let capsule = Capsule2d::new(7.5, 20.0); let triangle = Triangle2d::new( Vec2::new(0.0, 10.0), Vec2::new(-10.0, -10.0), Vec2::new(10.0, -10.0), ); let shapes = [ ( circle.collider(), meshes.add(circle).into(), materials.add(Color::srgb(0.29, 0.33, 0.64)), ), ( rectangle.collider(), meshes.add(rectangle).into(), materials.add(Color::srgb(0.47, 0.58, 0.8)), ), ( capsule.collider(), meshes.add(capsule).into(), materials.add(Color::srgb(0.63, 0.75, 0.88)), ), ( triangle.collider(), meshes.add(triangle).into(), materials.add(Color::srgb(0.77, 0.87, 0.97)), ), ]; for x in -12_i32..12 { for y in -8_i32..8 { let (collider, mesh, material) = shapes[(x + y) as usize % 4].clone(); commands.spawn(( MaterialMesh2dBundle { mesh, material, transform: Transform::from_xyz(x as f32 * 25.0, y as f32 * 25.0, 0.0), ..default() }, collider, RigidBody::Dynamic, Friction::new(0.1), Controllable, )); } } } fn movement( time: Res