今天準(zhǔn)備學(xué)習(xí)和研究下unity3d的四元數(shù) Quaternion
四元數(shù)在電腦圖形學(xué)中用于表示物體的旋轉(zhuǎn),在unity中由x,y,z,w 表示四個(gè)值。
四元數(shù)是最簡(jiǎn)單的超復(fù)數(shù)。復(fù)數(shù)是由實(shí)數(shù)加上元素 i 組成,其中i^2 = -1 \,。 相似地,四元數(shù)都是由實(shí)數(shù)加上三個(gè)元素 i、j、k 組成,而且它們有如下的關(guān)系: i^2 = j^2 = k^2 = ijk = -1 \, 每個(gè)四元數(shù)都是 1、i、j 和 k 的線性組合,即是四元數(shù)一般可表示為a + bi + cj + dk \,。
具體的四元數(shù)知識(shí)可從百度、維基等網(wǎng)站了解。
現(xiàn)在只說說在unity3D中如何使用Quaternion來表達(dá)物體的旋轉(zhuǎn)。
基本的旋轉(zhuǎn)我們可以用腳本內(nèi)置旋轉(zhuǎn)函數(shù)transform.Rotate()來實(shí)現(xiàn)。
function Rotate (eulerAngles : Vector3, relativeTo : Space = Space.Self) : void
但是當(dāng)我們希望對(duì)旋轉(zhuǎn)角度進(jìn)行一些計(jì)算的時(shí)候,就要用到四元數(shù)Quaternion了。我對(duì)高等數(shù)學(xué)來說就菜鳥一個(gè),只能用最樸素的方法看效果了。
Quaternion的變量比較少也沒什么可說的,大家一看都明白。唯一要說的就是x\y\z\w的取值范圍是[-1,1],物體并不是旋轉(zhuǎn)一周就所有數(shù)值回歸初始值,而是兩周。
初始值: (0,0,0,1)
沿著y軸旋轉(zhuǎn):180°(0,1,0,0) 360°(0,0,0,-1)540°(0,-1,0,0) 720°(0,0,0,1)
沿著x軸旋轉(zhuǎn):180°(-1,0,0,0) 360°(0,0,0,-1)540°(1,0,0,0) 720°(0,0,0,1)
無旋轉(zhuǎn)的寫法是Quaternion.identify
現(xiàn)在開始研究Quaternion的函數(shù)都有什么用。
函數(shù)
1)
function ToAngleAxis (out angle : float, out axis : Vector3) : voidDescription
Converts a rotation to angle-axis representation
這個(gè)函數(shù)的作用就是返回物體的旋轉(zhuǎn)角度(物體的z軸和世界坐標(biāo)z軸的夾角)和三維旋轉(zhuǎn)軸的向量到變量out angle 和out axis
腳本:
var a=0.0;
var b=Vector3.zero;
transform.rotation.ToAngleAxis(a,b);
輸入:transform.localEularAngles=(0,0,0);
輸出: a=0, b=(1,0,0);
輸入:transform.localEularAngles=(0,90,0);
輸出:a=90, b=(0,1,0);
輸入:transform.localEularAngles=(270,0,0);
輸出:a=90, b=(-1,0,0)
2)function SetFromToRotation (fromDirection : Vector3, toDirection : Vector3) : void Description
Creates a rotation which rotates from fromDirection to toDirection.
這個(gè)函數(shù)的作用是把物體的fromDirection旋轉(zhuǎn)到toDirection
腳本:
var a:Vector3;
var b:Vector3;
var q:Quaternion;
var headUpDir:Vector3;
q.SetFromToRotation(a,b);
transform.rotation=q;
headUpDir=transform.TransformDirection(Vector3.Forward);
輸入:a=Vector3(0,0,1); b=Vector3(0,1,0)//把z軸朝向y軸
輸出: q=(-0.7,0,0,0.7); headUpDir=(0,1,0)
輸入:a=Vector3(0,0,1); b=Vector3(1,0,0)//把z軸朝向x軸
輸出: q=(0,0.7,0,0.7); headUpDir=(1,0,0)
輸入:a=Vector3(0,1,0); b=Vector3(1,0,0)//把y軸朝向x軸
輸出: q=(0,0,-0.7,0.7); headUpDir=(0,0,1)
Description
Creates a rotation that looks along forward with the the head upwards along upwards
Logs an error if the forward direction is zero.
這個(gè)函數(shù)建立一個(gè)旋轉(zhuǎn)使z軸朝向view y軸朝向up。這個(gè)功能讓我想起了Maya里的一種攝像機(jī)lol,大家自己玩好了,很有趣。
腳本:
var obj1: Transform;
var obj2: Transform;
var q:Quaternion;
q.SetLookRotation(obj1.position, obj2.position);
transform.rotation=q;
然后大家拖動(dòng)obj1和obj2就可以看到物體永遠(yuǎn)保持z軸朝向obj1,并且以obj2的位置來保持y軸的傾斜度。
傻逗我玩了半天 哈哈^^ 這個(gè)功能挺實(shí)用的。
4)function ToString () : string
Description
Returns a nicely formatted string of the Quaternion
這個(gè)一般用不著吧?看不懂的一邊查字典去~
Class Functions
1)四元數(shù)乘法 *
建議非特別了解的人群就不要用了。
作用很簡(jiǎn)單,c=a*b (c,a,b∈Quaternion)可以理解為 ∠c=∠a+∠b
但是a*b 和b*a效果不一樣的。
2) == 和 !=
不解釋了
3)static function Dot (a : Quaternion, b : Quaternion) : float
Description
The dot product between two rotations
點(diǎn)積,返回一個(gè)float. 感覺用處不大。Vector3.Angle()比較常用。
4)static function AngleAxis (angle : float, axis : Vector3) : Quaternion
Description
Creates a rotation which rotates angle degrees around axis.
物體沿指定軸向axis旋轉(zhuǎn)角度angle, 很實(shí)用的一個(gè)函數(shù)也是。
腳本:
var obj1: Transform;
var obj2: Transform;
var q:Quaternion;
//物體沿obj2的z軸旋轉(zhuǎn),角度等于obj1的z軸。
q=Quaternion.AngleAxis(obj1.localEularAngle.z, obj2.TransformDirection(Vector3.forward));
transform.rotation=q;
5)static function FromToRotation (fromDirection : Vector3, toDirection : Vector3) : Quaternion
Description
Creates a rotation which rotates from fromDirection to toDirection.
Usually you use this to rotate a transform so that one of its axes eg. the y-axis - follows a target direction toDirection in world space.
跟SetFromToRotation差不多,區(qū)別是可以返回一個(gè)Quaternion。通常用來讓transform的一個(gè)軸向(例如 y軸)與toDirection在世界坐標(biāo)中同步。
6)static function LookRotation (forward : Vector3, upwards : Vector3 = Vector3.up) : Quaternion
Description
Creates a rotation that looks along forward with the the head upwards along upwards
Logs an error if the forward direction is zero.
跟SetLootRotation差不多,區(qū)別是可以返回一個(gè)Quaternion。
7)static function Slerp (from : Quaternion, to : Quaternion, t : float) : Quaternion
Description
Spherically interpolates from towards to by t.
從from 轉(zhuǎn)換到to,移動(dòng)距離為t。也是很常用的一個(gè)函數(shù),用法比較多,個(gè)人感覺比較難控制。當(dāng)兩個(gè)quaternion接近時(shí),轉(zhuǎn)換的速度會(huì)比較慢。
腳本:
var obj1: Transform;
var t=0.1;
var q:Quaternion;
//讓物體旋轉(zhuǎn)到與obj1相同的方向
q=Quaternion.Slerp(transform.rotation, obj1.rotation,t);
transform.rotation=q;
根據(jù)我個(gè)人推測(cè),可能t 代表的是from 和to 之間距離的比例。為此我做了實(shí)驗(yàn)并證明了這一點(diǎn)即:
q=Quaternion.Slerp(a,b,t);
q,a,b∈Quaternion
t[0,1]
q=a+(b-a)*t
并且t最大有效范圍為0~1
腳本:
var obj1: Transform;
var obj2:Transform;
var t=0.1;
var q:Quaternion;
//讓物體obj1和obj2 朝向不同的方向,然后改變t
q=Quaternion.Slerp(obj1.rotation, obj2.rotation,t);
transform.rotation=q;
t+=Input.GetAxis("horizontal")*0.1*Time.deltaTime;
7)static function Lerp (a : Quaternion, b : Quaternion, t : float) : Quaternion
Description
Interpolates from towards to by t and normalizes the result afterwards.
This is faster than Slerp but looks worse if the rotations are far apart
跟Slerp相似,且比Slerp快,.但是如果旋轉(zhuǎn)角度相距很遠(yuǎn)則會(huì)看起來很差。
8)static function Inverse (rotation : Quaternion) : Quaternion
Description
Returns the Inverse of rotation.
返回與rotation相反的方向
9)static function Angle (a : Quaternion, b : Quaternion) : float
Description
Returns the angle in degrees between two rotations a and b.
計(jì)算兩個(gè)旋轉(zhuǎn)之間的夾角。跟Vector3.Angle() 作用一樣。
10)static function Euler (x : float, y : float, z : float) : Quaternion
Description
Returns a rotation that rotates z degrees around the z axis, x degrees around the x axis, and y degrees around the y axis (in that order).
把旋轉(zhuǎn)角度變成對(duì)應(yīng)的Quaternion
以上就是Quaternion的所有函數(shù)了。
關(guān)于應(yīng)用,就說一個(gè),其他的有需要再補(bǔ)充。
Slerp 函數(shù)是非常常用的一個(gè)函數(shù),用來產(chǎn)生旋轉(zhuǎn)。
static function Slerp (from : Quaternion, to : Quaternion, t : float) : Quaternion
對(duì)于新手來說,最難的莫過于如何用它產(chǎn)生一個(gè)勻速的旋轉(zhuǎn)。如果想用它產(chǎn)生勻速轉(zhuǎn)動(dòng),最簡(jiǎn)單的辦法就是把form和to固定,然后勻速增加t
腳本:
var obj1: Transform;
var obj2:Transform;
var speed:float;
var t=0.1;
var q:Quaternion;
q=Quaternion.Slerp(obj1.rotation, obj2.rotation,t);
transform.rotation=q;
t+=Time.deltaTime;
但是這并不能解決所有情況。 很多時(shí)候from 和to都不是固定的,而且上一個(gè)腳本也不能保證所有角度下的旋轉(zhuǎn)速度一致。所以我寫了這個(gè)腳本來保證可以應(yīng)付大多數(shù)情況。
腳本:
var target: Transform;
var rotateSpeed=30.0;
var t=float;
var q:Quaternion;
var wantedRotation=Quaternion.FromToRotation(transform.position,target.position);
t=rotateSpeed/Quaternion.Angle(transform.rotation,wantedRotation)*Time.deltaTime;
q=Quaternion.Slerp(transform.rotation, target.rotation,t);
transform.rotation=q;
這個(gè)腳本可以保證物體的旋轉(zhuǎn)速度永遠(yuǎn)是rotateSpeed。
第七行用旋轉(zhuǎn)速度除以兩者之間的夾角得到一個(gè)比例。
如果自身坐標(biāo)和目標(biāo)之間的夾角是X度,我們想以s=30度每秒的速度旋轉(zhuǎn)到目標(biāo)的方向,則每秒旋轉(zhuǎn)的角度的比例為s/X。再乘以每次旋轉(zhuǎn)的時(shí)間Time.deltaTime我們就得到了用來勻速旋轉(zhuǎn)的t值