Struct gdnative::core_types::Vector2
source · Expand description
2D vector class.
See also Vector2 in the Godot API doc.
Fields§
§x: f32
§y: f32
Implementations§
source§impl Vector2
impl Vector2
Helper methods for Vector2
.
See the official Godot documentation
.
sourcepub const ONE: Vector2 = Vector2::new(1.0, 1.0)
pub const ONE: Vector2 = Vector2::new(1.0, 1.0)
A vector with all components set to 1. Typically used for scaling.
sourcepub const INF: Vector2 = Vector2::new(f32::INFINITY, f32::INFINITY)
pub const INF: Vector2 = Vector2::new(f32::INFINITY, f32::INFINITY)
A vector with all components set to +infinity.
sourcepub const UP: Vector2 = Vector2::new(0.0, -1.0)
pub const UP: Vector2 = Vector2::new(0.0, -1.0)
Unit vector in -Y direction (the Y axis points down in 2D).
sourcepub const DOWN: Vector2 = Vector2::new(0.0, 1.0)
pub const DOWN: Vector2 = Vector2::new(0.0, 1.0)
Unit vector in +Y direction (the Y axis points down in 2D).
sourcepub fn abs(self) -> Vector2
pub fn abs(self) -> Vector2
Returns a new vector with all components in absolute values (i.e. positive).
sourcepub fn angle(self) -> f32
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).
sourcepub fn angle_to_point(self, to: Vector2) -> f32
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
sourcepub fn bounce(self, n: Vector2) -> Vector2
pub fn bounce(self, n: Vector2) -> Vector2
Returns the vector “bounced off” from a plane defined by the given normal.
sourcepub fn ceil(self) -> Vector2
pub fn ceil(self) -> Vector2
Returns the vector with all components rounded up (towards positive infinity).
sourcepub fn clamped(self, length: f32) -> Vector2
pub fn clamped(self, length: f32) -> Vector2
Returns the vector with a maximum length by limiting its length to length
.
sourcepub fn cubic_interpolate(
self,
b: Vector2,
pre_a: Vector2,
post_b: Vector2,
t: f32
) -> Vector2
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.
sourcepub fn direction_to(self, other: Vector2) -> Vector2
pub fn direction_to(self, other: Vector2) -> Vector2
Returns the normalized vector pointing from this vector to other
.
sourcepub fn distance_squared_to(self, other: Vector2) -> f32
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.
sourcepub fn distance_to(self, other: Vector2) -> f32
pub fn distance_to(self, other: Vector2) -> f32
Returns the distance to other
.
sourcepub fn dot(self, with: Vector2) -> f32
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)
.
sourcepub fn floor(self) -> Vector2
pub fn floor(self) -> Vector2
Returns the vector with all components rounded down (towards negative infinity).
sourcepub fn is_equal_approx(self, v: Vector2) -> bool
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.
sourcepub fn is_normalized(self) -> bool
pub fn is_normalized(self) -> bool
Returns true
if the vector is normalized, and false otherwise.
sourcepub fn length_squared(self) -> f32
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.
sourcepub fn linear_interpolate(self, b: Vector2, t: f32) -> Vector2
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.
sourcepub fn move_toward(self, to: Vector2, delta: f32) -> Vector2
pub fn move_toward(self, to: Vector2, delta: f32) -> Vector2
Returns self
moved towards to
by the distance delta
, clamped by to
.
sourcepub fn normalized(self) -> Vector2
pub fn normalized(self) -> Vector2
Returns the vector scaled to unit length. Equivalent to v / v.length()
.
sourcepub fn posmod(self, rem: f32) -> Vector2
pub fn posmod(self, rem: f32) -> Vector2
Returns a vector composed of the @GDScript.fposmod of this vector’s components and rem
.
sourcepub fn posmodv(self, remv: Vector2) -> Vector2
pub fn posmodv(self, remv: Vector2) -> Vector2
Returns a vector composed of the @GDScript.fposmod of this vector’s components and remv
components.
sourcepub fn reflect(self, n: Vector2) -> Vector2
pub fn reflect(self, n: Vector2) -> Vector2
Returns the vector reflected from a plane defined by the given normal.
sourcepub fn round(self) -> Vector2
pub fn round(self) -> Vector2
Returns the vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
sourcepub fn sign(self) -> Vector2
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.
sourcepub fn slerp(self, b: Vector2, t: f32) -> Vector2
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.
sourcepub fn slide(self, normal: Vector2) -> Vector2
pub fn slide(self, normal: Vector2) -> Vector2
Returns the component of the vector along a plane defined by the given normal.
Trait Implementations§
source§impl AddAssign<Vector2> for Vector2
impl AddAssign<Vector2> for Vector2
source§fn add_assign(&mut self, with: Vector2)
fn add_assign(&mut self, with: Vector2)
+=
operation. Read moresource§impl CoerceFromVariant for Vector2
impl CoerceFromVariant for Vector2
fn coerce_from_variant(v: &Variant) -> Vector2
source§impl<'de> Deserialize<'de> for Vector2
impl<'de> Deserialize<'de> for Vector2
source§fn deserialize<__D>(
__deserializer: __D
) -> Result<Vector2, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D
) -> Result<Vector2, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
source§impl DivAssign<Vector2> for Vector2
impl DivAssign<Vector2> for Vector2
source§fn div_assign(&mut self, with: Vector2)
fn div_assign(&mut self, with: Vector2)
/=
operation. Read moresource§impl DivAssign<f32> for Vector2
impl DivAssign<f32> for Vector2
source§fn div_assign(&mut self, with: f32)
fn div_assign(&mut self, with: f32)
/=
operation. Read moresource§impl FromVariant for Vector2
impl FromVariant for Vector2
fn from_variant(variant: &Variant) -> Result<Vector2, FromVariantError>
source§impl MulAssign<Vector2> for Vector2
impl MulAssign<Vector2> for Vector2
source§fn mul_assign(&mut self, with: Vector2)
fn mul_assign(&mut self, with: Vector2)
*=
operation. Read moresource§impl MulAssign<f32> for Vector2
impl MulAssign<f32> for Vector2
source§fn mul_assign(&mut self, with: f32)
fn mul_assign(&mut self, with: f32)
*=
operation. Read moresource§impl PartialEq<Vector2> for Vector2
impl PartialEq<Vector2> for Vector2
source§impl Serialize for Vector2
impl Serialize for Vector2
source§fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
source§impl SubAssign<Vector2> for Vector2
impl SubAssign<Vector2> for Vector2
source§fn sub_assign(&mut self, with: Vector2)
fn sub_assign(&mut self, with: Vector2)
-=
operation. Read more