cyber_rider/src/lib.rs

48 lines
1.2 KiB
Rust
Raw Normal View History

2022-01-14 06:05:51 +00:00
use bevy::{
ecs::schedule::StageLabel,
2022-01-17 21:40:31 +00:00
prelude::{ResMut, SystemLabel, Vec3, Windows},
2022-01-14 06:05:51 +00:00
};
2022-01-19 04:08:47 +00:00
use heron::rapier_plugin::nalgebra::ComplexField;
2022-01-14 06:05:51 +00:00
pub mod action;
pub mod camera;
pub mod geometry;
2022-01-14 06:05:51 +00:00
pub mod glamor;
pub mod input;
pub mod lights;
pub mod ui;
2022-01-14 06:05:51 +00:00
#[derive(Clone, Debug, Hash, PartialEq, Eq, SystemLabel, StageLabel)]
pub enum Label {
Geometry,
Glamor,
Input,
Action,
}
pub fn disable_mouse_trap(mut windows: ResMut<Windows>) {
let window = windows.get_primary_mut().unwrap();
window.set_cursor_lock_mode(false);
window.set_cursor_visibility(true);
}
2022-01-17 21:40:31 +00:00
pub fn random_sphere_vec(r: &mut impl rand::prelude::Rng) -> Vec3 {
// https://mathworld.wolfram.com/SpherePointPicking.html
// Marsaglia (1972) for picking x1 and x2 from (-1, 1) and generating surface
// points directly if their sum is less than 1.
let mut x1 = 0.0;
let mut x2 = 0.0;
let mut ssum = 2.0;
while ssum >= 1.0 {
x1 = r.gen_range(-1.0..=1.0);
x2 = r.gen_range(-1.0..=1.0);
ssum = x1.powi(2) + x2.powi(2);
}
let sqrt = (1.0 - x1.powi(2) - x2.powi(2)).sqrt();
let x = 2.0 * x1 * sqrt;
let y = 2.0 * x2 * sqrt;
let z = 1.0 - 2.0 * ssum;
2022-01-19 04:08:47 +00:00
Vec3::new(x, y, z).normalize()
2022-01-17 21:40:31 +00:00
}