Skip to content

Translation notice

This page was translated with machine translation and may contain inaccuracies. If you can help improve it, please open an issue or submit a pull request.

Cover

Research on displaying entity rendering transformations

Xu Muxian

Xu Muxian

introduction ​

Display entities are one of the technical entities of Minecraft, and their role is mainly reflected in the visual aspect. These entities have no collision boxes, do not have any autonomous behavior, and can only be generated through technical means. If you don't specify NBT when generating, nothing will be displayed. Developers of vanilla technology can use the regular fields of the display entity to display some common content, such as normal-shaped blocks, items, and text. However, it would be a bit monotonous if only the display entity is used to display these regular contents.
The transformation field that displays the entity is a more complex field in the entity format. It uses matrix form or decomposition form to represent the rendering transformation of the entity, thereby creating some special effects.

matrix form ​

When using matrix form, the data type of the field transformation is a list. There are 16 elements in the list, and these elements are single-precision floating point numbers. This list is used to represent a4×44×4The row-major order affine transformation matrix of . In order to express the transformation of points in the three-dimensional space in matrix form, the original space is mapped to the affine space. For each point in the three-dimensional space(x0,y0,z0)(x_0,y_0,z_0), add a11To represent a point in affine space, that is(x0,y0,z0,1)(x_0,y_0,z_0,1). Let the point undergo a certain affine transformationA\boldsymbol{A}located after(x′,y′,z′,1)(x',y',z',1), then it is written in the form of matrix multiplication:

[x′y′z′1]=[a11a12a13a14a21a22a23a24a31a32a33a34a41a42a43a44][x0y0z01]\left[\begin{matrix} x'\\y'\\z'\\1 \end{matrix}\right]=\left[\begin{matrix} a_{11}&a_{12}&a_{13}&a_{14}\\ a_{21}&a_{22}&a_{23}&a_{24}\\ a_{31}&a_{32}&a_{33}&a_{34}\\ a_{41}&a_{42}&a_{43}&a_{44}\\ \end{matrix}\right]\left[\begin{matrix} x_0\\y_0\\z_0\\1 \end{matrix}\right]

Basic transformation forms include translation, rotation, scaling (mirror), and shearing. All transformations are based on the actual coordinates of the entity.

Pan ​

Assume that any point on the display entity(x0,y0,z0,1)(x_0,y_0,z_0,1)existxx、yy、zzAxis translation respectivelyaa、bb、ccget points after(x′,y′,z′,1)(x',y',z',1),but

{x′=x0+ay′=y0+bz′=z0+c1=1\left\{\begin{matrix*}[l] x'&=&x_0&&&&&+&a\\ y'&=&&&y_0&&&+&b\\ z'&=&&&&&z_0&+&c\\ 1&=&&&&&&&1 \end{matrix*}\right.

Then the translation matrixT\boldsymbol{T}for

T(a,b,c)=[100a010b001c0001]\boldsymbol{T}(a,b,c)=\left[\begin{matrix} 1&0&0&a\\ 0&1&0&b\\ 0&0&1&c\\ 0&0&0&1 \end{matrix}\right]

rotate ​

There are three ways of rotation, namely aroundxxaxis, aroundyyaxis and windingzzaxis rotation. to go aroundxxaxis rotationα\alphaFor example, assume that the entity has a pointAAand entity anchor pointOOThe straight line formed byzzThe angle between the axes isφ\varphi,makeOA→\overrightarrow{OA}The modulus isll, then there is

{x=xy=lcos⁡φz=lsin⁡φ\left\{\begin{array}{l} x=x\\ y=l\cos{\varphi}\\ z=l\sin{\varphi} \end{array}\right.

OA→\overrightarrow{OA}aroundxxaxis rotationα\alphagetOA′→\overrightarrow{OA'}, at this time there is

{x′=xy′=lcos⁡(φ+α)=lcos⁡φcos⁡α−lsin⁡φsin⁡αz′=lsin⁡(φ+α)=lsin⁡φcos⁡α+lcos⁡φsin⁡α\left\{\begin{array}{l} x'=x\\ y'=l\cos(\varphi+\alpha)=l\cos\varphi\cos\alpha-l\sin\varphi\sin\alpha\\ z'=l\sin(\varphi+\alpha)=l\sin\varphi\cos\alpha+l\cos\varphi\sin\alpha \end{array}\right.

So there is

{x′=xy′=ycos⁡α−zsin⁡αz′=ysin⁡α+zcos⁡α\left\{\begin{array}{l} x'=x\\ y'=y\cos\alpha-z\sin\alpha\\ z'=y\sin\alpha+z\cos\alpha \end{array}\right.

Convert it to an affine matrix and get

Rx(α)=[10000cos⁡α−sin⁡α00sin⁡αcos⁡α00001]\boldsymbol{R}_{x}(\alpha)=\left[\begin{matrix} 1&0&0&0\\ 0&\cos{\alpha}&-\sin{\alpha}&0\\ 0&\sin{\alpha}&\cos{\alpha}&0\\ 0&0&0&1 \end{matrix}\right]

In the same way, aroundyyaxis rotationβ\betaThe matrix form of

Ry(β)=[cos⁡β0sin⁡β00100−sin⁡β0cos⁡β00001]\boldsymbol{R}_{y}(\beta)=\left[\begin{matrix} \cos{\beta}&0&\sin{\beta}&0\\ 0&1&0&0\\ - \sin{\beta}&0&\cos{\beta}&0\\ 0&0&0&1 \end{matrix}\right]

aroundzzaxis rotationγ\gammaThe matrix form of

Rz(γ)=[cos⁡γ−sin⁡γ00sin⁡γcos⁡γ0000100001]\boldsymbol{R}_{z}(\gamma)=\left[\begin{matrix} \cos{\gamma}&-\sin{\gamma}&0&0\\ \sin{\gamma}&\cos{\gamma}&0&0\\ 0&0&1&0\\ 0&0&0&1 \end{matrix}\right]

Zoom ​

Assume that any point on the display entity(x0,y0,z0,1)(x_0,y_0,z_0,1)alongxx、yy、zzAxis scaled separatelymm、nn、ppGet points after doubling(x′,y′,z′,1)(x',y',z',1),but

{x′=mx0y′=ny0z′=pz01=1\left\{\begin{matrix*}[l] x'&=&mx_0&&&&&&\\ y'&=&&&ny_0&&&&\\ z'&=&&&&&pz_0&&\\ 1&=&&&&&&&1 \end{matrix*}\right.

Then the scaling matrixS\boldsymbol{S}for

S(m,n,p)=[m0000n0000p00001]\boldsymbol{S}(m,n,p)=\left[\begin{matrix} m&0&0&0\\ 0&n&0&0\\ 0&0&p&0\\ 0&0&0&1 \end{matrix}\right]

likem=n=pm=n=p, it is uniform scaling; otherwise, it is non-uniform scaling.

mirror ​

For a scaling matrix, in particular, ifmm、nn、ppIf at least one of the three is negative, a mirror transformation will be performed. Negative scaling factors invert the coordinate system on the corresponding axis and change the direction of the surface normal, resulting in concave rendering. \

Concave rendering caused by mirror transformation\

If you display any point on the entity(x0,y0,z0,1)(x_0,y_0,z_0,1)alongxxAxis mirroring, no changes in other directions, easy to get the mirror matrix

Mx(m)=[m000010000100001]\boldsymbol{M}_{x}(m)=\left[\begin{matrix} m&0&0&0\\ 0&1&0&0\\ 0&0&1&0\\ 0&0&0&1 \end{matrix}\right]

inm<0m<0. The same principle can be followedyyaxis mirror, alongzzaxis mirror matrixMy(n)\boldsymbol{M}_{y}(n)、Mz(p)\boldsymbol{M}_{z}(p). Mirror transformations in multiple directions also It is easy to derive, for example, inxxaxis,yyaxis, andzzThe matrix required to apply mirror transformation simultaneously in the axis direction (m<0m<0,n<0n<0,p<0p<0)for

Mx,y,z(m,n,p)=[m0000n0000p00001]\boldsymbol{M}_{x,y,z}(m,n,p)=\left[\begin{matrix} m&0&0&0\\ 0&n&0&0\\ 0&0&p&0\\ 0&0&0&1 \end{matrix}\right]

cut ​

The shear transformation moves all points on the entity in a certain direction. The distance of any point on the straight line passing through the origin in that direction changes linearly with the distance between the straight line and the origin, which makes the image tilt. A shear transformation occurs in a plane composed of two orthogonal coordinate axes, with shearing in one direction and no transformation in the other direction. There are six pairs of orthogonal relationships between coordinate axes in the three-dimensional coordinate system, so there are six elementary shear transformations.

Cut Transform

As shown in the figure, when the image is sheared in one direction, it actually has a shearing angle with the other direction.θi,j\theta_{i,j}, subscript (i,ji,j) represents theiiCut in the direction and matchjjThe direction is at a certain shear angle. If the horizontal direction in the figure isxxaxis, the longitudinal direction isyyaxis, the shear angle is recorded asθx,y\theta_{x,y}, obviously there are

{x′=x0+y0tan⁡θx,yy′=y0z′=z01=1\left\{\begin{matrix*}[l] x'&=&x_0+&y_{0}\tan{\theta_{x,y}}&&&&\\ y'&=&&y_0&&&&\\ z'&=&&&&z_0&&\\ 1&=&&&&&&&1 \end{matrix*}\right.

butxxMake shear in the axial direction and connect it withyyThe matrix required for a certain shear angle in the axis directionH\boldsymbol{H}for

H(θx,y)=[1tan⁡θx,y00010000100001]\boldsymbol{H}(\theta_{x,y})=\left[\begin{matrix} 1&\tan{\theta_{x,y}}&0&0\\ 0&1&0&0\\ 0&0&1&0\\ 0&0&0&1 \end{matrix}\right]

In the same way, the matrices required for the other six shear transformations can be derived. When the direction of the shear transformation isxxaxis, elementtan⁡θi,j\tan{\theta_{i,j}}must be located in the first row of the matrix,yyThe axis is the second row,zzThe axis is the third row; the direction at a shear angle to the transformation direction isxxaxis, elementtan⁡θi,j\tan{\theta_{i,j}}Must be in the first column,yyThe axis is the second column,zzThe axis is the third column. For example, a clipping transformation inzzaxis direction, andxxThe axis direction is at a shear angle, thentan⁡θz,x\tan{\theta_{z,x}}Located in the third row and first column.
The shear matrices described above only transform in one direction and form a certain shear angle with the other direction. If multiple different shear transformations are applied at the same time and the elements are filled in using the above rules, the shear matrix can be recorded as

H(θx,y,θx,z,θy,x,θy,z,θz,x,θz,y)=[1tan⁡θx,ytan⁡θx,z0tan⁡θy,x1tan⁡θy,z0tan⁡θz,xtan⁡θz,y100001]\boldsymbol{H}(\theta_{x,y},\theta_{x,z},\theta_{y,x},\theta_{y,z},\theta_{z,x},\theta_{z,y})=\left[\begin{matrix} 1&\tan{\theta_{x,y}}&\tan{\theta_{x,z}}&0\\ \tan{\theta_{y,x}}&1&\tan{\theta_{y,z}}&0\\ \tan{\theta_{z,x}}&\tan{\theta_{z,y}}&1&0\\ 0&0&0&1 \end{matrix}\right]

If the shear transformation in a certain direction is not used, the corresponding position in the matrix will betan⁡θi,j\tan{\theta_{i,j}}written as00That’s it.

Combined transformation ​

One transformation may not suffice, and sometimes multiple transformations need to be applied simultaneously to represent complex transformations. For a finite number of affine transformationsA1\boldsymbol{A}_1、A2\boldsymbol{A}_2、……An\boldsymbol{A}_n, apply them to one point in turnx\boldsymbol{x}, then the point obtained after transformationx′\boldsymbol{x'}for

x′=AnAn−1⋯A2A1x\boldsymbol{x'}=\boldsymbol{A}_{n}\boldsymbol{A}_{n-1}\cdots\boldsymbol{A}_{2}\boldsymbol{A}_{1}\boldsymbol{x}

Note that matrix multiplication follows the operation rules from right to left and does not support commutative law, but supports associative law, so there is

x′=(AnAn−1⋯A2A1)x\boldsymbol{x'}=(\boldsymbol{A}_{n}\boldsymbol{A}_{n-1}\cdots\boldsymbol{A}_{2}\boldsymbol{A}_{1})\boldsymbol{x}

makeA=AnAn−1⋯A2A1\boldsymbol{A}=\boldsymbol{A}_{n}\boldsymbol{A}_{n-1}\cdots\boldsymbol{A}_{2}\boldsymbol{A}_{1},butx=Ax′\boldsymbol{x}=\boldsymbol{A}\boldsymbol{x'},inA\boldsymbol{A}is the combined transformation matrix. The order of various transformations in a combined transformation is very important, as the previous transformation may affect the result of the next transformation.

The matrices used in tagtransformation are all combined transformation matrices.

Application examples ​

Modify a block to display the NBT data of the entity so that it flows around theyyaxis rotation30∘30^{\circ}, aroundxxaxis rotation45∘45^{\circ}, aroundzzaxis rotation90∘90^{\circ}。 Find the combined transformation matrix, paying attention to the calculation from right to left:

A=Rz(90∘)Rx(45∘)Ry(30∘)=[cos⁡90∘−sin⁡90∘00sin⁡90∘cos⁡90∘0000100001][10000cos⁡45∘−sin⁡45∘00sin⁡45∘cos⁡45∘00001][cos⁡30∘0sin⁡30∘00100−sin⁡30∘0cos⁡30∘00001]=[−24−22640320120−24226400001]≈[−0.35−0.710.6100.8700.50−0.350.710.6100001]\begin{align} \boldsymbol{A}&=\boldsymbol{R}_{z}(90^{\circ})\boldsymbol{R}_{x}(45^{\circ})\boldsymbol{R}_{y}(30^{\circ})\nonumber\\ &=\left[\begin{matrix}\cos{90^{\circ}}&-\sin{90^{\circ}}&0&0\\\sin{90^{\circ}}&\cos{90^{\circ}}&0&0\\0&0&1&0\\0&0&0&1\end{matrix}\right]\left[\begin{matrix}1&0&0&0\\0&\cos{45^{\circ}}&-\sin{45^{\circ}}&0\\0&\sin{45^{\circ}}&\cos{45^{\circ}}&0\\0&0&0&1\end{matrix}\right]\left[\begin{matrix}\cos{30^{\circ}}&0&\sin{30^{\circ}}&0\\0&1&0&0\\-\sin{30^{\circ}}&0&\cos{30^{\circ}}&0\\0&0&0&1\end{matrix}\right]\nonumber\\ &=\left[\begin{matrix}-\cfrac{\sqrt{2}}{4}&-\cfrac{\sqrt{2}}{2}&\cfrac{\sqrt{6}}{4}&0\\\cfrac{\sqrt{3}}{2}&0&\cfrac{1}{2}&0\\-\cfrac{\sqrt{2}}{4}&\cfrac{\sqrt{2}}{2}&\cfrac{\sqrt{6}}{4}&0\\0&0&0&1\end{matrix}\right]\approx\left[\begin{matrix}-0.35&-0.71&0.61&0\\0.87&0&0.5&0\\-0.35&0.71&0.61&0\\0&0&0&1\end{matrix}\right]\nonumber \end{align}

Therefore, command should be

mcfunction
data merge entity @e[type=block_display,limit=1] {transformation:[-0.35f,-0.71f,0.61f,0.0f,0.87f,0.0f,0.5f,0.0f,-0.35f,0.71f,0.61f,0.0f,0.0f,0.0f,0.0f,1.0f]}

Decomposed form ​

for these4×44\times 4affine transformation matrix of sizeA\boldsymbol{A}, whose elementsa41a_{41}、a42a_{42}、a43a_{43}is always 0,a44a_{44}is always 1, if not 1, the entire matrix is1a44\cfrac{1}{a_{44}}scaling, so thata44a_{44}is 1. It can be written in blocks as follows:

A=[a11a12a13a14a21a22a23a24a31a32a33a34a41a42a43a44]=[B3×3T3×1O1×3E1×1]\boldsymbol{A}=\left[\begin{array}{ccc|c} a_{11}&a_{12}&a_{13}&a_{14}\\ a_{21}&a_{22}&a_{23}&a_{24}\\ a_{31}&a_{32}&a_{33}&a_{34}\\ \hline a_{41}&a_{42}&a_{43}&a_{44}\\ \end{array}\right]=\left[\begin{matrix} \boldsymbol{B}_{3\times 3}&\boldsymbol{T}_{3\times 1}\\ \boldsymbol{O}_{1\times 3}&\boldsymbol{E}_{1\times 1}\\ \end{matrix}\right]

The block array in the formulaB\boldsymbol{B}It's the upper left corner3×33\times 3Area, this area represents the linear transformation of the model, and stores all linear transformation data including rotation, scaling, mirroring and shearing. Note that this block array is not suitable for translation transformation, because translation transformation is not a linear transformation. And the block arrayT\boldsymbol{T}The three elements of are used only by translation transformations.
The transformation field in decomposed form is a block arrayB\boldsymbol{B}Data used after singular value decomposition. For any square matrix of order 3B\boldsymbol{B}, there is always a third-order orthogonal square matrixU\boldsymbol{U}andV\boldsymbol{V}, 3rd order diagonal matrixΣ\boldsymbol{\varSigma},have

B=UΣVT\boldsymbol{B}=\boldsymbol{U\varSigma}\boldsymbol{V}^\mathrm{T}

In the formula:
VT\boldsymbol{V}^\mathrm{T}--matrixV\boldsymbol{V}the transposed matrix.
sayU\boldsymbol{U}is the left singular vector matrix,V\boldsymbol{V}is the right singular vector matrix, diagonal matrixΣ\boldsymbol{\varSigma}The three elements on the middle diagonal are called singular values. The calculation method of singular value decomposition is introduced below.
Taking the transposed matrix on the left and right sides of the equal sign in the above equation, we get

BT=VΣUT\boldsymbol{B}^\mathrm{T}=\boldsymbol{V\varSigma}\boldsymbol{U}^\mathrm{T}

Because of the square matrixU\boldsymbol{U}andV\boldsymbol{V}is orthogonal, thereforeVTV=E\boldsymbol{V}^\mathrm{T}\boldsymbol{V}=\boldsymbol{E}、UTU=E\boldsymbol{U}^\mathrm{T}\boldsymbol{U}=\boldsymbol{E}. then there is

BBT=UΣVTVΣUT=UΣ2UT\boldsymbol{B}\boldsymbol{B}^\mathrm{T}=\boldsymbol{U\varSigma}\boldsymbol{V}^\mathrm{T}\boldsymbol{V\varSigma}\boldsymbol{U}^\mathrm{T}=\boldsymbol{U}\boldsymbol{\varSigma}^{2}\boldsymbol{U}^\mathrm{T}

Transform the above formula:

UT(BBT)U=Σ2\boldsymbol{U}^\mathrm{T}(\boldsymbol{B}\boldsymbol{B}^\mathrm{T})\boldsymbol{U}=\boldsymbol{\varSigma}^{2}

phalanxBBT\boldsymbol{B}\boldsymbol{B}^\mathrm{T}is a real symmetric matrix, obviously the above formula describes theBBT\boldsymbol{B}\boldsymbol{B}^\mathrm{T}Similar diagonalization process, whereΣ=[σ1σ2σ3]\boldsymbol{\varSigma}=\left[\begin{matrix}\sigma_1&&\\&\sigma_2&\\&&\sigma_3\end{matrix}\right], the orthogonal matrix used is the left singular vector matrixU\boldsymbol{U}. If you rememberλ1\lambda_1、λ2\lambda_2、λ3\lambda_3yesBBT\boldsymbol{B}\boldsymbol{B}^\mathrm{T}The three eigenvalues ​​of , these eigenvalues ​​are non-negative, readers can prove by themselves, so we have

Σ2=[λ1λ2λ3]=[σ12σ22σ32]\boldsymbol{\varSigma}^{2}=\left[\begin{matrix}\lambda_1&&\\&\lambda_2&\\&&\lambda_3\end{matrix}\right]=\left[\begin{matrix}\sigma_1^2&&\\&\sigma_2^2&\\&&\sigma_3^2\end{matrix}\right]

find outBBT\boldsymbol{B}\boldsymbol{B}^\mathrm{T}The diagonal matrix can be obtained by the three eigenvalues ​​ofΣ\boldsymbol{\varSigma}. therefore,Σ\boldsymbol{\varSigma}andU\boldsymbol{U}The solution steps are as follows——
Step 1:
From the characteristic equation∣λE−BBT∣=0\left\lvert\lambda\boldsymbol{E}-\boldsymbol{B}\boldsymbol{B}^\mathrm{T}\right\rvert=0begBBT\boldsymbol{B}\boldsymbol{B}^\mathrm{T}All eigenvalues ​​ofλi\lambda_i, and then find the diagonal matrixΣ=diag(σ1,σ2,σ3)=diag(λ1,λ2,λ3)\boldsymbol{\varSigma}=\mathrm{diag}(\sigma_1,\sigma_2,\sigma_3)=\mathrm{diag}(\sqrt{\lambda_1},\sqrt{\lambda_2},\sqrt{\lambda_3})。
Step 2:
For each eigenvalueλi\lambda_i, by the system of equations(λiE−BBT)x=0(\lambda_{i}\boldsymbol{E}-\boldsymbol{B}\boldsymbol{B}^\mathrm{T})\boldsymbol{x}=\boldsymbol{0}Find the corresponding feature vectorαi\boldsymbol{\alpha_i}。
Step 3:
If the obtained eigenvectors are not orthogonal to each other, then for the eigenvectorsαi\boldsymbol{\alpha_i}Perform orthogonalization, and record the vector after orthogonalization asβi\boldsymbol{\beta_i}。
Step 4:
If the vector obtainedβi\boldsymbol{\beta_i}If there is no unitization, then unitize it asγi\boldsymbol{\gamma_i},makeU=[γ1,γ2,γ3]\boldsymbol{U}=\left[\gamma_1,\gamma_2,\gamma_3\right]. Calculation completed.
For the right singular vector matrixV\boldsymbol{V},have

BTB=VΣUTUΣVT=VΣ2VT\boldsymbol{B}^\mathrm{T}\boldsymbol{B}=\boldsymbol{V\varSigma}\boldsymbol{U}^\mathrm{T}\boldsymbol{U\varSigma}\boldsymbol{V}^\mathrm{T}=\boldsymbol{V}\boldsymbol{\varSigma}^{2}\boldsymbol{V}^\mathrm{T}

In the same way, the right singular vector matrix can be obtained. The calculation steps are the same as the above steps for calculating the left singular vector matrix, whereΣ\boldsymbol{\varSigma}It is the same matrix as above, so there is no need to repeat the calculation. likeB\boldsymbol{B}reversible, then

V=B−1UΣ\boldsymbol{V}=\boldsymbol{B}^{-1}\boldsymbol{U\varSigma}

In this way, the right singular vector matrix can be directly obtained without performing diagonalization calculations.V\boldsymbol{V}。
The results of matrix singular value decomposition have geometric meaning, whereU\boldsymbol{U}、V\boldsymbol{V}is the rotation transformation matrix,Σ\boldsymbol{\varSigma}is the scaling transformation matrix. Any transformation can be decomposed into four processes: initial rotation transformation, scaling transformation, second rotation transformation and translation transformation. Therefore, useV\boldsymbol{V}Represents the initial rotation transformation, useΣ\boldsymbol{\varSigma}Represents scaling transformation, useU\boldsymbol{U}Represents another rotation transformation, and then introduces a translation vector on this basisT\boldsymbol{T}, then we can get the transformation matrixA\boldsymbol{A}The decomposed form of , at this time the field transformation is a composite tag:

compoundtransformation:root tag
  • compoundhomolistright_rotation:The model performs rotation transformation before scaling transformation, that is, the first rotation transformation. Related to V in singular value decomposition. There are two available data forms: axial angle form and quaternion form. You can use axial angle form when writing, but when storing data, it will always be converted into quaternion form.
  • homolistscale:The scaling transformation of the model, related to ∑ in singular value decomposition. Use three-dimensional vectors.
  • compoundhomolistleft_rotation:The rotation transformation after the model is scaled and transformed, that is, rotated again, is related to U in singular value decomposition. There are also two expression methods: axis-angle form and quaternion form. You can use axial angle form when writing, but when storing data, it will always be converted into quaternion form.
  • homolisttranslation:The translation transformation T of the model. Corresponds to the elements in the first three rows of the last column of the matrix form. Use three-dimensional vectors.

For the two fields right_rotationandleft_rotation, there are two data forms representing rotation: axis angle form and quaternion form. These two data forms are introduced below:

axial angle ​

Angular rotation can be understood as: a vectorv\boldsymbol{v}Around an axis of length 1 passing through the origin (i.e. the actual position of the entity)u\boldsymbol{u}rotation angleθ\thetaget vectorv′\boldsymbol{v}'. At this time there is∥u∥=1\left\lVert\boldsymbol{u}\right\rVert=1。
Axis angle rotation diagram
For the convenience of analysis, the vectorv\boldsymbol{v}decomposed into parallel to the axisu\boldsymbol{u}vector ofv∥\boldsymbol{v}_{\parallel}and orthogonal to the axisu\boldsymbol{u}vector ofv⊥\boldsymbol{v}_{\perp}, so there is

v=v∥+v⊥\boldsymbol{v}=\boldsymbol{v}_{\parallel}+\boldsymbol{v}_{\perp}

Decomposition of vector v
Willv∥\boldsymbol{v}_{\parallel}Use containingv\boldsymbol{v}andu\boldsymbol{u}The formula expression of , that is, calculatingv\boldsymbol{v}existu\boldsymbol{u}Projection on:

v∥=∥v∥∥u∥u∥=(u⋅v)u∥u∥∥u∥=(u⋅v)u\boldsymbol{v}_{\parallel}=\left\lVert\boldsymbol{v}_{\parallel}\right\rVert\frac{\boldsymbol{u}}{\left\lVert\boldsymbol{u}\right\rVert}=\frac{(\boldsymbol{u}\cdot\boldsymbol{v})\boldsymbol{u}}{\left\lVert\boldsymbol{u}\right\rVert\left\lVert\boldsymbol{u}\right\rVert}=(\boldsymbol{u}\cdot\boldsymbol{v})\boldsymbol{u}

So we can getv⊥\boldsymbol{v}_{\perp}expression

v⊥=v−v∥=v−(u⋅v)u\boldsymbol{v}_{\perp}=\boldsymbol{v}-\boldsymbol{v}_{\parallel}=\boldsymbol{v}-(\boldsymbol{u}\cdot\boldsymbol{v})\boldsymbol{u}

for vectorsv′\boldsymbol{v}', which can also be decomposed to get

v′=v∥′+v⊥′\boldsymbol{v}'=\boldsymbol{v}_{\parallel}'+\boldsymbol{v}_{\perp}'

In fact, in the vectorv\boldsymbol{v}During the rotation process, the vectorv∥\boldsymbol{v}_{\parallel}No changes occurred, i.e.

v∥′=v∥\boldsymbol{v}_{\parallel}'=\boldsymbol{v}_{\parallel}

Rotation of vector v⊥
Now consider the vectorv⊥\boldsymbol{v}_{\perp}of rotation. It is not difficult to find that the rotation of the vector actually occurs on the circumference. At this time, it is orthogonal tou\boldsymbol{u}There are no other available axes in the plane of the axis, for which construction is simultaneously orthogonal tou\boldsymbol{u}andv⊥\boldsymbol{v}_{\perp}axisw\boldsymbol{w},have

w=u×v⊥\boldsymbol{w}=\boldsymbol{u}\times\boldsymbol{v}_{\perp}

Depend on

∥w∥=∥u×v⊥∥=∥u∥⋅∥v⊥∥⋅sin⁡90∘=∥v⊥∥\left\lVert\boldsymbol{w}\right\rVert=\left\lVert\boldsymbol{u}\times\boldsymbol{v}_{\perp}\right\rVert=\left\lVert\boldsymbol{u}\right\rVert\cdot\left\lVert\boldsymbol{v}_{\perp}\right\rVert\cdot\sin{90^{\circ}}=\left\lVert\boldsymbol{v}_{\perp}\right\rVert

Knoww\boldsymbol{w}andv⊥\boldsymbol{v}_{\perp}are equal, so the vectorv⊥′\boldsymbol{v}_{\perp}'can be decomposed into parallelw\boldsymbol{w}ofvw′\boldsymbol{v}_{\boldsymbol{w}}'and parallel tov⊥\boldsymbol{v}_{\perp}ofvv′\boldsymbol{v}_{\boldsymbol{v}}',have

v⊥′=vw′+vv′=wsin⁡θ+v⊥cos⁡θ=(u×v⊥)sin⁡θ+v⊥cos⁡θ\boldsymbol{v}_{\perp}'=\boldsymbol{v}_{\boldsymbol{w}}'+\boldsymbol{v}_{\boldsymbol{v}}'=\boldsymbol{w}\sin{\theta}+\boldsymbol{v}_{\perp}\cos{\theta}=(\boldsymbol{u}\times\boldsymbol{v}_{\perp})\sin{\theta}+\boldsymbol{v}_{\perp}\cos{\theta}

so get

v′=v∥′+v⊥′=v∥+(u×v⊥)sin⁡θ+v⊥cos⁡θ=v∥+[u×(v−v∥)]sin⁡θ+v⊥cos⁡θ=v∥+(u×v)sin⁡θ+v⊥cos⁡θ=(u⋅v)u+(u×v)sin⁡θ+[v−v∥=v−(u⋅v)u]cos⁡θ=(u⋅v)u(1−cos⁡θ)+(u×v)sin⁡θ+vcos⁡θ\begin{align} \boldsymbol{v}'&=\boldsymbol{v}_{\parallel}'+\boldsymbol{v}_{\perp}'\nonumber\\ &=\boldsymbol{v}_{\parallel}+(\boldsymbol{u}\times\boldsymbol{v}_{\perp})\sin{\theta}+\boldsymbol{v}_{\perp}\cos{\theta}\nonumber\\ &=\boldsymbol{v}_{\parallel}+[\boldsymbol{u}\times(\boldsymbol{v}-\boldsymbol{v}_{\parallel})]\sin{\theta}+\boldsymbol{v}_{\perp}\cos{\theta}\nonumber\\ &=\boldsymbol{v}_{\parallel}+(\boldsymbol{u}\times\boldsymbol{v})\sin{\theta}+\boldsymbol{v}_{\perp}\cos{\theta}\nonumber\\ &=(\boldsymbol{u}\cdot\boldsymbol{v})\boldsymbol{u}+(\boldsymbol{u}\times\boldsymbol{v})\sin{\theta}+[\boldsymbol{v}-\boldsymbol{v}_{\parallel}=\boldsymbol{v}-(\boldsymbol{u}\cdot\boldsymbol{v})\boldsymbol{u}]\cos{\theta}\nonumber\\ &=(\boldsymbol{u}\cdot\boldsymbol{v})\boldsymbol{u}(1-\cos{\theta})+(\boldsymbol{u}\times\boldsymbol{v})\sin{\theta}+\boldsymbol{v}\cos{\theta}\nonumber \end{align}

When using axial angle to represent rotation, the fields right_rotationandleft_rotation are composite tags:

compoundxxx_rotation:left_rotation or right_rotation
  • floatangle:The angle of rotation around the axis, that is, the θ angle, in the angle system.
  • homolistaxis:An ordered array of three elements used to define the rotation axis vector uu. Generally it can be written as a unit vector.

Quaternion form ​

When using quaternion form to represent rotation, the fields right_rotationandleft_rotation types are lists, and the data format is:

homolistleft_rotation: or homolistright_rotation:: Represents the four elements of the quaternion, in order x, y, z, w.
  • float(list element)An element in a quaternion

All quaternions can be written in the following form:

q=w+xi+yj+zkq=w+x\boldsymbol{i}+y\boldsymbol{j}+z\boldsymbol{k}

inww、xx、yy、z∈Rz\in\mathbb{R},sayxi+yj+zkx\boldsymbol{i}+y\boldsymbol{j}+z\boldsymbol{k}is a quaternionqqThe imaginary part ofwwFor the real part. Generally, vectors can be usedq=(w,x,y,z)q=(w,x,y,z)to represent a quaternion, or to(x,y,z)(x,y,z)treated as a vectorv\boldsymbol{v}, representing quaternions in scalar and vector formq=(w,v)q=(w,\boldsymbol{v}). The modulus of a quaternion is∥q∥=w2+x2+y2+z2\left\lVert q\right\rVert=\sqrt{w^2+x^2+y^2+z^2}, stipulation: when∥q∥=1\left\lVert q\right\rVert=1, the quaternion is a unit quaternion. At the same time, there is also a provision: whenw=0w=0When , the quaternion can be called a pure quaternion.
For the rotation axis and vector in the axis-angle formula, it can be written in the form of pure quaternions, such asu=(0,u)u=(0,\boldsymbol{u})、v=(0,v)v=(0,\boldsymbol{v}). So there are:

v=v∥+v⊥v=v_{\parallel}+v_{\perp}

v′=v∥′+v⊥′v'=v_{\parallel}'+v_{\perp}'

v∥v_{\parallel}The rotation can be expressed as

v∥′=v∥v_{\parallel}'=v_{\parallel}

If(usin⁡θ+cos⁡θ)(u\sin{\theta}+\cos{\theta})treated as a quaternionqq,Right nowq=(cos⁡θ,usin⁡θ)q=(\cos{\theta},\boldsymbol{u}\sin{\theta}), then we can get

v⊥′=qv⊥v_{\perp}'=qv_{\perp}

Notice that the quaternion q above has the following properties:

∥q∥=cos⁡2θ+usin⁡θ⋅usin⁡θ=cos⁡2θ+∥u∥2sin⁡2θ=1\left\lVert q\right\rVert=\sqrt{\cos^2{\theta}+\boldsymbol{u}\sin{\theta}\cdot\boldsymbol{u}\sin{\theta}}=\sqrt{\cos^2{\theta}+\left\lVert\boldsymbol{u}\right\rVert^{2}\sin^2{\theta}}=1

This is a unit quaternion. Quaternions generally used for rotation transformation are unit quaternions. \emphasize{Non-unit quaternions will cause the model to be scaled while rotating}. So the vector rotation expressed in quaternion form is

v′=v∥′+v⊥′=v∥+qv⊥v'=v_{\parallel}'+v_{\perp}'=v_{\parallel}+qv_{\perp}

makeq=p2q=p^2,inp=(cos⁡θ2,usin⁡θ2)p=\left(\cos{\cfrac{\theta}{2}},\boldsymbol{u}\sin{\cfrac{\theta}{2}}\right),but

v′=v∥+qv⊥=pp∗v∥+p2v⊥=pv∥p∗+pv⊥p∗=p(v∥+v⊥)p∗=pvp∗\begin{align} v'&=v_{\parallel}+qv_{\perp}\nonumber\\ &=pp^{*}v_{\parallel}+p^{2}v_{\perp}\nonumber\\ &=pv_{\parallel}p^{*}+pv_{\perp}p^{*}\nonumber\\ &=p(v_{\parallel}+v_{\perp})p^{*}\nonumber\\ &=pvp^{*}\nonumber \end{align}

In the formula: p∗p^{*}——QuaternionsppThe conjugate ofp=(w,v)p=(w,\boldsymbol{v}),butp∗=(w,−v)p^{*}=(w,-\boldsymbol{v})。
As a result, the rotation formula expressed in quaternion form is obtained:

v′=qvq∗v'=qvq^{*}

inq=(cos⁡θ2,usin⁡θ2)q=\left(\cos{\cfrac{\theta}{2}},\boldsymbol{u}\sin{\cfrac{\theta}{2}}\right). Each element in this quaternion isw=cos⁡θ2w=\cos{\cfrac{\theta}{2}},x=uxsin⁡θ2x=u_{x}\sin{\cfrac{\theta}{2}},y=uysin⁡θ2y=u_{y}\sin{\cfrac{\theta}{2}},z=uzsin⁡θ2z=u_{z}\sin{\cfrac{\theta}{2}}
In the formula:
θ\theta——Around the axisu\boldsymbol{u}The angle of rotation, the direction is counterclockwise.
uiu_{i}——Rotation axisu\boldsymbol{u}on the coordinate axisiion the weight.
For a rendering transformation, let the quaternion used for its initial rotation beqrq_r, the quaternion used to rotate again isqlq_l, let the scaled datas=(sx,sy,sz)s=(s_x,s_y,s_z), shift datat=(tx,ty,tz)t=(t_x,t_y,t_z). Display any point on the entityA(x0,y0,z0)A(x_0,y_0,z_0)Construct quaternions

q0=x0i+y0j+z0k=(0,OA→)q_{0}=x_0\boldsymbol{i}+y_0\boldsymbol{j}+z_0\boldsymbol{k}=(0,\overrightarrow{OA})

Perform the first rotation and get

q1=qrq0qr∗q_{1}=q_{r}q_{0}q_{r}^{*}

Then applying the scaling transformation, we get

q2=sxq1xi+syq1yj+szq1zkq_{2}=s_{x}q_{1x}\boldsymbol{i}+s_{y}q_{1y}\boldsymbol{j}+s_{z}q_{1z}\boldsymbol{k}

Under the combined action of the initial rotation and scaling transformation, the relative position of each point in the model will change. Only when rotating the quaternion for the first timeqr=(1,0)q_r=(1,\boldsymbol{0})(no rotation occurs) or scale the datas=(1,1,1)s=(1,1,1)(without scaling), the model will not deform. After that, the model will determine the final rotation angle based on another rotation transformation, and we get

q3=qlq2ql∗q_{3}=q_{l}q_{2}q_{l}^{*}

Finally, a translation transformation is applied to determine the final position of the model to obtain the pointAAFinal position:

q=q3+tq=q_{3}+t

Application examples ​

Use block to display entity to display a glass. Requirement: Generate this display entity so that the diagonal line of the glass body is equal toyyThe axes are parallel. Rotate the display entity diagonally around the body, taking 4 seconds to rotate once.
The diagonal line of the body in the model starts fromO(0,0,0)O(0,0,0)arriveA(1,1,1)A(1,1,1), now we need to make the model transform without deformingOA→\overrightarrow{OA}transformed into(0,1,0)(0,1,0)(yyaxis direction vector) parallel. It is now possible to directly determine the quaternion used to rotate againqlq_l, the quantity to be determined is the rotation angleθ\thetaand axis of rotationu\boldsymbol{u}。
Calculate the angle of rotation: convertOA→\overrightarrow{OA}Unitized, we get(13,13,13)\left(\cfrac{1}{\sqrt{3}},\cfrac{1}{\sqrt{3}},\cfrac{1}{\sqrt{3}}\right),therefore

θ=arccos⁡[(13,13,13)⋅(0,1,0)]=arccos⁡13≈54.74∘\theta=\arccos{\left[\left(\frac{1}{\sqrt{3}},\frac{1}{\sqrt{3}},\frac{1}{\sqrt{3}}\right)\cdot(0,1,0)\right]}=\arccos{\frac{1}{\sqrt{3}}}\approx 54.74^{\circ}

The axis of rotation is perpendicular to the vector before and after rotation, we have

u=(13,13,13)×(0,1,0)=(−13,0,13)\boldsymbol{u}=\left(\frac{1}{\sqrt{3}},\frac{1}{\sqrt{3}},\frac{1}{\sqrt{3}}\right)\times (0,1,0)=\left(-\frac{1}{\sqrt{3}},0,\frac{1}{\sqrt{3}}\right)

convert it into units(−12,0,12)\left(-\cfrac{1}{\sqrt{2}},0,\cfrac{1}{\sqrt{2}}\right). If the axis-angle formula is used, the data of left_rotation is:

snbt
left_rotation:{angle: 54.74f, axis: [-0.71f, 0.0f, 0.71f]}

Compute rotated quaternion again

q=(cos⁡θ2,uxsin⁡θ2,uysin⁡θ2,uzsin⁡θ2)≈(0.89,−0.33,0,0.33)q=\left(\cos{\cfrac{\theta}{2}},u_{x}\sin{\cfrac{\theta}{2}},u_{y}\sin{\cfrac{\theta}{2}},u_{z}\sin{\cfrac{\theta}{2}}\right)\approx (0.89,-0.33,0,0.33)

The model does not require initial rotation, scaling and translation, soqr=(1,0,0,0)q_r=(1,0,0,0),s=(1,1,1)s=(1,1,1),t=(0,0,0)t=(0,0,0). The transformation field in decomposed form is:

snbt
transformation:{right_rotation: [0.0f, 0.0f, 0.0f, 1.0f], scale: [1.0f, 1.0f, 1.0f], left_rotation: [-0.33f, 0.0f, 0.33f, 0.89f], translation: [1.0f, 1.0f, 1.0f]}

The commands required to generate this display entity are:

mcfunction
summon block_display ~ ~ ~ {block_state:{Name:"minecraft:glass"},transformation:{right_rotation:[0.0f,0.0f,0.0f,1.0f],scale:[1.0f,1.0f,1.0f],left_rotation:[-0.33f,0.0f,0.33f,0.89f],translation:[1.0f,1.0f,1.0f]}}

The value of the field left_rotationhas been defined. The rotation animation is completed by interpolation and can be defined byright_rotation. The quantity to be determined is still the rotation angle.θ\thetaand axis of rotationu\boldsymbol{u}. Obviously the axis of rotation is the body diagonal of the block model. At this time, the body diagonal is equal toyyThe axes are parallel, but the transformation is still based on the local coordinate of the model, so the axis vector is(1,1,1)(1,1,1), unitized into(13,13,13)\left(\cfrac{1}{\sqrt{3}},\cfrac{1}{\sqrt{3}},\cfrac{1}{\sqrt{3}}\right). When making interpolation animations, you can set four fixed rotation angles:0∘0^{\circ}、90∘90^{\circ}、180∘180^{\circ}、270∘270^{\circ}, so that the model is cyclically transformed in this order, and the duration of each interpolation is4÷4=14\div 4=1Second=20=20gt。
byθ=90∘\theta=90^{\circ}For example, if the axis-angle formula is used, the data of right_rotation is

snbt
right_rotation: {angle: 90, axis: [0.58f, 0.58f, 0.58f]}

Convert to quaternion formq≈(0.71,0.41,0.41,0.41)q\approx (0.71,0.41,0.41,0.41),Right now

snbt
right_rotation:[0.41f,0.41f,0.41f,0.71f]

Same reasonθ=180∘\theta=180^{\circ}、θ=270∘\theta=270^{\circ}、θ=0∘\theta=0^{\circ}The data are respectively

snbt
right_rotation:[0.58f,0.58f,0.58f,0.0f]}
snbt
right_rotation:[0.41f,0.41f,0.41f,-0.71f]}
snbt
right_rotation:[0.0f,0.0f,0.0f,1.0f]}

In order to smoothly transition the rotation angle of the model toθ=90∘\theta=90^{\circ}, the command for interpolation animation is

mcfunction
data merge entity @n[type=block_display] {transformation:{right_rotation:[0.41f,0.41f,0.41f,0.71f]},interpolation_duration:20}

After the command is executed, apply the command block circuit or function plan so that after 20gt, when the defined interpolation animation ends, the rotation angle of the model begins to smoothly transition toθ=180∘\theta=180^{\circ}:

mcfunction
data merge entity @n[type=block_display] {transformation:{right_rotation:[0.58f,0.58f,0.58f,0.0f]},interpolation_duration:20}

After 20gt, a smooth transition begins toθ=270∘\theta=270^{\circ}:

mcfunction
data merge entity @n[type=block_display] {transformation:{right_rotation:[0.41f,0.41f,0.41f,-0.71f]},interpolation_duration:20}

After 20gt, a smooth transition begins toθ=0∘\theta=0^{\circ}:

mcfunction
data merge entity @n[type=block_display] {transformation:{right_rotation:[0.0f,0.0f,0.0f,1.0f]},interpolation_duration:20}

After 20gt, a smooth transition begins toθ=90∘\theta=90^{\circ}, forming a cycle. If the command is executed in the command block circuit, a clock circuit with a period of 80gt can be manufactured. There needs to be a delay of 20gt between each command block, and at least 5 repeaters need to be used. If the command is executed in the function, four functions 90.mcfunction, 180.mcfunction, 270.mcfunction, and 0.mcfunctioncan be created in the directorydata\minecraft\function\animation. For example, the content of function90.mcfunction could look like this:

mcfunction
data merge entity @n[type=block_display] {transformation:{right_rotation:[0.41f,0.41f,0.41f,0.71f]},interpolation_duration:20}
schedule function minecraft:animation/180 20t

References ​

[1] https://zh.minecraft.wiki/w/展示实体
[2] https://krasjet.github.io/quaternion/quaternion.pdf
[3] https://blog.csdn.net/YiYeZhiNian/article/details/106750302
[4] https://zhuanlan.zhihu.com/p/45404840
[5] https://zhuanlan.zhihu.com/p/183973440

Powered by VitePress and GitHub Pages