36 views

Uploaded by Dean Parker

- 01 Vectors
- MatlabIntro_3
- Vectors in Two & Three Dimensions
- Section 2
- MIT8 02SC Notes
- vectors.ppt
- Contour Analysis for Image Recognition in C
- vectori
- On the Extraction of Curve Skeletons Using Gradient Vector Flow
- Vectors Imp Points
- Vector Calculus
- Dot Product Lecture
- Notes Important Questions Answers 12th Math Chapter 7 Exercise 7.2
- Blas1 z Test Output
- Motivating the Dot and Cross Products
- Spyrou 1
- Hw Chapter 27 Giancoli physics solutions
- m1 mod3(1)
- Hw1 Sol Amcs202
- lag9

You are on page 1of 16

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Helping you build your dream game engine.

Posted on February 4, 2011 by Jeremiah van Oosten

In this article I would like to discuss operations on vectors. This article assumes the reader has a basic knowledge of what vectors are and how they are represented. My goal here is simply to refresh your memory about what kind of operations can be performed on vectors specifically operations that are important if you are trying to create a 3D game engine.

Table of Contents Conventions Vector Operations Vector Negation Vector Addition and Subtraction

3dgep.com/?p=359 1/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Vector Multiplication by a Scalar Magnitude of a Vector Normalizing Vectors Distance Between two Points Vector Dot Product Vector Projection Vector Cross Product References

Conventions Throughout this article, I will use a convention when referring to vectors, scalars, and matrices. 1. Scalars are represented by lower-case italic characters ( 2. Vectors are represented by lower-case bold characters ( 3. Matrices are represented by upper-case bold characters ( Vector Operations 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. Vector Negation To negate a vector, we simply negate ervery component of the vector. Negating a vector, results in a vector of the same magnitude, but opposite in direction. A few examples of vector negation are: ) ) ).

Vector Addition and Subtraction We can perform vector addition and vector subtraction on two vectors of the same dimension. To perform vector addition and subtraction, we simply add or subtract each component of the first vector with the matching component of the second vector.

3dgep.com/?p=359

2/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

, and vector

. Visually,

is equivalent to , that is

vector. Vector subtraction is equivalent to addition of the negative of vector . Visually, we would negate vector way the tail of at the tail of vector placed at the head of

Personally, I have trouble remembering what order I have to subtract vectors in order to get the vector in the correct direction. I use the following mnemonic to help me remember: The vector from a to b is b minus a. The following images show examples of adding and subtracting vectors:

3dgep.com/?p=359

3/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Vector Multiplication by a Scalar Vectors can also be multiplied by a scalar. Scalar division is also supported, but this is equivalent to multiplying the vector by the reciprocal of the scalar. Vector multiplication by a scalar is show below:

Multiplying a vector by a positive scalar does not change the direction of the vector, it only changes the magnitude of the vector. Multiplying by a negative number will inverse the direction of the vector and scale the magnitude. Multiplying a vector by negating the vector. is equivalent to

Magnitude of a Vector 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 ( is calculated using the following general algebraic rule: ), not to be

mistaken as the notation used to denote the absolute value of a scalar. The length of a vector

Normalizing Vectors It is sometimes useful to express vectors only by their direction and not by their length. This is necessary for example when we want to find out if another object, or point is in front of or

3dgep.com/?p=359 4/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

behind our reference frame, or when we need to calculate a reflection vector that would occur from a surface with a upward facing plane. Vectors of unit length are also called normals. The normal of a vector can be calculated using the following equation:

In words, the normalized vector is the vector divided by the magnitude of itself. This results in a vector of unit length. We must take care when calculating the normalized vector because zero length vectors cannot be normalized. Normalizing a zero-length vector will usually result in a divide-by-zero error. Usually we resolve this by performing the normalization in multiple steps: 1. Calculate the length squared of the vector (only calculate the squared length of the vector because we only need the square root if the squared length is not zero). 2. If the squared length is greater than zero, then calculate the square root of that and multiply the vector components by the reciprocal of the length. Distance Between two Points Recall from the Vector Addition and Subtraction section that I stated the mnemonic the vector from a to b is b minus a? Well this is where this comes in handy. When we want to know the vector that goes from one point to another, we visualize the two points and as vectors from the origin and when we subtract them, the result is another vector which doesnt necessarily describe a point in space, but rather a direction from one point to the other. The images below shows an example of using vector subtraction to calculate a vector from one point in space to the other:

