Skip to content

Commit

Permalink
Try #2441:
Browse files Browse the repository at this point in the history
  • Loading branch information
bors[bot] authored Jul 6, 2021
2 parents bc3f80f + 0709b0f commit cc4259a
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,27 @@ path = "examples/shader/shader_defs.rs"
name = "bevymark"
path = "examples/tools/bevymark.rs"

# Transforms
[[example]]
name = "global_vs_local"
path = "examples/transforms/global_vs_local_translation.rs"

[[example]]
name = "rotation"
path = "examples/transforms/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"
Expand Down
116 changes: 116 additions & 0 deletions examples/transforms/global_vs_local_translation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use bevy::prelude::*;

struct SomeEntity {
spawn: Vec3,
stopped: bool,
}
struct GlobalStop;
struct LocalStop;

const MAX_DISTANCE: f32 = 5.0;

impl SomeEntity {
fn new(spawn: Vec3) -> Self {
SomeEntity {
spawn,
stopped: false,
}
}
}

fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(move_cubes.system())
.add_system(stop_too_far_local_distance.system())
.add_system(stop_too_far_global_distance.system())
.run();
}

fn stop_too_far_global_distance(
mut cubes: Query<(&GlobalTransform, &mut SomeEntity, &GlobalStop)>,
) {
for (transform, mut cube, _) in cubes.iter_mut() {
if (transform.translation - cube.spawn).length() >= MAX_DISTANCE {
cube.stopped = true;
}
}
}

fn stop_too_far_local_distance(mut cubes: Query<(&Transform, &mut SomeEntity, &LocalStop)>) {
// when checking the distance between a point with local transform as reference
// we'll get a different outcome due to the fact that this point is in local coordinates
// thus stopping the green cube only after reaching a distance of MAX_DISTANCE
// to its spawn point above the now moved yellow parent block
for (transform, mut cube, _) in cubes.iter_mut() {
if (transform.translation - cube.spawn).length() >= MAX_DISTANCE {
cube.stopped = true;
}
}
}

fn move_cubes(mut cubes: Query<(&mut Transform, &SomeEntity)>, timer: Res<Time>) {
for (mut transform, _) in cubes.iter_mut().filter(|(_, cube)| !cube.stopped) {
// using the local transform we can move an entity along it's local z axis
let dir = transform.local_x();
transform.translation += dir * timer.delta_seconds();
}
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});

commands.spawn_bundle(PointLightBundle {
transform: Transform::from_translation(Vec3::ONE * 3.0),
..Default::default()
});

let main_entity_spawn: Transform = Transform::from_translation(Vec3::ZERO);
let subcomponent_transform = Transform::from_translation(Vec3::Y);
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(StandardMaterial {
base_color: Color::YELLOW,
..Default::default()
}),
transform: main_entity_spawn,
..Default::default()
})
.insert(GlobalStop)
.insert(SomeEntity::new(main_entity_spawn.translation))
.with_children(|child_builder| {
child_builder
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.5 })),
material: materials.add(StandardMaterial {
base_color: Color::RED,
..Default::default()
}),
transform: subcomponent_transform,
..Default::default()
})
.insert(GlobalStop)
.insert(SomeEntity::new(subcomponent_transform.translation));
child_builder
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 0.5 })),
material: materials.add(StandardMaterial {
base_color: Color::GREEN,
..Default::default()
}),
transform: subcomponent_transform,
..Default::default()
})
.insert(LocalStop)
.insert(SomeEntity::new(subcomponent_transform.translation));
});
}
52 changes: 52 additions & 0 deletions examples/transforms/rotation.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use bevy::prelude::*;

use std::f32::consts::PI;

const FULL_TURN: f32 = 2.0 * PI;

struct Rotatable {
speed: f32,
}

fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(rotate_cube.system())
.run();
}

fn rotate_cube(mut cubes: Query<(&mut Transform, &Rotatable)>, timer: Res<Time>) {
for (mut transform, cube) in cubes.iter_mut() {
let rotation_change = Quat::from_rotation_y(FULL_TURN * cube.speed * timer.delta_seconds());
transform.rotate(rotation_change);
}
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});

commands.spawn_bundle(PointLightBundle {
transform: Transform::from_translation(Vec3::ONE * 3.0),
..Default::default()
});

commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(StandardMaterial {
base_color: Color::WHITE,
..Default::default()
}),
transform: Transform::from_translation(Vec3::ZERO),
..Default::default()
})
.insert(Rotatable { speed: 0.3 });
}
57 changes: 57 additions & 0 deletions examples/transforms/scale.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use bevy::prelude::*;

struct Scaling {
scale_axis: Vec3,
}

fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(scale_cube.system())
.run();
}

fn scale_cube(mut cubes: Query<(&mut Transform, &mut Scaling)>, timer: Res<Time>) {
for (mut transform, mut cube) in cubes.iter_mut() {
if transform.scale.length() >= 10.0 {
cube.scale_axis *= -1.0;
} else if transform.scale.length() < Vec3::ONE.length() {
// switch to next axis and expand
transform.scale = Vec3::ONE;
cube.scale_axis *= -1.0;
cube.scale_axis = Vec3::from((cube.scale_axis.z, cube.scale_axis.x, cube.scale_axis.y));
}
transform.scale += cube.scale_axis * timer.delta_seconds();
}
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});

commands.spawn_bundle(PointLightBundle {
transform: Transform::from_translation(Vec3::ONE * 3.0),
..Default::default()
});

commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(StandardMaterial {
base_color: Color::WHITE,
..Default::default()
}),
transform: Transform::from_translation(Vec3::ZERO),
..Default::default()
})
.insert(Scaling {
scale_axis: Vec3::X * 2.0,
});
}
113 changes: 113 additions & 0 deletions examples/transforms/transform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use bevy::prelude::*;

use std::f32::consts::PI;

struct CubeState {
start_pos: Vec3,
move_speed: f32,
turn_speed: f32,
}

struct Center {
min_size: f32,
}

fn main() {
App::build()
.add_plugins(DefaultPlugins)
.add_startup_system(setup.system())
.add_system(move_cube.system())
.add_system(rotate_cube.system())
.add_system(scale_sphere_proportional_to_cube_travel_distance.system())
.run();
}

fn move_cube(mut cubes: Query<(&mut Transform, &mut CubeState)>, timer: Res<Time>) {
for (mut transform, cube) in cubes.iter_mut() {
let forward = transform.forward();
transform.translation += forward * cube.move_speed * timer.delta_seconds();
}
}

fn rotate_cube(mut cubes: Query<(&mut Transform, &mut CubeState)>, timer: Res<Time>) {
for (mut transform, cube) in cubes.iter_mut() {
let full_turn = transform
.looking_at(Vec3::ZERO, transform.local_y())
.rotation;
let incremental_turn_weight = cube.turn_speed * timer.delta_seconds();
transform.rotation = transform.rotation.lerp(full_turn, incremental_turn_weight);
}
}

fn scale_sphere_proportional_to_cube_travel_distance(
mut transformabels: QuerySet<(
Query<(&Transform, &CubeState)>,
Query<(&mut Transform, &Center)>,
)>,
) {
let distance: f32 = transformabels
.q0()
.iter()
.map(|(transform, cube)| (cube.start_pos - transform.translation).length())
.fold(0.0, |x, y| x + y);
for (mut transform, center) in transformabels.q1_mut().iter_mut() {
let new_size = Vec3::ONE - 0.05 * Vec3::ONE * distance;
let min_size = center.min_size * Vec3::ONE;
transform.scale = if new_size.length() < min_size.length() {
min_size
} else {
new_size.abs()
}
}
}

fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn_bundle(PerspectiveCameraBundle {
transform: Transform::from_xyz(0.0, 10.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});

commands.spawn_bundle(PointLightBundle {
transform: Transform::from_translation(Vec3::ONE * 3.0),
..Default::default()
});

// add an object to circle around
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Icosphere {
radius: 3.0,
subdivisions: 32,
})),
material: materials.add(StandardMaterial {
base_color: Color::YELLOW,
..Default::default()
}),
transform: Transform::from_translation(Vec3::ZERO),
..Default::default()
})
.insert(Center { min_size: 0.1 });

// add circling cube
let mut cube_spawn = Transform::from_translation(Vec3::Z * -10.0);
cube_spawn.rotation = Quat::from_rotation_y(PI / 2.0);
commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(StandardMaterial {
base_color: Color::WHITE,
..Default::default()
}),
transform: cube_spawn,
..Default::default()
})
.insert(CubeState {
start_pos: cube_spawn.translation,
move_speed: 2.0,
turn_speed: 0.2,
});
}
Loading

0 comments on commit cc4259a

Please sign in to comment.