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.

Vector2

使用浮點數座標的 2D 向量。

說明

包含兩個元素的結構體,可用於代表 2D 座標或任何數值的二元組。

使用浮點數座標。預設情況下,這些浮點值為 32 位精度,與始終為 64 位的 float 並不相同。如果需要雙精度,請在編譯引擎時使用 precision=double 選項。

對應的整數版本見 Vector2i。

注意:在布林語境中,如果 Vector2 等於 Vector2(0, 0) 則求值結果為 false。否則 Vector2 的求值結果始終為 true。

教學

屬性

float

x

0.0

float

y

0.0

建構子

Vector2

Vector2()

Vector2

Vector2(from: Vector2)

Vector2

Vector2(from: Vector2i)

Vector2

Vector2(x: float, y: float)

方法

Vector2

abs() const

float

angle() const

float

angle_to(to: Vector2) const

float

angle_to_point(to: Vector2) const

float

aspect() const

Vector2

bezier_derivative(control_1: Vector2, control_2: Vector2, end: Vector2, t: float) const

Vector2

bezier_interpolate(control_1: Vector2, control_2: Vector2, end: Vector2, t: float) const

Vector2

bounce(n: Vector2) const

Vector2

ceil() const

Vector2

clamp(min: Vector2, max: Vector2) const

Vector2

clampf(min: float, max: float) const

float

cross(with: Vector2) const

Vector2

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

Vector2

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

Vector2

direction_to(to: Vector2) const

float

distance_squared_to(to: Vector2) const

float

distance_to(to: Vector2) const

float

dot(with: Vector2) const

Vector2

floor() const

Vector2

from_angle(angle: float) static

Vector2

inverse() const

bool

is_equal_approx(to: Vector2) const

bool

is_finite() const

bool

is_normalized() const

bool

is_zero_approx() const

float

length() const

float

length_squared() const

Vector2

lerp(to: Vector2, weight: float) const

Vector2

limit_length(length: float = 1.0) const

Vector2

max(with: Vector2) const

int

max_axis_index() const

Vector2

maxf(with: float) const

Vector2

min(with: Vector2) const

int

min_axis_index() const

Vector2

minf(with: float) const

Vector2

move_toward(to: Vector2, delta: float) const

Vector2

normalized() const

Vector2

orthogonal() const

Vector2

posmod(mod: float) const

Vector2

posmodv(modv: Vector2) const

Vector2

project(b: Vector2) const

Vector2

reflect(line: Vector2) const

Vector2

rotated(angle: float) const

Vector2

round() const

Vector2

sign() const

Vector2

slerp(to: Vector2, weight: float) const

Vector2

slide(n: Vector2) const

Vector2

snapped(step: Vector2) const

Vector2

snappedf(step: float) const

運算子

bool

operator !=(right: Vector2)

Vector2

operator *(right: Transform2D)

Vector2

operator *(right: Vector2)

Vector2

operator *(right: float)

Vector2

operator *(right: int)

Vector2

operator +(right: Vector2)

Vector2

operator -(right: Vector2)

Vector2

operator /(right: Vector2)

Vector2

operator /(right: float)

Vector2

operator /(right: int)

bool

operator <(right: Vector2)

bool

operator <=(right: Vector2)

bool

operator ==(right: Vector2)

bool

operator >(right: Vector2)

bool

operator >=(right: Vector2)

float

operator [](index: int)

Vector2

operator unary+()

Vector2

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() 返回。


常數

ZERO = Vector2(0, 0) 🔗

零向量,所有分量都設定為 0 的向量。

ONE = Vector2(1, 1) 🔗

一向量,所有分量都設定為 1 的向量。

INF = Vector2(inf, inf) 🔗

無窮大向量,所有分量都設定為 @GDScript.INF 的向量。

LEFT = Vector2(-1, 0) 🔗

左單位向量。代表左的方向。

RIGHT = Vector2(1, 0) 🔗

右單位向量。代表右的方向。

UP = Vector2(0, -1) 🔗

上單位向量。在 2D 中 Y 是向下的,所以這個向量指向 -Y。

DOWN = Vector2(0, 1) 🔗

下單位向量。在 2D 中 Y 是向下的,所以這個向量指向 +Y。


屬性說明

float x = 0.0 🔗

向量的 X 分量。也可以通過使用索引位置 [0] 存取。


float y = 0.0 🔗

向量的 Y 分量。也可以通過使用索引位置 [1] 存取。


建構子說明

