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.

Vector2i

使用整數座標的 2D 向量。

說明

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

使用整數座標,因此需要絕對精確時應比 Vector2 優先使用。請注意,取值範圍有 32 位的限制,與 Vector2 不同,這個型別的精度無法使用引擎的建構參數進行配置。如果需要 64 位元的值,請使用 intPackedInt64Array

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

教學

屬性

int

x

0

int

y

0

建構子

Vector2i

Vector2i()

Vector2i

Vector2i(from: Vector2i)

Vector2i

Vector2i(from: Vector2)

Vector2i

Vector2i(x: int, y: int)

方法

Vector2i

abs() const

float

aspect() const

Vector2i

clamp(min: Vector2i, max: Vector2i) const

Vector2i

clampi(min: int, max: int) const

int

distance_squared_to(to: Vector2i) const

float

distance_to(to: Vector2i) const

float

length() const

int

length_squared() const

Vector2i

max(with: Vector2i) const

int

max_axis_index() const

Vector2i

maxi(with: int) const

Vector2i

min(with: Vector2i) const

int

min_axis_index() const

Vector2i

mini(with: int) const

Vector2i

sign() const

Vector2i

snapped(step: Vector2i) const

Vector2i

snappedi(step: int) const

運算子

bool

operator !=(right: Vector2i)

Vector2i

operator %(right: Vector2i)

Vector2i

operator %(right: int)

Vector2i

operator *(right: Vector2i)

Vector2

operator *(right: float)

Vector2i

operator *(right: int)

Vector2i

operator +(right: Vector2i)

Vector2i

operator -(right: Vector2i)

Vector2i

operator /(right: Vector2i)

Vector2

operator /(right: float)

Vector2i

operator /(right: int)

bool

operator <(right: Vector2i)

bool

operator <=(right: Vector2i)

bool

operator ==(right: Vector2i)

bool

operator >(right: Vector2i)

bool

operator >=(right: Vector2i)

int

operator [](index: int)

Vector2i

operator unary+()

Vector2i

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 = Vector2i(0, 0) 🔗

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

ONE = Vector2i(1, 1) 🔗

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

MIN = Vector2i(-2147483648, -2147483648) 🔗

最小向量,所有分量等於 INT32_MIN 的向量。可用作 Vector2.INF 的負整數等價物。

MAX = Vector2i(2147483647, 2147483647) 🔗

最大向量,所有分量等於 INT32_MAX 的向量。可用作 Vector2.INF 的整數等價物。

LEFT = Vector2i(-1, 0) 🔗

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

RIGHT = Vector2i(1, 0) 🔗

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

UP = Vector2i(0, -1) 🔗

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

DOWN = Vector2i(0, 1) 🔗

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


屬性說明

int x = 0 🔗

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


int y = 0 🔗

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


建構子說明

Vector2i Vector2i() 🔗

建構預設初始化的 Vector2i,所有分量都為 0


Vector2i Vector2i(from: Vector2i)

建構給定 Vector2i 的副本。


Vector2i Vector2i(from: Vector2)

根據給定的 Vector2 建構 Vector2i,會將各個分量的小數部分截斷(向 0 取整)。要使用不同的行為,請考慮改為傳入 Vector2.ceil()Vector2.floor()Vector2.round() 的結果。


Vector2i Vector2i(x: int, y: int)

從給定的 xy 建構新的 Vector2i


方法說明

Vector2i abs() const 🔗

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


float aspect() const 🔗

返回該向量的長寬比,即 xy 的比例。


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

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


Vector2i clampi(min: int, max: int) const 🔗

Returns a new vector with all components clamped between min and max, by running @GlobalScope.clamp() on each component.


int distance_squared_to(to: Vector2i) const 🔗

Returns the squared Euclidean distance between this vector and to.

This method runs faster than distance_to(), so prefer it if you need to compare vectors or need the squared distance for some formula.


float distance_to(to: Vector2i) const 🔗

Returns the Euclidean distance between this vector and to.


float length() const 🔗

返回這個向量的長度,即大小。


int length_squared() const 🔗

返回這個向量的平方長度,即平方大小。

這個方法比 length() 運作得更快,所以如果你需要比較向量或需要一些公式的平方距離時,更喜歡用它。


Vector2i max(with: Vector2i) const 🔗

Returns the component-wise maximum of this and with, equivalent to Vector2i(maxi(x, with.x), maxi(y, with.y)).


int max_axis_index() const 🔗

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


Vector2i maxi(with: int) const 🔗

Returns the component-wise maximum of this and with, equivalent to Vector2i(maxi(x, with), maxi(y, with)).


Vector2i min(with: Vector2i) const 🔗

Returns the component-wise minimum of this and with, equivalent to Vector2i(mini(x, with.x), mini(y, with.y)).


int min_axis_index() const 🔗

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


Vector2i mini(with: int) const 🔗

Returns the component-wise minimum of this and with, equivalent to Vector2i(mini(x, with), mini(y, with)).


Vector2i sign() const 🔗

返回一個新的向量,如果是正數,每個分量被設定為1 ,如果是負數,-1 ,如果是零,0 。其結果與對每個分量呼叫@GlobalScope.sign()相同。


Vector2i snapped(step: Vector2i) const 🔗

返回新的向量,每個分量都吸附到了與 step 中對應分量最接近的倍數。


Vector2i snappedi(step: int) const 🔗

Returns a new vector with each component snapped to the closest multiple of step.


運算子說明

bool operator !=(right: Vector2i) 🔗

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


Vector2i operator %(right: Vector2i) 🔗

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

print(Vector2i(10, -20) % Vector2i(7, 8)) # Prints (3, -4)

Vector2i operator %(right: int) 🔗

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

print(Vector2i(10, -20) % 7) # Prints (3, -6)

Vector2i operator *(right: Vector2i) 🔗

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

print(Vector2i(10, 20) * Vector2i(3, 4)) # Prints (30, 80)

Vector2 operator *(right: float) 🔗

Multiplies each component of the Vector2i by the given float. Returns a Vector2.

print(Vector2i(10, 15) * 0.9) # Prints (9.0, 13.5)

Vector2i operator *(right: int) 🔗

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


Vector2i operator +(right: Vector2i) 🔗

Adds each component of the Vector2i by the components of the given Vector2i.

print(Vector2i(10, 20) + Vector2i(3, 4)) # Prints (13, 24)

Vector2i operator -(right: Vector2i) 🔗

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

print(Vector2i(10, 20) - Vector2i(3, 4)) # Prints (7, 16)

Vector2i operator /(right: Vector2i) 🔗

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

print(Vector2i(10, 20) / Vector2i(2, 5)) # Prints (5, 4)

Vector2 operator /(right: float) 🔗

Divides each component of the Vector2i by the given float. Returns a Vector2.

print(Vector2i(1, 2) / 2.5) # Prints (0.4, 0.8)

Vector2i operator /(right: int) 🔗

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


bool operator <(right: Vector2i) 🔗

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


bool operator <=(right: Vector2i) 🔗

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


bool operator ==(right: Vector2i) 🔗

如果向量相等,則返回 true


bool operator >(right: Vector2i) 🔗

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


bool operator >=(right: Vector2i) 🔗

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


int operator [](index: int) 🔗

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


Vector2i operator unary+() 🔗

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


Vector2i operator unary-() 🔗

返回該 Vector2i 的負值。和寫 Vector2i(-v.x, -v.y) 是一樣的。該操作在保持相同幅度的同時,翻轉向量的方向。