学习游戏编程(第 2 部分)(数学) [英] Learning game programming (part 2) (math)

查看:31
本文介绍了学习游戏编程(第 2 部分)(数学)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,自从我写这个问题以来已经有几个月了,从那时起我就玩起了使用原始"C++ D3D、Ogre 和 Irrlicht 图形引擎以及最近的 Microsoft XNA.我制作了一些 2D 游戏(主要是俄罗斯方块、星形等老东西的复制品),并通过上述技术向 3D 世界迈出了一些(非常)小的步骤.

So, it's been a few months since I wrote this question, since then I've toyed with "raw" C++ D3D, The Ogre and Irrlicht graphics engines and lately Microsoft XNA. I've built a few 2D games (mostly replicas of old stuff like tetris, astreoids, etc.) and made some (very) small steps into the 3D world in the above mentioned technologies.

我在创建实际游戏逻辑、抽象对象交互以允许我插入不同形式的控制(计算机、播放器、网络等)、执行线程或任何其他我需要的东西时几乎没有问题习惯了我的日常工作 - 这对我来说感觉非常自然.我很少涉及 HLSL 和粒子效果(非常非常基础).

I have little to no trouble creating the actual game logic, abstracting away object interactions to allow me to plug in different forms of control (computer, player, over network. etc.), doing threading or any of the other stuff I'm used to from my day to day work - which feels perfectly natural to me. I messed around very little with HLSL and particle effects (very very basic).

