Skip to content

Commit c6d5205

Browse files
authored
Merge pull request #860 from godot-rust/qol/update-deps
Dependency update, more tests for vector angle functions
2 parents e4ca34d + be05a7c commit c6d5205

File tree

8 files changed

+103
-53
lines changed

8 files changed

+103
-53
lines changed

godot-codegen/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ experimental-threads = []
2323
[dependencies]
2424
godot-bindings = { path = "../godot-bindings", version = "=0.1.3" }
2525

26-
heck = "0.4"
26+
heck = "0.5"
2727
nanoserde = "0.1.35"
2828

2929
# Minimum versions compatible with -Zminimal-versions

godot-core/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ api-4-3 = ["godot-ffi/api-4-3"]
4444
godot-ffi = { path = "../godot-ffi", version = "=0.1.3" }
4545

4646
# See https://docs.rs/glam/latest/glam/index.html#feature-gates
47-
glam = { version = "0.27", features = ["debug-glam-assert"] }
47+
glam = { version = "0.28", features = ["debug-glam-assert"] }
4848
serde = { version = "1", features = ["derive"], optional = true }
4949
godot-cell = { path = "../godot-cell", version = "=0.1.3" }
5050

godot-core/src/builtin/vectors/vector2.rs

+23-15
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ impl Vector2 {
7777
v.cast_float()
7878
}
7979

80+
/// Creates a unit Vector2 rotated to the given `angle` in radians. This is equivalent to doing `Vector2::new(angle.cos(), angle.sin())`
81+
/// or `Vector2::RIGHT.rotated(angle)`.
82+
///
83+
/// ```no_run
84+
/// use godot::prelude::*;
85+
///
86+
/// let a = Vector2::from_angle(0.0); // (1.0, 0.0)
87+
/// let b = Vector2::new(1.0, 0.0).angle(); // 0.0
88+
/// let c = Vector2::from_angle(real_consts::PI / 2.0); // (0.0, 1.0)
89+
/// ```
90+
#[inline]
91+
pub fn from_angle(angle: real) -> Self {
92+
Self::from_glam(RVec2::from_angle(angle))
93+
}
94+
8095
/// Returns this vector's angle with respect to the positive X axis, or `(1.0, 0.0)` vector, in radians.
8196
///
8297
/// 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 {
90105
self.y.atan2(self.x)
91106
}
92107

108+
/// Returns the **signed** angle between `self` and the given vector, as radians in `[-π, +π]`.
109+
///
110+
/// Note that behavior is different from 3D [`Vector3::angle_to()`] which returns the **unsigned** angle.
111+
#[inline]
112+
pub fn angle_to(self, to: Self) -> real {
113+
self.glam2(&to, |a, b| a.angle_to(b))
114+
}
115+
93116
/// Returns the angle to the given vector, in radians.
94117
///
95118
/// [Illustration of the returned angle.](https://raw.githubusercontent.com/godotengine/godot-docs/master/img/vector2_angle_to.png)
@@ -111,21 +134,6 @@ impl Vector2 {
111134
self.to_glam().perp_dot(with.to_glam())
112135
}
113136

