3d数学基础图形 教程

(You need Visual Studio to compile the project.)
(您需要Visual Studio来编译项目。)
介绍 (Introduction)
A voxel is just a point within a space, like an atom that composes the material, it's my own definition, for other engines, voxels are defined as volume of pixels, like in Minecraft/Roblox but I don't use this definition.
体素只是空间中的一个点,就像组成材料的原子一样,这是我自己的定义,对于其他引擎,体素定义为像素体积,就像在Minecraft / Roblox中一样,但我不使用此定义。
背景 (Background)
This article was possible with my wish (2010) to see a game with texture and object built only with voxel.
出于我的愿望(2010年),这篇文章是有可能看到的,这种游戏的纹理和对象仅由体素构建。
使用代码 (Using the Code)
First of all, we need to ask few questions before:
首先,我们需要先问几个问题:
- What is a space ? 什么是空间?
- What is an axis ? 什么是轴?
What is a dimension ?
什么是尺寸 ?
What is a plane ?
什么是飞机 ?
- What are trigonometric functions ? 什么是三角函数?
- What is a vector ? 什么是向量?
- What is a rotation matrix ? 什么是旋转矩阵?
- What is a rotation and translation ? 什么是旋转和平移?
Now let's give answers to these questions:
现在让我们为这些问题提供答案:
A space
is defined by points, mathematically localized by a number of axes
in unique directions that form the dimensions
of the space (a plane
is formed by 2 axes = 2D space).
一个space
是由点定义的,这些点在数学上被数个axes
沿唯一的方向定位,这些方向形成该空间的dimensions
(一个plane
由2个轴= 2D空间形成)。
Trigonometric functions
are functions such as sin(θ) and cos(θ) where θ represent the angle around an axis and draw a circle in a 2D plan.
Trigonometric functions
是诸如sin(θ)和cos(θ)之类的函数,其中θ表示围绕轴的角度并在2D平面中绘制一个圆。
A rotation matrix
is a set of equations made with trigonometric functions
.
rotation matrix
是由 trigonometric functions
一组方程 。
A vector
is a direction in a space (like for GPS), that is the result of the transformation (math operators) of a point by a rotation matrix
. The point is driven into the space by the rotation matrix, in order to reach the direction given by the angles inside trigo functions.
vector
是空间中的方向(类似于GPS),这是rotation matrix
对点进行变换(算术运算符)的结果。 该点由旋转矩阵驱动到空间中,以便到达trigo函数内部的角度所指定的方向。
A 1D space
is represented by a single line in one direction/axis
: ----------------------------
一维space
在一个方向/一个axis
由一条线表示:----------------------------
To move an object on this axis, the equations are:
要在此轴上移动对象,公式为:
x' = Dot.x + x To move right
x'=点x + x 向右移动
x' = Dot.x - x To move left
x'=点x-x 向左移动
-3 -2 -1 0 +1 +2 +3
X--|---|---|---|---|---|---|--
A translation
is done within a line, 1D space, so we have 1 translation
possible on X-axis.
在直线,一维空间内完成translation
,因此我们在X-axis.
可能有1个translation
X-axis.
A 2D space
is just a 1D space
with another line in a direction at 90° of the X-axis
where Y
represents it:
2D space
就是一维space
,在X-axis
90 °方向上有另一条线,其中Y
表示它:

