beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

38
Beginning Direct3D Game Programming: Mathematics 3 Vectors [email protected] Division of Digital Contents, DongSeo University. March 2016

Upload: jintaek-seo

Post on 13-Feb-2017

27 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Beginning Direct3D Game Programming:Mathematics 3

[email protected]

Division of Digital Contents, DongSeo University.March 2016

Page 2: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Vector Although more abstract definitions are possible, we usu-

ally restrict ourselves to vectors defined by n-tuples of real numbers, where n is typically 2, 3, or 4.

An n-dimensional vector V can be written as V=(v1,v2,…,vn)

2

Page 3: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Vector addition and scalar multiplication: a vector v (blue) is added to another vector w (red, upper illustration). Below, w is stretched by a factor of 2, yield-ing the sum v + 2w.

3

Page 4: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Vector space A vector space (also called a linear space) is a collec-

tion of objects called vectors, which may be added to-gether and multiplied ("scaled") by numbers, called scalars in this context. Scalars are often taken to be real numbers.

4

Page 5: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

First example: arrows in the plane The first example of a vector space consists of arrows in

a fixed plane, starting at one fixed point. This is used in physics to describe forces or velocities. Given any two such arrows, v and w, the parallelogram spanned by these two arrows contains one diagonal arrow that starts at the origin, too. This new arrow is called the sum of the two arrows and is denoted v + w.

5

Page 6: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Second example: ordered pairs of numbers A second key example of a vector space is provided by

pairs of real numbers x and y. (The order of the compo-nents x and y is significant, so such a pair is also called an ordered pair.)

Such a pair is written as (x, y). The sum of two such pairs: (x1, y1) + (x2, y2) = (x1 + x2, y1 + y2)

The multiplication of a pair: a(x, y) = (ax, ay).

6

Page 7: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Operations

7

Page 8: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Linear combination In mathematics, a linear combination is an expression

 constructed from a set of terms by multiplying each term by a constant and adding the results (e.g. a linear combination of x and y would be any expression of the form ax+ by, where a and b are constants).

8

Page 9: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Linear independence

Linearly independent vectors in R3.

9

Linearly dependent vectors in a plane in R3.

Page 10: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

In the theory of vector spaces the concept of linear de-pendence and linear independence of the vectors in a subset of the vector space is central to the definition of dimension.

A set of vectors is said to be linearly dependent if one of the vectors in the set can be defined as a linear combination of the other vectors. If no vector in the set can be written in this way, then the vectors are said to be linearly independent.

10

Page 11: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Basis(linear algebra) A set of vectors in a vector space V is called a basis, or

a set of basis vectors, if the vectors are linearly independent and every vector in the vector space is a linear combination of this set.

11

• The same vector can be represented in two dif -ferent bases (purple and red arrows).

Page 12: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

A vector v in R2 (blue) expressed in terms of different bases: using the standard basis of R2 v = xe1 + ye2 (black), and using a different, non-orthogonal basis: v = f1 + f2 (red).

12

Page 13: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Practice: Write a KVector class.

class KVector {public: KVector(float x=0, float y=0, float z=0); virtual ~KVector();

void Translate(float tx, float ty, float tz); void Scale(float sx, float sy, float sz); void RotateZ(float theta);

float m_x; float m_y; float m_z;};//class KVector

13

We assume the origin is located at the center of client area. Draw the x-axis with red line. Draw the y-axis with green line. Draw two vectors A(5,4), B(2,7) with black line. Draw A+B with blue line.

Page 14: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Length The magnitude of an n-dimensional vector V is a scalar

denoted by ||V|| and is given by the formula

The magnitude of a vector is also sometimes called the norm or the length of a vector. A vector having a mag-nitude of exactly one is said to have unit length, or may simply be called a unit vector.

When V represents a three-dimensional point or direc-tion, above equation can be written as:

14

Page 15: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Unit vector The normalized vector of a non-zero vector u is the

unit vector in the direction of u, i.e.,

Unit vectors may be used to represent the axes of a Cartesian coordinate system. For instance, the unit vec-tors in the direction of the x, y, and z axes of a three di-mensional Cartesian coordinate system are

15

Page 16: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Inner Product The dot product of two vectors, also known as the

scalar product or inner product, is one of the most heavily used operations in 3D graphics because it sup-plies a measure of the difference between the directions in which the two vectors point.

This definition states that the dot product of two vectors

is given by the sum of the products of each component. In three dimensions, we have

P·Q=PxQx+PyQy+PzQz

16

Page 17: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Given two n-dimensional vectors P and Q, the dot prod-uct P ⋅Q satisfies the equation

P·Q=||P|| ||Q||cos(α) where α is the planar angle between the lines connect-

