Skip to content

Commit

Permalink
math extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
mrchantey committed Mar 10, 2024
1 parent 942bda3 commit b453e3f
Show file tree
Hide file tree
Showing 22 changed files with 149 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// "rust-analyzer.cargo.features": [
// "web_sys_unstable_apis"
// ],
"rust-analyzer.cargo.target": "wasm32-unknown-unknown",
// "rust-analyzer.cargo.target": "wasm32-unknown-unknown",
"rust-analyzer.files.excludeDirs": [
"crates\\bevy_webxr"
],
Expand Down
36 changes: 18 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ members = [
exclude = ["crates/bevy_webxr"]

[workspace.package]
version = "0.1.41"
version = "0.1.42"
edition = "2021"
license = "MIT OR Apache-2.0"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion crates/forky/forky_bevy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ bevy_time = { workspace = true }
bevy_transform = { workspace = true }
bevy_hierarchy = { workspace = true }
bevy_utils = { workspace = true }
rand = { workspace = true }
# bevy = { workspace = true, optional = true }

anyhow = { workspace = true }
extend = { workspace = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
forky_web = { workspace = true }
leptos = { workspace = true }
js-sys = { workspace = true }
# wasm-bindgen = { workspace = true, optional = true }
# wasm-bindgen-futures = { workspace = true, optional = true }
Expand Down
1 change: 1 addition & 0 deletions crates/forky/forky_bevy/src/extensions/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub impl App {
fn with_app_res(self) -> RcCell<Self> { AppRes::init(self) }

#[cfg(target_arch = "wasm32")]
#[must_use]
fn run_on_animation_frame(mut self) -> forky_web::AnimationFrame {
forky_web::AnimationFrame::new(move || {
self.update();
Expand Down
2 changes: 1 addition & 1 deletion crates/forky/forky_bevy/src/extensions/quat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub impl Quat {
if angle == 0. {
return self;
};
let t = math::min(1., rad_step / angle);
let t = f32::min(1., rad_step / angle);
self.clone_from(&self.slerp(rhs, t));
return self;
}
Expand Down
33 changes: 31 additions & 2 deletions crates/forky/forky_bevy/src/extensions/vec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use bevy_math::prelude::*;
use extend::ext;
use forky_core::prelude::*;
use rand::Rng;
use std::f32::consts::PI;
use std::f32::consts::TAU;

#[ext]
pub impl Vec3 {
Expand Down Expand Up @@ -57,8 +59,35 @@ pub impl Vec3 {
self
}

/// Random position inside a unit cube
fn random_in_cube() -> Self {
let mut rng = rand::thread_rng();

Vec3::new(
rng.gen_range(-1.0..1.0),
rng.gen_range(-1.0..1.0),
rng.gen_range(-1.0..1.0),
)
}

/// Random position on a unit sphere
fn random_on_sphere() -> Self {
Vec3::new(random_value(), random_value(), random_value()) * 2. - 1.
let mut rng = rand::thread_rng();
let theta = rng.gen_range(0.0..TAU);
let phi = rng.gen_range(0.0..PI);
Vec3::new(phi.sin() * theta.cos(), phi.sin() * theta.sin(), phi.cos())
}

/// Random position inside a unit sphere
fn random_in_sphere() -> Self {
let mut rng = rand::thread_rng();
let theta = rng.gen_range(0.0..TAU);
let phi = rng.gen_range(0.0..PI);
let r = rng.gen_range(0.0f32..1.0).powf(1. / 3.);
Vec3::new(
r * phi.sin() * theta.cos(),
r * phi.sin() * theta.sin(),
r * phi.cos(),
)
}
}
3 changes: 3 additions & 0 deletions crates/forky/forky_bevy/test/extensions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub mod vec;
#[allow(unused_imports)]
pub use self::vec::*;
pub mod app;
#[allow(unused_imports)]
pub use self::app::*;
Expand Down
24 changes: 24 additions & 0 deletions crates/forky/forky_bevy/test/extensions/vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use bevy_math::prelude::*;
use forky_bevy::prelude::*;
use sweet::*;

#[sweet_test]
pub fn works() -> Result<()> {
for i in 0..10 {
let val = Vec3::random_in_cube();
expect(val.length()).to_be_less_than(1.5)?;
// println!("random_in_cube: {val}");
}
for i in 0..10 {
let val = Vec3::random_on_sphere();
expect(val.length()).to_be_close_to(1.)?;
// println!("random_on_sphere: {val}");
}
for i in 0..10 {
let val = Vec3::random_in_sphere();
expect(val.length()).to_be_less_than(1.)?;
// println!("random_in_sphere: {val}");
}

Ok(())
}
10 changes: 3 additions & 7 deletions crates/forky/forky_core/src/extensions/num_x.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use extend::ext;
use std::i32;

#[ext]
pub impl Option<&i32> {
fn or_default(&self) -> &i32 {
match &self {
Some(c) => c,
None => &0,
}
pub impl f32 {
fn lerp(start: Self, end: Self, t: Self) -> Self {
start + (end - start) * t
}
}
1 change: 0 additions & 1 deletion crates/forky/forky_core/src/math/funcs.rs
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
pub fn min(a: f32, b: f32) -> f32 { a.min(b) }
3 changes: 2 additions & 1 deletion crates/forky/forky_core/src/utility/random.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rand::Rng;


/// Returns a random value between 0.0 and 1.0
pub fn random_value() -> f32 { rand::thread_rng().gen_range(0.0..1.0) }
pub fn random_signed() -> f32 { rand::thread_rng().gen_range(-1.0..1.0) }
pub fn random_percent_i32() -> i32 { rand::thread_rng().gen_range(0..100) }
13 changes: 0 additions & 13 deletions crates/forky/forky_core/test/extensions/int_x.rs

This file was deleted.

4 changes: 2 additions & 2 deletions crates/forky/forky_core/test/extensions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ pub use self::str_x::*;
pub mod path;
#[allow(unused_imports)]
pub use self::path::*;
pub mod int_x;
pub mod num_x;
#[allow(unused_imports)]
pub use self::int_x::*;
pub use self::num_x::*;
22 changes: 22 additions & 0 deletions crates/forky/forky_core/test/extensions/num_x.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use forky_core::*;
use sweet::*;

#[sweet_test]
pub fn i32() -> Result<()> {
let a = 1;
let _a = Some(&a);
expect(*_a.unwrap()).to_be(1)?;
expect(*_a.or_default()).to_be(1)?;
let _a: Option<&i32> = None;
expect(*_a.or_default()).to_be(0)?;

Ok(())
}



#[sweet_test]
pub fn works() -> Result<()> {
expect(f32::lerp(1., 2., 0.5)).to_be(1.5)?;
Ok(())
}
2 changes: 1 addition & 1 deletion crates/forky/forky_fs/src/utility/terminal/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn clear_terminal() -> Result<()> {
let mut stdout = stdout();
stdout
.queue(terminal::Clear(terminal::ClearType::All))?
.queue(cursor::Hide)?
// .queue(cursor::Hide)?
.queue(cursor::MoveTo(0, 0))?;
stdout.flush()?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/forky/forky_play/src/physics/slerp_joint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ fn update_slerp(
) {
if timer.0.tick(time.delta()).just_finished() {
for (_, mut slerp) in query.iter_mut() {
slerp.target = Quat::look_at(Vec3::random_on_sphere())
slerp.target = Quat::look_at(Vec3::random_in_cube())
}
}
let d = time.delta_seconds();
for (mut tran, target) in query.iter_mut() {
// tran.rotation = Quat::look_at(Vec3::random_on_sphere());
// tran.rotation = Quat::look_at(Vec3::random_in_cube());
tran.rotation.rotate_towards(target.target, 1. * d);
}
}
4 changes: 2 additions & 2 deletions crates/forky/forky_web/macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = { workspace = true }
authors = { workspace = true }
documentation = { workspace = true }
license = { workspace = true }
readme = "README.md"
description = "macro crate for sweet"
readme = { workspace = true }
description = { workspace = true }
repository = { workspace = true }

[lib]
Expand Down
Loading

0 comments on commit b453e3f

Please sign in to comment.