Table of Contents

Struct Vector4

Namespace
Godot
Assembly
GodotSharp.dll

4-element structure that can be used to represent positions in 4D space or any other pair of numeric values.

public struct Vector4 : IEquatable<Vector4>
Implements
Inherited Members

Constructors

Vector4(float, float, float, float)

Constructs a new Vector4 with the given components.

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

Parameters

x float

The vector's X component.

y float

The vector's Y component.

z float

The vector's Z component.

w float

The vector's W component.

Fields

W

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

public float W

Field Value

float

X

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

public float X

Field Value

float

Y

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

public float Y

Field Value

float

Z

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

public float Z

Field Value

float

Properties

Inf

Infinity vector, a vector with all components set to Inf.

public static Vector4 Inf { get; }

Property Value

Vector4

Equivalent to new Vector4(Mathf.Inf, Mathf.Inf, Mathf.Inf, Mathf.Inf).

this[int]

Access vector components using their index.

public float this[int index] { readonly get; set; }

Parameters

index int

Property Value

float

[0] is equivalent to X, [1] is equivalent to Y, [2] is equivalent to Z. [3] is equivalent to W.

Exceptions

ArgumentOutOfRangeException

index is not 0, 1, 2 or 3.

One

One vector, a vector with all components set to 1.

public static Vector4 One { get; }

Property Value

Vector4

Equivalent to new Vector4(1, 1, 1, 1).

Zero

Zero vector, a vector with all components set to 0.

public static Vector4 Zero { get; }

Property Value

Vector4

Equivalent to new Vector4(0, 0, 0, 0).

Methods

Abs()

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

public readonly Vector4 Abs()

Returns

Vector4

A vector with Abs(float) called on each component.

Ceil()

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

public readonly Vector4 Ceil()

Returns

Vector4

A vector with Ceil(float) called on each component.

Clamp(Vector4, Vector4)

Returns a new vector with all components clamped between the components of min and max using Clamp(float, float, float).

public readonly Vector4 Clamp(Vector4 min, Vector4 max)

Parameters

min Vector4

The vector with minimum allowed values.

max Vector4

The vector with maximum allowed values.

Returns

Vector4

The vector with all components clamped.

CubicInterpolate(Vector4, Vector4, Vector4, float)

Performs a cubic interpolation between vectors preA, this vector, b, and postB, by the given amount weight.

public readonly Vector4 CubicInterpolate(Vector4 b, Vector4 preA, Vector4 postB, float weight)

Parameters

b Vector4

The destination vector.

preA Vector4

A vector before this vector.

postB Vector4

A vector after b.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector4

The interpolated vector.

CubicInterpolateInTime(Vector4, Vector4, Vector4, float, float, float, float)

Performs a cubic interpolation between vectors preA, this vector, b, and postB, by the given amount weight. It can perform smoother interpolation than CubicInterpolate(Vector4, Vector4, Vector4, float) by the time values.

public readonly Vector4 CubicInterpolateInTime(Vector4 b, Vector4 preA, Vector4 postB, float weight, float t, float preAT, float postBT)

Parameters

b Vector4

The destination vector.

preA Vector4

A vector before this vector.

postB Vector4

A vector after b.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

t float
preAT float
postBT float

Returns

Vector4

The interpolated vector.

Deconstruct(out float, out float, out float, out float)

Helper method for deconstruction into a tuple.

public readonly void Deconstruct(out float x, out float y, out float z, out float w)

Parameters

x float
y float
z float
w float

DirectionTo(Vector4)

Returns the normalized vector pointing from this vector to to.

public readonly Vector4 DirectionTo(Vector4 to)

Parameters

to Vector4

The other vector to point towards.

Returns

Vector4

The direction from this vector to to.

DistanceSquaredTo(Vector4)

Returns the squared distance between this vector and to. This method runs faster than DistanceTo(Vector4), so prefer it if you need to compare vectors or need the squared distance for some formula.

public readonly float DistanceSquaredTo(Vector4 to)

Parameters

to Vector4

The other vector to use.

Returns

float

The squared distance between the two vectors.

DistanceTo(Vector4)

Returns the distance between this vector and to.

public readonly float DistanceTo(Vector4 to)

Parameters

to Vector4

The other vector to use.

Returns

float

The distance between the two vectors.

Dot(Vector4)

Returns the dot product of this vector and with.

public readonly float Dot(Vector4 with)

Parameters

with Vector4

The other vector to use.

Returns

float

The dot product of the two vectors.

Equals(Vector4)

Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector4) instead, which is more reliable.

public readonly bool Equals(Vector4 other)

Parameters

other Vector4

The other vector.

Returns

