跳轉到內容

Celestia/Celx 指令碼/CELX Lua 方法/Celx 旋轉

來自華夏公益教科書,自由的教科書

Celx 指令碼: 旋轉

[編輯 | 編輯原始碼]

CELX "旋轉" 物件在內部是一個四元數,這是數學上描述三維空間旋轉的一種方式(例如,它可以轉換為旋轉矩陣)。旋轉也可以用來描述物體或觀察者的方向(例如,觀察者正在看哪裡,以及“向上”的方向)。

在 CELX 指令碼中,旋轉方法需要用獲取的 "旋轉" 物件作為字首,用冒號隔開。

可以使用以下方法獲得 "旋轉" 物件。

本章包含所有可用的旋轉方法列表,這些方法可用於 "旋轉" 物件。

vector rotation:imag()

將此旋轉的 X、Y 和 Z 值作為 "vector" 物件返回。

備註

  1. CELX "vector" 物件是三維座標系中具有長度和方向 [X, Y, Z] 的幾何物件。
  2. 可以使用 vector 方法處理 CELX "vector" 物件。 "Vector" 物件也可以在其他方法中使用,這些方法需要 "vector" 物件作為引數。

示例

obs = celestia:getobserver()
rot1 = obs:getorientation()
vector = rot1:imag()


返回到 旋轉 方法索引。


number rotation:real()

將此旋轉的 W 值作為數字返回。

示例

obs = celestia:getobserver()
rot1 = obs:getorientation()
number = rot1:real()


返回到 旋轉 方法索引。


transform

[編輯 | 編輯原始碼]

vector rotation:transform(vector:vec)

透過將旋轉應用於該向量來變換 "vector" 物件,並將結果返回到一個新的 "vector" 物件中。

引數

vec
將應用旋轉的向量。必須是 "vector" 物件。

備註

  1. CELX "vector" 物件是三維座標系中具有長度和方向 [X, Y, Z] 的幾何物件。
  2. 可以使用 vector 方法處理 CELX "vector" 物件。 "Vector" 物件也可以在其他方法中使用,這些方法需要 "vector" 物件作為引數。

示例
轉到地球 實際暗面 的位置。

obs = celestia:getobserver()
-- Set frame of reference to "universal".
obs:setframe(celestia:newframe("universal"))
-- Find the Sun and Earth and get their actual positions 
earth = celestia:find("Sol/Earth")
sun = celestia:find("Sol")
now = celestia:gettime()
earthpos = earth:getposition(now)
sunpos = sun:getposition(now)
-- Get vector from Earth to the Sun
vector1 = earthpos:vectorto(sunpos)
-- Transform that vector over a rotation of 180 degrees = math.pi
up_v = celestia:newvector(0,1,0)
opposite = celestia:newrotation(up_v, math.pi)
vector2 = opposite:transform(vector1)
-- Adjust the length of the opposite vector
vector2 = 0.0003 * vector2
-- Goto new position and center Earth.
newpos = earthpos + vector2
obs:goto(newpos, 0.0)
wait(0.0)
obs:center(earth, 0.0)
wait(0.0)


返回到 旋轉 方法索引。


setaxisangle

[編輯 | 編輯原始碼]

rotation:setaxisangle(vector:vec, number:angle)

設定軸和角度,如向量和角度中定義的此旋轉。

引數

vec
描述此旋轉 [X,Y,Z] 軸的向量。必須是 "vector" 物件。
angle
此旋轉的弧度角。必須是數字。

備註

  1. CELX 中的角度定義為弧度,而不是度數。180 度(半圓)等於 π 弧度 = 3.14159265 弧度 = math.pi 弧度 (LUA)。您可以將度數轉換為弧度,反之亦然,如下所示
    • radians = math.rad( number:degrees ) = ( number:degrees / 180 * math.pi) = ( number:degrees / 180 * 3.14159265).
    • degrees = math.deg( number:radians ) = ( number:radians * 180 / math.pi) = ( number:radians * 180 / 3.14159265).
  2. 此方法等效於 celestia:newrotation(軸-角) 方法。只是使用此方法,"旋轉" 物件需要先存在,而不是由方法建立。為了獲得這樣的 "旋轉" 物件,可以使用 celestia:newrotation() 方法,或者您可以使用之前在指令碼中定義的現有 "旋轉" 物件。

示例
將當前觀察者檢視更改 180 度(就像後視鏡)。

obs = celestia:getobserver()
-- Create a rotation object from all zero numbers.
rot1 = celestia:newrotation(0, 0, 0, 0)
-- Define the axis for the rotation as a vector
up_vec = celestia:newvector(0, 1, 0)
rot1:setaxisangle(up_vec, math.pi)
obs:rotate(rot1)
wait(0.0)


返回到 旋轉 方法索引。


rotation:rot3 rotation:slerp(rotation:rot2, number:percent)

返回此旋轉和另一個 "旋轉" 物件 (rot2) 的插值,使用數字表示每個旋轉應該使用多少。將結果返回到一個新的 "旋轉" 物件 (rot3) 中。

引數

rot2
將用於插值的第二個旋轉。必須是 "旋轉" 物件。
percent
用於在兩個旋轉之間進行插值的百分數。必須是 0 到 1 之間的數字。
  • 如果 percent 為 0,則在 rotation:rot3 中返回此旋轉。
  • 如果 percent 為 1,則在 rotation:rot3 中返回第二個旋轉 (rot2)。

備註

  1. CELX "旋轉" 物件在內部是一個四元數,這是數學上描述三維空間旋轉的一種方式(例如,它可以轉換為旋轉矩陣)。旋轉也可以用來描述物體或觀察者的方向(例如,觀察者正在看哪裡,以及“向上”的方向)。
  2. 可以使用 旋轉 方法處理 CELX "旋轉" 物件。 "旋轉" 物件也可以在其他方法中使用,這些方法需要 "旋轉" 物件作為引數。

示例
轉到地月系中的一個位置,並將觀察者方向設定為地月位置的中間位置。

obs = celestia:getobserver()
-- Set frame of reference to "universal".
obs:setframe(celestia:newframe( "universal"))
-- Find the actual position of the Earth.
earth = celestia:find("Sol/Earth")
earthpos = earth:getposition()
-- Find the actual position of the Moon.
moon = celestia:find("Sol/Earth/Moon")
moonpos = moon:getposition()
-- Calculate new position to goto
newpos = earthpos + 0.5 * (moonpos - earthpos)
newpos = newpos + celestia:newvector(0, 0, 0.075)
obs:goto(newpos, 0.0)
wait(0.0)
-- Orientation from this position towards Earth
orient_earth = newpos:orientationto(earthpos, celestia:newvector(0, 1, 0))
-- Orientation from this position towards Moon
orient_moon = newpos:orientationto(moonpos, celestia:newvector(0, 1, 0))
-- Interpolate new observer orientation (middle)
orientation = orient_earth:slerp(orient_moon, 0.5)
obs:setorientation(orientation)


返回到 旋轉 方法索引。


華夏公益教科書