Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Vector4

使用浮点数坐标的 4D 向量。

描述

包含四个元素的结构体,可用于代表 4D 坐标或任何数值的四元组。

使用浮点数坐标。默认情况下,这些浮点值为 32 位精度,与始终为 64 位的 float 并不相同。如果需要双精度,请在编译引擎时使用 precision=double 选项。

对应的整数版本见 Vector4i

注意:在布尔语境中,如果 Vector4 等于 Vector4(0, 0, 0, 0) 则求值结果为 false。否则 Vector4 的求值结果始终为 true

属性

float

w

0.0

float

x

0.0

float

y

0.0

float

z

0.0

构造函数

Vector4

Vector4()

Vector4

Vector4(from: Vector4)

Vector4

Vector4(from: Vector4i)

Vector4

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

方法

Vector4

abs() const

Vector4

ceil() const

Vector4

clamp(min: Vector4, max: Vector4) const

Vector4

clampf(min: float, max: float) const

Vector4

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

Vector4

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

Vector4

direction_to(to: Vector4) const

float

distance_squared_to(to: Vector4) const

float

distance_to(to: Vector4) const

float

dot(with: Vector4) const

Vector4

floor() const

Vector4

inverse() const

bool

is_equal_approx(to: Vector4) const

bool

is_finite() const

bool

is_normalized() const

bool

is_zero_approx() const

float

length() const

float

length_squared() const

Vector4

lerp(to: Vector4, weight: float) const

Vector4

max(with: Vector4) const

int

max_axis_index() const

Vector4

maxf(with: float) const

Vector4

min(with: Vector4) const

int

min_axis_index() const

Vector4

minf(with: float) const

Vector4

normalized() const

Vector4

posmod(mod: float) const

Vector4

posmodv(modv: Vector4) const

Vector4

round() const

Vector4

sign() const

Vector4

snapped(step: Vector4) const

Vector4

snappedf(step: float) const

运算符

bool

operator !=(right: Vector4)

Vector4

operator *(right: Projection)

Vector4

operator *(right: Vector4)

Vector4

operator *(right: float)

Vector4

operator *(right: int)

Vector4

operator +(right: Vector4)

Vector4

operator -(right: Vector4)

Vector4

operator /(right: Vector4)

Vector4

operator /(right: float)

Vector4

operator /(right: int)

bool

operator <(right: Vector4)

bool

operator <=(right: Vector4)

bool

operator ==(right: Vector4)

bool

operator >(right: Vector4)

bool

operator >=(right: Vector4)

float

operator [](index: int)

Vector4

operator unary+()

Vector4

operator unary-()


枚举

enum Axis: 🔗

Axis AXIS_X = 0

X 轴的枚举值。由 max_axis_index()min_axis_index() 返回。

Axis AXIS_Y = 1

Y 轴的枚举值。由 max_axis_index()min_axis_index() 返回。

Axis AXIS_Z = 2

Z 轴的枚举值。由 max_axis_index()min_axis_index() 返回。

Axis AXIS_W = 3

W 轴的枚举值。由 max_axis_index()min_axis_index() 返回。


常量

ZERO = Vector4(0, 0, 0, 0) 🔗

零向量,所有分量都设置为 0 的向量。

ONE = Vector4(1, 1, 1, 1) 🔗

一向量,所有分量都设置为 1 的向量。

INF = Vector4(inf, inf, inf, inf) 🔗

无穷大向量,所有分量都设置为 @GDScript.INF 的向量。


属性说明

float w = 0.0 🔗

该向量的 W 分量。也可以通过使用索引位置 [3] 访问。


float x = 0.0 🔗

向量的 X 分量。也可以通过使用索引位置 [0] 访问。


float y = 0.0 🔗

向量的 Y 分量。也可以通过使用索引位置 [1] 访问。


float z = 0.0 🔗

向量的 Z 分量。也可以通过使用索引位置 [2] 访问。


构造函数说明

Vector4 Vector4() 🔗

构造默认初始化的 Vector4,所有分量都为 0


Vector4 Vector4(from: Vector4)

构造给定 Vector4 的副本。


Vector4 Vector4(from: Vector4i)

从给定的 Vector4i 构造新的 Vector4


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

返回具有给定分量的 Vector4


方法说明

Vector4 abs() const 🔗

返回一个新向量,其所有分量都是绝对值,即正值。


Vector4 ceil() const 🔗

返回一个新向量,所有的分量都是向上舍入(正无穷大方向)。


Vector4 clamp(min: Vector4, max: Vector4) const 🔗

返回一个新向量,每个分量都使用 @GlobalScope.clamp() 限制在 minmax 之间。


Vector4 clampf(min: float, max: float) const 🔗

返回一个新向量,每个分量都使用 @GlobalScope.clamp() 限制在 minmax 之间。


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

