Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
124: Implement the matrix types r=Bromeon a=sayaks

Implement the four matrix types godot has: `Basis`, `Transform2D`, `Transform3D`, `Projection`.

`Transform2D` in godot consists of an array of 3 `Vector2`s, where the first two are the basis.
This means godot has some methods named `Transform2D::basis_<something>()`. I
decided to instead make a struct `Basis2D`. so now any method like the above would be
called `trans.basis.<something>()` instead.

`Projection` has a lot of intricate math for many of its functions so it is largely
a wrapper around `InnerProjection`, except for some methods that were
easy to reimplement.

Depends on godot-rust#128 

Co-authored-by: Lili Zoey <[email protected]>
  • Loading branch information
bors[bot] and lilizoey authored Mar 4, 2023
2 parents e573ba8 + 8232785 commit 72870d1
Show file tree
Hide file tree
Showing 14 changed files with 2,771 additions and 26 deletions.
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

0 comments on commit 72870d1

Please sign in to comment.