To add an object in this 2D space
, we need to set 2 coordinates for each point of the object and to move the object, we apply an addition like we did before to translate
the object on x
-axis
.
要在此2D space
添加对象,我们需要为该对象的每个点设置2个坐标并移动该对象,就像在x
-axis
translate
该对象一样,我们像以前一样进行了加法运算。
But we can also rotate the object around the center of the 2 axis
, for doing that, an addition is not enough, we need to call math operators that is designed for rotation
calculations.
但是我们也可以绕2 axis
的中心旋转对象,为此,加法是不够的,我们需要调用专门用于rotation
计算的数学运算符。
A rotation
is done within a plane, 2D space
, so we have 1 rotation
possible around the center of the two axis
.
rotation
是在一个平面2D space
内完成的,因此我们可以绕两个axis
的中心进行1 rotation
。
These math operators are called, trigonometric functions: sin()
and cos()
that take the angle between the two lines: [center, object's point] and x-axis
for cos() and y-axis
for sin()
.
这些数学运算符称为三角函数: sin()
和cos()
,它们占据两条线之间的角度:[中心,对象的点], x-axis
表示cos(), y-axis
表示sin()
。
Visually, we have a point at 0°
(x = 1
, y = 0
) and we apply a rotation
of +90°
(+ is anticlockwise and - is clockwise) around the center, the result is x = 0
, y = 1
.
视觉上,我们有一个0 °
( x = 1
, y = 0
)的点,并且绕中心rotation
+90 °
(+为逆时针,-为顺时针),结果为x = 0
, y = 1
。
And the calculus to find this result is:
找到这个结果的演算是:
x' = x.cos(90) - y.sin(90)
y' = x.sin(90) + y.cos(90)
It's quite easy to retrieve this equation from scratch, it's what I did and surprisingly I have seen that this equation was the linear form of the Z rotation matrix.
从头开始检索该方程式非常容易,这就是我所做的,而且令人惊讶的是,我已经看到该方程式是Z旋转矩阵的线性形式。
Here's how I did it:
这是我的做法:
We have a 2D XY circle with a diameter of 8.
我们有一个直径为8的2D XY圆。
- We set 1 point, rotate it and try to retrieve the equation of its new locations, we will find the final equation once we have operate on a couple of different XY combinations. 我们设置1点,旋转它并尝试检索其新位置的方程式,一旦我们对几个不同的XY组合进行运算,我们将找到最终方程式。
Let's begin with P(4, 0), a red point:
让我们从一个红点P(4,0)开始:
Now we rotate this point of +90° (θ) and try to find the equation of its new location, visually the result to find is P(0, 4):
现在我们将这个点旋转+ 90°(θ)并尝试找到其新位置的方程,在视觉上找到的结果为P(0,4):
X = sin(θ) = 1 ❌ X = cos(θ) = 0 ✔ Y = cos(θ) = 0 ❌ Y = sin(θ) = 1 ❌✔ Y = 4 × sin(θ) = 4 ✔
So for P(4, 0) and θ = 90, we have:
因此,对于P(4,0)和θ= 90,我们有:
X = cos(θ) Y = 4 × sin(θ)
Try with any other angles for P(4, 0) and different X value with Y at zero (use mathsisfun for that), you will find the same equations, with 4× for X, but it's not alterate the final equation:
尝试将P( 4,0 )的任何其他角度和Y都为零的不同X值(使用mathsisfun)进行尝试 ,您会发现相同的方程式,其中X的角度为4×,但不会改变最终方程式:
X' = 4 × cos(θ) = X × cos(θ) Y' = 4 × sin(θ) = X × sin(θ)
Now we set the red point at P(0, 4):
现在我们将红点设置为P(0,4):
We rotate this point of +90°, visually the result is P(-4, 0):
我们将此点旋转+ 90°,在视觉上结果为P(-4,0):
X = cos(θ) = 0 ❌ X = sin(θ) = 1 ❌✔ X = 4 × -sin(θ) = -4 ✔ Y = sin(θ) = 1 ❌ Y = cos(θ) = 0 ✔
So for P(0, 4) and θ = 90, we have:
因此,对于P(0,4)和θ= 90,我们有:
X = 4 × -sin(θ) Y = cos(θ)
Try with any other angles for P(0, 4) and different Y values with X at zero , you will find the same equations, with 4 × for Y, but it's not alterate the final equation:
尝试使用P(0,4)的任何其他角度以及X值 为零的 不同Y值 ,您会发现相同的方程式,其中 Y的角度为 4×,但不会改变最终方程式:
X = 4 × -sin(θ) = Y × -sin(θ) Y = 4 × cos(θ) = Y × cos(θ)
Now let's summarize:
现在让我们总结一下:
_____ ________________ ________________ | | | | | | P(X, 0) | P(0, Y) | |_____|________________|________________| | | | | | θ | X = X × cos(θ) | X = Y × -sin(θ)| | | Y = X × sin(θ) | Y = Y × cos(θ)| |_____|________________|________________|
As we can see, different XY combinations generate different equations, so let's work on X and Y set above 0:
如我们所见,不同的XY组合生成不同的方程式,因此让我们对设置为0以上的X和Y进行操作:
Hence we set the red point at P(2, 4×√3/2):
因此,我们将红点设置为P(2,4×√3/ 2):
Now we rotate this point of +30°, visually the result to find is P(0, 4):
现在我们将这个点旋转+ 30°,在视觉上找到的结果是P(0,4):
X = sin(θ) = 0.5 ❌ X = cos(θ) = √3/2 ❌ Y = sin(θ) = 0.5 ❌ Y = cos(θ) = √3/2 ❌
We have a problem, none of the trigonometric functions work, so we need to find the solution away while still working with
sin()
andcos()
.我们有一个问题,三角函数都不起作用,因此我们需要在仍然使用
sin()
和cos()
找到解决方案。Let's review the previous equations found:
让我们回顾一下之前找到的等式:
_____ ________________ ________________ | | | | | | P(X, 0) | P(0, Y) | |_____|________________|________________| | | | | | θ | X = X × cos(θ) | X = Y × -sin(θ)| | | Y = X × sin(θ) | Y = Y × cos(θ)| |_____|________________|________________|
These are strange equations that we have, what if we have P(X, Y), it should be a mix of the both equations:
这些是我们有的奇怪方程,如果我们有P(X,Y),应该将这两个方程混合在一起:
X' = X × cos(θ) X = Y × -sin(θ) Y' = X × sin(θ) Y = Y × cos(θ) X' = X × cos(θ) Y × -sin(θ) Y' = X × sin(θ) Y × cos(θ)
Let's see if an addition is working:
让我们看看添加是否有效:
X' = X × cos(θ) + Y × -sin(θ) X × cos(θ) - Y × sin(θ) Y' = X × sin(θ) + Y × cos(θ) X = 2 × cos(θ) - 4×√3/2 × sin(θ) = 0 ✔ Y = 2 × sin(θ) + 4×√3/2 × cos(θ) = 4 ✔
Perfect, this equation is working, so at the end, the final equations on a XY plane is:
完美,此方程式有效,因此最后,XY平面上的最终方程式为:
X' = X × cos(θ) - Y × sin(θ) Y' = X × sin(θ) + Y × cos(θ)
But let's remove the doubt about P(X, 0) and P(0, Y):
但是,让我们消除对P(X,0)和P(0,Y)的疑问:
For P(4, 0), we apply a rotation of +90°, visually the result to find is P(0, 4):
对于P(4,0),我们应用+ 90°旋转,在视觉上找到的结果为P(0,4):
X' = 4 × cos(θ) - 0 × sin(θ) = 0 ✔ Y' = 4 × sin(θ) + 0 × cos(θ) = 4 ✔
For P(0, 4), we apply a rotation of +90°, visually the result to find is P(-4, 0):
对于P(0,4),我们应用+ 90°旋转,在视觉上找到的结果是P(-4,0):
X' = 0 × cos(θ) - 4 × sin(θ) = -4 ✔ Y' = 0 × sin(θ) + 4 × cos(θ) = 0 ✔
Good, all is working, you can test this equation with your own XY and angle values to verifiy if it's working.
很好,一切正常,您可以使用自己的XY和角度值测试此方程式,以验证其是否正常运行。
This equation is also surprisingly (as said earlier) just a linear form of the Z rotation (XZ plane rotation matrix)
vector
, whereZ
is the center of the bothaxis
)该方程式还令人惊讶地(如前所述)只是Z旋转的线性形式(XZ平面旋转矩阵)
vector
的点,其中Z
是两个axis
的中心)Rotation matrix on z: Vector: _______________ _______________ _______________ _______________ | | | | | | | cos(θz) | -sin(θz) | 0 | | x | |_______________|_______________|_______________| |_______________| | | | | | | | sin(θz) | cos(θz) | 0 | × | y | |_______________|_______________|_______________| |_______________| | | | | | | | 0 | 0 | 1 | | z | |_______________|_______________|_______________| |_______________|
And a 3D
space
is 2Dspace
with another axis calledZ
and at90°
of theXY
plan.3D
space
是2Dspace
其中另一个轴称为Z
且与XY
平面成90°
。The moves possible are
translation
(addition equation) androtation
(sin
andcos
).可能的移动是
translation
(加法方程)和rotation
(sin
和cos
)。But now
rotations
are in number of 3, aroundZ
, aroundX
and aroundY
.但是现在
rotations
数为3,围绕Z
,围绕X
和围绕Y
Because as we said before, a rotation is made on a 2D plane, but now we have a mix of 3x 2D
space/plane
:因为如前所述,旋转是在2D平面上进行的,但是现在我们混合使用3x 2D
space/plane
:X/Y
,Y/Z
andZ/X
.X/Y
,Y/Z
和Z/X
So we need to gather all the
rotation
matrices and use them.因此,我们需要收集所有
rotation
矩阵并使用它们。
X旋转矩阵(XY平面旋转矩阵) (X Rotation Matrix (XY Plane Rotation Matrix))
_______________ _______________ _______________
| | | |
| 1 | 0 | 0 |
|_______________|_______________|_______________|
| | | |
| 0 | cos(θx) | -sin(θx) |
|_______________|_______________|_______________|
| | | |
| 0 | sin(θx) | cos(θx) |
|_______________|_______________|_______________|
Y旋转矩阵(YZ平面旋转矩阵) (Y Rotation Matrix (YZ Plane Rotation Matrix))
_______________ _______________ _______________
| | | |
| cos(θy) | 0 | -sin(θy) |
|_______________|_______________|_______________|
| | | |
| 0 | 1 | 0 |
|_______________|_______________|_______________|
| | | |
| sin(θy) | 0 | cos(θy) |
|_______________|_______________|_______________|
Z旋转矩阵(XZ平面旋转矩阵) (Z Rotation Matrix (XZ Plane Rotation Matrix))
_______________ _______________ _______________
| | | |
| cos(θz) | -sin(θz) | 0 |
|_______________|_______________|_______________|
| | | |
| sin(θz) | cos(θz) | 0 |
|_______________|_______________|_______________|
| | | |
| 0 | 0 | 1 |
|_______________|_______________|_______________|
But we have a problem, if we are doing that, we will not be able to rotate an object around XYZ
axis
together, it's one rotation
on one axis
only with other angles set as "zero" because they are not present in the matrix chosen.
但是我们有一个问题,如果这样做,我们将无法一起围绕XYZ
axis
旋转对象,因为仅在其他角度设为“零”的情况下,它只能在一个axis
rotation
一次,因为它们不在所选矩阵中。
To fix that, we need to form 1 single matrix by multiplying XYZ
matrices together (mul's order matter).
为了解决这个问题,我们需要通过将XYZ
矩阵相乘形成1个单个矩阵(mul的顺序问题)。
XYZ旋转矩阵(XY YZ XZ平面旋转矩阵) (XYZ Rotation Matrix (XY YZ XZ Planes Rotation Matrix))
______________________________ ______________________________ ____________________
| | | |
| cos(θy) × cos(θz) | cos(θy) × -sin(θz) | -sin(θy) |
|______________________________|______________________________|____________________|
| | | |
| -sin(θx) × sin(θy) × cos(θz) | sin(θx) × sin(θy) × sin(θz) | -sin(θx) × cos(θy) |
| + cos(θx) × sin(θz) | + cos(θx) × cos(θz) | |
|______________________________|______________________________|____________________|
| | | |
| cos(θx) × sin(θy) × cos(θz) | cos(θx) × sin(θy) × -sin(θz) | cos(θx) × cos(θy) |
| + sin(θx) × sin(θz) | + sin(θx) × cos(θz) | |
|______________________________|______________________________|____________________|
Now, we just need to multiply this rotation matrix
(based on object's angles) to the vector
point of the object to be able to rotate
the object around XYZ
axes
.
现在,我们只需要将此rotation matrix
(基于对象的角度)乘以对象的vector
点即可围绕XYZ
axes
rotate
对象。
Obj Vec: Rotation Matrix:
_____ ______________________________ ______________________________ ____________________
| | | | | |
| x | | cos(θy) × cos(θz) | cos(θy) × -sin(θz) | -sin(θy) |
|_____| |______________________________|______________________________|____________________|
| | | | | |
| y | × | -sin(θx) × sin(θy) × cos(θz) | sin(θx) × sin(θy) × sin(θz) | -sin(θx) × cos(θy) |
| | | + cos(θx) × sin(θz) | + cos(θx) × cos(θz) | |
|_____| |______________________________|______________________________|____________________|
| | | | | |
| z | | cos(θx) × sin(θy) × cos(θz) | cos(θx) × sin(θy) × -sin(θz) | cos(θx) × cos(θy) |
| | | + sin(θx) × sin(θz) | + sin(θx) × cos(θz) | |
|_____| |______________________________|______________________________|____________________|
Now it's almost finished, we need to create a camera to be able to navigate into the scene.
现在快要完成了,我们需要创建一个摄像机以能够导航到场景中。
The camera is defined by 3 vectors
: Forward
, Right
and Up
:
摄像机由3个vectors
定义: Forward
, Right
和Up
:
X Y Z
Right/Left (1, 0, 0)
Up/Down (0, 1, 0)
Forward/Backward (0, 0,-1)
Then, we multiply these 3 vectors
to a rotation matrix
(based on camera's angles) separately because we need each of them to be able to move the camera Forward/Backward/Right/Left/Up/Down
.
然后,将这三个vectors
乘以一个rotation matrix
(基于摄像机的角度),因为我们需要它们中的每一个都能够使摄像机Forward/Backward/Right/Left/Up/Down
。
And, adding the result to the object vector.
并且,将结果添加到对象向量。
Example: If press W, Forward vector
is used If press S, -Forward vector
is used If press D, Right vector
is used ...
示例 :如果按W,则使用Forward vector
如果按S, - Forward vector
,如果按D, Right vector
Right vector
Right vector
使用 ...
Object Vector: Forward Vector:
__________________ __________________
| | | |
| x | | x |
|__________________| |__________________|
| | | |
| y | + | y |
|__________________| |__________________|
| | | |
| z | | z |
|__________________| |__________________|
Object Vector: Forward Vector:
__________________ __________________
| | | |
| x | | x |
|__________________| |__________________|
| | | |
| y | - | y |
|__________________| |__________________|
| | | |
| z | | z |
|__________________| |__________________|
Object Vector: Right Vector:
__________________ __________________
| | | |
| x | | x |
|__________________| |__________________|
| | | |
| y | + | y |
|__________________| |__________________|
| | | |
| z | | z |
|__________________| |__________________|
Finally, we multiply another rotation matrix
(based on camera's angles) with each vector
point from the object to be able to move the object depending on the camera angle (modified with the mouse movement).
最后,我们将另一个rotation matrix
(基于相机的角度)乘以对象的每个vector
点,从而能够根据相机的角度(随鼠标移动而修改)移动对象。
That's all for now, I hope I will give updates often because this code is not perfect, it has some bugs and does not have all the features of 3D math.
到此为止,我希望我会经常进行更新,因为该代码并不完美,存在一些错误并且不具备3D数学的所有功能。
翻译自: https://www.codeproject.com/Articles/1247960/Learning-Basic-Math-Used-In-3D-Graphics-Engines
3d数学基础图形 教程