Vector2 Vector2() 🔗

建構預設初始化的 Vector2,所有分量均為 0。


Vector2 Vector2(from: Vector2)

建構給定 Vector2 的副本。


Vector2 Vector2(from: Vector2i)

從 Vector2i 建構新的 Vector2。


Vector2 Vector2(x: float, y: float)

從給定的 x 和 y 建構新的 Vector2。


方法說明

Vector2 abs() const 🔗

返回一個新向量,其所有分量都是絕對值,即正值。


float angle() const 🔗

回傳該向量與 X 軸正方向,即 (1, 0),的夾角,單位為弧度。

例如,Vector2.RIGHT.angle() 將回傳零,Vector2.DOWN.angle() 將回傳 PI / 2(四分之一圈,即 90 度),而 Vector2(1, -1).angle() 將回傳 -PI / 4(負八分之一圈,即 -45 度)。

這等同於傳遞 y 和 x 呼叫 @GlobalScope.atan2()。

回傳角度的示意圖。


float angle_to(to: Vector2) const 🔗

回傳與給定向量的帶符號夾角,單位為弧度。結果範圍從 -PI 到 PI(含)。

回傳的夾角的示意圖。


float angle_to_point(to: Vector2) const 🔗

回傳 X 軸與從該向量到點 to 的直線之間的有符號夾角,單位為弧度。結果範圍從 -PI 到 PI(含)。

a.angle_to_point(b) 等價於 (b - a).angle()。另見 angle()。

回傳的夾角的示意圖。


float aspect() const 🔗

回傳該向量的縱橫比,即 x 與 y 的比例。


Vector2 bezier_derivative(control_1: Vector2, control_2: Vector2, end: Vector2, t: float) const 🔗

返回貝賽爾曲線上 t 處的導數,該曲線由此向量和控制點 control_1、control_2、終點 end 定義。


Vector2 bezier_interpolate(control_1: Vector2, control_2: Vector2, end: Vector2, t: float) const 🔗

返回貝賽爾曲線上 t 處的點,該曲線由此向量和控制點 control_1、control_2、終點 end 定義。


Vector2 bounce(n: Vector2) const 🔗

回傳從由給定垂直法線 n 定義的線“反彈”的向量。

注意: bounce() 執行大多數引擎和框架呼叫 reflect() 的操作。


Vector2 ceil() const 🔗

返回一個新向量,所有的分量都是向上四捨五入(正無窮大方向)。


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

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


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

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


float cross(with: Vector2) const 🔗

回傳該向量和 with 的 2D 類比叉積。

這是由兩個向量所形成的平行四邊形的有符號面積。如果第二個向量是從第一個向量的順時針方向出發的,則叉積為正面積。如果是逆時針方向,則叉積為負面積。如果兩個向量平行,則回傳零,這對於測試兩個向量是否平行很有用。

注意:數學中沒有定義二維空間的叉乘。此方法是將 2D 向量嵌入到 3D 空間的 XY 平面中,並使用它們的叉積的 Z 分量作為類比。


