Struct gdnative::core_types::Vector2

#[repr(C)]
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§

§

impl Vector2

Helper methods for Vector2.

See the official Godot documentation.

pub const ZERO: Vector2 = _

The zero vector.

pub const ONE: Vector2 = _

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

pub const INF: Vector2 = _

A vector with all components set to +infinity.

pub const LEFT: Vector2 = _

Unit vector in -X direction.

pub const RIGHT: Vector2 = _

Unit vector in +X direction.

pub const UP: Vector2 = _

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

pub const DOWN: Vector2 = _

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

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

Constructs a new Vector2 from the given x and y.

pub fn abs(self) -> Vector2

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

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).

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

Returns the angle to the given vector, in radians.

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

pub fn aspect(self) -> f32

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

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

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

pub fn ceil(self) -> Vector2

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

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

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

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

Returns the cross product of this vector and with.

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.

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

Returns the normalized vector pointing from this vector to other.

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.

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

Returns the distance to other.

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).

pub fn floor(self) -> Vector2

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

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.

pub fn is_normalized(self) -> bool

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

pub fn length(self) -> f32

Returns the length (magnitude) of this vector.

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.

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.

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

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

pub fn normalized(self) -> Vector2

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

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

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

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

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

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

Returns the vector projected onto the vector b.

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

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

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

Returns the vector rotated by angle radians.

pub fn round(self) -> Vector2

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

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.

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.

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

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

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

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

pub fn tangent(self) -> Vector2

Returns a perpendicular vector.

Trait Implementations§

§

impl Add for Vector2

§

type Output = Vector2

The resulting type after applying the + operator.
§

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

Performs the + operation. Read more
§

impl AddAssign for Vector2

§

fn add_assign(&mut self, with: Vector2)

Performs the += operation. Read more
§

impl Clone for Vector2

§

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
§

impl CoerceFromVariant for Vector2

§

impl Debug for Vector2

§

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

Formats the value using the given formatter. Read more
§

impl Default for Vector2

§

fn default() -> Vector2

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

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

§

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
§

impl Div<f32> for Vector2

§

type Output = Vector2

The resulting type after applying the / operator.
§

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

Performs the / operation. Read more
§

impl Div for Vector2

§

type Output = Vector2

The resulting type after applying the / operator.
§

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

Performs the / operation. Read more
§

impl DivAssign<f32> for Vector2

§

fn div_assign(&mut self, with: f32)

Performs the /= operation. Read more
§

impl DivAssign for Vector2

§

fn div_assign(&mut self, with: Vector2)

Performs the /= operation. Read more
§

impl Export for Vector2

§

type Hint = NoHint

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

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

Returns ExportInfo given an optional typed hint.
§

impl FromVariant for Vector2

§

impl Mul<f32> for Vector2

§

type Output = Vector2

The resulting type after applying the * operator.
§

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

Performs the * operation. Read more
§

impl Mul for Vector2

§

type Output = Vector2

The resulting type after applying the * operator.
§

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

Performs the * operation. Read more
§

impl MulAssign<f32> for Vector2

§

fn mul_assign(&mut self, with: f32)

Performs the *= operation. Read more
§

impl MulAssign for Vector2

§

fn mul_assign(&mut self, with: Vector2)

Performs the *= operation. Read more
§

impl Neg for Vector2

§

type Output = Vector2

The resulting type after applying the - operator.
§

fn neg(self) -> Vector2

Performs the unary - operation. Read more
§

impl PartialEq for Vector2

§

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

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

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

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

impl Serialize for Vector2

§

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
§

impl Sub for Vector2

§

type Output = Vector2

The resulting type after applying the - operator.
§

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

Performs the - operation. Read more
§

impl SubAssign for Vector2

§

fn sub_assign(&mut self, with: Vector2)

Performs the -= operation. Read more
§

impl ToVariant for Vector2

§

fn to_variant(&self) -> Variant

§

impl Copy for Vector2

§

impl PoolElement for Vector2

§

impl StructuralPartialEq for Vector2

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

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.

§

impl<T> OwnedToVariant for T
where T: ToVariant,

source§

impl<T> ToOwned for T
where T: Clone,

source§

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 T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

source§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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