Class Vector4
A 4D vector using floating-point coordinates.
Remarks
A 4-element structure that can be used to represent 4D coordinates or any other quadruplet of numeric values.
It uses floating-point coordinates. By default, these floating-point values use 32-bit precision, unlike float which is always 64-bit. If double precision is needed, compile the engine with the option precision=double
.
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
.
Constructors
Vector4
Constructs a default-initialized Vector4 with all components set to 0
.
Vector4 Vector4
Vector4(Vector4)
Constructs a Vector4 as a copy of the given Vector4.
Vector4 Vector4(Vector4 from)
Parameters
from
Vector4
Vector4(Vector4i)
Constructs a new Vector4 from the given Vector4i.
Vector4 Vector4(Vector4i from)
Parameters
from
Vector4i
Vector4(float, float, float, float)
Returns a Vector4 with the given components.
Vector4 Vector4(float x, float y, float z, float w)
Parameters
Fields
ZERO
Zero vector, a vector with all components set to 0
.
const ZERO = Vector4(0, 0, 0, 0)
ONE
One vector, a vector with all components set to 1
.
const ONE = Vector4(1, 1, 1, 1)
INF
Infinity vector, a vector with all components set to INF.
const INF = Vector4(inf, inf, inf, inf)
Properties
w
The vector's W component. Also accessible by using the index position [3]
.
var w : float = 0.0
Property Value
x
The vector's X component. Also accessible by using the index position [0]
.
var x : float = 0.0
Property Value
y
The vector's Y component. Also accessible by using the index position [1]
.
var y : float = 0.0
Property Value
z
The vector's Z component. Also accessible by using the index position [2]
.
var z : float = 0.0
Property Value
Methods
abs
Qualifiers: const
Returns a new vector with all components in absolute values (i.e. positive).
Vector4 abs
ceil
Qualifiers: const
Returns a new vector with all components rounded up (towards positive infinity).
Vector4 ceil
clamp(Vector4, Vector4)
Qualifiers: const
Returns a new vector with all components clamped between the components of min
and max
, by running @GlobalScope.clamp on each component.
Vector4 clamp(Vector4 min, Vector4 max)
Parameters
clampf(float, float)
Qualifiers: const
Returns a new vector with all components clamped between min
and max
, by running @GlobalScope.clamp on each component.
Vector4 clampf(float min, float max)
Parameters
cubic_interpolate(Vector4, Vector4, Vector4, float)
Qualifiers: const
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.
Vector4 cubic_interpolate(Vector4 b, Vector4 pre_a, Vector4 post_b, float weight)
Parameters
cubic_interpolate_in_time(Vector4, Vector4, Vector4, float, float, float, float)
Qualifiers: const
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 Vector4.cubic_interpolate by the time values.
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)
Parameters
direction_to(Vector4)
Qualifiers: const
Returns the normalized vector pointing from this vector to to
. This is equivalent to using (b - a).normalized()
.
Vector4 direction_to(Vector4 to)
Parameters
to
Vector4
distance_squared_to(Vector4)
Qualifiers: const
Returns the squared distance between this vector and to
.
This method runs faster than Vector4.distance_to, so prefer it if you need to compare vectors or need the squared distance for some formula.
float distance_squared_to(Vector4 to)
Parameters
to
Vector4
distance_to(Vector4)
Qualifiers: const
Returns the distance between this vector and to
.
float distance_to(Vector4 to)
Parameters
to
Vector4
dot(Vector4)
Qualifiers: const
Returns the dot product of this vector and with
.
float dot(Vector4 with)
Parameters
with
Vector4
floor
Qualifiers: const
Returns a new vector with all components rounded down (towards negative infinity).
Vector4 floor
inverse
Qualifiers: const
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)
.
Vector4 inverse
is_equal_approx(Vector4)
Qualifiers: const
Returns true
if this vector and to
are approximately equal, by running @GlobalScope.is_equal_approx on each component.
bool is_equal_approx(Vector4 to)
Parameters
to
Vector4
is_finite
Qualifiers: const
Returns true
if this vector is finite, by calling @GlobalScope.is_finite on each component.
bool is_finite
is_normalized
Qualifiers: const
Returns true
if the vector is normalized, i.e. its length is approximately equal to 1.
bool is_normalized
is_zero_approx
Qualifiers: 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 Vector4.is_equal_approx with one value as a zero vector.
bool is_zero_approx
length
Qualifiers: const
Returns the length (magnitude) of this vector.
float length
length_squared
Qualifiers: const
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.
float length_squared
lerp(Vector4, float)
Qualifiers: const
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.
Vector4 lerp(Vector4 to, float weight)
Parameters
max(Vector4)
Qualifiers: const
Returns the component-wise maximum of this and with
, equivalent to Vector4(maxf(x, with.x), maxf(y, with.y), maxf(z, with.z), maxf(w, with.w))
.
Vector4 max(Vector4 with)
Parameters
with
Vector4
max_axis_index
Qualifiers: const
Returns the axis of the vector's highest value. See AXIS_*
constants. If all components are equal, this method returns Vector4.AXIS_X.
int max_axis_index
maxf(float)
Qualifiers: const
Returns the component-wise maximum of this and with
, equivalent to Vector4(maxf(x, with), maxf(y, with), maxf(z, with), maxf(w, with))
.
Vector4 maxf(float with)
Parameters
with
float
min(Vector4)
Qualifiers: const
Returns the component-wise minimum of this and with
, equivalent to Vector4(minf(x, with.x), minf(y, with.y), minf(z, with.z), minf(w, with.w))
.
Vector4 min(Vector4 with)
Parameters
with
Vector4
min_axis_index
Qualifiers: const
Returns the axis of the vector's lowest value. See AXIS_*
constants. If all components are equal, this method returns Vector4.AXIS_W.
int min_axis_index
minf(float)
Qualifiers: const
Returns the component-wise minimum of this and with
, equivalent to Vector4(minf(x, with), minf(y, with), minf(z, with), minf(w, with))
.
Vector4 minf(float with)
Parameters
with
float
normalized
Qualifiers: const
Returns the result of scaling the vector to unit length. Equivalent to v / v.length()
. Returns (0, 0, 0, 0)
if v.length() == 0
. See also is_normalized.
Note: This function may return incorrect values if the input vector length is near zero.
Vector4 normalized
posmod(float)
Qualifiers: const
Returns a vector composed of the @GlobalScope.fposmod of this vector's components and mod
.
Vector4 posmod(float mod)
Parameters
mod
float
posmodv(Vector4)
Qualifiers: const
Returns a vector composed of the @GlobalScope.fposmod of this vector's components and modv
's components.
Vector4 posmodv(Vector4 modv)
Parameters
modv
Vector4
round
Qualifiers: const
Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
Vector4 round
sign
Qualifiers: const
Returns a new vector with each component set to 1.0
if it's positive, -1.0
if it's negative, and 0.0
if it's zero. The result is identical to calling @GlobalScope.sign on each component.
Vector4 sign
snapped(Vector4)
Qualifiers: const
Returns a new vector with each component snapped to the nearest multiple of the corresponding component in step
. This can also be used to round the components to an arbitrary number of decimals.
Vector4 snapped(Vector4 step)
Parameters
step
Vector4
snappedf(float)
Qualifiers: const
Returns a new vector with each component snapped to the nearest multiple of step
. This can also be used to round the components to an arbitrary number of decimals.
Vector4 snappedf(float step)
Parameters
step
float
Operators
!= (Vector4)
Returns true
if the vectors are not equal.
Note: Due to floating-point precision errors, consider using Vector4.is_equal_approx instead, which is more reliable.
Note: Vectors with NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.
bool != (Vector4 right)
Parameters
right
Vector4
* (Projection)
Transforms (multiplies) the Vector4 by the transpose of the given Projection matrix.
For transforming by inverse of a projection projection.inverse() * vector
can be used instead. See inverse.
Vector4 * (Projection right)
Parameters
right
Projection
* (Vector4)
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.0, 80.0, 150.0, 240.0)
Vector4 * (Vector4 right)
Parameters
right
Vector4
* (float)
Multiplies each component of the Vector4 by the given float.
print(Vector4(10, 20, 30, 40) * 2) # Prints (20.0, 40.0, 60.0, 80.0)
Vector4 * (float right)
Parameters
right
float
* (int)
Multiplies each component of the Vector4 by the given int.
Vector4 * (int right)
Parameters
right
int
+ (Vector4)
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.0, 24.0, 35.0, 46.0)
Vector4 + (Vector4 right)
Parameters
right
Vector4
- (Vector4)
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.0, 16.0, 25.0, 34.0)
Vector4 - (Vector4 right)
Parameters
right
Vector4
/ (Vector4)
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.0, 4.0, 10.0, 10.0)
Vector4 / (Vector4 right)
Parameters
right
Vector4
/ (float)
Divides each component of the Vector4 by the given float.
print(Vector4(10, 20, 30, 40) / 2) # Prints (5.0, 10.0, 15.0, 20.0)
Vector4 / (float right)
Parameters
right
float
/ (int)
Divides each component of the Vector4 by the given int.
Vector4 / (int right)
Parameters
right
int
< (Vector4)
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.
Note: Vectors with NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.
bool < (Vector4 right)
Parameters
right
Vector4
<= (Vector4)
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.
Note: Vectors with NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.
bool <= (Vector4 right)
Parameters
right
Vector4
== (Vector4)
Returns true
if the vectors are exactly equal.
Note: Due to floating-point precision errors, consider using Vector4.is_equal_approx instead, which is more reliable.
Note: Vectors with NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.
bool == (Vector4 right)
Parameters
right
Vector4
> (Vector4)
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.
Note: Vectors with NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.
bool > (Vector4 right)
Parameters
right
Vector4
>= (Vector4)
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.
Note: Vectors with NAN elements don't behave the same as other vectors. Therefore, the results from this operator may not be accurate if NaNs are included.
bool >= (Vector4 right)
Parameters
right
Vector4
[] (int)
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
.
float [] (int index)
Parameters
index
int
unary+
Returns the same value as if the +
was not there. Unary +
does nothing, but sometimes it can make your code more readable.
Vector4 unary+
unary-
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.
Vector4 unary-