Aevyrie 2e69f80b8d
Reference Frames (#16)
Adds the concept of reference frames, allowing hierarchies of high
precision objects, e.g. objects in the reference frame of a planet,
which is itself rotating, and orbiting about a star.

---------

Co-authored-by: Oliver Scherer <github@oli-obk.de>
2024-04-13 22:33:45 -07:00

134 lines
3.9 KiB
Rust

#![allow(clippy::type_complexity)]
use bevy::prelude::*;
use big_space::{reference_frame::ReferenceFrame, FloatingOrigin, GridCell};
fn main() {
App::new()
.add_plugins((
DefaultPlugins.build().disable::<TransformPlugin>(),
big_space::FloatingOriginPlugin::<i64>::new(0.5, 0.01),
big_space::debug::FloatingOriginDebugPlugin::<i64>::default(),
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, setup)
.add_systems(Update, (movement, rotation))
.run()
}
#[derive(Component)]
struct Mover<const N: usize>;
fn movement(
time: Res<Time>,
mut q: ParamSet<(
Query<&mut Transform, With<Mover<1>>>,
Query<&mut Transform, With<Mover<2>>>,
Query<&mut Transform, With<Mover<3>>>,
Query<&mut Transform, With<Mover<4>>>,
)>,
) {
let delta_translation = |offset: f32, scale: f32| -> Vec3 {
let t_1 = time.elapsed_seconds() * 0.1 + offset;
let dt = time.delta_seconds() * 0.1;
let t_0 = t_1 - dt;
let pos =
|t: f32| -> Vec3 { Vec3::new(t.cos() * 2.0, t.sin() * 2.0, (t * 1.3).sin() * 2.0) };
let p0 = pos(t_0) * scale;
let p1 = pos(t_1) * scale;
p1 - p0
};
q.p0().single_mut().translation += delta_translation(20.0, 1.0);
q.p1().single_mut().translation += delta_translation(251.0, 1.0);
q.p2().single_mut().translation += delta_translation(812.0, 1.0);
q.p3().single_mut().translation += delta_translation(863.0, 0.4);
}
#[derive(Component)]
struct Rotator;
fn rotation(time: Res<Time>, mut query: Query<&mut Transform, With<Rotator>>) {
for mut transform in &mut query {
transform.rotate_z(3.0 * time.delta_seconds() * 0.2);
}
}
fn setup(
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
let mesh_handle = meshes.add(Sphere::new(0.1).mesh().ico(16).unwrap());
let matl_handle = materials.add(StandardMaterial {
base_color: Color::YELLOW,
..default()
});
commands.spawn((
PbrBundle {
mesh: mesh_handle.clone(),
material: matl_handle.clone(),
transform: Transform::from_xyz(0.0, 0.0, 1.0),
..default()
},
GridCell::<i64>::default(),
Mover::<1>,
));
commands.spawn((
PbrBundle {
mesh: mesh_handle.clone(),
material: matl_handle.clone(),
transform: Transform::from_xyz(1.0, 0.0, 0.0),
..default()
},
GridCell::<i64>::default(),
Mover::<2>,
));
commands
.spawn((
PbrBundle {
mesh: mesh_handle.clone(),
material: matl_handle.clone(),
transform: Transform::from_xyz(0.0, 1.0, 0.0),
..default()
},
GridCell::<i64>::default(),
ReferenceFrame::<i64>::new(0.2, 0.01),
Rotator,
Mover::<3>,
))
.with_children(|parent| {
parent.spawn((
PbrBundle {
mesh: mesh_handle,
material: matl_handle,
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..default()
},
GridCell::<i64>::default(),
Mover::<4>,
));
});
// light
commands.spawn((
PointLightBundle {
transform: Transform::from_xyz(4.0, 8.0, 4.0),
..default()
},
GridCell::<i64>::default(),
));
// camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 8.0)
.looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
..default()
},
GridCell::<i64>::default(),
FloatingOrigin,
));
}