Struct Vector2
#[repr(C)]pub struct Vector2 {
pub x: f32,
pub y: f32,
}
Expand description
Vector used for 2D math using floating point coordinates.
2-element structure that can be used to represent continuous positions or directions in 2D space, as well as any other pair 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
Vector2
functions - Float-specific functions
- 2D functions
- 2D and 3D functions
- Trait impls + operators
§All vector types
You can convert to 3D vectors using to_3d(z)
, and to Vector2i
using cast_int()
.
§Godot docs
Fields§
§x: f32
The vector’s X component.
y: f32
The vector’s Y component.
Implementations§
§impl Vector2
impl Vector2
§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 from_tuple(tuple: (f32, f32)) -> Vector2
pub const fn from_tuple(tuple: (f32, f32)) -> Vector2
Creates a vector from the given tuple.
pub const fn from_array(array: [f32; 2]) -> Vector2
pub const fn from_array(array: [f32; 2]) -> Vector2
Creates a vector from the given array.
pub fn abs(self) -> Vector2
pub fn abs(self) -> Vector2
Returns a new vector with all components in absolute values (i.e. positive or zero).
pub fn clamp(self, min: Vector2, max: Vector2) -> Vector2
pub fn clamp(self, min: Vector2, max: Vector2) -> Vector2
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: Vector2) -> Vector2
pub fn coord_min(self, other: Vector2) -> Vector2
Returns a new vector containing the minimum of the two vectors, component-wise.
You may consider using the fully-qualified syntax Vector2::coord_min(a, b)
for symmetry.
§impl Vector2
impl Vector2
§Specialized Vector2
functions
pub const fn from_vector2i(v: Vector2i) -> Vector2
Vector2i::cast_float()
pub fn from_angle(angle: f32) -> Vector2
pub fn from_angle(angle: f32) -> Vector2
Creates a unit Vector2 rotated to the given angle
in radians. This is equivalent to doing Vector2::new(angle.cos(), angle.sin())
or Vector2::RIGHT.rotated(angle)
.
use godot::prelude::*;
let a = Vector2::from_angle(0.0); // (1.0, 0.0)
let b = Vector2::new(1.0, 0.0).angle(); // 0.0
let c = Vector2::from_angle(real_consts::PI / 2.0); // (0.0, 1.0)
pub fn angle(self) -> f32
pub fn angle(self) -> f32
Returns this vector’s angle with respect to the positive X axis, or (1.0, 0.0)
vector, in radians.
For example, Vector2::RIGHT.angle()
will return zero, Vector2::DOWN.angle()
will return PI / 2
(a quarter turn, or 90 degrees),
and Vector2::new(1.0, -1.0).angle()
will return -PI / 4
(a negative eighth turn, or -45 degrees).
Illustration of the returned angle.
Equivalent to the result of y.atan2(x)
.
pub fn angle_to(self, to: Vector2) -> f32
pub fn angle_to(self, to: Vector2) -> f32
Returns the signed angle between self
and the given vector, as radians in [-π, +π]
.
Note that behavior is different from 3D Vector3::angle_to()
which returns the unsigned angle.
pub fn angle_to_point(self, to: Vector2) -> f32
pub fn angle_to_point(self, to: Vector2) -> f32
Returns the angle to the given vector, in radians.
pub fn cross(self, with: Vector2) -> f32
pub fn cross(self, with: Vector2) -> f32
Returns the 2D analog of the cross product for this vector and with
.
This is the signed area of the parallelogram formed by the two vectors. If the second vector is clockwise from the first vector, then the cross product is the positive area. If counter-clockwise, the cross product is the negative area. If the two vectors are parallel this returns zero, making it useful for testing if two vectors are parallel.
Note: Cross product is not defined in 2D mathematically. This method embeds the 2D vectors in the XY plane of 3D space and uses their cross product’s Z component as the analog.
pub fn orthogonal(self) -> Vector2
pub fn orthogonal(self) -> Vector2
Returns a perpendicular vector rotated 90 degrees counter-clockwise compared to the original, with the same length.
pub fn rotated(self, angle: f32) -> Vector2
pub fn rotated(self, angle: f32) -> Vector2
Returns the result of rotating this vector by angle
(in radians).
pub fn slerp(self, to: Vector2, weight: f32) -> Vector2
pub fn slerp(self, to: Vector2, weight: f32) -> Vector2
Returns the result of spherical 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.
This method also handles interpolating the lengths if the input vectors have different lengths.
For the special case of one or both input vectors having zero length, this method behaves like Vector2::lerp
.
§impl Vector2
impl Vector2
§Float-specific functions
The following methods are only available on floating-point vectors.
pub const fn cast_int(self) -> Vector2i
pub const fn cast_int(self) -> Vector2i
Converts to a vector with integer components, using as
casts.
pub fn floor(self) -> Vector2
pub fn floor(self) -> Vector2
Returns a new vector with all components rounded down (towards negative infinity).
pub fn ceil(self) -> Vector2
pub fn ceil(self) -> Vector2
Returns a new vector with all components rounded up (towards positive infinity).
pub fn cubic_interpolate(
self,
b: Vector2,
pre_a: Vector2,
post_b: Vector2,
weight: f32,
) -> Vector2
pub fn cubic_interpolate( self, b: Vector2, pre_a: Vector2, post_b: Vector2, weight: f32, ) -> Vector2
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: Vector2,
pre_a: Vector2,
post_b: Vector2,
weight: f32,
b_t: f32,
pre_a_t: f32,
post_b_t: f32,
) -> Vector2
pub fn cubic_interpolate_in_time( self, b: Vector2, pre_a: Vector2, post_b: Vector2, weight: f32, b_t: f32, pre_a_t: f32, post_b_t: f32, ) -> Vector2
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: Vector2) -> Option<Vector2>
pub fn try_direction_to(self, to: Vector2) -> Option<Vector2>
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: Vector2) -> Vector2
pub fn direction_to(self, to: Vector2) -> Vector2
⚠️ 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: Vector2) -> f32
pub fn distance_squared_to(self, to: Vector2) -> 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: Vector2) -> f32
pub fn distance_to(self, to: Vector2) -> 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: Vector2, weight: f32) -> Vector2
pub fn lerp(self, other: Vector2, weight: f32) -> Vector2
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<Vector2>
pub fn try_normalized(self) -> Option<Vector2>
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) -> Vector2
pub fn normalized(self) -> Vector2
⚠️ 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) -> Vector2
pub fn normalized_or_zero(self) -> Vector2
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) -> Vector2
pub fn posmod(self, pmod: f32) -> Vector2
Returns a vector composed of the FloatExt::fposmod()
of this vector’s components and pmod
.
pub fn posmodv(self, modv: Vector2) -> Vector2
pub fn posmodv(self, modv: Vector2) -> Vector2
Returns a vector composed of the FloatExt::fposmod()
of this vector’s components and modv
’s components.
§impl Vector2
impl Vector2
§2D functions
The following methods are only available on 2D vectors (for both float and int).
pub fn to_3d(self, z: f32) -> Vector3
pub fn to_3d(self, z: f32) -> Vector3
Increases dimension to 3D, accepting a new value for the Z component.
pub fn max_axis(self) -> Option<Vector2Axis>
pub fn max_axis(self) -> Option<Vector2Axis>
Returns the axis of the vector’s highest value. See Vector2Axis
enum. If all components are equal, this method returns None
.
To mimic Godot’s behavior, unwrap this function’s result with unwrap_or(Vector2Axis::X)
.
pub fn min_axis(self) -> Option<Vector2Axis>
pub fn min_axis(self) -> Option<Vector2Axis>
Returns the axis of the vector’s lowest value. See Vector2Axis
enum. If all components are equal, this method returns None
.
To mimic Godot’s behavior, unwrap this function’s result with unwrap_or(Vector2Axis::Y)
.
§impl Vector2
impl Vector2
§2D and 3D functions
The following methods are available on both 2D and 3D float vectors.
pub fn bezier_derivative(
self,
control_1: Vector2,
control_2: Vector2,
end: Vector2,
t: f32,
) -> Vector2
pub fn bezier_derivative( self, control_1: Vector2, control_2: Vector2, end: Vector2, t: f32, ) -> Vector2
Returns the derivative at the given t
on the Bézier
curve defined by this vector and the given control_1
, control_2
, and end
points.
pub fn bezier_interpolate(
self,
control_1: Vector2,
control_2: Vector2,
end: Vector2,
t: f32,
) -> Vector2
pub fn bezier_interpolate( self, control_1: Vector2, control_2: Vector2, end: Vector2, t: f32, ) -> Vector2
Returns the point at the given t
on the Bézier
curve defined by this vector and the given control_1
, control_2
, and end
points.
pub fn bounce(self, n: Vector2) -> Vector2
pub fn bounce(self, n: Vector2) -> Vector2
Returns a new vector “bounced off” from a plane defined by the given normal.
§Panics
If n
is not normalized.
pub fn limit_length(self, length: Option<f32>) -> Vector2
pub fn limit_length(self, length: Option<f32>) -> Vector2
Returns the vector with a maximum length by limiting its length to length
.
pub fn move_toward(self, to: Vector2, delta: f32) -> Vector2
pub fn move_toward(self, to: Vector2, delta: f32) -> Vector2
Returns a new vector moved toward to
by the fixed delta
amount. Will not go past the final value.
pub fn project(self, b: Vector2) -> Vector2
pub fn project(self, b: Vector2) -> Vector2
Returns the result of projecting the vector onto the given vector b
.
Trait Implementations§
§impl AddAssign for Vector2
impl AddAssign for Vector2
§fn add_assign(&mut self, rhs: Vector2)
fn add_assign(&mut self, rhs: Vector2)
+=
operation. Read more§impl ArrayElement for Vector2
impl ArrayElement for Vector2
fn debug_validate_elements(_array: &Array<Self>) -> Result<(), ConvertError>
§impl DivAssign<f32> for Vector2
impl DivAssign<f32> for Vector2
§fn div_assign(&mut self, rhs: f32)
fn div_assign(&mut self, rhs: f32)
/=
operation. Read more§impl DivAssign for Vector2
impl DivAssign for Vector2
§fn div_assign(&mut self, rhs: Vector2)
fn div_assign(&mut self, rhs: Vector2)
/=
operation. Read more§impl Export for Vector2
impl Export for Vector2
§fn export_hint() -> PropertyHintInfo
fn export_hint() -> PropertyHintInfo
§fn as_node_class() -> Option<ClassName>
fn as_node_class() -> Option<ClassName>
§impl Extend<Vector2> for PackedVector2Array
impl Extend<Vector2> for PackedVector2Array
Extends aPackedVector2Array
with the contents of an iterator
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Vector2>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = Vector2>,
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 Vector2
impl FromGodot for Vector2
§fn try_from_godot(
via: <Vector2 as GodotConvert>::Via,
) -> Result<Vector2, ConvertError>
fn try_from_godot( via: <Vector2 as GodotConvert>::Via, ) -> Result<Vector2, 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<Vector2> for PackedVector2Array
impl FromIterator<Vector2> for PackedVector2Array
Creates a PackedVector2Array
from an iterator.
§fn from_iter<I>(iter: I) -> PackedVector2Arraywhere
I: IntoIterator<Item = Vector2>,
fn from_iter<I>(iter: I) -> PackedVector2Arraywhere
I: IntoIterator<Item = Vector2>,
§impl GodotConvert for Vector2
impl GodotConvert for Vector2
§impl Index<Vector2Axis> for Vector2
impl Index<Vector2Axis> for Vector2
§impl IndexMut<Vector2Axis> for Vector2
impl IndexMut<Vector2Axis> for Vector2
§fn index_mut(&mut self, axis: Vector2Axis) -> &mut f32
fn index_mut(&mut self, axis: Vector2Axis) -> &mut f32
container[index]
) operation. Read more§impl Mul<Vector2> for Transform2D
impl Mul<Vector2> for Transform2D
§impl MulAssign<f32> for Vector2
impl MulAssign<f32> for Vector2
§fn mul_assign(&mut self, rhs: f32)
fn mul_assign(&mut self, rhs: f32)
*=
operation. Read more§impl MulAssign for Vector2
impl MulAssign for Vector2
§fn mul_assign(&mut self, rhs: Vector2)
fn mul_assign(&mut self, rhs: Vector2)
*=
operation. Read more§impl SubAssign for Vector2
impl SubAssign for Vector2
§fn sub_assign(&mut self, rhs: Vector2)
fn sub_assign(&mut self, rhs: Vector2)
-=
operation. Read more§impl ToGodot for Vector2
impl ToGodot for Vector2
§type ToVia<'v> = <Vector2 as GodotConvert>::Via
type ToVia<'v> = <Vector2 as GodotConvert>::Via
to_godot()
, which differs from Via
for pass-by-reference types.§fn to_godot(&self) -> <Vector2 as ToGodot>::ToVia<'_>
fn to_godot(&self) -> <Vector2 as ToGodot>::ToVia<'_>
§fn to_variant(&self) -> Variant
fn to_variant(&self) -> Variant
§impl Var for Vector2
impl Var for Vector2
fn get_property(&self) -> <Vector2 as GodotConvert>::Via
fn set_property(&mut self, value: <Vector2 as GodotConvert>::Via)
§fn var_hint() -> PropertyHintInfo
fn var_hint() -> PropertyHintInfo
GodotType::property_info
, e.g. for enums/newtypes.impl Copy for Vector2
impl GodotType for Vector2
impl PackedArrayElement for Vector2
impl StructuralPartialEq for Vector2
Auto Trait Implementations§
impl Freeze for Vector2
impl RefUnwindSafe for Vector2
impl Send for Vector2
impl Sync for Vector2
impl Unpin for Vector2
impl UnwindSafe for Vector2
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
)