返回该向量和 b 之间进行三次插值 weight 处的结果,使用 pre_apost_b 作为控制柄。weight 在 0.0 到 1.0 的范围内,代表插值的量。


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

返回该向量和 b 之间进行三次插值 weight 处的结果,使用 pre_apost_b 作为控制柄。weight 在 0.0 到 1.0 的范围内,代表插值的量。

通过使用时间值,可以比 cubic_interpolate() 进行更平滑的插值。


Vector4 direction_to(to: Vector4) const 🔗

返回从该向量指向 to 的归一化向量。相当于使用 (b - a).normalized()


float distance_squared_to(to: Vector4) const 🔗

返回该向量与 to 之间的距离的平方。

该方法比 distance_to() 运行得更快,因此请在需要比较向量或者用于某些公式的平方距离时,优先使用这个方法。


float distance_to(to: Vector4) const 🔗

返回该向量与 to 之间的距离。


float dot(with: Vector4) const 🔗

返回该向量与 with 的点积。


Vector4 floor() const 🔗

返回一个新的向量,所有的向量都被四舍五入,向负无穷大。


Vector4 inverse() const 🔗

返回该向量的逆向量。与 Vector4(1.0 / v.x, 1.0 / v.y, 1.0 / v.z, 1.0 / v.w) 相同。


bool is_equal_approx(to: Vector4) const 🔗

如果这个向量与 to 大致相等,则返回 true,判断方法是对每个分量执行 @GlobalScope.is_equal_approx()


bool is_finite() const 🔗

如果该向量无穷,则返回 true,判断方法是对每个分量调用 @GlobalScope.is_finite()


bool is_normalized() const 🔗

如果该向量是归一化的,即长度约等于 1,则返回 true


bool is_zero_approx() const 🔗

如果该向量的值大约为零,则返回 true,判断方法是对每个分量运行 @GlobalScope.is_zero_approx()

该方法比使用 is_equal_approx() 和零向量比较要快。


float length() const 🔗

返回这个向量的长度,即大小。


float length_squared() const 🔗

返回这个向量的平方长度,即平方大小。

这个方法比 length() 运行得更快,所以如果你需要比较向量或需要一些公式的平方距离时,更喜欢用它。


Vector4 lerp(to: Vector4, weight: float) const 🔗

返回此向量和 to 之间,按数量 weight 线性插值结果。weight0.01.0 的范围内,代表插值的量。


Vector4 max(with: Vector4) const 🔗

返回自身与 with 各分量的最大值,等价于 Vector4(maxf(x, with.x), maxf(y, with.y), maxf(z, with.z), maxf(w, with.w))


int max_axis_index() const 🔗

返回该向量中最大值的轴。见 AXIS_* 常量。如果所有分量相等,则该方法返回 AXIS_X


Vector4 maxf(with: float) const 🔗

返回自身与 with 各分量的最大值,等价于 Vector4(maxf(x, with), maxf(y, with), maxf(z, with), maxf(w, with))


Vector4 min(with: Vector4) const 🔗

返回自身与 with 各分量的最小值,等价于 Vector4(minf(x, with.x), minf(y, with.y), minf(z, with.z), minf(w, with.w))


int min_axis_index() const 🔗

返回该向量中最小值的轴。见 AXIS_* 常量。如果所有分量相等,则该方法返回 AXIS_W


Vector4 minf(with: float) const 🔗

返回自身与 with 各分量的最小值,等价于 Vector4(minf(x, with), minf(y, with), minf(z, with), minf(w, with))


Vector4 normalized() const 🔗

返回该向量缩放至单位长度的结果。等价于 v / v.length()。如果 v.length() == 0 则返回 (0, 0, 0, 0)。另见 is_normalized()

注意:如果输入向量的长度接近零,则这个函数可能返回不正确的值。


Vector4 posmod(mod: float) const 🔗

返回由该向量的分量与 mod 执行 @GlobalScope.fposmod() 运算后组成的向量。


Vector4 posmodv(modv: Vector4) const 🔗

返回由该向量的分量与 modv 的分量执行 @GlobalScope.fposmod() 运算后组成的向量。


Vector4 round() const 🔗

返回所有分量都被四舍五入为最接近的整数的向量,中间情况向远离零的方向舍入。


Vector4 sign() const 🔗

返回新的向量,分量如果为正则设为 1.0,如果为负则设为 -1.0,如果为零则设为 0.0。结果与对每个分量调用 @GlobalScope.sign() 一致。


Vector4 snapped(step: Vector4) const 🔗

返回新的向量,每个分量都吸附到了与 step 中对应分量最接近的倍数。也可以用于将分量四舍五入至小数点后的任意位置。


Vector4 snappedf(step: float) const 🔗

返回一个新向量,其中每个分量都吸附到 step 的最接近的倍数。这也可以用于将分量四舍五入为任意数位的小数。


运算符说明

bool operator !=(right: Vector4) 🔗

如果向量不相等,则返回 true