3dgep.com/?p=359

5/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

by subtracting point

from

The vector

3dgep.com/?p=359

6/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

The resulting vector is a vector from the origin to the head of vector

To get the distance between these two points, we simply calculate the magnitude of the resulting vector .

Notice that if the only thing we need to calculate is the distance between the two points, it doesnt matter if we calculate Vector Dot Product The result of a dot product on two vectors is a sum of the products of the matching components of each vector. Dot products are defined on vectors of all dimensions, but both vectors must have the same dimension (same number of components) to calculate a valid dot product. The result of a dot product is always a scalar. The general form for the dot product rule is: or , the result will be the same.

3dgep.com/?p=359 7/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

You may have noticed that if you calculate the dot product of a vector on itself give the squared distance of the vector. The dot product of two vectors is equal to the magnitude of the vectors multiplied by the cosine of the angle between them.

and

to say that the vectors are normalized). In such a case, the denominator becomes 1:

If we are only interested in the position of an object relative to another, we only need to calculate the dot product. The following table shows the result of the dot product under specific conditions:

and relative to eachother vector and vector are pointing in the same direction. vector and vector are perpendicular to eachother. vector and vector are pointing in opposite directions.

3dgep.com/?p=359 8/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Vector Projection 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 denoted and the part that is perpendicular to is denoted that is parallel to vector . is

Vector Projection

Using what we know about the dot product show in the previous example, we can solve for

normalization formula. The next part of the formula might be a bit confusing,

probably thinking how can we know the length of the vector that we are trying to calculate?. Well, this is easy if we remember back from our trigonometry class the simple trigonometric ratio for right-angle trangles:

3dgep.com/?p=359

9/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

We can re-write the equation and multiply both the numerator and denominator by

to get:

Now we see that the numerator of our fraction looks like the dot product shown earlier, which we can easily solve. So substituting the numerator for the dot product equation gives:

is

Be careful to only use this form of the equation when we know that Now that we know how to solve for

3dgep.com/?p=359

is of unit length.

10/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

know that:

Subtracting

And substituting

Vector Cross Product Unlike the dot product, the cross product is only defined for three-dimensional vectors. In addition, the result of the cross product is a vector as opposed to the result of a dot product is a scalar. The cross product is written using the multiplication. symbol, but it should not be mistaken with scalar

The result of the cross product between vector to both vectors and

and vector

3dgep.com/?p=359

11/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

(blue) which lie in the plane with normal and and parallel to .

is perpendicular to both

If the dot product and the cross product are used together, then the cross product must be calculated first, as in the formula . This is obvious if you try to cross a vector with the result of a dot product, which is a scalar, it simply wont work. This form of taking the dot product of a cross product is known as the triple product and will be discussed further in the article about matrices. It is useful to note that the cross product is not commutative, in fact, the cross product is anticommutative. That is to say that if we reverse the operands, the resulting vector is negated:

The result of a cross product is a vector which is perpendicular to both vectors, even if the original vectors are not perpendicular to each other. This is a useful property of the cross product and can be exploited when we need to find a vector basis where all three axes are perpendicular to each other, for example when we need to othogonolize the basis vectors for a cameras view matrix. This will be explained in more detail in the article on matrices. Another useful property of the cross product is that the magnitude of the cross product is equal to the product of the magnitudes of the two vectors and the sine of the angle between them. The expression of this is shown below:

It is also true that the magnitude of the cross product is the positive area of the parallelogram ( ) formed by the edges of the two vectors.

3dgep.com/?p=359

12/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Area of a Parallelogram

If vector

and vector

or vector

are the vectors that form the orthogonal basis for our coordinate

And if we swap the order of the vectors for the cross product, the result will be negated:

It is also useful to note, that when switching from a left-handed coordinate system and a righthanded 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

3dgep.com/?p=359 13/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