bool

Whether or not the vectors are exactly equal.

Equals(object)

Returns true if the vector is exactly equal to the given object (obj). Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector4) instead, which is more reliable.

public override readonly bool Equals(object obj)

Parameters

obj object

The object to compare with.

Returns

bool

Whether or not the vector and the object are equal.

Floor()

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

public readonly Vector4 Floor()

Returns

Vector4

A vector with Floor(float) called on each component.

GetHashCode()

Serves as the hash function for Vector4.

public override readonly int GetHashCode()

Returns

int

A hash code for this vector.

Inverse()

Returns the inverse of this vector. This is the same as new Vector4(1 / v.X, 1 / v.Y, 1 / v.Z, 1 / v.W).

public readonly Vector4 Inverse()

Returns

Vector4

The inverse of this vector.

IsEqualApprox(Vector4)

Returns true if this vector and other are approximately equal, by running IsEqualApprox(float, float) on each component.

public readonly bool IsEqualApprox(Vector4 other)

Parameters

other Vector4

The other vector to compare.

Returns

bool

Whether or not the vectors are approximately equal.

IsFinite()

Returns true if this vector is finite, by calling IsFinite(float) on each component.

public readonly bool IsFinite()

Returns

bool

Whether this vector is finite or not.

IsNormalized()

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

public readonly bool IsNormalized()

Returns

bool

A bool indicating whether or not the vector is normalized.

IsZeroApprox()

Returns true if this vector's values are approximately zero, by running IsZeroApprox(float) on each component. This method is faster than using IsEqualApprox(Vector4) with one value as a zero vector.

public readonly bool IsZeroApprox()

Returns

bool

Whether or not the vector is approximately zero.

Length()

Returns the length (magnitude) of this vector.

public readonly float Length()

Returns

float

The length of this vector.

See Also

LengthSquared()

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 length for some formula.

public readonly float LengthSquared()

Returns

float

The squared length of this vector.

Lerp(Vector4, float)

Returns the result of the linear interpolation between this vector and to by amount weight.

public readonly Vector4 Lerp(Vector4 to, float weight)

Parameters

to Vector4

The destination vector for interpolation.

weight float

A value on the range of 0.0 to 1.0, representing the amount of interpolation.

Returns

Vector4

The resulting vector of the interpolation.

MaxAxisIndex()

Returns the axis of the vector's highest value. See Vector4.Axis. If all components are equal, this method returns X.

public readonly Vector4.Axis MaxAxisIndex()

Returns

Vector4.Axis

The index of the highest axis.

MinAxisIndex()

Returns the axis of the vector's lowest value. See Vector4.Axis. If all components are equal, this method returns W.

public readonly Vector4.Axis MinAxisIndex()

Returns

Vector4.Axis

The index of the lowest axis.

Normalized()

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

public readonly Vector4 Normalized()

Returns

Vector4

A normalized version of the vector.

PosMod(Vector4)

Returns a vector composed of the PosMod(float, float) of this vector's components and modv's components.

public readonly Vector4 PosMod(Vector4 modv)

Parameters

modv Vector4

A vector representing the divisors of the operation.

Returns

Vector4

A vector with each component PosMod(float, float) by modv's components.

PosMod(float)

Returns a vector composed of the PosMod(float, float) of this vector's components and mod.

public readonly Vector4 PosMod(float mod)

Parameters

mod float

A value representing the divisor of the operation.

Returns

Vector4

A vector with each component PosMod(float, float) by mod.

Round()

Returns this vector with all components rounded to the nearest integer, with halfway cases rounded towards the nearest multiple of two.

public readonly Vector4 Round()

Returns

Vector4

The rounded vector.

Sign()

Returns a vector with each component set to one or negative one, depending on the signs of this vector's components, or zero if the component is zero, by calling Sign(float) on each component.

public readonly Vector4 Sign()

Returns

Vector4

A vector with all components as either 1, -1, or 0.

Snapped(Vector4)

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.

public readonly Vector4 Snapped(Vector4 step)

Parameters

step Vector4

A vector value representing the step size to snap to.

Returns

Vector4

The snapped vector.

ToString()

Converts this Vector4 to a string.

public override string ToString()

Returns

string

A string representation of this vector.

ToString(string)

Converts this Vector4 to a string with the given format.

public readonly string ToString(string format)

Parameters

format string

Returns

string

A string representation of this vector.

Operators

operator +(Vector4, Vector4)

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

public static Vector4 operator +(Vector4 left, Vector4 right)

Parameters

left Vector4

The left vector.

right Vector4

The right vector.

Returns

Vector4

The added vector.

operator /(Vector4, Vector4)

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

