@@ -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