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() 限制在 min 和 max 之間。


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

回傳一個新向量,每個分量都使用 @GlobalScope.clamp() 限制在 min 和 max 之間。


Vector4 cubic_interpolate(b: Vector4, pre_a: Vector4, post_b: Vector4, weight: float) 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_in_time(b: Vector4, pre_a: Vector4, post_b: Vector4, weight: float, b_t: float, pre_a_t: float, post_b_t: float) 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 cubic_interpolate() by the time values.


Vector4 direction_to(to: Vector4) const 🔗

回傳從該向量指向 to 的正規化向量。

a.direction_to(b) 等價於 (b - a).normalized()。另見 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 線性插值結果。weight 在 0.0 到 1.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.x,v[1] 等價於 v.y,v[2] 等價於 v.z,v[3] 等價於 v.w。


Vector4 operator unary+() 🔗

返回與 + 不存在時相同的值。單目 + 沒有作用,但有時可以使你的程式碼更具可讀性。


Vector4 operator unary-() 🔗

返回該 Vector4 的負值。和寫 Vector4(-v.x, -v.y, -v.z, -v.w) 是一樣的。該操作在保持相同幅度的同時,翻轉向量的方向。對於浮點數,零也有正負兩種。