但是涉及矩阵和向量的 3D 数学(以及 Ogre3D 中的四元数(?),这些真的需要吗?)...真的让我明白,我可以学习例子(例如,我从 O'Reilly 购买的 Learning XNA 3.0 书,这是一本很棒的书顺便说一句)并且我理解为什么如何在示例中发生某些事情,但是当我尝试自己做一些事情,我觉得我缺乏对这种数学类型的理解,无法真正自己掌握它并使其发挥作用.

But 3D math involving Matrices and Vectors (and Quaternions(?) in Ogre3D, are these really needed?)... really gets me, I can follow examples (e.g. the Learning XNA 3.0 book I bought from O'Reilly, which is an awesome book btw) and I understand why and how something happens in the example, but when I try to do something myself I feel that I'm lacking the understanding of this type of math to be able to really get it and make it work by myself.

所以我正在寻找有关学习 3D 数学(主要是)和一些着色器/粒子效果书籍的资源.我更喜欢具有教学意义的资源,并且在诸如向量数学的博士论文之类的东西上放慢速度,这将方式超出我的脑海.理想的资源应该是在 D3D 中展示这一切的东西.

So I'm looking for resources on learning 3D math (mostly) and some Shader/Particle Effects books. I would prefer resources that are pedagogic and take it slow above something like a doctors thesis on vector math which will be way over my head. The ideal resource would be something that demonstrates it all in D3D.

推荐答案

好的,矩阵/向量计算的快速课程:

Ok, a quick course in Matrix/Vector calculation:

矩阵是按矩形网格排列的数字集合,例如:

A matrix is a collection of numbers ordered in a rectangular grid like:

[ 0,  1,  2 ]
[ 2,  3,  5 ]
[ 2,  1,  3 ]
[ 0,  0,  1 ]

上述矩阵有 4 行 3 列,因此是一个 4 x 3 矩阵.向量是具有 1 行(行向量)或 1 列(列向量)的矩阵.正常数被称为标量以与矩阵形成对比.

The above matrix has 4 rows and 3 columns and as such is a 4 x 3 matrix. A vector is a matrix with 1 row (a row vector) or 1 column (a column vector). Normal numbers are called scalars to contrast with matrices.

矩阵使用大写字母,标量使用小写字母也很常见.

It is also common to use capital letters for matrices and lowercase letters for scalars.

我们可以用矩阵做基本的计算,但有一些条件.

We can do basic calculation with matrices but there are some conditions.

添加

如果矩阵具有相同的维度,则可以添加它们.所以 2x2 矩阵可以添加到 2x2 矩阵,但不能添加到 3x5 矩阵.

Matrices can be added if they have the same dimensions. So a 2x2 matrix can be added to a 2x2 matrix but not to a 3x5 matrix.

[ 1,  2 ] + [ 2,  5 ] = [ 3,  7 ]
[ 2,  4 ]   [ 0,  3 ]   [ 2,  7 ]

您会看到,通过相加,每个单元格中的每个数字都与另一个矩阵中相同位置的数字相加.

You see that by addition each number at each cell is added to the number on the same position in the other matrix.

矩阵乘法

矩阵可以相乘,但这有点复杂.为了将矩阵 A 与矩阵 B 相乘,您需要将矩阵 A 的每一行中的数字与矩阵 B 中的每一列相乘. 这意味着如果将 axb 矩阵与 acxd 矩阵相乘,b 和 c 必须相等,并且结果矩阵是 axd:

Matrices can be multiplied, but this is a bit more complex. In order to multiply matrix A with matrix B, you need to multiply the numbers in each row if matrix A with each column in matrix B. This means that if you multiply an a x b matrix with a c x d matrix, b and c must be equal and the resulting matrix is a x d:

[1,2,3] x [4,6] = [1x4+2x2+3x2, 1x6+2x1+3x3 ] = [4+4+6,  6+2+9  ] = [14, 20]
[1,4,5]   [2,1]   [1x4+4x2+5x2, 1x6+4x1+5x3 ]   [4+8+10, 6+4+15 ]   [22, 25]
          [2,3] 

如您所见,对于矩阵,A x B 与 B x A 不同.

As you can see, with matrixes, A x B differs from B x A.

矩阵标量乘法

您可以将矩阵与标量相乘.在这种情况下,每个单元格都乘以该数字:

You can multiply a matrix with a scalar. In that case, each cell is multiplied with that number:

3 x [1,2] = [ 3, 6]
    [4,7]   [12,21]

矩阵求逆矩阵除法是不可能的,但您可以创建矩阵的求逆,使得 A x A-inv 是一个除主对角线外全为零的矩阵:

Inverting a matrix Matrix division is not possible, but you can create an inversion of a matrix such that A x A-inv is a matrix with all zero's except for that main diagonal:

[ 1, 0, 0 ]
[ 0, 1, 0 ]
[ 0, 0, 1 ]

矩阵求逆只能用方阵完成,这是一项复杂的工作,不需要有结果.

Inverting a matrix can only be done with square matrices and it is a complex job that does not neccesary have a result.

从矩阵 A 开始:

    [ 1, 2, 3 ]
A = [ 1, 3, 4 ]
    [ 2, 5, 1 ]

我们添加了 3 个额外的列并用单位矩阵填充它们:

We add 3 extra columns and fill them with the unit matrix:

[ 1, 2, 3, 1, 0, 0 ]
[ 1, 3, 4, 0, 1, 0 ]
[ 2, 5, 1, 0, 0, 1 ]

现在我们从第一列开始.我们需要从每一行中减去第一行,这样第一列除了第一行之外只包含零.为了做到这一点,我们从第二行中减去第一行一次,从第三行中减去两次:

Now we start with the first column. We need to subtract the first row from each other row such that the first column contains only zeroes except for the first row. In order to do that we subtract the first row once from the second and twice from the third:

[ 1, 2, 3, 1, 0, 0 ]
[ 0, 1, 1,-1, 1, 0 ]
[ 0, 1,-5,-2, 0, 1 ]

现在我们对第二列重复此操作(第一行两次,第三行一次)

Now we repeat this with the second column (twice from the first row and once from the third)

[ 1, 0, 1, 3,-2, 0 ]
[ 0, 1, 1,-1, 1, 0 ]
[ 0, 0,-6,-1,-1, 1 ]

对于第三列,我们有一个小问题.主元数是 -6 而不是 1.但是我们可以通过将整行乘以 -1/6 来解决这个问题:

For the third column, we have a slight problem. The pivot number is -6 and not 1. But we can solve this by multiplying the entire row with -1/6:

[ 1, 0, 1,   3,  -2,    0 ]
[ 0, 1, 1,  -1,   1,    0 ]
[ 0, 0, 1, 1/6, 1/6, -1/6 ]

现在我们可以从第一行和第二行中减去第三行:

And now we can subtract the third row from the first and the second:

[ 1, 0, 0, 17/6,-13/6,  1/6 ]
[ 0, 1, 0, -7/6,  5/6,  1/6 ]
[ 0, 0, 1,  1/6,  1/6, -1/6 ]

好的,现在我们有了 A 的逆:

Ok now we have the inverse of A:

[ 17/6,-13/6,  1/6 ]
[ -7/6,  5/6,  1/6 ]
[  1/6,  1/6, -1/6 ]

我们可以这样写:

      [ 17,-13,  1 ]
1/6 * [ -7,  5,  1 ]
      [  1,  1, -1 ]



    [ 1, 2, 3 ]   [ 17,-13,  1 ]                [ 6, 0, 0 ]    [ 1, 0, 0 ]
A = [ 1, 3, 4 ] x [ -7,  5,  1 ] x 1/6  = 1/6 x [ 0, 6, 0 ] =  [ 0, 1, 0 ]
    [ 2, 5, 1 ]   [  1,  1, -1 ]                [ 0, 0, 6 ]    [ 0, 0, 1 ]

希望这会有所帮助.

这篇关于学习游戏编程(第 2 部分)(数学)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