From 2e69f80b8d8d26442fdc06058ec058675ef0c995 Mon Sep 17 00:00:00 2001 From: Aevyrie Date: Sat, 13 Apr 2024 22:33:45 -0700 Subject: [PATCH] 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 --- Cargo.toml | 13 + examples/debug.rs | 39 +- examples/demo.rs | 22 +- examples/error.rs | 51 +- examples/error_child.rs | 100 ++++ examples/planets.rs | 193 ++++++++ src/camera.rs | 28 +- src/debug.rs | 51 +- src/lib.rs | 301 ++++-------- src/propagation.rs | 188 ++++--- src/reference_frame/local_origin.rs | 735 ++++++++++++++++++++++++++++ src/reference_frame/mod.rs | 173 +++++++ src/world_query.rs | 28 +- 13 files changed, 1576 insertions(+), 346 deletions(-) create mode 100644 examples/error_child.rs create mode 100644 examples/planets.rs create mode 100644 src/reference_frame/local_origin.rs create mode 100644 src/reference_frame/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 15c508d..6b795ed 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,6 +23,7 @@ bevy = { version = "0.13", default-features = false, features = [ "tonemapping_luts", ] } bevy_framepace = { version = "0.15", default-features = false } +rand = "0.8.5" [features] default = ["debug", "camera", "bevy_render"] @@ -47,3 +48,15 @@ name = "error" path = "examples/error.rs" required-features = ["default"] doc-scrape-examples = true + +[[example]] +name = "error_child" +path = "examples/error_child.rs" +required-features = ["default"] +doc-scrape-examples = true + +[[example]] +name = "planets" +path = "examples/planets.rs" +required-features = ["default"] +doc-scrape-examples = true diff --git a/examples/debug.rs b/examples/debug.rs index 1127bc4..162b7d9 100644 --- a/examples/debug.rs +++ b/examples/debug.rs @@ -1,7 +1,7 @@ #![allow(clippy::type_complexity)] use bevy::prelude::*; -use big_space::{FloatingOrigin, GridCell}; +use big_space::{reference_frame::ReferenceFrame, FloatingOrigin, GridCell}; fn main() { App::new() @@ -25,22 +25,24 @@ fn movement( Query<&mut Transform, With>>, Query<&mut Transform, With>>, Query<&mut Transform, With>>, + Query<&mut Transform, With>>, )>, ) { - let delta_translation = |offset: f32| -> Vec3 { - let t_1 = time.elapsed_seconds() + offset; - let dt = time.delta_seconds(); + 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); - let p1 = pos(t_1); + let p0 = pos(t_0) * scale; + let p1 = pos(t_1) * scale; p1 - p0 }; - q.p0().single_mut().translation += delta_translation(20.0); - q.p1().single_mut().translation += delta_translation(251.0); - q.p2().single_mut().translation += delta_translation(812.0); + 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)] @@ -48,7 +50,7 @@ struct Rotator; fn rotation(time: Res