注意:由于浮点数精度误差,请考虑改用 is_equal_approx(),会更可靠。

注意:包含 @GDScript.NAN 元素的向量的行为与其他向量不同。因此,如果包含 NaN,则这个方法的结果可能不准确。


Vector4 operator *(right: Projection) 🔗

通过给定 Projection 矩阵的转置变换(乘以)该 Vector4

对于通过投影的逆进行的变换,可以使用 projection.inverse() * vector 代替。请参阅 Projection.inverse()


Vector4 operator *(right: Vector4) 🔗

将该 Vector4 的每个分量乘以给定 Vector4 的对应分量。

print(Vector4(10, 20, 30, 40) * Vector4(3, 4, 5, 6)) # 输出 (30.0, 80.0, 150.0, 240.0)

Vector4 operator *(right: float) 🔗

将该 Vector4 的每个分量乘以给定的 float

print(Vector4(10, 20, 30, 40) * 2) # 输出 (20.0, 40.0, 60.0, 80.0)

Vector4 operator *(right: int) 🔗

将该 Vector4 的每个分量乘以给定的 int


Vector4 operator +(right: Vector4) 🔗

将该 Vector4 的每个分量加上给定 Vector4 的对应分量。

print(Vector4(10, 20, 30, 40) + Vector4(3, 4, 5, 6)) # 输出 (13.0, 24.0, 35.0, 46.0)

Vector4 operator -(right: Vector4) 🔗

将该 Vector4 的每个分量减去给定 Vector4 的对应分量。

print(Vector4(10, 20, 30, 40) - Vector4(3, 4, 5, 6)) # 输出 (7.0, 16.0, 25.0, 34.0)

Vector4 operator /(right: Vector4) 🔗

将该 Vector4 的每个分量除以给定 Vector4 的对应分量。

print(Vector4(10, 20, 30, 40) / Vector4(2, 5, 3, 4)) # 输出 (5.0, 4.0, 10.0, 10.0)

Vector4 operator /(right: float) 🔗

将该 Vector4 的每个分量除以给定的 float

print(Vector4(10, 20, 30, 40) / 2 # 输出 (5.0, 10.0, 15.0, 20.0)

Vector4 operator /(right: int) 🔗

将该 Vector4 的每个分量除以给定的 int


bool operator <(right: Vector4) 🔗

比较两个 Vector4 向量,首先检查左向量的 X 值是否小于 right 向量的 X 值。如果 X 值完全相等,则用相同的方法检查两个向量的 Y 值、Z 值、W 值。该运算符可用于向量排序。

注意:包含 @GDScript.NAN 元素的向量的行为与其他向量不同。因此,如果包含 NaN,则这个方法的结果可能不准确。


bool operator <=(right: Vector4) 🔗

比较两个 Vector4 向量,首先检查左向量的 X 值是否小于等于 right 向量的 X 值。如果 X 值完全相等,则用相同的方法检查两个向量的 Y 值、Z 值、W 值。该运算符可用于向量排序。

注意:包含 @GDScript.NAN 元素的向量的行为与其他向量不同。因此,如果包含 NaN,则这个方法的结果可能不准确。


bool operator ==(right: Vector4) 🔗

如果向量完全相等,则返回 true

注意:由于浮点数精度误差,请考虑改用 is_equal_approx(),会更可靠。

注意:包含 @GDScript.NAN 元素的向量的行为与其他向量不同。因此,如果包含 NaN,则这个方法的结果可能不准确。


bool operator >(right: Vector4) 🔗

比较两个 Vector4 向量,首先检查左向量的 X 值是否大于 right 向量的 X 值。如果 X 值完全相等,则用相同的方法检查两个向量的 Y 值、Z 值、W 值。该运算符可用于向量排序。

注意:包含 @GDScript.NAN 元素的向量的行为与其他向量不同。因此,如果包含 NaN,则这个方法的结果可能不准确。


bool operator >=(right: Vector4) 🔗

比较两个 Vector4 向量,首先检查左向量的 X 值是否大于等于 right 向量的 X 值。如果 X 值完全相等,则用相同的方法检查两个向量的 Y 值、Z 值、W 值。该运算符可用于向量排序。

注意:包含 @GDScript.NAN 元素的向量的行为与其他向量不同。因此,如果包含 NaN,则这个方法的结果可能不准确。


float operator [](index: int) 🔗

使用向量分量的 index 来访问向量分量。v[0] 等价于 v.xv[1] 等价于 v.yv[2] 等价于 v.zv[3] 等价于 v.w


Vector4 operator unary+() 🔗

返回与 + 不存在时相同的值。单目 + 没有作用,但有时可以使你的代码更具可读性。


Vector4 operator unary-() 🔗

返回该 Vector4 的负值。和写 Vector4(-v.x, -v.y, -v.z, -v.w) 是一样的。该操作在保持相同幅度的同时,翻转向量的方向。对于浮点数,零也有正负两种。