public static Vector4 operator /(Vector4 vec, Vector4 divisorv)

Parameters

vec Vector4

The dividend vector.

divisorv Vector4

The divisor vector.

Returns

Vector4

The divided vector.

operator /(Vector4, float)

Divides each component of the Vector4 by the given float.

public static Vector4 operator /(Vector4 vec, float divisor)

Parameters

vec Vector4

The dividend vector.

divisor float

The divisor value.

Returns

Vector4

The divided vector.

operator ==(Vector4, Vector4)

Returns true if the vectors are exactly equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector4) instead, which is more reliable.

public static bool operator ==(Vector4 left, Vector4 right)

Parameters

left Vector4

The left vector.

right Vector4

The right vector.

Returns

bool

Whether or not the vectors are exactly equal.

operator >(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, Z and finally W values of the two vectors. This operator is useful for sorting vectors.

public static bool operator >(Vector4 left, Vector4 right)

Parameters

left Vector4

The left vector.

right Vector4

The right vector.

Returns

bool

Whether or not the left is greater than the right.

operator >=(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, Z and finally W values of the two vectors. This operator is useful for sorting vectors.

public static bool operator >=(Vector4 left, Vector4 right)

Parameters

left Vector4

The left vector.

right Vector4

The right vector.

Returns

bool

Whether or not the left is greater than or equal to the right.

operator !=(Vector4, Vector4)

Returns true if the vectors are not equal. Note: Due to floating-point precision errors, consider using IsEqualApprox(Vector4) instead, which is more reliable.

public static bool operator !=(Vector4 left, Vector4 right)

Parameters

left Vector4

The left vector.

right Vector4

The right vector.

Returns

bool

Whether or not the vectors are not equal.

operator <(Vector4, 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, Z and finally W values of the two vectors. This operator is useful for sorting vectors.

public static bool operator <(Vector4 left, Vector4 right)

Parameters

left Vector4

The left vector.

right Vector4

The right vector.

Returns

bool

Whether or not the left is less than the right.

operator <=(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, Z and finally W values of the two vectors. This operator is useful for sorting vectors.

public static bool operator <=(Vector4 left, Vector4 right)

Parameters

left Vector4

The left vector.

right Vector4

The right vector.

Returns

bool

Whether or not the left is less than or equal to the right.

operator %(Vector4, Vector4)

Gets the remainder of each component of the Vector4 with the components of the given Vector4. This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using PosMod(Vector4) instead if you want to handle negative numbers.

public static Vector4 operator %(Vector4 vec, Vector4 divisorv)

Parameters

vec Vector4

The dividend vector.

divisorv Vector4

The divisor vector.

Returns

Vector4

The remainder vector.

Examples

GD.Print(new Vector4(10, -20, 30, 10) % new Vector4(7, 8, 9, 10)); // Prints "(3, -4, 3, 0)"

operator %(Vector4, float)

Gets the remainder of each component of the Vector4 with the components of the given float. This operation uses truncated division, which is often not desired as it does not work well with negative numbers. Consider using PosMod(float) instead if you want to handle negative numbers.

public static Vector4 operator %(Vector4 vec, float divisor)

Parameters

vec Vector4

The dividend vector.

divisor float

The divisor value.

Returns

Vector4

The remainder vector.

Examples

GD.Print(new Vector4(10, -20, 30, 40) % 7); // Prints "(3, -6, 2, 5)"

operator *(Vector4, Vector4)

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

public static Vector4 operator *(Vector4 left, Vector4 right)

Parameters

left Vector4

The left vector.

right Vector4

The right vector.

Returns

Vector4

The multiplied vector.

operator *(Vector4, float)

Multiplies each component of the Vector4 by the given float.

public static Vector4 operator *(Vector4 vec, float scale)

Parameters

vec Vector4

The vector to multiply.

scale float

The scale to multiply by.

Returns

Vector4

The multiplied vector.

operator *(float, Vector4)

Multiplies each component of the Vector4 by the given float.

public static Vector4 operator *(float scale, Vector4 vec)

Parameters

scale float

The scale to multiply by.

vec Vector4

The vector to multiply.

Returns

Vector4

The multiplied vector.

operator -(Vector4, Vector4)

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

public static Vector4 operator -(Vector4 left, Vector4 right)

Parameters

left Vector4

The left vector.

right Vector4

The right vector.

Returns

Vector4

The subtracted vector.

operator -(Vector4)

Returns the negative value of the Vector4. This is the same as writing new 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.

public static Vector4 operator -(Vector4 vec)

Parameters

vec Vector4

The vector to negate/flip.

Returns

Vector4

The negated/flipped vector.