3D数学基础 旋转 四元数与矩阵

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 (xcosθysinθxsinθ+ycosθ)
用矩阵表示为:
[ 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(θ)=pqr=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(θ)=pqr=cos(θ)0sin(θ)010sin(θ)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(θ)=pqr=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 =(xcosθysinθ)+(xsinθ+ycosθ)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(xcosθysinθxsinθ+ycosθ)

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数学基础:图形与游戏开发

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值