Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the matrix types #124

Merged
merged 4 commits into from
Mar 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
840 changes: 840 additions & 0 deletions godot-core/src/builtin/basis.rs

Large diffs are not rendered by default.

38 changes: 28 additions & 10 deletions godot-core/src/builtin/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use std::f32::consts::TAU;

use super::Vector2;

pub const CMP_EPSILON: f32 = 0.00001;

pub fn lerp(a: f32, b: f32, t: f32) -> f32 {
Expand All @@ -23,6 +25,18 @@ pub fn is_equal_approx(a: f32, b: f32) -> bool {
(a - b).abs() < tolerance
}

/// Check if two angles are approximately equal, by comparing the distance
/// between the points on the unit circle with 0 using [`is_equal_approx`].
pub fn is_angle_equal_approx(a: f32, b: f32) -> bool {
let (x1, y1) = a.sin_cos();
let (x2, y2) = b.sin_cos();

is_equal_approx(
Vector2::distance_to(Vector2::new(x1, y1), Vector2::new(x2, y2)),
0.0,
)
}

pub fn is_zero_approx(s: f32) -> bool {
s.abs() < CMP_EPSILON
}
Expand Down Expand Up @@ -190,6 +204,15 @@ mod test {
assert_ne_approx!(1.0, 2.0, is_equal_approx, "Message {}", "formatted");
}

#[test]
fn angle_equal_approx() {
assert_eq_approx!(1.0, 1.000001, is_angle_equal_approx);
assert_eq_approx!(0.0, TAU, is_angle_equal_approx);
assert_eq_approx!(PI, -PI, is_angle_equal_approx);
assert_eq_approx!(4.45783, -(TAU - 4.45783), is_angle_equal_approx);
assert_eq_approx!(31.0 * PI, -13.0 * PI, is_angle_equal_approx);
}

#[test]
#[should_panic(expected = "I am inside format")]
fn eq_approx_fail_with_message() {
Expand All @@ -198,22 +221,17 @@ mod test {

#[test]
fn lerp_angle_test() {
assert_eq_approx!(lerp_angle(0.0, PI, 0.5), -FRAC_PI_2, is_equal_approx);
assert_eq_approx!(lerp_angle(0.0, PI, 0.5), -FRAC_PI_2, is_angle_equal_approx);
assert_eq_approx!(
lerp_angle(0.0, PI + 3.0 * TAU, 0.5),
FRAC_PI_2,
is_equal_approx
is_angle_equal_approx
);
let angle = PI * 2.0 / 3.0;
assert_eq_approx!(
lerp_angle(-5.0 * TAU, angle + 3.0 * TAU, 0.5).sin(),
(angle / 2.0).sin(),
is_equal_approx
);
assert_eq_approx!(
lerp_angle(-5.0 * TAU, angle + 3.0 * TAU, 0.5).cos(),
(angle / 2.0).cos(),
is_equal_approx
lerp_angle(-5.0 * TAU, angle + 3.0 * TAU, 0.5),
(angle / 2.0),
is_angle_equal_approx
);
}
}
8 changes: 8 additions & 0 deletions godot-core/src/builtin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,19 @@
pub use crate::{array, dict, varray};

pub use array_inner::{Array, TypedArray};
pub use basis::*;
pub use color::*;
pub use dictionary_inner::Dictionary;
pub use math::*;
pub use node_path::*;
pub use others::*;
pub use packed_array::*;
pub use projection::*;
pub use quaternion::*;
pub use string::*;
pub use string_name::*;
pub use transform2d::*;
pub use transform3d::*;
pub use variant::*;
pub use vector2::*;
pub use vector2i::*;
Expand Down Expand Up @@ -79,15 +83,19 @@ mod array_inner;
#[path = "dictionary.rs"]
mod dictionary_inner;

mod basis;
mod color;
mod glam_helpers;
mod math;
mod node_path;
mod others;
mod packed_array;
mod projection;
mod quaternion;
mod string;
mod string_name;
mod transform2d;
mod transform3d;
mod variant;
mod vector2;
mod vector2i;
Expand Down
4 changes: 0 additions & 4 deletions godot-core/src/builtin/others.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ impl_builtin_stub!(Rect2, OpaqueRect2);
impl_builtin_stub!(Rect2i, OpaqueRect2i);
impl_builtin_stub!(Plane, OpaquePlane);
impl_builtin_stub!(Aabb, OpaqueAabb);
impl_builtin_stub!(Basis, OpaqueBasis);
impl_builtin_stub!(Transform2D, OpaqueTransform2D);
impl_builtin_stub!(Transform3D, OpaqueTransform3D);
impl_builtin_stub!(Projection, OpaqueProjection);
impl_builtin_stub!(Rid, OpaqueRid);
impl_builtin_stub!(Callable, OpaqueCallable);
impl_builtin_stub!(Signal, OpaqueSignal);
Expand Down
Loading