This means that in a left-handed coordinate system, the result of the cross product will point away from the viewer (into the screen), but in a right-handed coordinate system, the result of the same cross product will point towards the viewer (out of the screen). This is a by-product of the handedness of the coordinate system. References

Fletcher Dunn and Ian Parberry (2002). 3D Math Primer for Graphics and Game Development. Wordware Publishing.

Coordinate Systems

Matrices

Post to Facebook 4 4

Like

4 4 4 4

3dgep.com/?p=359

14/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

Introduction to OpenCL

Understanding Quaternions

3D Game Engines

This entry was posted in Graphics Programming, Math, Programming and tagged cross product, distance , dot product, length, math, normal, normalization, primer, scalar, vector, vector operations, vectors by Jeremiah van Oosten. Bookmark the permalink [http://3dgep.com/?p=359] .

5 THOUGHTS ON 3D MATH PRIMER FOR GAME PROGRAMMERS (VECTOR OPERATIONS)

Thank you so much! I was looking for a good simple description of the Look At algorithm and none of the other sites I found provided such a clear and simple explanation. Spent 4 hours trying to debug why mine didnt work and couldnt figure it out until I found this article.

3dgep.com/?p=359 15/16

28/12/2013

3D Math Primer for Game Programmers (Vector Operations) | 3D Game Engine Programming

that would occur from a serface with a upward facing plane spelling at surface (feel free to delete this comment)

The result of the cross product between vector a and vector b is a vector that is parallel to both vectors as show in the image below: Plz change parallel to perpendicular.

3dgep.com/?p=359

16/16

- 01 VectorsUploaded byIsrael Lives
- MatlabIntro_3Uploaded byNilabja Saha
- Vectors in Two & Three DimensionsUploaded bywolfretonmaths
- Section 2Uploaded byAhmed Shreef
- MIT8 02SC NotesUploaded byRottenDinkle
- vectors.pptUploaded byLouise Marie
- Contour Analysis for Image Recognition in CUploaded byBàn Tay Của Chúa
- vectoriUploaded bycc777
- On the Extraction of Curve Skeletons Using Gradient Vector FlowUploaded byoash
- Vectors Imp PointsUploaded byMohammed Aftab Ahmed
- Vector CalculusUploaded byDan_OC
- Dot Product LectureUploaded byMaireAveiro
- Notes Important Questions Answers 12th Math Chapter 7 Exercise 7.2Uploaded byshahid
- Blas1 z Test OutputUploaded bymnbvcxz0987123
- Motivating the Dot and Cross ProductsUploaded bydidizzle
- Spyrou 1Uploaded byEviVardaki
- Hw Chapter 27 Giancoli physics solutionsUploaded byBecky Dominguez
- m1 mod3(1)Uploaded byhoney1002
- Hw1 Sol Amcs202Uploaded byFadi Awni Eleiwi
- lag9Uploaded byKhmer Cham
- ece assUploaded byOyongpogi Harden
- 3. Force SystemsUploaded byjrvinod
- LADE13 Inner Product SpacesUploaded byRoumen Guha
- Tutorial 1 ANSWERUploaded bymashitahradzi
- Cinematica VectorialUploaded byViviana Paternoster
- ASME B89.4.19 Performance Evaluation Tests and Geometric Misalignments in Laser Trackers VolumeUploaded byJD
- Statics Lecture Notes1Uploaded bydraqbhatti
- essential physicsUploaded byputih_138242459
- KINEMATICS 2 .docUploaded byShiva Ram Prasad Pulagam
- Script Swiss Physics Olympiad 2 EditionUploaded byBertram Reichardt

- Alienware Area51 r2 Reference Guide en UsUploaded byDean Parker
- Alienware Area51 r2 Setup Guide en UsUploaded byDean Parker
- Koreographer Quick Start GuideUploaded byDean Parker
- Alienware Area51 r2 Service Manual en UsUploaded byDean Parker
- Police interview of Robert PICKTONUploaded byTheGlobeandMail
- Police interview of Robert PICKTONUploaded byTheGlobeandMail
- August SmartLock HomeKit Edition Installation Guide.Uploaded byDean Parker
- Karaoke Demo OverviewUploaded byDean Parker
- AlgorithmsUploaded byMuhammad Arif Said
- Circle Measurements in Ancient ChinaUploaded byDean Parker
- body bio bulletin phosphatidylcholineUploaded byDean Parker
- Steely_Taws_Puzzle_Game_README.pdfUploaded byDean Parker
- BitTube Case Study of a Web-Based Peer-Assisted ViUploaded byDean Parker
- Behavioral Interview InfoUploaded byAbeer Kabbag
- CaronteFX_Manual_2016_04_15_2.pdfUploaded byDean Parker
- ICS OPS Description2012Uploaded byDean Parker
- Easy Journey to Other Planets Original India SP Edition ScanUploaded byinternetidentityscri
- BraidwoodHearingsMay22-09Uploaded byDean Parker
- Banana Island–Bobo's Epic Tale Unity Complete Project Documentation.pdfUploaded byDean Parker
- 2D Experimental Preview R1 Reference GuideUploaded byDean Parker
- Tracking Nazi ScUploaded byDean Parker
- Jewish activities in the United StatesUploaded byDean Parker
- A Complete Book of Magic ScienceUploaded byRed Phoenix
- Canada Police Research Center - Lance ValcourUploaded byDean Parker
- Bitcoin: A Peer-to-Peer Electronic Cash SystemUploaded bymkriegs100
- Occult TheocracyUploaded byDean Parker
- Rise of American FascismUploaded byDean Parker
- 2DExperimentalPreviewR1ReleaseNotesUploaded byDean Parker
- Neck Restraints in PolicingUploaded byDean Parker
- 2016 02 18 Detention Conditions in NunavikUploaded byDean Parker

- Lecture 01Uploaded byfred5599
- 15. MathematicsUploaded byAruna Vijayan
- 04 01 Product of Vectors1Uploaded byRaju Raju
- Fundamentals of Continuum Mechanics RudnickiUploaded byMateus
- Electromagnetic Waves & Antennas Solutions - 2008Uploaded byDM2
- Lecture 1Uploaded bySimran Radheshyam Soni
- Dr. Hua Ma Explains Why the Universe Has Three Large Spatial DimensionsUploaded byPR.com
- Chapter 05Uploaded bybella
- Appendix VectorsUploaded byJ G Ravi Kumar
- Multiplication of VectorsUploaded bychashma
- Parte 1 Fundamentos Electromagneticos Con Matlab_Lonngren & SavovUploaded byAlbert Hernández
- Revised Syllabus DAE Mech Sep 2007Uploaded byAwais Sheikh
- vb-workshop-harvard-gsd-woojsung-com1.pdfUploaded byJose Quintero
- Math53 WorksheetUploaded byAndrew Berger
- Vector Calculus GATE Study Material in PDFUploaded bykavinkumarece
- GCE Mathematics (6360) Further Pure unit 4 (MFP4)Uploaded byTom
- 1 VectorsUploaded byGopal Krishan
- In Compressible Navier-Stokes Equations Reduce to Bernoulli's LawUploaded byReed Reader
- [Exam Crack] G. Tewani - Mathematics for Joint Entrance Examination JEE (Advanced)_ Vectors and 3D Geometry (2015, Cengage Learning India)Uploaded byOscar Reynaga Alarcón
- EMTL-NOTESUploaded byramyaraki
- Hestenes (GA1) 2002 - Reforming the Mathematical Language of PhysicsUploaded byvanespp
- Physics 1Uploaded byneeraj0107
- Cxc PhysicsUploaded byKatrina Lee Gagan
- 1000 Electromagnetic Theory MCQsUploaded bykibrom atsbha
- Vector and Tensor Analysis 1950Uploaded byRay Minich
- BYUOpticsBookUploaded byLuke Seed
- Differential Surface VectorsUploaded byJohn Doe
- E-Notes 7 - Vector Cross ProductUploaded byKesava Dass
- BiomechanicsUploaded byLukman Hakim
- Tensor Calculus for Physics Concise by Dwight NeuenschwanderUploaded bySebastián Silva