Vector4

Vector used for 4D math using floating point coordinates.

Description

4-element structure that can be used to represent any quadruplet of numeric values.

It uses floating-point coordinates. See Vector4i for its integer counterpart.

Note: In a boolean context, a Vector4 will evaluate to false if it's equal to Vector4(0, 0, 0, 0). Otherwise, a Vector4 will always evaluate to true.

Properties

float

w

0.0

float

x

0.0

float

y

0.0

float

z

0.0

Constructors

Vector4

Vector4 ( )

Vector4

Vector4 ( Vector4 from )

Vector4

Vector4 ( Vector4i from )

Vector4

Vector4 ( float x, float y, float z, float w )

Methods

Vector4

abs ( ) const

Vector4

ceil ( ) const

Vector4

clamp ( Vector4 min, Vector4 max ) const

Vector4

cubic_interpolate ( Vector4 b, Vector4 pre_a, Vector4 post_b, float weight ) const

Vector4

cubic_interpolate_in_time ( Vector4 b, Vector4 pre_a, Vector4 post_b, float weight, float b_t, float pre_a_t, float post_b_t ) const

Vector4

direction_to ( Vector4 to ) const

float

distance_squared_to ( Vector4 to ) const

float

distance_to ( Vector4 to ) const

float

dot ( Vector4 with ) const

Vector4

floor ( ) const

Vector4

inverse ( ) const

bool

is_equal_approx ( Vector4 with ) const

bool

is_normalized ( ) const

bool

is_zero_approx ( ) const

float

length ( ) const

float

length_squared ( ) const

Vector4

lerp ( Vector4 to, float weight ) const

int

max_axis_index ( ) const

int

min_axis_index ( ) const

Vector4

normalized ( ) const

Vector4

posmod ( float mod ) const

Vector4

posmodv ( Vector4 modv ) const

Vector4

round ( ) const

Vector4

sign ( ) const

Vector4

snapped ( Vector4 step ) const

Operators

bool

operator != ( Vector4 right )

Vector4

operator * ( Projection right )

Vector4

operator * ( Vector4 right )

Vector4

operator * ( float right )

Vector4

operator * ( int right )

Vector4

operator + ( Vector4 right )

Vector4

operator - ( Vector4 right )

Vector4

operator / ( Vector4 right )

Vector4

operator / ( float right )

Vector4

operator / ( int right )

bool

operator < ( Vector4 right )

bool

operator <= ( Vector4 right )

bool

operator == ( Vector4 right )

bool

operator > ( Vector4 right )

bool

operator >= ( Vector4 right )

float

operator [] ( int index )

Vector4

operator unary+ ( )

Vector4

operator unary- ( )

Constants

  • AXIS_X = 0 --- Enumerated value for the X axis. Returned by max_axis_index and min_axis_index.

  • AXIS_Y = 1 --- Enumerated value for the Y axis. Returned by max_axis_index and min_axis_index.

  • AXIS_Z = 2 --- Enumerated value for the Z axis. Returned by max_axis_index and min_axis_index.

  • AXIS_W = 3 --- Enumerated value for the W axis. Returned by max_axis_index and min_axis_index.

  • ZERO = Vector4(0, 0, 0, 0) --- Zero vector, a vector with all components set to 0.

  • ONE = Vector4(1, 1, 1, 1) --- One vector, a vector with all components set to 1.

  • INF = Vector4(inf, inf, inf, inf) --- Infinity vector, a vector with all components set to @GDScript.INF.

Property Descriptions

Default

0.0

The vector's W component. Also accessible by using the index position [3].


Default

0.0

The vector's X component. Also accessible by using the index position [0].


Default

0.0

The vector's Y component. Also accessible by using the index position [1].


Default

0.0

The vector's Z component. Also accessible by using the index position [2].

Constructor Descriptions

Constructs a default-initialized Vector4 with all components set to 0.


Constructs a Vector4 as a copy of the given Vector4.


Constructs a new Vector4 from the given Vector4i.


Returns a Vector4 with the given components.

Method Descriptions

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


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


Returns a new vector with all components clamped between the components of min and max, by running @GlobalScope.clamp on each component.