ing the origin to the points represented by P and Q.

17

Page 18: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

The sign of the dot product tells us whether two vec-tors lie on the same side or on opposite sides of a plane.

18

Page 19: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

The situation often arises in which we need to decom-pose a vector P into components that are parallel and perpendicular to another vector Q.

19

Page 20: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Practice: Extend the KVector class.

20

Add Length() function. Add Normalize() function. Add Dot() function. Print the angle between A(5,4) and B(2,7).

class KVector {public:

float Length() const; void Normalize() KVector Dot(const KVector3& rhs_) const;

};//class KVector

Page 21: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Matrix In mathematics, a matrix (plural matrices) is a 

rectangular array of numbers, symbols, or expressions, arranged in rows and columns. The dimensions of below matrix are 2 × 3 (read "two by three"), because there are two rows and three columns.

21

Page 22: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Each element of a matrix is often denoted by a variable with two subscripts. For instance, a2,1 represents the el-ement at the second row and first column of a matrix A.

22

Page 23: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Size The size of a matrix is defined by the number of rows

and columns that it contains. A matrix with m rows and n columns is called an m × n matrix or m-by-n matrix, while m and n are called its dimensions.

23

Page 24: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Notation Matrices are commonly written in box brackets or 

parentheses:

The (1,3) entry of the following matrix A is 5 (also de-noted a13, a1,3, A[1,3] or A1,3):

24

Page 25: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Basic operations There are a number of basic operations that can be ap-

plied to modify matrices, called matrix addition, scalar multiplication, transposition, matrix multiplication and row operations.

25

Page 26: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Addition The sum A+B of two m-by-n matrices A and B is calcu-

lated entrywise: (A + B)i,j = Ai,j+ Bi,j, where 1 ≤ i ≤ m and 1 ≤ j ≤ n.

26

Page 27: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Transposition The transpose of an m-by-n matrix A is the n-by-m ma-

trix AT (also denoted Atr) formed by turning rows into columns and vice versa:

(AT)i,j = Aj,i.

27

Page 28: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Scalar multiplication The product cA of a number c (also called a scalar in the

parlance of abstract algebra) and a matrix A is com-puted by multiplying every entry of A by c:

(cA)i,j = c · Ai,j. This operation is called scalar multiplication, but its re-

sult is not named “scalar product” to avoid confusion, since “scalar product” is sometimes used as a synonym for “inner product”.

28

Page 29: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Determinant In linear algebra, the determinant is a useful value that

can be computed from the elements of a square matrix. The determinant of a matrix A is denoted det(A), det A, or |A|.

29

Page 30: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Cross Product The cross product of two three-dimensional vectors,

also known as the vector product, returns a new vec-tor that is perpendicular to both of the vectors being multiplied together.

30

• The cross-product in respect to a left-handed coordi-nate system.

Page 31: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Cross Product Used in a method for calculating a surface normal at a

particular point given two distinct tangent vectors. P×Q=(PyQz-PzQy, PzQx-PxQz, PxQy-PyQx)

31

• The cross-product in respect to a left-handed coordi-nate system.

Page 32: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

A commonly used tool for remembering this formula is to calculate cross products by evaluating the pseudo-determinant.

where i, j, and k are unit vectors parallel to the x, y, and z axes:

i=(1,0,0) j=(0,1,0) k=(0,0,1)

32

Page 33: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Cross ProductP×Q=(PyQz-PzQy, PzQx-PxQz, PxQy-PyQx)

33

Page 34: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Geometrix meaning

The magnitude of the cross product can be interpreted as the positive area of the parallelogram having a and b as sides:

34

Page 35: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Algebraic properties If the cross product of two vectors is the zero vector

(i.e. a × b = 0), then either one or both of the inputs is the zero vector, (a = 0 and/or b = 0) or else they are parallel or antiparallel (a ∥ b) so that the sine of the angle between them is zero (θ = 0° or θ = 180° and sinθ = 0).

The self cross product of a vector is the zero vector, i.e., a × a = 0.

The cross product is anticommutative, a×b=-(b×a) distributive over addition, a×(b+c)=(a×b)+(a×c) and compatible with scalar multiplication so that (ra)×b=a×(rb)=r(a×b)

35

Page 36: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

Relation with dot product The cross product and the dot product are related by:

36

Page 37: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

A rotating body In mathematics a rotating body is commonly repre-

sented by a vector along the axis of rotation. The length of the vector gives the speed of rotation and the direction of the axis gives the direction of rotation ac-cording to the right-hand rule.

37

Page 38: Beginning direct3d gameprogrammingmath03_vectors_20160328_jintaeks

References https://en.wikipedia.org/wiki/Vector_space

38