旋转 四元数与矩阵
1.旋转:用矩阵表示
1.1 2D旋转
1.1.1 x=1,y=0
p → ( 1 , 0 ) \overrightarrow{p}(1,0) p(1,0)旋转θ度后,得到 p → ′ ( c o s θ , s i n θ ) \overrightarrow{p}'(cosθ,sinθ) p′(cosθ,sinθ)
1.1.2 x=0,y=0
p → ( 0 , 1 ) \overrightarrow{p}(0,1) p(0,1)旋转θ度后,得到 p → ′ ( − s i n θ , c o s θ ) \overrightarrow{p}'(-sinθ,cosθ) p′(−sinθ,cosθ)
1.1.3 x=1,y=1
p → ( 1 , 1 ) \overrightarrow{p}(1,1) p(1,1)旋转θ度后,得到 p → ′ ( c o s θ − s i n θ , s i n θ + c o s θ ) \overrightarrow{p}'(cosθ-sinθ,sinθ+cosθ) p′(cosθ−sinθ,sinθ+cosθ)
1.1.4 x,y为任意数
p
→
(
x
,
y
)
\overrightarrow{p}(x,y)
p(x,y)旋转θ度后,得到
p
→
′
(
x
∗
c
o
s
θ
−
y
∗
s
i
n
θ
,
x
∗
s
i
n
θ
+
y
∗
c
o
s
θ
)
\overrightarrow{p}'(x*cosθ-y*sinθ,x*sinθ+y*cosθ)
p′(x∗cosθ−y∗sinθ,x∗sinθ+y∗cosθ)
用矩阵表示为:
[
x
y
]
∗
[
c
o
s
θ
s
i
n
θ
−
s
i
n
θ
c
o
s
θ
]
\begin{bmatrix} x \\ y \end{bmatrix} * \begin{bmatrix} cos\theta & sin\theta \\ -sin\theta & cos\theta \end{bmatrix}
[xy]∗[cosθ−sinθsinθcosθ]
1.2 3D旋转
1.2.1 绕x轴旋转
R x ( θ ) = [ p ′ q ′ r ′ ] = [ 1 0 0 0 c o s ( θ ) s i n ( θ ) 0 − s i n ( θ ) c o s ( θ ) ] R_x(\theta)=\begin{bmatrix} p'\\ q'\\ r' \end{bmatrix}= \begin{bmatrix} 1&0 &0 \\ 0 & cos(\theta)&sin(\theta)\\ 0& -sin(\theta)& cos(\theta) \end{bmatrix} Rx(θ)=⎣⎡p′q′r′⎦⎤=⎣⎡1000cos(θ)−sin(θ)0sin(θ)cos(θ)⎦⎤
1.2.2 绕y轴旋转
R x ( θ ) = [ p ′ q ′ r ′ ] = [ c o s ( θ ) 0 − s i n ( θ ) 0 1 0 s i n ( θ ) 0 c o s ( θ ) ] R_x(\theta)=\begin{bmatrix} p'\\ q'\\ r' \end{bmatrix}= \begin{bmatrix} cos(\theta)&0 &-sin(\theta) \\ 0 & 1&0\\ sin(\theta)& 0& cos(\theta) \end{bmatrix} Rx(θ)=⎣⎡p′q′r′⎦⎤=⎣⎡cos(θ)0sin(θ)010−sin(θ)0cos(θ)⎦⎤
1.2.3 绕z轴旋转
R x ( θ ) = [ p ′ q ′ r ′ ] = [ c o s ( θ ) s i n ( θ ) 0 − s i n ( θ ) c o s ( θ ) 0 0 0 1 ] R_x(\theta)=\begin{bmatrix} p'\\ q'\\ r' \end{bmatrix}= \begin{bmatrix} cos(\theta)&sin(\theta) &0 \\ -sin(\theta) & cos(\theta)&0\\ 0& 0& 1 \end{bmatrix} Rx(θ)=⎣⎡p′q′r′⎦⎤=⎣⎡cos(θ)−sin(θ)0sin(θ)cos(θ)0001⎦⎤
2.旋转:用四元数表示
2.1 2D旋转(复数)
(1)复数的定义:
复数(a,b)定义了数a+bi,i为虚数满足 i 2 = − 1 i^2=-1 i2=−1。a为实部,b为虚部
(2)用复数表示旋转
p
(
x
,
y
)
p(x,y)
p(x,y)旋转θ度后,即产生一个复数
(
c
o
s
θ
,
s
i
n
θ
)
(cos\theta,sin\theta)
(cosθ,sinθ),使之相乘得到旋转后的向量
p
′
=
(
x
+
y
i
)
∗
(
c
o
s
θ
+
i
s
i
n
θ
)
p'=(x+yi)*(cos\theta+isin\theta)
p′=(x+yi)∗(cosθ+isinθ)
=
(
x
∗
c
o
s
θ
−
y
∗
s
i
n
θ
)
+
(
x
∗
s
i
n
θ
+
y
∗
c
o
s
θ
)
i
=(x*cosθ-y*sinθ)+(x*sinθ+y*cosθ)i
=(x∗cosθ−y∗sinθ)+(x∗sinθ+y∗cosθ)i
得到的结果与矩阵旋转得到的结果是相同的
p
′
(
x
∗
c
o
s
θ
−
y
∗
s
i
n
θ
,
x
∗
s
i
n
θ
+
y
∗
c
o
s
θ
)
p'(x*cosθ-y*sinθ,x*sinθ+y*cosθ)
p′(x∗cosθ−y∗sinθ,x∗sinθ+y∗cosθ)
2.2 3D旋转(四元数)
(1)四元数的定义:
i
2
=
j
2
=
k
2
=
−
1
i^2=j^2=k^2=-1
i2=j2=k2=−1
i
j
=
k
,
j
i
=
−
k
ij=k,ji=-k
ij=k,ji=−k
j
k
=
i
,
k
j
=
−
i
jk=i,kj=-i
jk=i,kj=−i
k
i
=
j
,
i
k
=
−
j
ki=j,ik=-j
ki=j,ik=−j
一个四元数
[
w
,
(
x
,
y
,
z
)
]
[w,(x,y,z)]
[w,(x,y,z)]定义了复数
w
+
x
i
+
x
j
+
z
k
w+xi+xj+zk
w+xi+xj+zk
(2)四元数表示旋转
四元数表示的核心在于轴-角对:
(
n
,
θ
)
(n,\theta)
(n,θ)
n为旋转轴,θ为绕轴旋转的量
四元数的表示则为
q
=
[
c
o
s
(
θ
/
2
)
,
s
i
n
(
θ
/
2
)
n
]
q=[cos(\theta/2),sin(\theta/2)n]
q=[cos(θ/2),sin(θ/2)n]
=
[
c
o
s
(
θ
/
2
)
,
(
s
i
n
(
θ
/
2
)
n
x
,
s
i
n
(
θ
/
2
)
n
y
,
s
i
n
(
θ
/
2
)
n
z
)
]
=[cos(\theta/2),(sin(\theta/2)n_x,sin(\theta/2)n_y,sin(\theta/2)n_z)]
=[cos(θ/2),(sin(θ/2)nx,sin(θ/2)ny,sin(θ/2)nz)]
设a为位置向量,b表示绕x轴旋转90度,c表示绕y轴旋转90度
若要先旋转b再旋转c,则四元数相乘需要反着来,即cba(基于四元数遵从结合律,不遵从交换律的原则可以推导出(c(ba)),即先被b处理,再被c处理)
参考:
【1】3D数学基础:图形与游戏开发