Struct Vector4
#[repr(C)]pub struct Vector4 {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}
Expand description
Vector used for 4D math using floating point coordinates.
4-element structure that can be used to represent any quadruplet of numeric values.
It uses floating-point coordinates of 32-bit precision, unlike the engine’s float
type which
is always 64-bit. The engine can be compiled with the option precision=double
to use 64-bit
vectors; use the gdext library with the double-precision
feature in that case.
Conversions are provided via various from_*
and to_*
functions, not via the From
trait. This encourages new()
as the main way to construct vectors, is explicit about the conversion taking place, needs no type inference, and works in const
contexts.
§Navigation to impl
blocks within this page
- Constants
- Constructors and general vector functions
- Specialized
Vector4
functions - Float-specific functions
- 4D functions
- 3D and 4D functions
- Trait impls + operators
§All vector types
§Godot docs
Fields§
§x: f32
The vector’s X component.
y: f32
The vector’s Y component.
z: f32
The vector’s Z component.
w: f32
The vector’s W component.
Implementations§
§impl Vector4
impl Vector4
§Constructors and general vector functions
The following associated functions and methods are available on all vectors (2D, 3D, 4D; float and int).
pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Vector4
pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Vector4
Creates a vector with the given components.
pub const fn from_tuple(tuple: (f32, f32, f32, f32)) -> Vector4
pub const fn from_tuple(tuple: (f32, f32, f32, f32)) -> Vector4
Creates a vector from the given tuple.
pub const fn from_array(array: [f32; 4]) -> Vector4
pub const fn from_array(array: [f32; 4]) -> Vector4
Creates a vector from the given array.
pub const fn to_tuple(&self) -> (f32, f32, f32, f32)
pub const fn to_tuple(&self) -> (f32, f32, f32, f32)
Returns a tuple with the components of the vector.
pub fn abs(self) -> Vector4
pub fn abs(self) -> Vector4
Returns a new vector with all components in absolute values (i.e. positive or zero).
pub fn clamp(self, min: Vector4, max: Vector4) -> Vector4
pub fn clamp(self, min: Vector4, max: Vector4) -> Vector4
Returns a new vector with all components clamped between the components of min
and max
.
§Panics
If min
> max
, min
is NaN, or max
is NaN.
pub fn length_squared(self) -> f32
pub fn length_squared(self) -> f32
Squared length (squared magnitude) of this vector.
Runs faster than length()
, so prefer it if you need to compare vectors or need the
squared distance for some formula.
pub fn coord_min(self, other: Vector4) -> Vector4
pub fn coord_min(self, other: Vector4) -> Vector4
Returns a new vector containing the minimum of the two vectors, component-wise.
You may consider using the fully-qualified syntax Vector4::coord_min(a, b)
for symmetry.
§impl Vector4
impl Vector4
§Specialized Vector4
functions
pub const fn from_vector4i(v: Vector4i) -> Vector4
Vector4i::cast_float()
§impl Vector4
impl Vector4
§Float-specific functions
The following methods are only available on floating-point vectors.
pub const fn cast_int(self) -> Vector4i
pub const fn cast_int(self) -> Vector4i
Converts to a vector with integer components, using as
casts.
pub fn floor(self) -> Vector4
pub fn floor(self) -> Vector4
Returns a new vector with all components rounded down (towards negative infinity).
pub fn ceil(self) -> Vector4
pub fn ceil(self) -> Vector4
Returns a new vector with all components rounded up (towards positive infinity).
pub fn cubic_interpolate(
self,
b: Vector4,
pre_a: Vector4,
post_b: Vector4,
weight: f32,
) -> Vector4
pub fn cubic_interpolate( self, b: Vector4, pre_a: Vector4, post_b: Vector4, weight: f32, ) -> Vector4
Cubic interpolation between self
and b
using pre_a
and post_b
as handles,
and returns the result at position weight
.
weight
is on the range of 0.0 to 1.0, representing the amount of interpolation.
pub fn cubic_interpolate_in_time(
self,
b: Vector4,
pre_a: Vector4,
post_b: Vector4,
weight: f32,
b_t: f32,
pre_a_t: f32,
post_b_t: f32,
) -> Vector4
pub fn cubic_interpolate_in_time( self, b: Vector4, pre_a: Vector4, post_b: Vector4, weight: f32, b_t: f32, pre_a_t: f32, post_b_t: f32, ) -> Vector4
Cubic interpolation between self
and b
using pre_a
and post_b
as handles,
and returns the result at position weight
.
weight
is on the range of 0.0 to 1.0, representing the amount of interpolation.
It can perform smoother interpolation than cubic_interpolate()
by the time values.
pub fn try_direction_to(self, to: Vector4) -> Option<Vector4>
pub fn try_direction_to(self, to: Vector4) -> Option<Vector4>
Returns the normalized vector pointing from this vector to to
or None
, if self
and to
are equal.
This is equivalent to using (b - a).try_normalized()
. See also direction_to()
.
pub fn direction_to(self, to: Vector4) -> Vector4
pub fn direction_to(self, to: Vector4) -> Vector4
⚠️ Returns the normalized vector pointing from this vector to to
.
This is equivalent to using (b - a).normalized()
. See also try_direction_to()
.
§Panics
If self
and to
are equal.
pub fn distance_squared_to(self, to: Vector4) -> f32
pub fn distance_squared_to(self, to: Vector4) -> f32
Returns the squared distance between this vector and to
.
This method runs faster than Self::distance_to
, so prefer it if you need to compare vectors or need the squared distance for some formula.
pub fn distance_to(self, to: Vector4) -> f32
pub fn distance_to(self, to: Vector4) -> f32
Returns the distance between this vector and to
.
pub fn is_normalized(self) -> bool
pub fn is_normalized(self) -> bool
Returns true
if the vector is normalized, i.e. its length is approximately equal to 1.
pub fn is_zero_approx(self) -> bool
pub fn is_zero_approx(self) -> bool
Returns true
if this vector’s values are approximately zero.
This method is faster than using approx_eq()
with one value as a zero vector.
pub fn lerp(self, other: Vector4, weight: f32) -> Vector4
pub fn lerp(self, other: Vector4, weight: f32) -> Vector4
Returns the result of the linear interpolation between this vector and to
by amount weight
.
weight
is on the range of 0.0
to 1.0
, representing the amount of interpolation.
pub fn try_normalized(self) -> Option<Vector4>
pub fn try_normalized(self) -> Option<Vector4>
Returns the vector scaled to unit length or None
, if called on a zero vector.
Computes self / self.length()
. See also normalized()
and is_normalized()
.
pub fn normalized(self) -> Vector4
pub fn normalized(self) -> Vector4
⚠️ Returns the vector scaled to unit length.
Computes self / self.length()
. See also try_normalized()
and is_normalized()
.
§Panics
If called on a zero vector.
pub fn normalized_or_zero(self) -> Vector4
pub fn normalized_or_zero(self) -> Vector4
Returns the vector scaled to unit length or Self::ZERO
, if called on a zero vector.
Computes self / self.length()
. See also try_normalized()
and is_normalized()
.
pub fn posmod(self, pmod: f32) -> Vector4
pub fn posmod(self, pmod: f32) -> Vector4
Returns a vector composed of the FloatExt::fposmod()
of this vector’s components and pmod
.
pub fn posmodv(self, modv: Vector4) -> Vector4
pub fn posmodv(self, modv: Vector4) -> Vector4
Returns a vector composed of the FloatExt::fposmod()
of this vector’s components and modv
’s components.
§impl Vector4
impl Vector4
§4D functions
The following methods are only available on 4D vectors (for both float and int).
pub fn max_axis(self) -> Option<Vector4Axis>
pub fn max_axis(self) -> Option<Vector4Axis>
Returns the axis of the vector’s highest value. See Vector4Axis
enum. If all components are equal, this method returns None
.
To mimic Godot’s behavior, unwrap this function’s result with unwrap_or(Vector4Axis::X)
.
pub fn min_axis(self) -> Option<Vector4Axis>
pub fn min_axis(self) -> Option<Vector4Axis>
Returns the axis of the vector’s lowest value. See Vector4Axis
enum. If all components are equal, this method returns None
.
To mimic Godot’s behavior, unwrap this function’s result with unwrap_or(Vector4Axis::W)
.
Trait Implementations§
§impl AddAssign for Vector4
impl AddAssign for Vector4
§fn add_assign(&mut self, rhs: Vector4)
fn add_assign(&mut self, rhs: Vector4)
+=
operation. Read more§impl ArrayElement for Vector4
impl ArrayElement for Vector4
fn debug_validate_elements(_array: &Array<Self>) -> Result<(), ConvertError>
§impl DivAssign<f32> for Vector4
impl DivAssign<f32> for Vector4
§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/=
operation. Read more§impl DivAssign for Vector4
impl DivAssign for Vector4
§fn div_assign(&mut self, rhs: Vector4)
fn div_assign(&mut self, rhs: Vector4)
/=
operation. Read more§impl Export for Vector4
impl Export for Vector4
§fn export_hint() -> PropertyHintInfo
fn export_hint() -> PropertyHintInfo
§fn as_node_class() -> Option<ClassName>
fn as_node_class() -> Option<ClassName>
§impl Extend<Vector4> for PackedVector4Array
impl Extend<Vector4> for PackedVector4Array
Extends aPackedVector4Array
with the contents of an iterator
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Vector4>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Vector4>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)§impl FromGodot for Vector4
impl FromGodot for Vector4
§fn try_from_godot(
via: <Vector4 as GodotConvert>::Via,
) -> Result<Vector4, ConvertError>
fn try_from_godot( via: <Vector4 as GodotConvert>::Via, ) -> Result<Vector4, ConvertError>
Err
on failure.§fn from_godot(via: Self::Via) -> Self
fn from_godot(via: Self::Via) -> Self
§fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
fn try_from_variant(variant: &Variant) -> Result<Self, ConvertError>
Variant
, returning Err
on failure.§fn from_variant(variant: &Variant) -> Self
fn from_variant(variant: &Variant) -> Self
§impl FromIterator<Vector4> for PackedVector4Array
impl FromIterator<Vector4> for PackedVector4Array
Creates a PackedVector4Array
from an iterator.
§fn from_iter<I>(iter: I) -> PackedVector4Arraywhere
I: IntoIterator<Item = Vector4>,
fn from_iter<I>(iter: I) -> PackedVector4Arraywhere
I: IntoIterator<Item = Vector4>,
§impl GodotConvert for Vector4
impl GodotConvert for Vector4
§impl Index<Vector4Axis> for Vector4
impl Index<Vector4Axis> for Vector4
§impl IndexMut<Vector4Axis> for Vector4
impl IndexMut<Vector4Axis> for Vector4
§fn index_mut(&mut self, axis: Vector4Axis) -> &mut f32
fn index_mut(&mut self, axis: Vector4Axis) -> &mut f32
container[index]
) operation. Read more§impl Mul<Vector4> for Projection
impl Mul<Vector4> for Projection
§impl MulAssign<f32> for Vector4
impl MulAssign<f32> for Vector4
§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*=
operation. Read more§impl MulAssign for Vector4
impl MulAssign for Vector4
§fn mul_assign(&mut self, rhs: Vector4)
fn mul_assign(&mut self, rhs: Vector4)
*=
operation. Read more§impl SubAssign for Vector4
impl SubAssign for Vector4
§fn sub_assign(&mut self, rhs: Vector4)
fn sub_assign(&mut self, rhs: Vector4)
-=
operation. Read more§impl ToGodot for Vector4
impl ToGodot for Vector4
§type ToVia<'v> = <Vector4 as GodotConvert>::Via
type ToVia<'v> = <Vector4 as GodotConvert>::Via
to_godot()
, which differs from Via
for pass-by-reference types.§fn to_godot(&self) -> <Vector4 as ToGodot>::ToVia<'_>
fn to_godot(&self) -> <Vector4 as ToGodot>::ToVia<'_>
§fn to_variant(&self) -> Variant
fn to_variant(&self) -> Variant
§impl Var for Vector4
impl Var for Vector4
fn get_property(&self) -> <Vector4 as GodotConvert>::Via
fn set_property(&mut self, value: <Vector4 as GodotConvert>::Via)
§fn var_hint() -> PropertyHintInfo
fn var_hint() -> PropertyHintInfo
GodotType::property_info
, e.g. for enums/newtypes.impl Copy for Vector4
impl GodotType for Vector4
impl PackedArrayElement for Vector4
since_api="4.3"
only.impl StructuralPartialEq for Vector4
Auto Trait Implementations§
impl Freeze for Vector4
impl RefUnwindSafe for Vector4
impl Send for Vector4
impl Sync for Vector4
impl Unpin for Vector4
impl UnwindSafe for Vector4
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)