smooth_follow.rs:
use bevy::{
math::{prelude::*, vec3, NormedVectorSpace},
prelude::*,
};
use rand::SeedableRng;
use rand_chacha::ChaCha8Rng;
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (move_target, move_follower).chain())
.run();
}
#[derive(Component)]
struct TargetSphere;
#[derive(Resource)]
struct TargetSphereSpeed(f32);
#[derive(Resource)]
struct TargetPosition(Vec3);
#[derive(Resource)]
struct DecayRate(f32);
#[derive(Component)]
struct FollowingSphere;
#[derive(Resource)]
struct RandomSource(ChaCha8Rng);
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn((
Mesh3d(meshes.add(Plane3d::default().mesh().size(12.0, 12.0))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.15, 0.3))),
Transform::from_xyz(0.0, -2.5, 0.0),
));
commands.spawn((
Mesh3d(meshes.add(Sphere::new(0.3))),
MeshMaterial3d(materials.add(Color::srgb(0.3, 0.15, 0.9))),
TargetSphere,
));
commands.spawn((
Mesh3d(meshes.add(Sphere::new(0.3))),
MeshMaterial3d(materials.add(Color::srgb(0.9, 0.3, 0.3))),
Transform::from_translation(vec3(0.0, -2.0, 0.0)),
FollowingSphere,
));
commands.spawn((
PointLight {
intensity: 15_000_000.0,
shadows_enabled: true,
..default()
},
Transform::from_xyz(4.0, 8.0, 4.0),
));
commands.spawn((
Camera3d::default(),
Transform::from_xyz(-2.0, 3.0, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
));
commands.insert_resource(TargetSphereSpeed(5.0));
commands.insert_resource(DecayRate(2.0));
commands.insert_resource(TargetPosition(Vec3::ZERO));
commands.insert_resource(RandomSource(ChaCha8Rng::seed_from_u64(68941654987813521)));
}
fn move_target(
mut target: Single<&mut Transform, With<TargetSphere>>,
target_speed: Res<TargetSphereSpeed>,
mut target_pos: ResMut<TargetPosition>,
time: Res<Time>,
mut rng: ResMut<RandomSource>,
) {
match Dir3::new(target_pos.0 - target.translation) {
Ok(dir) => {
let delta_time = time.delta_secs();
let abs_delta = (target_pos.0 - target.translation).norm();
let magnitude = f32::min(abs_delta, delta_time * target_speed.0);
target.translation += dir * magnitude;
}
Err(_) => {
let legal_region = Cuboid::from_size(Vec3::splat(4.0));
*target_pos = TargetPosition(legal_region.sample_interior(&mut rng.0));
}
}
}
fn move_follower(
mut following: Single<&mut Transform, With<FollowingSphere>>,
target: Single<&Transform, (With<TargetSphere>, Without<FollowingSphere>)>,
decay_rate: Res<DecayRate>,
time: Res<Time>,
) {
let decay_rate = decay_rate.0;
let delta_time = time.delta_secs();
following
.translation
.smooth_nudge(&target.translation, decay_rate, delta_time);
}