//! Shows multiple transformations of objects. use std::f32::consts::PI; use bevy::{color::palettes::basic::YELLOW, 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(( Mesh3d(meshes.add(Sphere::new(3.0).mesh().ico(32).unwrap())), MeshMaterial3d(materials.add(Color::from(YELLOW))), Transform::from_translation(Vec3::ZERO), 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(( Mesh3d(meshes.add(Cuboid::default())), MeshMaterial3d(materials.add(Color::WHITE)), cube_spawn, 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(( Camera3d::default(), Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y), )); // Add a light source for better 3d visibility. commands.spawn(( DirectionalLight::default(), Transform::from_xyz(3.0, 3.0, 3.0).looking_at(Vec3::ZERO, Vec3::Y), )); } // This system will move the cube forward. fn move_cube(mut cubes: Query<(&mut Transform, &mut CubeState)>, timer: Res