-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
409 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
Oops, something went wrong.