diff --git a/godot-core/src/builtin/mod.rs b/godot-core/src/builtin/mod.rs index 40f326bff..c4c935bdf 100644 --- a/godot-core/src/builtin/mod.rs +++ b/godot-core/src/builtin/mod.rs @@ -37,6 +37,7 @@ mod vector_macros; mod arrays; mod color; +mod math; mod node_path; mod others; mod string; @@ -53,6 +54,7 @@ pub mod meta; pub use arrays::*; pub use color::*; +pub use math::*; pub use node_path::*; pub use others::*; pub use string::*; diff --git a/godot-core/src/builtin/vector2.rs b/godot-core/src/builtin/vector2.rs index 159b9085e..9ff752ada 100644 --- a/godot-core/src/builtin/vector2.rs +++ b/godot-core/src/builtin/vector2.rs @@ -3,12 +3,13 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ -use std::ops::*; use std::fmt; +use std::ops::*; use godot_ffi as sys; use sys::{ffi_methods, GodotFfi}; +use crate::builtin::math::*; use crate::builtin::{inner, Vector2i}; /// Vector used for 2D math using floating point coordinates. @@ -80,10 +81,6 @@ impl Vector2 { glam::Vec2::new(self.x, self.y) } - pub fn abs(self) -> Self { - Self(self.to_glam().abs()) - } - pub fn angle(self) -> f32 { self.y.atan2(self.x) } @@ -100,54 +97,18 @@ impl Vector2 { self.x / self.y } - pub fn bezier_derivative( - self, - control_1: Self, - control_2: Self, - end: Self, - t: f32, - ) -> Self { - let x = bezier_derivative( - self.x, - control_1.x, - control_2.x, - end.x, - t, - ); - let y = bezier_derivative( - self.y, - control_1.y, - control_2.y, - end.y, - t, - ); + pub fn bezier_derivative(self, control_1: Self, control_2: Self, end: Self, t: f32) -> Self { + let x = bezier_derivative(self.x, control_1.x, control_2.x, end.x, t); + let y = bezier_derivative(self.y, control_1.y, control_2.y, end.y, t); - Self::new(x, y) + Self::new(x, y) } - pub fn bezier_interpolate( - self, - control_1: Self, - control_2: Self, - end: Self, - t: f32, - ) -> Self { - let x = bezier_interpolate( - self.x, - control_1.x, - control_2.x, - end.x, - t, - ); - let y = bezier_interpolate( - self.y, - control_1.y, - control_2.y, - end.y, - t, - ); + pub fn bezier_interpolate(self, control_1: Self, control_2: Self, end: Self, t: f32) -> Self { + let x = bezier_interpolate(self.x, control_1.x, control_2.x, end.x, t); + let y = bezier_interpolate(self.y, control_1.y, control_2.y, end.y, t); - Self::new(x, y) + Self::new(x, y) } pub fn bounce(self, normal: Self) -> Self { @@ -155,11 +116,11 @@ impl Vector2 { } pub fn ceil(self) -> Self { - Self(self.to_glam().ceil()) + Self::from_glam(self.to_glam().ceil()) } pub fn clamp(self, min: Self, max: Self) -> Self { - Self(self.to_glam().clamp(min.to_glam(), max.to_glam())) + Self::from_glam(self.to_glam().clamp(min.to_glam(), max.to_glam())) } pub fn cross(self, with: Self) -> f32 { @@ -167,22 +128,10 @@ impl Vector2 { } pub fn cubic_interpolate(self, b: Self, pre_a: Self, post_b: Self, weight: f32) -> Self { - let x = cubic_interpolate( - self.x, - b.x, - pre_a.x, - post_b.x, - weight, - ); - let y = cubic_interpolate( - self.y, - b.y, - pre_a.y, - post_b.y, - weight, - ); + let x = cubic_interpolate(self.x, b.x, pre_a.x, post_b.x, weight); + let y = cubic_interpolate(self.y, b.y, pre_a.y, post_b.y, weight); - Self::new(x, y) + Self::new(x, y) } pub fn cubic_interpolate_in_time( @@ -196,27 +145,13 @@ impl Vector2 { post_b_t: f32, ) -> Self { let x = cubic_interpolate_in_time( - self.x, - b.x, - pre_a.x, - post_b.x, - weight, - b_t, - pre_a_t, - post_b_t, + self.x, b.x, pre_a.x, post_b.x, weight, b_t, pre_a_t, post_b_t, ); let y = cubic_interpolate_in_time( - self.y, - b.y, - pre_a.y, - post_b.y, - weight, - b_t, - pre_a_t, - post_b_t, + self.y, b.y, pre_a.y, post_b.y, weight, b_t, pre_a_t, post_b_t, ); - Self::new(x, y) + Self::new(x, y) } pub fn direction_to(self, to: Self) -> Self { @@ -264,11 +199,11 @@ impl Vector2 { } pub fn lerp(self, to: Self, weight: f32) -> Self { - Self(self.to_glam().lerp(to.to_glam(), weight)) + Self::from_glam(self.to_glam().lerp(to.to_glam(), weight)) } pub fn limit_length(self, length: Option) -> Self { - Self(self.to_glam().clamp_length_max(length.unwrap_or(1.0))) + Self::from_glam(self.to_glam().clamp_length_max(length.unwrap_or(1.0))) } pub fn max_axis_index(self) -> i32 { @@ -291,10 +226,10 @@ impl Vector2 { let vd = to - self; let len = vd.length(); if len <= delta || len < CMP_EPSILON { - return to; + to } else { - return self + vd / len * delta; - }; + self + vd / len * delta + } } pub fn orthogonal(self) -> Self { @@ -306,10 +241,7 @@ impl Vector2 { } pub fn posmodv(self, modv: Self) -> Self { - Self::new( - fposmod(self.x, modv.x), - fposmod(self.y, modv.y), - ) + Self::new(fposmod(self.x, modv.x), fposmod(self.y, modv.y)) } pub fn project(self, b: Self) -> Self { @@ -345,17 +277,14 @@ impl Vector2 { } pub fn snapped(self, step: Self) -> Self { - Self::new( - snapped(self.x, step.x), - snapped(self.y, step.y), - ) + Self::new(snapped(self.x, step.x), snapped(self.y, step.y)) } /// Returns the result of rotating this vector by `angle` (in radians). pub fn rotated(self, angle: f32) -> Self { Self::from_glam(glam::Affine2::from_angle(angle).transform_vector2(self.to_glam())) } - + #[cfg(not(any(gdext_test, doctest)))] #[doc(hidden)] pub fn as_inner(&self) -> inner::InnerVector2 { diff --git a/godot-core/src/builtin/vector3.rs b/godot-core/src/builtin/vector3.rs index 0b4d96f80..d19746e22 100644 --- a/godot-core/src/builtin/vector3.rs +++ b/godot-core/src/builtin/vector3.rs @@ -10,6 +10,7 @@ use std::fmt; use godot_ffi as sys; use sys::{ffi_methods, GodotFfi}; +use crate::builtin::math::*; use crate::builtin::Vector3i; /// Vector used for 3D math using floating point coordinates. @@ -87,76 +88,24 @@ impl Vector3 { glam::Vec3::new(self.x, self.y, self.z) } - pub fn abs(self) -> Self { - Self::from_glam(self.to_glam().abs()) - } - pub fn angle_to(self, to: Self) -> f32 { self.to_glam().angle_between(to.to_glam()) } - pub fn bezier_derivative( - self, - control_1: Self, - control_2: Self, - end: Self, - t: f32, - ) -> Self { - let x = bezier_derivative( - self.x, - control_1.x, - control_2.x, - end.x, - t, - ); - let y = bezier_derivative( - self.y, - control_1.y, - control_2.y, - end.y, - t, - ); - let z = bezier_derivative( - self.z, - control_1.z, - control_2.z, - end.z, - t, - ); + pub fn bezier_derivative(self, control_1: Self, control_2: Self, end: Self, t: f32) -> Self { + let x = bezier_derivative(self.x, control_1.x, control_2.x, end.x, t); + let y = bezier_derivative(self.y, control_1.y, control_2.y, end.y, t); + let z = bezier_derivative(self.z, control_1.z, control_2.z, end.z, t); Self::new(x, y, z) } - pub fn bezier_interpolate( - self, - control_1: Self, - control_2: Self, - end: Self, - t: f32, - ) -> Self { - let x = bezier_interpolate( - self.x, - control_1.x, - control_2.x, - end.x, - t, - ); - let y = bezier_interpolate( - self.y, - control_1.y, - control_2.y, - end.y, - t, - ); - let z = bezier_interpolate( - self.z, - control_1.z, - control_2.z, - end.z, - t, - ); + pub fn bezier_interpolate(self, control_1: Self, control_2: Self, end: Self, t: f32) -> Self { + let x = bezier_interpolate(self.x, control_1.x, control_2.x, end.x, t); + let y = bezier_interpolate(self.y, control_1.y, control_2.y, end.y, t); + let z = bezier_interpolate(self.z, control_1.z, control_2.z, end.z, t); - Self::new(x, y, z) + Self::new(x, y, z) } pub fn bounce(self, normal: Self) -> Self { @@ -176,29 +125,11 @@ impl Vector3 { } pub fn cubic_interpolate(self, b: Self, pre_a: Self, post_b: Self, weight: f32) -> Self { - let x = cubic_interpolate( - self.x, - b.x, - pre_a.x, - post_b.x, - weight, - ); - let y = cubic_interpolate( - self.y, - b.y, - pre_a.y, - post_b.y, - weight, - ); - let z = cubic_interpolate( - self.z, - b.z, - pre_a.z, - post_b.z, - weight, - ); + let x = cubic_interpolate(self.x, b.x, pre_a.x, post_b.x, weight); + let y = cubic_interpolate(self.y, b.y, pre_a.y, post_b.y, weight); + let z = cubic_interpolate(self.z, b.z, pre_a.z, post_b.z, weight); - Self::new(x, y, z) + Self::new(x, y, z) } pub fn cubic_interpolate_in_time( @@ -212,37 +143,16 @@ impl Vector3 { post_b_t: f32, ) -> Self { let x = cubic_interpolate_in_time( - self.x, - b.x, - pre_a.x, - post_b.x, - weight, - b_t, - pre_a_t, - post_b_t, + self.x, b.x, pre_a.x, post_b.x, weight, b_t, pre_a_t, post_b_t, ); let y = cubic_interpolate_in_time( - self.y, - b.y, - pre_a.y, - post_b.y, - weight, - b_t, - pre_a_t, - post_b_t, + self.y, b.y, pre_a.y, post_b.y, weight, b_t, pre_a_t, post_b_t, ); let z = cubic_interpolate_in_time( - self.z, - b.z, - pre_a.z, - post_b.z, - weight, - b_t, - pre_a_t, - post_b_t, + self.z, b.z, pre_a.z, post_b.z, weight, b_t, pre_a_t, post_b_t, ); - Self::new(x, y, z) + Self::new(x, y, z) } pub fn direction_to(self, to: Self) -> Self { @@ -325,10 +235,10 @@ impl Vector3 { let vd = to - self; let len = vd.length(); if len <= delta || len < CMP_EPSILON { - return to; + to } else { - return self + vd / len * delta; - }; + self + vd / len * delta + } } pub fn posmod(self, pmod: f32) -> Self { @@ -339,7 +249,7 @@ impl Vector3 { ) } - pub fn posmodv(self, modv: Self) -> Self { + pub fn posmodv(self, modv: Self) -> Self { Self::new( fposmod(self.x, modv.x), fposmod(self.y, modv.y), @@ -348,15 +258,15 @@ impl Vector3 { } pub fn project(self, b: Self) -> Self { - Self(self.to_glam().project_onto(b.to_glam())) + Self::from_glam(self.to_glam().project_onto(b.to_glam())) } pub fn reflect(self, normal: Self) -> Self { - Self(self.to_glam().reject_from(normal.to_glam())) + Self::from_glam(self.to_glam().reject_from(normal.to_glam())) } pub fn round(self) -> Self { - Self(self.to_glam().round()) + Self::from_glam(self.to_glam().round()) } pub fn sign(self) -> Self { @@ -364,14 +274,14 @@ impl Vector3 { } pub fn signed_angle_to(self, to: Self, axis: Self) -> f32 { - let cross_to = self.cross(to); - let unsigned_angle = to.dot().atan2(cross_to.length()); - let sign = cross_to.dot(axis); - if sign < 0 { - -unsigned_angle - } else { - unsigned_angle - } + let cross_to = self.cross(to); + let unsigned_angle = self.dot(to).atan2(cross_to.length()); + let sign = cross_to.dot(axis); + if sign < 0.0 { + -unsigned_angle + } else { + unsigned_angle + } } pub fn slide(self, normal: Self) -> Self { diff --git a/godot-core/src/builtin/vector4.rs b/godot-core/src/builtin/vector4.rs index 6edeaaa9b..9a8274010 100644 --- a/godot-core/src/builtin/vector4.rs +++ b/godot-core/src/builtin/vector4.rs @@ -106,4 +106,4 @@ pub enum Vector4Axis { impl GodotFfi for Vector4Axis { ffi_methods! { type sys::GDExtensionTypePtr = *mut Self; .. } -} \ No newline at end of file +}