Vector2 cubic_interpolate(b: Vector2, pre_a: Vector2, post_b: Vector2, 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.


Vector2 cubic_interpolate_in_time(b: Vector2, pre_a: Vector2, post_b: Vector2, 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.


Vector2 direction_to(to: Vector2) const 🔗

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

a.direction_to(b) 等價於 (b - a).normalized()。另見 normalized()。


float distance_squared_to(to: Vector2) const 🔗

回傳該向量與 to 之間的歐幾里得距離的平方。

該方法比 distance_to() 執行速度更快,因此如果需要比較向量或需要平方距離來計算某些公式,請優先使用該方法。


float distance_to(to: Vector2) const 🔗

回傳該向量與 to 之間的 歐幾里得距離。


float dot(with: Vector2) const 🔗

回傳該向量與 with 的點積。可用於比較兩個向量之間的夾角。例如,可用於確定敵人是否面向玩家。

直角(90 度)的點積為 0;大於 0 則夾角小於 90 度;小於 0 則夾角大於 90 度。

使用(正規化的)單位向量時,如果向量朝向相反,則結果始終為 -1.0(180 度角);如果向量方向一致,則結果始終為 1.0(0 度角)。

注意:a.dot(b) 等價於 b.dot(a)。


Vector2 floor() const 🔗

返回一個新的向量,所有的向量都被四捨五入,向負無窮大。


Vector2 from_angle(angle: float) static 🔗

建立旋轉至 angle 角度的 Vector2,旋轉單位為弧度。相當於執行 Vector2(cos(angle), sin(angle)) 或 Vector2.RIGHT.rotated(angle)。

print(Vector2.from_angle(0)) # 輸出 (1.0, 0.0)
print(Vector2(1, 0).angle()) # 輸出 0.0,即上一行所使用的角度。
print(Vector2.from_angle(PI / 2)) # 輸出 (0.0, 1.0)

注意:回傳的 Vector2 的長度約等於 1.0,由於浮點數精度問題,不保證精確等於 1.0。如果需要單位向量,請呼叫回傳的 Vector2 的 normalized()。


Vector2 inverse() const 🔗

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


bool is_equal_approx(to: Vector2) 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() 運作得更快,所以如果你需要比較向量或需要一些公式的平方距離時,更喜歡用它。


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

返回此向量和 to 之間,按數量 weight 線性插值結果。weight 在 0.0 到 1.0 的範圍內,代表插值的量。


Vector2 limit_length(length: float = 1.0) const 🔗

回傳應用了最大長度限制的向量,長度被限制到 length。如果向量非有限,則結果未定義。


Vector2 max(with: Vector2) const 🔗

回傳自身與 with 各分量的最大值,等價於 Vector2(maxf(x, with.x), maxf(y, with.y))。


int max_axis_index() const 🔗

返回該向量中最大值的軸。見 AXIS_* 常數。如果所有分量相等,則該方法返回 AXIS_X。


Vector2 maxf(with: float) const 🔗

回傳自身與 with 各分量的最大值,等價於 Vector2(maxf(x, with), maxf(y, with))。


Vector2 min(with: Vector2) const 🔗

回傳自身與 with 各分量的最小值,等價於 Vector2(minf(x, with.x), minf(y, with.y))。


int min_axis_index() const 🔗

返回該向量中最小值的軸。見 AXIS_* 常數。如果所有分量相等,則該方法返回 AXIS_Y。


Vector2 minf(with: float) const 🔗

回傳自身與 with 各分量的最小值,等價於 Vector2(minf(x, with), minf(y, with))。


Vector2 move_toward(to: Vector2, delta: float) const 🔗

返回一個新向量,該向量朝 to 移動了固定的量 delta。不會超過最終值。


Vector2 normalized() const 🔗

回傳該向量縮放至單位長度的結果。等價於 v / v.length()。如果 v.length() == 0 則回傳 (0, 0)。另見 is_normalized()。

注意:如果輸入向量的長度接近零,則這個函式可能回傳不正確的值。


Vector2 orthogonal() const 🔗

返回一個與原來相比逆時針旋轉 90 度的垂直向量,長度不變。


Vector2 posmod(mod: float) const 🔗

返回由該向量的分量與 mod 執行 @GlobalScope.fposmod() 運算後組成的向量。


Vector2 posmodv(modv: Vector2) const 🔗

返回由該向量的分量與 modv 的分量執行 @GlobalScope.fposmod() 運算後組成的向量。


Vector2 project(b: Vector2) const 🔗

回傳將該向量投影到給定的 b 向量上所得到的新向量。得到的新向量與 b 平行。另見 slide()。

注意:如果 b 向量為零向量,得到的新向量的分量均為 @GDScript.NAN。


Vector2 reflect(line: Vector2) const 🔗

回傳從給定方向向量 line 定義的線反射向量的結果。

注意:reflect() 與其他引擎和框架呼叫的 reflect() 不同。在其他引擎中,reflect() 採用法線方向,即垂直於線的方向。在 Godot 中,你可以直接指定線的方向。另見 bounce(),它執行大多數引擎呼叫的 reflect()。


Vector2 rotated(angle: float) const 🔗

返回將這個向量旋轉 angle 的結果(單位為弧度)。另見 @GlobalScope.deg_to_rad()。


Vector2 round() const 🔗

返回所有分量都被四捨五入為最接近的整數的向量,中間情況向遠離零的方向四捨五入。


Vector2 sign() const 🔗

返回新的向量,分量如果為正則設為 1.0,如果為負則設為 -1.0,如果為零則設為 0.0。結果與對每個分量呼叫 @GlobalScope.sign() 一致。


Vector2 slerp(to: Vector2, weight: float) const 🔗

返回在這個向量和 to 之間進行 weight 的球面線性插值的結果。weight 在 0.0 和 1.0 的範圍內,代表插值的量。

如果輸入向量的長度不同,這個函式也會對長度進行插值處理。對於輸入向量中存在長度為零的向量的特殊情況,這個方法的行為與 lerp() 一致。


Vector2 slide(n: Vector2) const 🔗

回傳將該向量沿著法線為 n 的直線滑動所得到的新向量。得到的新向量與 n 垂直,等價於將該向量減去在 n 上的投影。另見 project()。

注意:向量 n 必須為正規化的向量。另見 normalized()。


Vector2 snapped(step: Vector2) const 🔗

返回新的向量,每個分量都吸附到了與 step 中對應分量最接近的倍數。也可以用於將分量四捨五入至小數點後的任意位置。


Vector2 snappedf(step: float) const 🔗

回傳一個新向量,其中每個分量都吸附到 step 的最接近的倍數。這也可以用於將分量四捨五入為任意數位的小數。


運算子說明

bool operator !=(right: Vector2) 🔗

如果向量不相等,則返回 true。

注意:由於浮點數精度誤差,請考慮改用 is_equal_approx(),會更可靠。

注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


Vector2 operator *(right: Transform2D) 🔗

假設該變換的基是正交的(即旋轉/反射可以,縮放/傾斜不行),將 Vector2 逆向變換(乘以)給定的 Transform2D 變換矩陣。

vector * transform 相當於 transform.inverse() * vector。請參閱 Transform2D.inverse()。

對於通過仿射變換的逆進行的變換(例如縮放),可以使用 transform.affine_inverse() * vector 代替。請參閱 Transform2D.affine_inverse()。


Vector2 operator *(right: Vector2) 🔗

將該 Vector2 的每個分量乘以給定 Vector2 的對應分量。

print(Vector2(10, 20) * Vector2(3, 4)) # 輸出 (30.0, 80.0)

Vector2 operator *(right: float) 🔗

將該 Vector2 的每個分量乘以給定的 float。


Vector2 operator *(right: int) 🔗

將該 Vector2 的每個分量乘以給定的 int。


Vector2 operator +(right: Vector2) 🔗

將該 Vector2 的每個分量加上給定 Vector2 的對應分量。

print(Vector2(10, 20) + Vector2(3, 4)) # 輸出 (13.0, 24.0)

Vector2 operator -(right: Vector2) 🔗

將該 Vector2 的每個分量減去給定 Vector2 的對應分量。

print(Vector2(10, 20) - Vector2(3, 4)) # 輸出 (7.0, 16.0)

Vector2 operator /(right: Vector2) 🔗

將該 Vector2 的每個分量除以給定 Vector2 的對應分量。

print(Vector2(10, 20) / Vector2(2, 5)) # 輸出 (5.0, 4.0)

Vector2 operator /(right: float) 🔗

將該 Vector2 的每個分量除以給定的 float。


Vector2 operator /(right: int) 🔗

將該 Vector2 的每個分量除以給定的 int。


bool operator <(right: Vector2) 🔗

比較兩個 Vector2 向量,首先檢查左向量的 X 值是否小於 right 向量的 X 值。如果 X 值完全相等,則用相同的方法檢查兩個向量的 Y 值。該運算子可用於向量排序。

注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


bool operator <=(right: Vector2) 🔗

比較兩個 Vector2 向量,首先檢查左向量的 X 值是否小於等於 right 向量的 X 值。如果 X 值完全相等,則用相同的方法檢查兩個向量的 Y 值。該運算子可用於向量排序。

注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


bool operator ==(right: Vector2) 🔗

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

注意:由於浮點數精度誤差,請考慮改用 is_equal_approx(),會更可靠。

注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


bool operator >(right: Vector2) 🔗

比較兩個 Vector2 向量,首先檢查左向量的 X 值是否大於 right 向量的 X 值。如果 X 值完全相等,則用相同的方法檢查兩個向量的 Y 值。該運算子可用於向量排序。

注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


bool operator >=(right: Vector2) 🔗

比較兩個 Vector2 向量,首先檢查左向量的 X 值是否大於等於 right 向量的 X 值。如果 X 值完全相等,則用相同的方法檢查兩個向量的 Y 值。該運算子可用於向量排序。

注意:包含 @GDScript.NAN 元素的向量的行為與其他向量不同。因此,如果包含 NaN,則這個方法的結果可能不準確。


float operator [](index: int) 🔗

使用向量分量的 index 來存取向量分量。v[0] 等價於 v.x、v[1] 等價於 v.y。


Vector2 operator unary+() 🔗

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


Vector2 operator unary-() 🔗

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