1. What is a vector?

2. Equality of vectors

3. Basic vector operations

4. Left Handed and Right Handed Coordinate Systems

5. Magnitude

6. Dot Product

7. Cross Product  

      1. What is a vector?

When you want to learn 3D programming you should always start with geometry math. Why? Because sooner or later you ‘ll have to use it in your games and applications. Think like this, you can’t eat without  your mouth, you can’t play guitar without guitar (I’m also a guitar player :D), you can’t do 3D programming without math.

Now, the first thing we should know are Vectors. What are Vectors and why we need them?

Well you can use vectors to represent forces, directions, velocities,normals etc. So they are used in many scientific fields.  Ok, but why we need them in 3D graphics? Maybe I want to show the direction I want to go in a First Person Shoter(FPS) or I want to set the direction of  a light or polygon normals and so on.

A vector has two components :   Magnitude(or length) and   Direction

We can represent vectors like this :


We can say that for vector AB , A is the tail and B is the head of the vector. If tail coincides with the oringin then we say that the vector is in standard position. The origin is set in O(0,0) for 2D coords or in O(0,0,0) for 3D coord.In the image above , W is in standard position.

  2.Equality of vectors 

Two or more vectors are equal if and only if they have the same magnitude and same direction. So the vectors in the next figure are equal:


If you overlap w over u,you will not going to see any more they will be indistinguishable.

3.Basic Vector operations

 Ok , so there are 3 simple operations we need to know between vectors. There are other operations that we need to know but we will see them later or in another tutorial very soon.

  First let me define 2 vectors in 3D space, that means we have  vector [math]w=(w_x,w_y,w_z)[/math] and  [math]u=(u_x,u_y,u_z)[/math], of course we could define this 2 vectors in 2D space without z component.

 1.Addition is very simple we just add their components  [math]w+u=(w_x+u_x,w_y+u_y,w_z+u_z)[/math] and get another vector much longer. For example in Physics if you add 2 forces(same direction) you get a much stronger force(called net force). Of course [math]w+u = u+w[/math]


        2.Multiplication with a scalar. If m is our scalar then [math] mw=(mw_x,mw_y,mw_z)[/math].


          3. Subtraction is like addition [math]w-u=(w_x-u_x,w_y-u_y,w_z-u_z)[/math]. You can think subtraction like a  addition and a -1 scalar multiplication [math]w-u=w + (-1)u[/math]

  You can represent subtraction geometrically with parallelogram method or head to tail method.  You should know that you can translate a vector everywhere you want and you also need to know how to find the opposite direction vector. In the images below you ll see the parallelogram method , how to translate and find opposite direction of a vector. Just follow the steps, it should be easy.

                   Step 1:  Find the opposite direction

                   Step2:  Make the addition between vectors u and -w and get the new vector

                  Step3:   Translate  the new vector in the initial position

                  Let’s see another example :


4. Left Handed and Right Handed Coordinate Systems

 We should know in which coordinate system we are going to work .For example in DirectX uses Left Handed coordinate system but OpenGL uses Right Handed coordinate system. The whole idea is to find out the positive z-axis and obviously the negative z-axis.


To find this we are going to use our hands. Take your left hand place it along +x-axis then lift your fingers on +y-axis,you should see your palm by now :).  Make a 90°  angle between your palm and your thumb. Now the thumb will show you the +z-axis. For the right hand is the same procedure but the trick is to put your right hand to the right of your body,just like the left hand, on +x-axis, don t make the mistake and put it on the left of your body because it will point on – x-axis.

In DirectX +z-axis will goes into your  computer screen, -z -axis will be towards you. In OpenGL/WebGL, +z-axis will point to you and -z-axis will go into your computer screen.

 5. Magnitude

 The magnitude is the length of a vector. The standard notation is between 2 bars [math] ||u|| [/math] but you can also  find it written with single bar [math] |u| [/math].

The magnitude of a 2D vector is computed with Pythagorean thorem, therefore we get a single value.

The magnitude of a 3D vector is computed also with Pythagorean thorem but we have to apply it twice to get the answer.

We will use magnitude to normalize the vector.


6. Dot Product

Dot Product is a 2 vector multiplication that returns a single value. The standard notation is ” [math]\cdot[/math] “and you can also find it under the name of scalar product.

