Math for Games - WordPress.comThe inverse of a matrix A is another matrix (denoted A– 1) that...
Transcript of Math for Games - WordPress.comThe inverse of a matrix A is another matrix (denoted A– 1) that...
Math for Gameswww.asyrani.com
Points and Vectors
Points and Cartesian Coordinates
a point is a location in n-dimensional space. (In games, n is usually equal to 2 or 3.)
The Cartesian coordinate system is by far themost common coordinate system employed by game programmers.
It uses two or three mutually perpendicular axes to specify a position in 2D or 3D space.
So a point P is represented by a pair or triple of real numbers, (Px , Py) or (Px , Py , Pz).
Other Coordinates system?
Cylindrical coordinates . This system employs a vertical “height” axis h, a radial axis r emanating out from the vertical, and a yaw angle theta (θ).
In cylindrical coordinates, a point P is represented by the triple of numbers (Ph , Pr , Pθ).
Spherical coordinates . This system employs a pitch angle phi (φ), a yaw angle theta (θ), and a radial measurement r.
Points are therefore representedby the triple of numbers (Pr , Pφ , Pθ).
In Cartesian Coordinates system
Left-Handed Right-Handed
Most Frequently Used
Left-Handed
Vectors
Remember!!! Vector is not a point. It is a direction shows between two points or more
A vector is a quantity that has both a magnitude and a direction in n-dimensional space.
A vector can be visualized as a directed line segment extending from a point called the tail to a point called the head.
2D Vector
A 3D vector can be represented by a triple of scalars (x, y, z)
If the coordinates of A and B are: A(x1, y1, z1) and B(x2, y2, z2) the coordinates or components of the vector is
𝐴𝐵
Vector are the coordinates of the head minus the coordinates of the tail.
Where,
𝐴𝐵 = x2 − x1, y2 − y1, z2 − z1𝑜𝑟𝐵𝐴 = x1 − x2, y1 − y2, z1 − z2
A
B
𝑨𝑩
A
B
𝑩𝑨
Example: Calculate the components of the vectors that can be drawn in the triangle with vertices A(−3, 4, 0), B(3, 6, 3) and C(−1, 2, 1).
Any point or vector can be expressed as a sum of scalars (real numbers) multiplied by these unit basis vectors.
For example,(5, 3, –2) = 5i + 3j – 2k.
Vector Operations
Multiplication of a vector a by a scalar s is accomplished by multiplying theindividual components of a by s:
sa = ( s𝒂𝑥 , s𝒂𝑦, s𝒂𝑧)
Multiplication by a scalar has the effect of scaling the magnitude of the vector, while leaving its direction unchanged
Non-uniform scale
a scaling vector s is really just a compact way to represent a 3 × 3 diagonal scaling matrix S
The scale factor can be different along each axis
𝑎𝑆 = 𝑎𝑥 𝑎𝑦 𝑎𝑧
𝑠𝑥 0 00 𝑠𝑦 0
0 0 𝑠𝑧
= 𝑠𝑥𝑎𝑥 𝑠𝑦𝑎𝑦 𝑠𝑧𝑎𝑧
Addition and Subtraction
𝑎 + 𝑏 = [ 𝑎𝑥 + 𝑏𝑥 , 𝑎𝑦 + 𝑏𝑦 , 𝑎𝑧 + 𝑏𝑧 ]
Addition
𝑎 + (−𝑏) = [ 𝑎𝑥 − 𝑏𝑥 , 𝑎𝑦 − 𝑏𝑦 , 𝑎𝑧 − 𝑏𝑧 ]
Subtraction (actually no subtraction in vector)
or
(−𝑎) + 𝑏 = [ −𝑎𝑥 + 𝑏𝑥 , −𝑎𝑦 + 𝑏𝑦 , −𝑎𝑧 + 𝑏𝑧 ]
Logically you can do this
direction + direction = direction
direction – direction = direction
point + direction = point
point – point = directionpoint + point = nonsense (don’t do it!)
Magnitude
The magnitude of a
vector is a scalar
representing the
length of the vector as
it would be measured in 2D or 3D space
How to calculate magnitude
𝑎 = 𝑎𝑥2 + 𝑎𝑦
2 + 𝑎𝑧2
Application of vectors
if we have the current position vector of an A.I. character P1, and a vector v representing her current velocity,
Then we can find her position on the next frame P2 by scaling the velocity vector by the frame time interval Δt, and then adding it to the current position.
𝑃2 = 𝑃1 + ∆𝑡 𝑣
Steps to calculate sphere-sphere intersection
Step 1: Get vector 𝐶1 and 𝐶2 based on origin
𝐶1 = (𝑪𝟏𝒙, 𝑪𝟏𝒚
, 𝑪𝟏𝒛)
𝐶2 = (𝑪𝟐𝒙, 𝑪𝟐𝒚
, 𝑪𝟐𝒛)
Steps to calculate sphere-sphere intersection
Step 2: Get 𝐶1𝐶2 (𝐶2 − 𝐶1)
𝐶2 − 𝐶1 = (𝑪𝟐𝒙− 𝑪𝟏𝒙
, 𝑪𝟐𝒚− 𝑪𝟏𝒚
, 𝑪𝟐𝒛− 𝑪𝟏𝒛
)
Steps to calculate sphere-sphere intersection
Step 3: d = 𝐶2 − 𝐶1
𝑑 = (𝑑𝑥 , 𝑑𝑦 , 𝑑𝑧)
where𝑑𝑥 = 𝑪𝟐𝒙 − 𝑪𝟏𝒙
𝑑𝑦 = 𝑪𝟐𝒚 − 𝑪𝟏𝒚
𝑑𝑧 = 𝑪𝟐𝒛 − 𝑪𝟏𝒛
Steps to calculate sphere-sphere intersection
Step 4: 𝑑 (magnitude) for the length
𝑑 = 𝑑𝑥2 + 𝑑𝑦
2 + 𝑑𝑧2
The magnitude of this vector d = |d| determines how far apart the spheres’ centers are.
If this distance is less than thesum of the spheres’ radii, they are intersecting; otherwise they’re not
Normalization and Unit Vectors
A unit vector is a vector with a magnitude (length) of one.
Given an arbitrary vector v of length v = , we can convert it to a unit vector u that points in the same direction as v, but has unit length.
To do this, we simply multiply v by the reciprocal of its magnitude. We call this normalization:
The formula
𝑢 =v
v=1
𝑣v
A vector is said to be normal to a surface if it is perpendicular to that surface.
Normal vectors are highly useful in games and computer graphics. For example, a plane can be defined by a point and a normal vector.
And in 3D graphics, lighting calculations make heavy use of normal vectors to define the direction of surfaces relative to the direction of the light rays impinging upon them.
Dot/Cross Product and Projection
Two Types
the dot product (a.k.a. scalar product or inner product), and
the cross product (a.k.a. vector product or outer product).
The dot product of two vectors yields a scalar; it is defined by adding the products of the individual components of the two vectors
a. b = 𝑎𝑥𝑏𝑥 + 𝑎𝑦𝑏𝑦 + 𝑎𝑧𝑏𝑧 = 𝑑
The dot product can also be written as the product of the magnitudes of the two vectors and the cosine of the angle between them:
a. b = a b cos 𝜃 = 𝑑
The dot product is commutative (i.e., the order of the two vectors can be reversed) and distributive over addition:
a. b = b. aa. b + c = a. b + a. c
And the dot product combines with scalar multiplication as follows:
𝐬a. b = a. 𝐬b = 𝐬(a. b)
Vector Projection
If u is a unit vector ( = 1), then the dot product (a ⋅ u) represents the length of the projection of vector a onto the infinite line defined by the direction of u
Magnitude as a Dot Product
The squared magnitude of a vector can be found by taking the dot product of that vector with itself.
Its magnitude is then easily found by taking the square root:
a 2 = a. aa = a. a
This works because the cosine of zero degrees is 1, so all that is left is
a a = a 2
Dot Product Test
Dot products are great for testing if two vectors are collinear or perpendicular, or whether they point in roughly the same or roughly opposite directions.
For any two arbitrary vectors a and b:
Collinear
Perpendicular
Collinear but opposite
Opposite DirectionSame direction
Cross Product
The cross product (also known as the outer product or vector product) of two vectorsyields another vector that is perpendicular to the two vectors being multiplied
a = 𝑎𝑥 , 𝑎𝑦, 𝑎𝑧b = (𝑏𝑥 , 𝑏𝑦, 𝑏𝑧)
a × b
= 𝑎𝑦 𝑏𝑧 − 𝑏𝑦 𝑎𝑧 𝑖
− 𝑎𝑥 𝑏𝑧 − 𝑏𝑥 𝑎𝑧 𝑗
+ 𝑎𝑥 𝑏𝑦 − 𝑏𝑥 𝑎𝑦 𝑘
Magnitude of the Cross Product
The magnitude of the cross product vector is the product of the magnitudes of the two vectors and the sine of the angle between them. (This is similar to the definition of the dot product, but it replaces the cosine with the sine.)
a × b = a b sin 𝜃 = 𝑑
The magnitude of the cross product is equal to the area of the parallelogram whose sides are a and b
Since a triangle is one-half of a parallelogram, the area of a triangle whose vertices are specified by the position vectors V1 , V2 , and V3 can be calculated as one-half of themagnitude of the cross product of any two of its sides
𝐴𝑡𝑟𝑖𝑎𝑛𝑔𝑙𝑒 =1
2(𝑉2−𝑉1)×(𝑉3−𝑉1)
Properties of Cross Product
a × b ≠ b × a
The cross product is not commutative
a × b = −b × a
However, it is anti-commutative
a × b + c = a × b + a × c
The cross product is distributive over addition:
𝐬a × b = a × 𝒔b = 𝐬 a × b
And it combines with scalar multiplication as follows:
i × j = − j × i = k,j × k = − k × j = i,k × i = − i × k = j.
The Cartesian basis vectors are related by cross products as follows:
Cross products are also used in physics simulations. When a force is applied to an object, it will give rise to rotational motion if and only if it is applied off -center.
This rotational force is known as a torque , and it is calculated as follows.
Given a force F, and a vector r from the center of mass to the point at which the force is applied, the torque N = r × F.
MATRICES
A matrix is a rectangular array of m × n scalars. Matrices are a convenient way of representing linear transformations such as translation, rotation, and scale.
M=
𝑀11 𝑀12 𝑀13
𝑀21 𝑀22 𝑀23
𝑀31 𝑀32 𝑀33
Matrix Multiplication
The product P of two matrices A and B is written P = AB.
If A and B are transformation matrices, then the product P is another transformation matrixthat performs both of the original transformations.
For example, if A is a scale matrix and B is a rotation, the matrix P would both scale and rotate the points or vectors to which it is applied.
Representing Points and Vectors as Matrices
Points and vectors can be represented as row matrices (1 × n) or column matrices (n × 1),
where n is the dimension of the space we’re working with (usually 2 or 3)
v = 3, 4, −1
Can be written as:
v1 = [3 4 − 1]
or
v2 =34−1
= v1T
Identity Matrix
The identity matrix is a matrix that, when multiplied by any other matrix, yields the very same matrix.
It is usually represented by the symbol I. Theidentity matrix is always a square matrix with 1’s along the diagonal and 0’s everywhere else
Matrix Inversion
The inverse of a matrix A is another matrix (denoted A–1) that undoes the effects of matrix A.
So, for example, if A rotates objects by 37 degrees about the z-axis, then A–1 will rotate by –37 degrees about the z-axis.
Likewise, if A scales objects to be twice their original size, then A–1 scales objects to be half-sized. When a matrix is multiplied by its own inverse, the result is always the identity matrix
𝑨(𝑨−1) ≡ (𝑨−1)𝑨 ≡ 𝑰
Not all matrices have inverses. However, all affine matrices (combinations of pure rotations, translations, scales, and shears) do have inverses.
Gaussian elimination or LU decomposition can be used to find the inverse, if one exists
How to find an inverse matrix?
Transposition
Homogeneous Coordinates
You may recall from high-school algebra that a 2 × 2 matrix can represent a rotation in two dimensions. To rotate a vector r through an angle of φ degrees (where positive rotations are counter-clockwise), we can write
𝑟𝑥′ 𝑟𝑦
′ = 𝑟𝑥 𝑟𝑦cos ∅ sin∅− sin∅ cos∅
It’s probably no surprise that rotations in three dimensions can be represented by a 3 ×3 matrix. The two-dimensional example above is really just a three dimensionalrotation about the z-axis, so we can write
𝑟𝑥′ 𝑟𝑦
′ 𝑟𝑧′ = 𝑟𝑥 𝑟𝑦 𝑟𝑧
cos∅ sin∅ 0−sin∅ cos∅ 0
0 0 1
The question naturally arises: Can a 3 × 3 matrix be used to represent translations? Sadly, the answer is no. The result of translating a point r by a translation t requires adding the components of t to the components of r individually:
r + t = (𝑟𝑥+t) (𝑟𝑦 + t) (𝑟𝑧 + t)
Coordinate Spaces
In physics, a set of coordinate axes represents a frame of reference, so we sometimes refer to a set of axes as a coordinate frame (or just a frame).
People in the game industry also use the term coordinate space (or simply space) to refer to a set of coordinate axes. In the following sections, we’ll look at a few of the most common coordinate spaces used in games and computer graphics.
Model Space
World Space
View space (also known as camera space)
Good Websites
http://www.codinglabs.net/article_world_view_projection_matrix