Skip to content

Commit dfadfe1

Browse files
bors[bot]evaporei
andauthored
Merge #611
611: feat: Add 'clamped' to 'Vector2' r=toasteater a=otaviopace Hiii!!! First and foremost thanks for this project! I am trying to learn Godot, but I want to use Rust instead of GDScript, and this is helping a lot! 😊 🦀 So I was following a GDScript tutorial, and was having a lot of trouble to replicate the Godot's `Vector2::clamped` (https://docs.godotengine.org/en/stable/classes/class_vector2.html?highlight=clamped#class-vector2-method-clamped). After a lot of tries, I've discovered that the `euclid`'s `clamp_length` (https://docs.rs/euclid/0.22.1/euclid/struct.Vector2D.html#method.clamp_length) is actually really similar to the Godot one (https://github.com/godotengine/godot/blob/master/core/math/vector2.cpp#L131). For it to be the same, you just have to pass `0.0` as the minimum length, so I've added here to ease future people stumbling the same issue/wanting to use the same function 🙂 Co-authored-by: Otavio Pace <[email protected]>
2 parents 836fa99 + d74ddeb commit dfadfe1

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

gdnative-core/src/core_types/vector2.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub trait Vector2Godot {
3030
/// This method runs faster than distance_to, so prefer it if you need to compare vectors or
3131
/// need the squared distance for some formula.
3232
fn distance_squared_to(self, other: Vector2) -> f32;
33+
/// Returns the vector with a maximum length by limiting its length to `length`.
34+
fn clamped(self, length: f32) -> Self;
3335
/// Internal API for converting to `sys` representation. Makes it possible to remove
3436
/// `transmute`s elsewhere.
3537
#[doc(hidden)]
@@ -125,6 +127,11 @@ impl Vector2Godot for Vector2 {
125127
(other - self).square_length()
126128
}
127129

130+
#[inline]
131+
fn clamped(self, length: f32) -> Self {
132+
self.clamp_length(0.0, length)
133+
}
134+
128135
#[inline]
129136
fn to_sys(self) -> sys::godot_vector2 {
130137
unsafe { std::mem::transmute(self) }

0 commit comments

Comments
 (0)