We got 2 formulae: the algebric formula and geometric formula .

Algebric formula:


So for  2 3D vectors we will have :[math]w\cdot u= w_x u_x+w_y u_y+ w_z u_z[/math]. Pretty simple.

Geometric formula:

We can write Dot Product geometrically using the angle between the two vectors.

 [math] u \cdot v = || u || ||v|| cos \theta [/math]

There are some properties that we need to know before we go on:

  1)  if [math] \theta = 90 , cos \theta=0 [/math], [math] u , v [/math] are  orthogonal so  [math] u \cdot v = || u || ||v|| cos \theta = 0 [/math]

  2) if  [math] u = v [/math] then [math] \theta =0 ,cos \theta=1 [/math]  so  [math] u \cdot v = || u || ||v|| cos \theta =u \cdot u [/math]

  3)  [math] u \cdot u = u_x u_x+u_y u_y+ u_z u_z = ||u||^{ 2}[/math], magnitude squared.

  4)  [math] w \cdot( u + v) = w \cdot u + w \cdot v[/math].

Let’s demonstrate  [math] u \cdot v = || u || ||v|| cos \theta [/math]. First we need triangle  build from vectors, to see  the Low of Cosines and properties from above.

u-v DotProduct

If we write Low of Cosines:  [math] || v-u ||^{2}= ||v||^{2}+ ||u||^{2}-2*||u||||v||cos \theta[/math]

then we move first 2 right  terms to the left of the equation:

[math] || v-u ||^{2}-||v||^{2}- ||u||^{2}= -2*||u||||v||cos \theta[/math]

Now we can write [math] || v-u ||^{2}[/math] as [math] (v-u) \cdot (v-u)[/math],

  [math] (v-u) \cdot (v-u) = v \cdot v + u \cdot u -2*( v \cdot u)[/math]

  [math] v \cdot v + u \cdot u -2*( v \cdot u) = ||v||^{2}+ ||u||^{2}-2*( v \cdot u)[/math]

Now we go in second eqution and substitute [math] || v-u ||^{2} [/math] with the above result

 [math] ||v||^{2}+ ||u||^{2}-2*( v \cdot u)-||v||^{2}- ||u||^{2}= -2*||u||||v||cos \theta[/math]

We can see terms reduction and , -2 goes away as well and voilà 🙂

 [math] ( v \cdot u)= ||u||||v||cos \theta[/math].

We will demonstrate in another tutorial that algebric definition and geometry definition are equivalent but now lets see the cross product.

 7. Cross Product

Cross product is another form of multiplication. The result is a vector this time, unlike dot product which the result is a scalar.The standard notation is ” x “

Given two 3D vectors [math]u , v[/math] ,making the cross product will result vector [math] w [/math] which is orthogonal on [math] u [/math] and orthogonal on [math] v[/math]. Remember that cross product works only for 3D vectors.

The tail of the vector [math] w[/math] is in the same point with the [math] u,v [/math] vectors and the direction of [math] w[/math] is given by the Right Hand Thumb Rule or Left Hand Thumb Rule.

Always keep the smallest angle [math] \theta [/math] between the vectors to find the cross product. [math] 0\leq\theta\geq\pi [/math]. The index finger will always point to the first vector from cross product.

The formula is [math] u\phantom xx\phantom vv = ||u||||v||sin \theta n[/math] where [math] n [/math] is the unit vector and points out the direction.

Or you can compute like this:

 [math]w=u\phantom xx\phantom vv=(u_y v_z-u_z v_y,u_z v_x-u_x v_z,u_x v_y-u_yv_x)[/math]

The above formula is computed using Sarrus rule on the determinant below where [math] i, j, k [/math] are unit vectors


I will explain Sarrus rule or Cofactor rule in another post for determinants

Here are some Cross Product properties:

1) anticommuative  [math] u\phantom xx\phantom vv =-v\phantom xx\phantom uu [/math]

2) distributive  [math] u\phantom xx\phantom v(v+w) =( u\phantom xx\phantom vv) + ( u\phantom xx\phantom ww)[/math]

3) scalar multiplication  [math] (m u)\phantom xx\phantom vv =m( u\phantom xx\phantom vv) [/math]

Please leave a comment below

blog comments powered by Disqus