Matrices, Vectors, And 3d Math
In one word, Dual-Quaternion is a beautiful mathematical concept. It combines dual-number theory with quaternion mathematics. A quaternion provides a rotation representation about an axis, but it does not provide any translation representation. A Dual-Quaternion allows us to represent the rotation and translation of a vector into a single entity.
Matrices, Vectors, and 3d Math
Computer graphics requires more math than is covered here.The purpose of these notes is to expand on the mathematical appendix included with most graphics books, not to teach the mathematical material in the main text of those books.
Although primarily aimed at university computer science students, this tutorial is useful to any programmer interested in 3D computer graphics or 3D computer game programming. In spite of their appealing blood-and-gore covers, mass trade books on game programming require the same understanding of vectors and matrices as college text books (and usually defer these topics to the same skimpy mathematical appendix).
Of course we don't want to actually compute any trig functions. Given our unit vectors, we note that $\cos\theta=A\cdot B$, and $\sin\theta=A\times B$. Thus $$G=\beginpmatrixA\cdot B & -\A\times B\ & 0 \\\A\times B\ & A\cdot B & 0 \\0 & 0 & 1\endpmatrix.$$
Here is an elegant proof that does not depend too much on geometrical constructions but it requires simple linear ordinary differential equations for matrices, in addition to the cross product representation as a matrix operator.
Matrices are very powerful mathematical constructs that seem scary at first, but once you'll grow accustomed to them they'll prove extremely useful. When discussing matrices, we'll have to make a small dive into some mathematics and for the more mathematically inclined readers I'll post additional resources for further reading.
However, to fully understand transformations we first have to delve a bit deeper into vectors before discussing matrices. The focus of this chapter is to give you a basic mathematical background in topics we will require later on. If the subjects are difficult, try to understand them as much as you can and come back to this chapter later to review the concepts whenever you need them.
To retrieve the length/magnitude of a vector we use the Pythagoras theorem that you may remember from your math classes. A vector forms a triangle when you visualize its individual x and y component as two sides of a triangle:
Now that we've discussed almost all there is to vectors it is time to enter the matrix! A matrix is a rectangular array of numbers, symbols and/or mathematical expressions. Each individual item in a matrix is called an element of the matrix. An example of a 2x3 matrix is shown below: \[\beginbmatrix 1 & 2 & 3 \\ 4 & 5 & 6 \endbmatrix\] Matrices are indexed by (i,j) where i is the row and j is the column, that is why the above matrix is called a 2x3 matrix (3 columns and 2 rows, also known as the dimensions of the matrix). This is the opposite of what you're used to when indexing 2D graphs as (x,y). To retrieve the value 4 we would index it as (2,1) (second row, first column).
Matrices are basically nothing more than that, just rectangular arrays of mathematical expressions. They do have a very nice set of mathematical properties and just like vectors we can define several operations on matrices, namely: addition, subtraction and multiplication.
As you can see, matrix-matrix multiplication is quite a cumbersome process and very prone to errors (which is why we usually let computers do this) and this gets problematic real quick when the matrices become larger. If you're still thirsty for more and you're curious about some more of the mathematical properties of matrices I strongly suggest you take a look at these Khan Academy videos about matrices.
Now that we've explained all the theory behind transformations, it's time to see how we can actually use this knowledge to our advantage. OpenGL does not have any form of matrix or vector knowledge built in, so we have to define our own mathematics classes and functions. In this book we'd rather abstract from all the tiny mathematical details and simply use pre-made mathematics libraries. Luckily, there is an easy-to-use and tailored-for-OpenGL mathematics library called GLM.
Matrix-matrix multiplication is again done with operator*. Since vectors are a special case of matrices, they are implicitly handled there too, so matrix-vector product is really just a special case of matrix-matrix product, and so is vector-vector outer product. Thus, all these cases are handled by just two operators:
Games, especially game graphics, are made up of a lot of mathematics. The mathematics used in game graphics, physics, and so forth can become quite complex and advanced. Having a firm understanding of the different types of mathematics that are used in game development can allow you to have an easier time understanding and implementing the information.
In this article we review briefly the different types of mathematics that are common in game development. One could write an entire book on game math, so this article will serve as a quick review of the topics you should be familiar with coming into game development.
Vectors are the fundamental mathematical objects that are used in every 3D game and game engine. Vectors define a direction in virtual space, but they can also be used to define points called vertices (plural of vertex), orientations, or even surface normals, which are the directions surfaces face. For example, a triangle is made up of three of these points. In game related books the terms vertex and vector are often used synonymously to refer to a virtual point.
Turning a vector into a unit vector is known as normalization and is a highly common operation in computer graphics. Other very common operations are the dot product and cross product vector operations. The dot product of two vectors, also known as the scalar product, calculates the difference between the directions the two vectors are pointing. The dot product is used to calculate the cosine angle between vectors without using the cosine mathematical formula, which can be more CPU expensive, and the result is not another vector, but a scalar value.
The cross product of two vectors, also known as the vector product, is used to find a new vector that is perpendicular to two tangent vectors. This is commonly used to find the direction a polygon is facing by normalizing the cross product of the edge vectors of a triangle. The cross product is calculated by multiplying the cross components together and then adding them all together component by component. This is shown below.
Once an object in object-space is transformed to world-space, it is transformed to screen-space, which corresponds to the X and Y axes that are aligned to the screen. Since the 3D information has depth and distance with objects that are farther from the camera, a projection is applied to the geometry to add perspective to the data. The projection matrices that are used on geometry are called homogeneous clip space matrices, which clip the geometry to the boundaries of the screen to which they are being rendered. Two types of projection are generally used in video games: orthogonal projection and perspective projection.
A matrix is a mathematical structure that is used in computer graphics to store information about a space. In computer graphics matrices are often used for storing orientations, translations, scaling, coordinate spaces, and more. In game development we usually work with 3x3 and 4x4 matrices. A matrix is essentially a table, for example,
A matrix is a table that can be represented in code as a 2D array or as a set of vectors. Matrices with the same number of rows as columns are called square matrices. A vector can be considered a 1D array, whereas a matrix can be considered an array of vectors that together represent a space. For example, a 33 matrix can be created out of three 3D vectors, as follows:
There are several useful operations that can be performed on vectors. These operations include negating a vector, adding two vectors, subtracting two vectors, calculating the length (or magnitude) of a vector, calculating the distance between two vectors and normalizing a vector. Other operations on vectors that might not be immediately obvious are calculating the dot product between two vectors and calculating the cross-product between two vectors.
The magnitude of a vector, also called the length, or the norm of the vector is a scalar and is represented using the double vertical bars on either side of the vector variable (\(\\mathbfv\\)), not to be mistaken as the notation used to denote the absolute value of a scalar. The length of a vector is calculated using the following general algebraic rule:
We can further simplify the equation if we assume that both \(\mathbfa\) and \(\mathbfb\) are of unit-length (that is to say that the vectors are normalized). In such a case, the denominator becomes 1:
It is sometimes useful to know how much of one vector is parallel to another, and how much of a vector is perpendicular to another. The part of the vector \(\mathbfv\) that is parallel to vector \(\mathbfn\) is denoted \(\mathbfv_\parallel\) and the part that is perpendicular to \(\mathbfn\) is denoted \(\mathbfv_\perp\).
If we say the vectors \(\left(\mathbfi,\mathbfj,\mathbfk\right)\) are the vectors that form the orthogonal basis for our coordinate space, then the cross product satisfies the following identities:
It is also useful to note, that when switching from a left-handed coordinate system and a right-handed coordinate system, one of the axes is inverted (traditionally the z-axis is inverted). This means that if in a left-handed coordinate system the result of the cross product on the unit basis vectors \(\left(\mathbfx,\mathbfy,\mathbfz\right)\):
DirectXMath uses row-major matrices, row vectors, and pre-multiplication. Handedness is determined by which function version is used (RH vs. LH), otherwise the function works with either left-handed or right-handed view coordinates. 041b061a72