Skip to content

Commit d5efdcc

Browse files
committed
Implement Mul<Transform> for Vector2, Vector3
1 parent d4fb159 commit d5efdcc

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use sys::{ffi_methods, GodotFfi};
1111

1212
use crate::builtin::math::{FloatExt, GlamConv, GlamType};
1313
use crate::builtin::vectors::Vector2Axis;
14-
use crate::builtin::{inner, real, RAffine2, RVec2, Vector2i};
14+
use crate::builtin::{inner, real, RAffine2, RVec2, Transform2D, Vector2i};
1515

1616
use std::fmt;
17+
use std::ops::Mul;
1718

1819
/// Vector used for 2D math using floating point coordinates.
1920
///
@@ -177,6 +178,14 @@ impl_vector2_vector3_fns!(Vector2, (x, y));
177178

178179
impl_vector_operators!(Vector2, real, (x, y));
179180

181+
impl Mul<Transform2D> for Vector2 {
182+
type Output = Self;
183+
184+
fn mul(self, rhs: Transform2D) -> Self::Output {
185+
rhs.glam2(&self, |t, v| t.inverse().transform_point2(v))
186+
}
187+
}
188+
180189
/// Formats the vector like Godot: `(x, y)`.
181190
impl fmt::Display for Vector2 {
182191
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -235,6 +244,20 @@ mod test {
235244
assert_eq!(vector.sign(), Vector2::new(1., 0.));
236245
}
237246

247+
#[test]
248+
fn transform_multiplication() {
249+
let transform = Transform2D::from_cols(
250+
Vector2::new(1.0, 0.0),
251+
Vector2::new(0.0, 1.0),
252+
Vector2::new(5.0, 50.0),
253+
)
254+
.rotated(1.0);
255+
256+
let vector = Vector2::new(10.0, 100.0);
257+
assert_eq_approx!(transform * vector, Vector2::new(-118.1161, 93.6674));
258+
assert_eq_approx!(vector * transform, Vector2::new(84.55011, -4.38448));
259+
}
260+
238261
#[cfg(feature = "serde")]
239262
#[test]
240263
fn serde_roundtrip() {

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,10 @@ use sys::{ffi_methods, GodotFfi};
1111

1212
use crate::builtin::math::{FloatExt, GlamConv, GlamType};
1313
use crate::builtin::vectors::Vector3Axis;
14-
use crate::builtin::{inner, real, Basis, RVec3, Vector2, Vector3i};
14+
use crate::builtin::{inner, real, Basis, RVec3, Transform3D, Vector2, Vector3i};
1515

1616
use std::fmt;
17+
use std::ops::Mul;
1718

1819
/// Vector used for 3D math using floating point coordinates.
1920
///
@@ -248,6 +249,14 @@ impl_vector3_vector4_fns!(Vector3, (x, y, z));
248249

249250
impl_vector_operators!(Vector3, real, (x, y, z));
250251

252+
impl Mul<Transform3D> for Vector3 {
253+
type Output = Self;
254+
255+
fn mul(self, rhs: Transform3D) -> Self::Output {
256+
rhs.glam2(&self, |t, v| t.inverse().transform_point3(v))
257+
}
258+
}
259+
251260
/// Formats the vector like Godot: `(x, y, z)`.
252261
impl fmt::Display for Vector3 {
253262
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -412,6 +421,21 @@ mod test {
412421
assert_eq_approx!(sum_refs, Vector3::new(12.0, 15.0, 18.0));
413422
}
414423

424+
#[test]
425+
fn transform_multiplication() {
426+
let transform = Transform3D::new(Basis::IDENTITY, Vector3::new(5., 50., 0.5))
427+
.rotated(Vector3::new(1.0, 2.0, 3.0).normalized(), 1.0);
428+
let vector = Vector3::new(10., 100., 1.0);
429+
assert_eq_approx!(
430+
transform * vector,
431+
Vector3::new(-81.93149, 111.8101, 59.27044)
432+
);
433+
assert_eq_approx!(
434+
vector * transform,
435+
Vector3::new(74.41499, 11.49629, 3.03081)
436+
);
437+
}
438+
415439
#[cfg(feature = "serde")]
416440
#[test]
417441
fn serde_roundtrip() {

0 commit comments

Comments
 (0)