Skip to content

Commit 7d1888b

Browse files
committed
Vector conversion functions between 2D and 3D
1 parent 4d396e7 commit 7d1888b

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ 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, Vector2i, Vector3};
1515

1616
use std::fmt;
1717

@@ -165,7 +165,7 @@ impl Vector2 {
165165
}
166166

167167
impl_float_vector_fns!(Vector2, (x, y));
168-
impl_vector2x_fns!(Vector2, real);
168+
impl_vector2x_fns!(Vector2, Vector3, real);
169169
impl_vector2_vector3_fns!(Vector2, (x, y));
170170

171171
impl_vector_operators!(Vector2, real, (x, y));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::cmp::Ordering;
1010
use sys::{ffi_methods, GodotFfi};
1111

1212
use crate::builtin::math::{GlamConv, GlamType};
13-
use crate::builtin::{inner, real, RVec2, Vector2, Vector2Axis};
13+
use crate::builtin::{inner, real, RVec2, Vector2, Vector2Axis, Vector3i};
1414

1515
use std::fmt;
1616

@@ -91,7 +91,7 @@ impl Vector2i {
9191
}
9292

9393
impl_vector_fns!(Vector2i, glam::IVec2, i32, (x, y));
94-
impl_vector2x_fns!(Vector2i, i32);
94+
impl_vector2x_fns!(Vector2i, Vector3i, i32);
9595

9696
impl_vector_operators!(Vector2i, i32, (x, y));
9797

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ impl Vector3 {
232232
}
233233

234234
impl_float_vector_fns!(Vector3, (x, y, z));
235-
impl_vector3x_fns!(Vector3, real);
235+
impl_vector3x_fns!(Vector3, Vector2, real);
236236
impl_vector2_vector3_fns!(Vector3, (x, y, z));
237237
impl_vector3_vector4_fns!(Vector3, (x, y, z));
238238

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use godot_ffi as sys;
1212
use sys::{ffi_methods, GodotFfi};
1313

1414
use crate::builtin::math::{GlamConv, GlamType};
15-
use crate::builtin::{inner, real, RVec3, Vector3, Vector3Axis};
15+
use crate::builtin::{inner, real, RVec3, Vector2i, Vector3, Vector3Axis};
1616

1717
/// Vector used for 3D math using integer coordinates.
1818
///
@@ -96,7 +96,7 @@ impl Vector3i {
9696
}
9797
}
9898

99-
impl_vector3x_fns!(Vector3i, i32);
99+
impl_vector3x_fns!(Vector3i, Vector2i, i32);
100100

101101
impl_vector_operators!(Vector3i, i32, (x, y, z));
102102

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,12 +766,20 @@ macro_rules! impl_vector2x_fns {
766766
(
767767
// Name of the vector type.
768768
$Vector:ty,
769+
// Name of the 3D-equivalent vector type.
770+
$Vector3D:ty,
769771
// Type of target component, for example `real`.
770772
$Scalar:ty
771773
) => {
772774
/// # 2D functions
773775
/// The following methods are only available on 2D vectors (for both float and int).
774776
impl $Vector {
777+
/// Increases dimension to 3D, accepting a new value for the Z component.
778+
#[inline]
779+
pub fn to_3d(self, z: $Scalar) -> $Vector3D {
780+
<$Vector3D>::new(self.x, self.y, z)
781+
}
782+
775783
/// Returns the aspect ratio of this vector, the ratio of [`Self::x`] to [`Self::y`].
776784
#[inline]
777785
pub fn aspect(self) -> real {
@@ -820,12 +828,23 @@ macro_rules! impl_vector3x_fns {
820828
(
821829
// Name of the vector type.
822830
$Vector:ty,
831+
// Name of the vector type.
832+
$Vector2D:ty,
823833
// Type of target component, for example `real`.
824834
$Scalar:ty
825835
) => {
826836
/// # 3D functions
827837
/// The following methods are only available on 3D vectors (for both float and int).
828838
impl $Vector {
839+
/// Reduces dimension to 2D, discarding the Z component.
840+
///
841+
/// See also [`swizzle!`][crate::builtin::swizzle] for a more general way to extract components.
842+
/// `self.to_2d()` is equivalent to `swizzle!(self => x, y)`.
843+
#[inline]
844+
pub fn to_2d(self) -> $Vector2D {
845+
<$Vector2D>::new(self.x, self.y)
846+
}
847+
829848
/// Returns the axis of the vector's highest value. See [`Vector3Axis`] enum. If all components are equal, this method returns [`None`].
830849
///
831850
/// To mimic Godot's behavior, unwrap this function's result with `unwrap_or(Vector3Axis::X)`.

0 commit comments

Comments
 (0)