pub struct Vector2 {
    pub x: f32,
    pub y: f32,
}
Expand description

2D vector class.

See also Vector2 in the Godot API doc.

Fields§

§x: f32§y: f32

Implementations§

source§

impl Vector2

Helper methods for Vector2.

See the official Godot documentation.

source

pub const ZERO: Vector2 = Vector2::new(0.0, 0.0)

The zero vector.

source

pub const ONE: Vector2 = Vector2::new(1.0, 1.0)

A vector with all components set to 1. Typically used for scaling.

source

pub const INF: Vector2 = Vector2::new(f32::INFINITY, f32::INFINITY)

A vector with all components set to +infinity.

source

pub const LEFT: Vector2 = Vector2::new(-1.0, 0.0)

Unit vector in -X direction.

source

pub const RIGHT: Vector2 = Vector2::new(1.0, 0.0)

Unit vector in +X direction.

source

pub const UP: Vector2 = Vector2::new(0.0, -1.0)

Unit vector in -Y direction (the Y axis points down in 2D).

source

pub const DOWN: Vector2 = Vector2::new(0.0, 1.0)

Unit vector in +Y direction (the Y axis points down in 2D).

source

pub const fn new(x: f32, y: f32) -> Vector2

Constructs a new Vector2 from the given x and y.

source

pub fn abs(self) -> Vector2

Returns a new vector with all components in absolute values (i.e. positive).

source

pub fn angle(self) -> f32

