//! Shows multiple transformations of objects. use std::f32::consts::PI; use bevy::prelude::*; // A struct for additional data of for a moving cube. #[derive(Component)] struct CubeState { start_pos: Vec3, move_speed: f32, turn_speed: f32, } // A struct adding information to a scalable entity, // that will be stationary at the center of the scene. #[derive(Component)] struct Center { max_size: f32, min_size: f32, scale_factor: f32, } fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .add_systems( Update, ( move_cube, rotate_cube, scale_down_sphere_proportional_to_cube_travel_distance, ) .chain(), ) .run(); } // Startup system to setup the scene and spawn all relevant entities. fn setup( mut commands: Commands, mut meshes: ResMut>, mut materials: ResMut>, ) { // Add an object (sphere) for visualizing scaling. commands.spawn(( PbrBundle { mesh: meshes.add( Mesh::try_from(shape::Icosphere { radius: 3.0, subdivisions: 32, }) .unwrap(), ), material: materials.add(Color::YELLOW), transform: Transform::from_translation(Vec3::ZERO), ..default() }, Center { max_size: 1.0, min_size: 0.1, scale_factor: 0.05, }, )); // Add the cube to visualize rotation and translation. // This cube will circle around the center_sphere // by changing its rotation each frame and moving forward. // Define a start transform for an orbiting cube, that's away from our central object (sphere) // and rotate it so it will be able to move around the sphere and not towards it. let cube_spawn = Transform::from_translation(Vec3::Z * -10.0).with_rotation(Quat::from_rotation_y(PI / 2.)); commands.spawn(( PbrBundle { mesh: meshes.add(shape::Cube { size: 1.0 }), material: materials.add(Color::WHITE), transform: cube_spawn, ..default() }, CubeState { start_pos: cube_spawn.translation, move_speed: 2.0, turn_speed: 0.2, }, )); // Spawn a camera looking at the entities to show what's happening in this example. commands.spawn(Camera3dBundle { transform: Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y), ..default() }); // Add a light source for better 3d visibility. commands.spawn(PointLightBundle { transform: Transform::from_translation(Vec3::ONE * 3.0), ..default() }); } // This system will move the cube forward. fn move_cube(mut cubes: Query<(&mut Transform, &mut CubeState)>, timer: Res