From 11b3ea6ba2b14a6f188594e17fefdf5b43785fc6 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sat, 10 Aug 2024 22:00:07 +0200 Subject: [PATCH 1/3] Update dependencies to next incompatible version Do not apply non-breaking updates, as that shrinks the compatible version range --- godot-codegen/Cargo.toml | 2 +- godot-core/Cargo.toml | 2 +- godot-macros/Cargo.toml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/godot-codegen/Cargo.toml b/godot-codegen/Cargo.toml index ada9686b0..b3144f72a 100644 --- a/godot-codegen/Cargo.toml +++ b/godot-codegen/Cargo.toml @@ -22,7 +22,7 @@ experimental-godot-api = [] [dependencies] godot-bindings = { path = "../godot-bindings", version = "=0.1.3" } -heck = "0.4" +heck = "0.5" nanoserde = "0.1.35" # Minimum versions compatible with -Zminimal-versions diff --git a/godot-core/Cargo.toml b/godot-core/Cargo.toml index 390edd12f..2013a247a 100644 --- a/godot-core/Cargo.toml +++ b/godot-core/Cargo.toml @@ -43,7 +43,7 @@ api-4-2-2 = ["godot-ffi/api-4-2-2"] godot-ffi = { path = "../godot-ffi", version = "=0.1.3" } # See https://docs.rs/glam/latest/glam/index.html#feature-gates -glam = { version = "0.27", features = ["debug-glam-assert"] } +glam = { version = "0.28", features = ["debug-glam-assert"] } serde = { version = "1", features = ["derive"], optional = true } godot-cell = { path = "../godot-cell", version = "=0.1.3" } diff --git a/godot-macros/Cargo.toml b/godot-macros/Cargo.toml index a1b4dd9ff..363ef68a2 100644 --- a/godot-macros/Cargo.toml +++ b/godot-macros/Cargo.toml @@ -23,7 +23,7 @@ proc-macro2 = "1.0.80" # Literal::c_string() added in 1.0.80. quote = "1.0.29" # Enabled by `docs` -markdown = { version = "1.0.0-alpha.17", optional = true } +markdown = { version = "1.0.0-alpha.19", optional = true } venial = "0.6" [build-dependencies] From 0cc5036f6d9b8755063622259e0995cab30a35c7 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sat, 10 Aug 2024 23:43:44 +0200 Subject: [PATCH 2/3] glam: Vec2::angle_between() deprecated -> angle_to() --- godot-core/src/builtin/vectors/vector2.rs | 38 +++++++++++-------- godot-core/src/builtin/vectors/vector3.rs | 16 +++++++- .../src/builtin/vectors/vector_macros.rs | 6 --- 3 files changed, 37 insertions(+), 23 deletions(-) diff --git a/godot-core/src/builtin/vectors/vector2.rs b/godot-core/src/builtin/vectors/vector2.rs index 0fe7c26fe..5cb7452db 100644 --- a/godot-core/src/builtin/vectors/vector2.rs +++ b/godot-core/src/builtin/vectors/vector2.rs @@ -77,6 +77,21 @@ impl Vector2 { v.cast_float() } + /// Creates a unit Vector2 rotated to the given `angle` in radians. This is equivalent to doing `Vector2::new(angle.cos(), angle.sin())` + /// or `Vector2::RIGHT.rotated(angle)`. + /// + /// ```no_run + /// use godot::prelude::*; + /// + /// let a = Vector2::from_angle(0.0); // (1.0, 0.0) + /// let b = Vector2::new(1.0, 0.0).angle(); // 0.0 + /// let c = Vector2::from_angle(real_consts::PI / 2.0); // (0.0, 1.0) + /// ``` + #[inline] + pub fn from_angle(angle: real) -> Self { + Self::from_glam(RVec2::from_angle(angle)) + } + /// Returns this vector's angle with respect to the positive X axis, or `(1.0, 0.0)` vector, in radians. /// /// For example, `Vector2::RIGHT.angle()` will return zero, `Vector2::DOWN.angle()` will return `PI / 2` (a quarter turn, or 90 degrees), @@ -90,6 +105,14 @@ impl Vector2 { self.y.atan2(self.x) } + /// Returns the **signed** angle between `self` and the given vector, as radians in `[-π, +π]`. + /// + /// Note that behavior is different from 3D [`Vector3::angle_to()`] which returns the **unsigned** angle. + #[inline] + pub fn angle_to(self, to: Self) -> real { + self.glam2(&to, |a, b| a.angle_to(b)) + } + /// Returns the angle to the given vector, in radians. /// /// [Illustration of the returned angle.](https://raw.githubusercontent.com/godotengine/godot-docs/master/img/vector2_angle_to.png) @@ -111,21 +134,6 @@ impl Vector2 { self.to_glam().perp_dot(with.to_glam()) } - /// Creates a unit Vector2 rotated to the given `angle` in radians. This is equivalent to doing `Vector2::new(angle.cos(), angle.sin())` - /// or `Vector2::RIGHT.rotated(angle)`. - /// - /// ```no_run - /// use godot::prelude::*; - /// - /// let a = Vector2::from_angle(0.0); // (1.0, 0.0) - /// let b = Vector2::new(1.0, 0.0).angle(); // 0.0 - /// let c = Vector2::from_angle(real_consts::PI / 2.0); // (0.0, 1.0) - /// ``` - #[inline] - pub fn from_angle(angle: real) -> Self { - Self::from_glam(RVec2::from_angle(angle)) - } - /// Returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length. #[inline] pub fn orthogonal(self) -> Self { diff --git a/godot-core/src/builtin/vectors/vector3.rs b/godot-core/src/builtin/vectors/vector3.rs index 617c1c7e3..9d42e09e2 100644 --- a/godot-core/src/builtin/vectors/vector3.rs +++ b/godot-core/src/builtin/vectors/vector3.rs @@ -180,8 +180,20 @@ impl Vector3 { Basis::from_axis_angle(axis, angle) * self } - /// Returns the signed angle to the given vector, in radians. The sign of the angle is positive in a counter-clockwise direction and - /// negative in a clockwise direction when viewed from the side specified by the `axis`. + /// Returns the **unsigned** angle between `self` and the given vector, as radians in `[0, +π]`. + /// + /// Note that behavior is different from 2D [`Vector2::angle_to()`], which returns the **signed** angle. + #[inline] + pub fn angle_to(self, to: Self) -> real { + self.glam2(&to, |a, b| a.angle_between(b)) + } + + /// Returns the signed angle to the given vector, as radians in `[-π, +π]`. + /// + /// The sign of the angle is positive in a counter-clockwise direction and negative in a clockwise direction, when viewed from + /// the side specified by the `axis`. + /// + /// For unsigned angles, use [`Vector3::angle_to()`]. #[inline] pub fn signed_angle_to(self, to: Self, axis: Self) -> real { let cross_to = self.cross(to); diff --git a/godot-core/src/builtin/vectors/vector_macros.rs b/godot-core/src/builtin/vectors/vector_macros.rs index 59b0c02dd..043be4e27 100644 --- a/godot-core/src/builtin/vectors/vector_macros.rs +++ b/godot-core/src/builtin/vectors/vector_macros.rs @@ -1009,12 +1009,6 @@ macro_rules! impl_vector2_vector3_fns { /// # 2D and 3D functions /// The following methods are available on both 2D and 3D float vectors. impl $Vector { - /// Returns the angle to the given vector, in radians. - #[inline] - pub fn angle_to(self, to: Self) -> real { - self.glam2(&to, |a, b| a.angle_between(b)) - } - /// Returns the derivative at the given `t` on the [Bézier](https://en.wikipedia.org/wiki/B%C3%A9zier_curve) /// curve defined by this vector and the given `control_1`, `control_2`, and `end` points. #[inline] From be05a7cf11df970ef9fde75f347ae774c5ea8dec Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sat, 10 Aug 2024 23:46:47 +0200 Subject: [PATCH 3/3] More examples in Vector2/Vector3 angle tests --- .../geometry/vector_test/vector2_test.rs | 35 +++++++++--- .../geometry/vector_test/vector3_test.rs | 55 ++++++++++++------- 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/itest/rust/src/builtin_tests/geometry/vector_test/vector2_test.rs b/itest/rust/src/builtin_tests/geometry/vector_test/vector2_test.rs index d39135b55..fbaf9f398 100644 --- a/itest/rust/src/builtin_tests/geometry/vector_test/vector2_test.rs +++ b/itest/rust/src/builtin_tests/geometry/vector_test/vector2_test.rs @@ -7,13 +7,10 @@ use crate::framework::itest; -use godot::builtin::{ - inner::InnerVector2, - math::{assert_eq_approx, ApproxEq}, - real, - real_consts::PI, - Vector2, Vector2Axis, -}; +use godot::builtin::inner::InnerVector2; +use godot::builtin::math::{assert_eq_approx, ApproxEq}; +use godot::builtin::real_consts::{FRAC_PI_2, PI}; +use godot::builtin::{real, Vector2, Vector2Axis}; #[itest] fn abs() { @@ -25,8 +22,16 @@ fn abs() { #[itest] fn angle() { let a = Vector2::new(1.2, -3.4); + let b = Vector2::new(-5.6, 7.8); assert_eq!(a.angle(), a.as_inner().angle() as real); + assert_eq!(b.angle(), b.as_inner().angle() as real); + + // Check direction (note: DOWN=(0, 1)). + assert_eq_approx!(Vector2::RIGHT.angle(), 0.0); + assert_eq_approx!(Vector2::DOWN.angle(), FRAC_PI_2); + assert_eq_approx!(Vector2::LEFT.angle(), PI); + assert_eq_approx!(Vector2::UP.angle(), -FRAC_PI_2); } #[itest] @@ -35,6 +40,11 @@ fn angle_to() { let b = Vector2::new(-5.6, 7.8); assert_eq_approx!(a.angle_to(b), a.as_inner().angle_to(b) as real); + assert_eq_approx!(b.angle_to(a), b.as_inner().angle_to(a) as real); + + // Check direction (note: DOWN=(0, 1)). + assert_eq_approx!(Vector2::RIGHT.angle_to(Vector2::DOWN), FRAC_PI_2); + assert_eq_approx!(Vector2::DOWN.angle_to(Vector2::RIGHT), -FRAC_PI_2); } #[itest] @@ -43,6 +53,17 @@ fn angle_to_point() { let b = Vector2::new(-5.6, 7.8); assert_eq!(a.angle_to_point(b), a.as_inner().angle_to_point(b) as real); + assert_eq!(b.angle_to_point(a), b.as_inner().angle_to_point(a) as real); + + // Check absolute value. + assert_eq_approx!( + Vector2::new(1.0, 1.0).angle_to_point(Vector2::new(1.0, 2.0)), + FRAC_PI_2 + ); // up + assert_eq_approx!( + Vector2::new(1.0, 1.0).angle_to_point(Vector2::new(1.0, -1.0)), + -FRAC_PI_2 + ); // down } #[itest] diff --git a/itest/rust/src/builtin_tests/geometry/vector_test/vector3_test.rs b/itest/rust/src/builtin_tests/geometry/vector_test/vector3_test.rs index 481d5e02e..0d756ba0a 100644 --- a/itest/rust/src/builtin_tests/geometry/vector_test/vector3_test.rs +++ b/itest/rust/src/builtin_tests/geometry/vector_test/vector3_test.rs @@ -7,13 +7,11 @@ use crate::framework::itest; -use godot::builtin::{ - inner::InnerVector3, - math::{assert_eq_approx, ApproxEq}, - real, - real_consts::PI, - Vector3, Vector3Axis, -}; +use godot::builtin::inner::InnerVector3; +use godot::builtin::math::{assert_eq_approx, ApproxEq}; +use godot::builtin::real; +use godot::builtin::real_consts::{FRAC_PI_4, PI}; +use godot::builtin::{Vector3, Vector3Axis}; #[itest] fn abs() { @@ -28,6 +26,36 @@ fn angle_to() { let b = Vector3::new(-7.8, 9.1, -11.12); assert_eq_approx!(a.angle_to(b), a.as_inner().angle_to(b) as real); + + // Concrete example (135°). + let right = Vector3::new(1.0, 0.0, 0.0); + let back_left = Vector3::new(-1.0, 0.0, 1.0); + + assert_eq_approx!(right.angle_to(back_left), 3.0 * FRAC_PI_4); + assert_eq_approx!(back_left.angle_to(right), 3.0 * FRAC_PI_4); +} + +#[itest] +fn signed_angle_to() { + let a = Vector3::new(1.0, 1.0, 0.0); + let b = Vector3::new(1.0, 1.0, 1.0); + let c = Vector3::UP; + + assert_eq_approx!( + a.signed_angle_to(b, c), + a.as_inner().signed_angle_to(b, c) as real, + "signed_angle_to\n", + ); + + // Concrete example (135°). + let right = Vector3::new(1.0, 0.0, 0.0); + let back_left = Vector3::new(-1.0, 0.0, 1.0); + + let pi_3_4 = 3.0 * FRAC_PI_4; + assert_eq_approx!(right.signed_angle_to(back_left, Vector3::UP), -pi_3_4); + assert_eq_approx!(right.signed_angle_to(back_left, Vector3::DOWN), pi_3_4); + assert_eq_approx!(back_left.signed_angle_to(right, Vector3::UP), pi_3_4); + assert_eq_approx!(back_left.signed_angle_to(right, Vector3::DOWN), -pi_3_4); } #[itest] @@ -402,19 +430,6 @@ fn sign() { assert_eq!(b.sign(), b.as_inner().sign()); } -#[itest] -fn signed_angle_to() { - let a = Vector3::new(1.0, 1.0, 0.0); - let b = Vector3::new(1.0, 1.0, 1.0); - let c = Vector3::UP; - - assert_eq_approx!( - a.signed_angle_to(b, c), - a.as_inner().signed_angle_to(b, c) as real, - "signed_angle_to\n", - ); -} - #[itest] fn slerp() { let a = Vector3::new(1.2, -3.4, 5.6);