Returns this vector’s angle with respect to the positive X axis, or (1, 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(1, -1).angle() will return -PI / 4 (a negative eighth turn, or -45 degrees).

Equivalent to the result of @GDScript.atan2 when called with the vector’s y and x as parameters: atan2(y, x).

source

pub fn angle_to(self, to: Vector2) -> f32

Returns the angle to the given vector, in radians.

source

pub fn angle_to_point(self, to: Vector2) -> f32

Returns the angle between the line connecting the two points and the X axis, in radians

source

pub fn aspect(self) -> f32

Returns the aspect ratio of this vector, the ratio of x to y.

source

pub fn bounce(self, n: Vector2) -> Vector2

Returns the vector “bounced off” from a plane defined by the given normal.

source

pub fn ceil(self) -> Vector2

Returns the vector with all components rounded up (towards positive infinity).

source

pub fn clamped(self, length: f32) -> Vector2

Returns the vector with a maximum length by limiting its length to length.

source

pub fn cross(self, with: Vector2) -> f32

Returns the cross product of this vector and with.

source

pub fn cubic_interpolate(
    self,
    b: Vector2,
    pre_a: Vector2,
    post_b: Vector2,
    t: f32
) -> Vector2

Cubicly interpolates between this vector and b using pre_a and post_b as handles, and returns the result at position t. t is in the range of 0.0 - 1.0, representing the amount of interpolation.

source

pub fn direction_to(self, other: Vector2) -> Vector2

Returns the normalized vector pointing from this vector to other.

source

pub fn distance_squared_to(self, other: Vector2) -> f32

Returns the squared distance to other.

This method runs faster than distance_to, so prefer it if you need to compare vectors or need the squared distance for some formula.

source

pub fn distance_to(self, other: Vector2) -> f32

Returns the distance to other.

source

pub fn dot(self, with: Vector2) -> f32

Returns the dot product of this vector and with. This can be used to compare the angle between two vectors. For example, this can be used to determine whether an enemy is facing the player.

The dot product will be 0 for a straight angle (90 degrees), greater than 0 for angles narrower than 90 degrees and lower than 0 for angles wider than 90 degrees.

When using unit (normalized) vectors, the result will always be between -1.0 (180 degree angle) when the vectors are facing opposite directions, and 1.0 (0 degree angle) when the vectors are aligned.

Note: a.dot(b) is equivalent to b.dot(a).

source

pub fn floor(self) -> Vector2

Returns the vector with all components rounded down (towards negative infinity).

source

pub fn is_equal_approx(self, v: Vector2) -> bool

Returns true if this vector and v are approximately equal, by running @GDScript.is_equal_approx on each component.

source

pub fn is_normalized(self) -> bool

Returns true if the vector is normalized, and false otherwise.

source

pub fn length(self) -> f32

Returns the length (magnitude) of this vector.

source

pub fn length_squared(self) -> f32

Returns the squared length (squared magnitude) of this vector.

This method runs faster than length, so prefer it if you need to compare vectors or need the squared distance for some formula.

source

pub fn linear_interpolate(self, b: Vector2, t: f32) -> Vector2

Returns the result of the linear interpolation between this vector and b by amount t. t is on the range of 0.0 to 1.0, representing the amount of interpolation.

source

pub fn move_toward(self, to: Vector2, delta: f32) -> Vector2

Returns self moved towards to by the distance delta, clamped by to.

source

pub fn normalized(self) -> Vector2

Returns the vector scaled to unit length. Equivalent to v / v.length().

source

pub fn posmod(self, rem: f32) -> Vector2

Returns a vector composed of the @GDScript.fposmod of this vector’s components and rem.

source

pub fn posmodv(self, remv: Vector2) -> Vector2

Returns a vector composed of the @GDScript.fposmod of this vector’s components and remv components.

source

pub fn project(self, b: Vector2) -> Vector2

Returns the vector projected onto the vector b.

source

pub fn reflect(self, n: Vector2) -> Vector2

Returns the vector reflected from a plane defined by the given normal.

source

pub fn rotated(self, angle: f32) -> Vector2

Returns the vector rotated by angle radians.

source

pub fn round(self) -> Vector2

Returns the vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.

source

pub fn sign(self) -> Vector2

Returns the vector with each component set to one or negative one, depending on the signs of the components, or zero if the component is zero, by calling @GDScript.sign on each component.

source

pub fn slerp(self, b: Vector2, t: f32) -> Vector2

Returns the result of spherical linear interpolation between this vector and b, by amount t. t is on the range of 0.0 to 1.0, representing the amount of interpolation.

Note: Both vectors must be normalized.

source

pub fn slide(self, normal: Vector2) -> Vector2

Returns the component of the vector along a plane defined by the given normal.

source

pub fn snapped(self, by: Vector2) -> Vector2

Returns the vector snapped to a grid with the given size.

source

pub fn tangent(self) -> Vector2

Returns a perpendicular vector.

Trait Implementations§

source§

impl Add<Vector2> for Vector2

§

type Output = Vector2

The resulting type after applying the + operator.
source§

fn add(self, with: Vector2) -> Vector2

Performs the + operation. Read more
source§

impl AddAssign<Vector2> for Vector2

source§

fn add_assign(&mut self, with: Vector2)

Performs the += operation. Read more
source§

impl Clone for Vector2

source§

fn clone(&self) -> Vector2

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl CoerceFromVariant for Vector2

source§

impl Debug for Vector2

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl Default for Vector2

source§

fn default() -> Vector2

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Vector2

source§

fn deserialize<__D>(
    __deserializer: __D
) -> Result<Vector2, <__D as Deserializer<'de>>::Error>where
    __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Div<Vector2> for Vector2

§

type Output = Vector2

The resulting type after applying the / operator.
source§

fn div(self, with: Vector2) -> Vector2

Performs the / operation. Read more
source§

impl Div<f32> for Vector2

§

type Output = Vector2

The resulting type after applying the / operator.
source§

fn div(self, with: f32) -> Vector2

Performs the / operation. Read more
source§

impl DivAssign<Vector2> for Vector2

source§

fn div_assign(&mut self, with: Vector2)

Performs the /= operation. Read more
source§

impl DivAssign<f32> for Vector2

source§

fn div_assign(&mut self, with: f32)

Performs the /= operation. Read more
source§

impl Export for Vector2

§

type Hint = NoHint

A type-specific hint type that is valid for the type being exported. Read more
source§

fn export_info(_hint: Option<<Vector2 as Export>::Hint>) -> ExportInfo

Returns ExportInfo given an optional typed hint.
source§

impl FromVariant for Vector2

source§

impl Mul<Vector2> for Vector2

§

type Output = Vector2

The resulting type after applying the * operator.
source§

fn mul(self, with: Vector2) -> Vector2

Performs the * operation. Read more
source§

impl Mul<f32> for Vector2

§

type Output = Vector2

The resulting type after applying the * operator.
source§

fn mul(self, with: f32) -> Vector2

Performs the * operation. Read more
source§

impl MulAssign<Vector2> for Vector2

source§

fn mul_assign(&mut self, with: Vector2)

Performs the *= operation. Read more
source§

impl MulAssign<f32> for Vector2

source§

fn mul_assign(&mut self, with: f32)

Performs the *= operation. Read more
source§

impl Neg for Vector2

§

type Output = Vector2

The resulting type after applying the - operator.
source§

fn neg(self) -> Vector2

Performs the unary - operation. Read more
source§

impl PartialEq<Vector2> for Vector2

source§

fn eq(&self, other: &Vector2) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for Vector2

source§

fn serialize<__S>(
    &self,
    __serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
    __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Sub<Vector2> for Vector2

§

type Output = Vector2

The resulting type after applying the - operator.
source§

fn sub(self, with: Vector2) -> Vector2

Performs the - operation. Read more
source§

impl SubAssign<Vector2> for Vector2

source§

fn sub_assign(&mut self, with: Vector2)

Performs the -= operation. Read more
source§

impl ToVariant for Vector2

source§

impl Copy for Vector2

source§

impl PoolElement for Vector2

source§

impl StructuralPartialEq for Vector2

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere
    T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere
    U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> OwnedToVariant for Twhere
    T: ToVariant,

source§

impl<T> ToOwned for Twhere
    T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere
    U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere
    T: for<'de> Deserialize<'de>,