Performs a cubic interpolation between this vector 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.


Performs a cubic interpolation between this vector 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.


Returns the normalized vector pointing from this vector to to. This is equivalent to using (b - a).normalized().


Returns the squared distance between this vector and to.

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


Returns the distance between this vector and to.


Returns the dot product of this vector and with.


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


Returns the inverse of the vector. This is the same as Vector4(1.0 / v.x, 1.0 / v.y, 1.0 / v.z, 1.0 / v.w).


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


  • bool is_normalized ( ) const

Returns true if the vector is normalized, i.e. its length is equal to 1.


  • bool is_zero_approx ( ) const

Returns true if this vector's values are approximately zero, by running @GlobalScope.is_zero_approx on each component.

This method is faster than using is_equal_approx with one value as a zero vector.


Returns the length (magnitude) of this vector.


  • float length_squared ( ) const

Returns the squared length (squared magnitude) of this vector. This method runs faster than length.


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.


  • int max_axis_index ( ) const

Returns the axis of the vector's highest value. See AXIS_* constants. If all components are equal, this method returns AXIS_X.


  • int min_axis_index ( ) const

Returns the axis of the vector's lowest value. See AXIS_* constants. If all components are equal, this method returns AXIS_W.


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


Returns a vector composed of the @GlobalScope.fposmod of this vector's components and mod.


Returns a vector composed of the @GlobalScope.fposmod of this vector's components and modv's components.


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


Returns a new 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 @GlobalScope.sign on each component.


Returns this vector with each component snapped to the nearest multiple of step. This can also be used to round to an arbitrary number of decimals.

Operator Descriptions

Returns true if the vectors are not equal.

Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.


Inversely transforms (multiplies) the Vector4 by the given Projection matrix.


Multiplies each component of the Vector4 by the components of the given Vector4.

print(Vector4(10, 20, 30, 40) * Vector4(3, 4, 5, 6)) # Prints "(30, 80, 150, 240)"

Multiplies each component of the Vector4 by the given float.

print(Vector4(10, 20, 30, 40) * 2) # Prints "(20, 40, 60, 80)"

Multiplies each component of the Vector4 by the given int.


Adds each component of the Vector4 by the components of the given Vector4.

print(Vector4(10, 20, 30, 40) + Vector4(3, 4, 5, 6)) # Prints "(13, 24, 35, 46)"

Subtracts each component of the Vector4 by the components of the given Vector4.

print(Vector4(10, 20, 30, 40) - Vector4(3, 4, 5, 6)) # Prints "(7, 16, 25, 34)"

Divides each component of the Vector4 by the components of the given Vector4.

print(Vector4(10, 20, 30, 40) / Vector4(2, 5, 3, 4)) # Prints "(5, 4, 10, 10)"

Divides each component of the Vector4 by the given float.

print(Vector4(10, 20, 30, 40) / 2 # Prints "(5, 10, 15, 20)"

Divides each component of the Vector4 by the given int.


Compares two Vector4 vectors by first checking if the X value of the left vector is less than the X value of the right vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.


Compares two Vector4 vectors by first checking if the X value of the left vector is less than or equal to the X value of the right vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.


Returns true if the vectors are exactly equal.

Note: Due to floating-point precision errors, consider using is_equal_approx instead, which is more reliable.


Compares two Vector4 vectors by first checking if the X value of the left vector is greater than the X value of the right vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.


Compares two Vector4 vectors by first checking if the X value of the left vector is greater than or equal to the X value of the right vector. If the X values are exactly equal, then it repeats this check with the Y values of the two vectors, Z values of the two vectors, and then with the W values. This operator is useful for sorting vectors.


Access vector components using their index. v[0] is equivalent to v.x, v[1] is equivalent to v.y, v[2] is equivalent to v.z, and v[3] is equivalent to v.w.


Returns the same value as if the + was not there. Unary + does nothing, but sometimes it can make your code more readable.


Returns the negative value of the Vector4. This is the same as writing Vector4(-v.x, -v.y, -v.z, -v.w). This operation flips the direction of the vector while keeping the same magnitude. With floats, the number zero can be either positive or negative.