114-
/// Creates a unit Vector2 rotated to the given `angle` in radians. This is equivalent to doing `Vector2::new(angle.cos(), angle.sin())`
115-
/// or `Vector2::RIGHT.rotated(angle)`.
116-
///
117-
/// ```no_run
118-
/// use godot::prelude::*;
119-
///
120-
/// let a = Vector2::from_angle(0.0); // (1.0, 0.0)
121-
/// let b = Vector2::new(1.0, 0.0).angle(); // 0.0
122-
/// let c = Vector2::from_angle(real_consts::PI / 2.0); // (0.0, 1.0)
123-
/// ```
124-
#[inline]
125-
pub fn from_angle(angle: real) -> Self {
126-
Self::from_glam(RVec2::from_angle(angle))
127-
}
128-
129137
/// Returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length.
130138
#[inline]
131139
pub fn orthogonal(self) -> Self {

godot-core/src/builtin/vectors/vector3.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,20 @@ impl Vector3 {
180180
Basis::from_axis_angle(axis, angle) * self
181181
}
182182

183-
/// Returns the signed angle to the given vector, in radians. The sign of the angle is positive in a counter-clockwise direction and
184-
/// negative in a clockwise direction when viewed from the side specified by the `axis`.
183+
/// Returns the **unsigned** angle between `self` and the given vector, as radians in `[0, +π]`.
184+
///
185+
/// Note that behavior is different from 2D [`Vector2::angle_to()`], which returns the **signed** angle.
186+
#[inline]
187+
pub fn angle_to(self, to: Self) -> real {
188+
self.glam2(&to, |a, b| a.angle_between(b))
189+
}
190+
191+
/// Returns the signed angle to the given vector, as radians in `[-π, +π]`.
192+
///
193+
/// The sign of the angle is positive in a counter-clockwise direction and negative in a clockwise direction, when viewed from
194+
/// the side specified by the `axis`.
195+
///
196+
/// For unsigned angles, use [`Vector3::angle_to()`].
185197
#[inline]
186198
pub fn signed_angle_to(self, to: Self, axis: Self) -> real {
187199
let cross_to = self.cross(to);

godot-core/src/builtin/vectors/vector_macros.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1009,12 +1009,6 @@ macro_rules! impl_vector2_vector3_fns {
10091009
/// # 2D and 3D functions
10101010
/// The following methods are available on both 2D and 3D float vectors.
10111011
impl $Vector {
1012-
/// Returns the angle to the given vector, in radians.
1013-
#[inline]
1014-
pub fn angle_to(self, to: Self) -> real {
1015-
self.glam2(&to, |a, b| a.angle_between(b))
1016-
}
1017-
10181012
/// Returns the derivative at the given `t` on the [Bézier](https://en.wikipedia.org/wiki/B%C3%A9zier_curve)
10191013
/// curve defined by this vector and the given `control_1`, `control_2`, and `end` points.
10201014
#[inline]

godot-macros/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ proc-macro2 = "1.0.80" # Literal::c_string() added in 1.0.80.
2323
quote = "1.0.29"
2424

2525
# Enabled by `docs`
26-
markdown = { version = "1.0.0-alpha.17", optional = true }
26+
markdown = { version = "1.0.0-alpha.19", optional = true }
2727
venial = "0.6"
2828

2929
[build-dependencies]

itest/rust/src/builtin_tests/geometry/vector_test/vector2_test.rs

+28-7
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,10 @@
77

88
use crate::framework::itest;
99

10-
use godot::builtin::{
11-
inner::InnerVector2,
12-
math::{assert_eq_approx, ApproxEq},
13-
real,
14-
real_consts::PI,
15-
Vector2, Vector2Axis,
16-
};
10+
use godot::builtin::inner::InnerVector2;
11+
use godot::builtin::math::{assert_eq_approx, ApproxEq};
12+
use godot::builtin::real_consts::{FRAC_PI_2, PI};
13+
use godot::builtin::{real, Vector2, Vector2Axis};
1714

1815
#[itest]
1916
fn abs() {
@@ -25,8 +22,16 @@ fn abs() {
2522
#[itest]
2623
fn angle() {
2724
let a = Vector2::new(1.2, -3.4);
25+
let b = Vector2::new(-5.6, 7.8);
2826

2927
assert_eq!(a.angle(), a.as_inner().angle() as real);
28+
assert_eq!(b.angle(), b.as_inner().angle() as real);
29+
30+
// Check direction (note: DOWN=(0, 1)).
31+
assert_eq_approx!(Vector2::RIGHT.angle(), 0.0);
32+
assert_eq_approx!(Vector2::DOWN.angle(), FRAC_PI_2);
33+
assert_eq_approx!(Vector2::LEFT.angle(), PI);
34+
assert_eq_approx!(Vector2::UP.angle(), -FRAC_PI_2);
3035
}
3136

3237
#[itest]
@@ -35,6 +40,11 @@ fn angle_to() {
3540
let b = Vector2::new(-5.6, 7.8);
3641

3742
assert_eq_approx!(a.angle_to(b), a.as_inner().angle_to(b) as real);
43+
assert_eq_approx!(b.angle_to(a), b.as_inner().angle_to(a) as real);
44+
45+
// Check direction (note: DOWN=(0, 1)).
46+
assert_eq_approx!(Vector2::RIGHT.angle_to(Vector2::DOWN), FRAC_PI_2);
47+
assert_eq_approx!(Vector2::DOWN.angle_to(Vector2::RIGHT), -FRAC_PI_2);
3848
}
3949

4050
#[itest]
@@ -43,6 +53,17 @@ fn angle_to_point() {
4353
let b = Vector2::new(-5.6, 7.8);
4454

4555
assert_eq!(a.angle_to_point(b), a.as_inner().angle_to_point(b) as real);
56+
assert_eq!(b.angle_to_point(a), b.as_inner().angle_to_point(a) as real);
57+
58+
// Check absolute value.
59+
assert_eq_approx!(
60+
Vector2::new(1.0, 1.0).angle_to_point(Vector2::new(1.0, 2.0)),
61+
FRAC_PI_2
62+
); // up
63+
assert_eq_approx!(
64+
Vector2::new(1.0, 1.0).angle_to_point(Vector2::new(1.0, -1.0)),
65+
-FRAC_PI_2
66+
); // down
4667
}
4768

4869
#[itest]

itest/rust/src/builtin_tests/geometry/vector_test/vector3_test.rs

+35-20
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77

88
use crate::framework::itest;
99

10-
use godot::builtin::{
11-
inner::InnerVector3,
12-
math::{assert_eq_approx, ApproxEq},
13-
real,
14-
real_consts::PI,
15-
Vector3, Vector3Axis,
16-
};
10+
use godot::builtin::inner::InnerVector3;
11+
use godot::builtin::math::{assert_eq_approx, ApproxEq};
12+
use godot::builtin::real;
13+
use godot::builtin::real_consts::{FRAC_PI_4, PI};
14+
use godot::builtin::{Vector3, Vector3Axis};
1715

1816
#[itest]
1917
fn abs() {
@@ -28,6 +26,36 @@ fn angle_to() {
2826
let b = Vector3::new(-7.8, 9.1, -11.12);
2927

3028
assert_eq_approx!(a.angle_to(b), a.as_inner().angle_to(b) as real);
29+
30+
// Concrete example (135°).
31+
let right = Vector3::new(1.0, 0.0, 0.0);
32+
let back_left = Vector3::new(-1.0, 0.0, 1.0);
33+
34+
assert_eq_approx!(right.angle_to(back_left), 3.0 * FRAC_PI_4);
35+
assert_eq_approx!(back_left.angle_to(right), 3.0 * FRAC_PI_4);
36+
}
37+
38+
#[itest]
39+
fn signed_angle_to() {
40+
let a = Vector3::new(1.0, 1.0, 0.0);
41+
let b = Vector3::new(1.0, 1.0, 1.0);
42+
let c = Vector3::UP;
43+
44+
assert_eq_approx!(
45+
a.signed_angle_to(b, c),
46+
a.as_inner().signed_angle_to(b, c) as real,
47+
"signed_angle_to\n",
48+
);
49+
50+
// Concrete example (135°).
51+
let right = Vector3::new(1.0, 0.0, 0.0);
52+
let back_left = Vector3::new(-1.0, 0.0, 1.0);
53+
54+
let pi_3_4 = 3.0 * FRAC_PI_4;
55+
assert_eq_approx!(right.signed_angle_to(back_left, Vector3::UP), -pi_3_4);
56+
assert_eq_approx!(right.signed_angle_to(back_left, Vector3::DOWN), pi_3_4);
57+
assert_eq_approx!(back_left.signed_angle_to(right, Vector3::UP), pi_3_4);
58+
assert_eq_approx!(back_left.signed_angle_to(right, Vector3::DOWN), -pi_3_4);
3159
}
3260

3361
#[itest]
@@ -402,19 +430,6 @@ fn sign() {
402430
assert_eq!(b.sign(), b.as_inner().sign());
403431
}
404432

405-
#[itest]
406-
fn signed_angle_to() {
407-
let a = Vector3::new(1.0, 1.0, 0.0);
408-
let b = Vector3::new(1.0, 1.0, 1.0);
409-
let c = Vector3::UP;
410-
411-
assert_eq_approx!(
412-
a.signed_angle_to(b, c),
413-
a.as_inner().signed_angle_to(b, c) as real,
414-
"signed_angle_to\n",
415-
);
416-
}
417-
418433
#[itest]
419434
fn slerp() {
420435
let a = Vector3::new(1.2, -3.4, 5.6);

0 commit comments

Comments
 (0)