From 9dfd4e4b08aba146e4d14e457b7b2ac6560e8ea3 Mon Sep 17 00:00:00 2001 From: Mizu <5565979+l4desu-mizu@users.noreply.github.com> Date: Tue, 15 Mar 2022 05:49:49 +0000 Subject: [PATCH] Add examples for Transforms (#2441) # Add Transform Examples - Adding examples for moving/rotating entities (with its own section) to resolve #2400 I've stumbled upon this project and been fiddling around a little. Saw the issue and thought I might just add some examples for the proposed transformations. Mind to check if I got the gist correctly and suggest anything I can improve? --- Cargo.toml | 21 ++ examples/README.md | 11 + examples/transforms/3d_rotation.rs | 57 ++++++ .../transforms/global_vs_local_translation.rs | 188 ++++++++++++++++++ examples/transforms/scale.rs | 96 +++++++++ examples/transforms/transform.rs | 151 ++++++++++++++ examples/transforms/translation.rs | 71 +++++++ 7 files changed, 595 insertions(+) create mode 100644 examples/transforms/3d_rotation.rs create mode 100644 examples/transforms/global_vs_local_translation.rs create mode 100644 examples/transforms/scale.rs create mode 100644 examples/transforms/transform.rs create mode 100644 examples/transforms/translation.rs diff --git a/Cargo.toml b/Cargo.toml index 5efa161158..9f93875617 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -502,6 +502,27 @@ path = "examples/shader/compute_shader_game_of_life.rs" name = "bevymark" path = "examples/tools/bevymark.rs" +# Transforms +[[example]] +name = "global_vs_local_translation" +path = "examples/transforms/global_vs_local_translation.rs" + +[[example]] +name = "3d_rotation" +path = "examples/transforms/3d_rotation.rs" + +[[example]] +name = "scale" +path = "examples/transforms/scale.rs" + +[[example]] +name = "transform" +path = "examples/transforms/transform.rs" + +[[example]] +name = "translation" +path = "examples/transforms/translation.rs" + # UI (User Interface) [[example]] name = "button" diff --git a/examples/README.md b/examples/README.md index 53d276d646..9f2e01ae80 100644 --- a/examples/README.md +++ b/examples/README.md @@ -52,6 +52,7 @@ git checkout v0.4.0 - [Shaders](#shaders) - [Tests](#tests) - [Tools](#tools) + - [Transforms](#transforms) - [UI (User Interface)](#ui-user-interface) - [Window](#window) - [Platform-Specific Examples](#platform-specific-examples) @@ -247,6 +248,16 @@ Example | File | Description --- | --- | --- `bevymark` | [`tools/bevymark.rs`](./tools/bevymark.rs) | A heavy sprite rendering workload to benchmark your system with Bevy +## Transforms + +Example | File | Description +--- | --- | --- +`global_vs_local_translation` | [`transforms/global_vs_local_translation.rs`](./transforms/global_vs_local_translation.rs) | Illustrates the difference between direction of a translation in respect to local object or global object Transform +`3d_rotation` | [`transforms/3d_rotation.rs`](./transforms/3d_rotation.rs) | Illustrates how to (constantly) rotate an object around an axis +`scale` | [`transforms/scale.rs`](./transforms/scale.rs) | Illustrates how to scale an object in each direction +`transform` | [`transforms/transfrom.rs`](./transforms/transform.rs) | Shows multiple transformations of objects +`translation` | [`transforms/translation.rs`](./transforms/translation.rs) | Illustrates how to move an object along an axis + ## UI (User Interface) Example | File | Description diff --git a/examples/transforms/3d_rotation.rs b/examples/transforms/3d_rotation.rs new file mode 100644 index 0000000000..4afa84e417 --- /dev/null +++ b/examples/transforms/3d_rotation.rs @@ -0,0 +1,57 @@ +use bevy::prelude::*; + +use std::f32::consts::PI; + +const FULL_TURN: f32 = 2.0 * PI; + +// Define a component to designate a rotation speed to an entity. +#[derive(Component)] +struct Rotatable { + speed: f32, +} + +fn main() { + App::new() + .add_plugins(DefaultPlugins) + .add_startup_system(setup) + .add_system(rotate_cube) + .run(); +} + +fn setup( + mut commands: Commands, + mut meshes: ResMut>, + mut materials: ResMut>, +) { + // Spawn a cube to rotate. + commands + .spawn_bundle(PbrBundle { + mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })), + material: materials.add(Color::WHITE.into()), + transform: Transform::from_translation(Vec3::ZERO), + ..Default::default() + }) + .insert(Rotatable { speed: 0.3 }); + + // Spawn a camera looking at the entities to show what's happening in this example. + commands.spawn_bundle(PerspectiveCameraBundle { + transform: Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y), + ..Default::default() + }); + + // Add a light source for better 3d visibility. + commands.spawn_bundle(PointLightBundle { + transform: Transform::from_translation(Vec3::ONE * 3.0), + ..Default::default() + }); +} + +// This system will rotate any entity in the scene with an assigned Rotatable around its z-axis. +fn rotate_cube(mut cubes: Query<(&mut Transform, &Rotatable)>, timer: Res