Celestia/Celx 指令碼/CELX Lua 方法/Celx 向量
Celestia 中的 "向量" 物件是三個雙精度浮點數的元組,它代表一個幾何物件,在三維座標系中具有長度和方向 [X, Y, Z]。
在 CELX 指令碼中,向量方法需要以獲取的 "向量" 物件為字首,用分號隔開。
可以使用以下方法來獲取 "向量" 物件
- 使用 celestia:newvector() 方法。
- 使用 position:vectorto() 方法。
- 使用 vector:normalize() 方法。
- 使用 rotation:imag() 方法。
- 使用 rotation:transform() 方法。
- 透過減去兩個 "位置" 物件。
注意
- Celestia 中的向量分量 (X,Y,Z) 儲存為百萬分之一光年。因此,當您在指令碼中定義了以公里或英里為單位的向量時,您需要將這些向量進行轉換。您可以使用一個常量,它必須首先在您的指令碼中初始化。
- 從公里轉換為百萬分之一光年,使用常量 uly_to_km = 9460730.4725808。
- 從英里轉換為百萬分之一光年,使用常量 uly_to_mls = 5912956.5453630。
- 接下來,您可以將公里或英里轉換為百萬分之一光年,如下所示
- 百萬分之一光年 = 數字:公里 / uly_to_km
- 百萬分之一光年 = 數字:英里 / uly_to_mls
"向量" 物件可以相互加減。
也可以將 "位置" 物件與 "向量" 物件相加,或從 "位置" 物件中減去 "向量" 物件。
- position + vector → position
- position - vector → position
- vector + vector → vector
- vector - vector → vector
標量乘以 "向量" 物件
- number * vector → vector
- vector * number → vector
* 運算子給出兩個向量 v 和 w 的 點積
- v * w = v.x*w.x + v.y*w.y + v.z*w.z → number
^ 運算子給出兩個向量 v 和 w 的 叉積
- v ^ w = [(v.y*w.z - v.z*w.y), (v.z*w.x - v.x*w.z), (v.x*w.y - v.y*w.x)] → vector
新向量垂直於包含兩個輸入向量的平面(根據右手定則)。
- v ^ w = - w ^ v
使用時要注意:^ 通常是 Lua 中的指數運算子,因此優先順序高於幾乎所有其他運算子,包括一元減號。
您可以直接訪問和分配 "向量" 物件的 x、y 和 z 分量,例如
vec = celestia:newvector(0, 0, 0) vec.x = vec.x + 1
本章包含所有可用於 "向量" 物件的可用向量方法的列表。
number vector:getx()
將向量的 X 座標作為數字返回。
示例
獲取一個向量,該向量給出地球的日心位置,並在螢幕左下角顯示該向量的 X、Y 和 Z 分量。
now = celestia:gettime()
sunpos = celestia:find("Sol"):getposition(now)
earthpos = celestia:find("Sol/Earth"):getposition(now)
heliovector = sunpos:vectorto(earthpos)
heliovector_x = heliovector:getx()
heliovector_y = heliovector:gety()
heliovector_z = heliovector:getz()
celestia:print("Heliocentric position of Earth:\nX = " .. heliovector_x ..
"\nY = " .. heliovector_y .. "\nZ = " .. heliovector_z, 10, -1, -1, 1, 6)
wait(10.0)
number vector:gety()
將向量的 Y 座標作為數字返回。
示例
獲取一個向量,該向量給出地球的日心位置,並在螢幕左下角顯示該向量的 X、Y 和 Z 分量。
now = celestia:gettime()
sunpos = celestia:find("Sol"):getposition(now)
earthpos = celestia:find("Sol/Earth"):getposition(now)
heliovector = sunpos:vectorto(earthpos)
heliovector_x = heliovector:getx()
heliovector_y = heliovector:gety()
heliovector_z = heliovector:getz()
celestia:print("Heliocentric position of Earth:\nX = " .. heliovector_x ..
"\nY = " .. heliovector_y .. "\nZ = " .. heliovector_z, 10, -1, -1, 1, 6)
wait(10.0)
number vector:getz()
將向量的 Z 座標作為數字返回。
示例
獲取一個向量,該向量給出地球的日心位置,並在螢幕左下角顯示該向量的 X、Y 和 Z 分量。
now = celestia:gettime()
sunpos = celestia:find("Sol"):getposition(now)
earthpos = celestia:find("Sol/Earth"):getposition(now)
heliovector = sunpos:vectorto(earthpos)
heliovector_x = heliovector:getx()
heliovector_y = heliovector:gety()
heliovector_z = heliovector:getz()
celestia:print("Heliocentric position of Earth:\nX = " .. heliovector_x ..
"\nY = " .. heliovector_y .. "\nZ = " .. heliovector_z, 10, -1, -1, 1, 6)
wait(10.0)
vector vector:normalize()
返回一個歸一化的 "向量" 物件。
歸一化向量是指一個向量,它與原始向量指向相同的方向,但 長度 = 1。
注意
- 此方法不會修改原始向量。
- 歸一化零長度向量會導致除以零。結果將是一個 NaN 向量。
- CELX "向量" 物件是一個幾何物件,它在三維座標系中具有長度和方向 [X, Y, Z]。
- 向量方法可以用於 CELX "向量" 物件。"向量" 物件也可以用在其他方法中,這些方法需要 "向量" 物件作為引數。
示例
建立一個向量,該向量從地球的實際位置指向月球的實際位置,長度 = 1。
earth = celestia:find("Sol/Earth")
moon = celestia:find("Sol/Earth/moon")
now = celestia:gettime()
earthpos = earth:getposition(now)
moonpos = moon:getposition(now)
vector = earthpos:vectorto(moonpos)
n_vector = vector:normalize()
number vector:length()
將此向量的長度(以百萬分之一光年為單位)作為數字返回。
示例
以公里為單位顯示從地球 中心 到月球 中心 的實際距離。
uly_to_km = 9460730.4725808
earth = celestia:find("Sol/Earth")
moon = celestia:find("Sol/Earth/moon")
now = celestia:gettime()
earthpos = earth:getposition(now)
moonpos = moon:getposition(now)
vector = earthpos:vectorto(moonpos)
distance = vector:length() * uly_to_km
celestia:print("Actual distance center Earth - center Moon: " .. distance .. " km.", 10.0, -1, -1, 2, 4)
wait(10.0)