Example improvements

This commit is contained in:
Aevyrie 2024-05-06 00:16:25 -07:00
parent 2eb87f948d
commit cf926e7edb
No known key found for this signature in database
GPG Key ID: 6BBB78EF9FF0883A
2 changed files with 35 additions and 17 deletions

View File

@ -6,6 +6,7 @@ use bevy::{
use big_space::{
camera::{CameraController, CameraInput},
propagation::IgnoreFloatingOrigin,
reference_frame::RootReferenceFrame,
world_query::GridTransformReadOnly,
FloatingOrigin, GridCell,
};
@ -21,10 +22,10 @@ fn main() {
))
.insert_resource(ClearColor(Color::BLACK))
.add_systems(Startup, (setup, ui_setup))
.add_systems(Update, (cursor_grab_system, ui_text_system))
.add_systems(PreUpdate, cursor_grab_system)
.add_systems(
PostUpdate,
highlight_nearest_sphere.after(TransformSystem::TransformPropagate),
(highlight_nearest_sphere, ui_text_system).after(TransformSystem::TransformPropagate),
)
.run()
}
@ -98,7 +99,7 @@ fn ui_setup(mut commands: Commands) {
TextBundle::from_section(
"",
TextStyle {
font_size: 28.0,
font_size: 18.0,
color: Color::WHITE,
..default()
},
@ -148,9 +149,9 @@ fn highlight_nearest_sphere(
return;
};
// Ignore rotation due to panicking in gizmos, as of bevy 0.13
let (scale, _, translation) = transform.to_scale_rotation_translation();
let (scale, rotation, translation) = transform.to_scale_rotation_translation();
gizmos
.sphere(translation, Quat::IDENTITY, scale.x * 0.505, Color::RED)
.sphere(translation, rotation, scale.x * 0.505, Color::RED)
.circle_segments(128);
}
@ -165,6 +166,7 @@ fn ui_text_system(
origin: Query<GridTransformReadOnly<i128>, With<FloatingOrigin>>,
camera: Query<&CameraController>,
objects: Query<&Transform, With<Handle<Mesh>>>,
reference_frame: Res<RootReferenceFrame<i128>>,
) {
let origin = origin.single();
let translation = origin.transform.translation;
@ -175,10 +177,20 @@ fn ui_text_system(
);
let translation_text = format!(
"Transform: {:>8.2}x, {:>8.2}y, {:>8.2}z",
"Transform: {}x, {}y, {}z",
translation.x, translation.y, translation.z
);
let real_position = reference_frame.grid_position_double(origin.cell, origin.transform);
let real_position_f64_text = format!(
"Combined (f64): {}x, {}y, {}z",
real_position.x, real_position.y, real_position.z
);
let real_position_f32_text = format!(
"Combined (f32): {}x, {}y, {}z",
real_position.x as f32, real_position.y as f32, real_position.z as f32
);
let velocity = camera.single().velocity();
let speed = velocity.0.length() / time.delta_seconds_f64();
let camera_text = if speed > 3.0e8 {
@ -204,8 +216,9 @@ fn ui_text_system(
let mut debug_text = debug_text.single_mut();
debug_text.0.sections[0].value =
format!("{grid_text}\n{translation_text}\n{camera_text}\n{nearest_text}");
debug_text.0.sections[0].value = format!(
"{grid_text}\n{translation_text}\n\n{real_position_f64_text}\n{real_position_f32_text}\n\n{camera_text}\n{nearest_text}"
);
fun_text.single_mut().sections[0].value = fact_text
}

View File

@ -17,10 +17,12 @@ fn main() {
.run()
}
// The distance being used to test precision. A sphere is placed at this position, and a child is
// added in the opposite direction. This should sum to zero if we had infinite precision.
const DISTANT: DVec3 = DVec3::new(1e17, 0.0, 0.0);
const ORIGIN: DVec3 = DVec3::new(200.0, 0.0, 0.0);
// The nearby object is 200 meters away from us. The distance object is 100 quadrillion meters away
// from us, and has a child that is 100 quadrillion meters toward us (relative its parent) minus 200
// meters. If we had infinite precision, the child of the distant object would be at the same
// position as the nearby object, only 200 meters away.
const DISTANT: DVec3 = DVec3::new(1e17, 1e17, 0.0);
const NEARBY: DVec3 = DVec3::new(200.0, 200.0, 0.0);
fn setup_scene(
mut commands: Commands,
@ -34,19 +36,22 @@ fn setup_scene(
..default()
});
// A red sphere located at the origin
// A red sphere located nearby
commands.spawn((
PbrBundle {
mesh: mesh_handle.clone(),
material: materials.add(Color::RED),
transform: Transform::from_translation(ORIGIN.as_vec3()),
transform: Transform::from_translation(NEARBY.as_vec3()),
..default()
},
GridCell::<i128>::default(),
));
let parent = root.translation_to_grid(DISTANT);
let child = root.translation_to_grid(-DISTANT + ORIGIN);
// This function introduces a small amount of error, because it can only work up to double
// precision floats. (f64).
let child = root.translation_to_grid(-DISTANT + NEARBY);
commands
.spawn((
// A sphere very far from the origin
@ -86,8 +91,8 @@ fn setup_scene(
// camera
commands.spawn((
Camera3dBundle {
transform: Transform::from_translation(ORIGIN.as_vec3() + Vec3::new(0.0, 0.0, 8.0))
.looking_at(Vec3::ZERO, Vec3::Y),
transform: Transform::from_translation(NEARBY.as_vec3() + Vec3::new(0.0, 0.0, 20.0))
.looking_at(NEARBY.as_vec3(), Vec3::Y),
..default()
},
GridCell::<i128>::default(),