Content

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 u 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 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.

I have been working as a software engineer in computer graphics for almost 5 years. In the free time I love playing guitar, playing video games, hiking and studying computer graphics techniques.Follow me on : Google + , Twitter, Facebook