3D Math


Most of the 3D Math only uses Vectors, seldom Vertices. So we will start of with some Vector-Maths

Vector Addition


(Vector Addition)


When adding up 2 Vectors, you receive a 3rd Vector, like in the above picture. Like this: v1(x1,y1,z1) + v2(x2,y2,z2) = v3(x1+x2,y1+y2,z1+z2).

Vector Subtraction


(Vector subtraction)


Vector subtraction works basically the same way as Addition: v1(x1,y1,z1) - v2(x2,y2,z2) = v3(x1-x2,y1-y2,z1-z2). To make it easier to understand, i drew in the -V2 vector, so that you could see, that subtracting Vector 2 from Vector 1, is nothing more than adding Vector 1 to -Vector 2.

Vector Multiplication/Division


(Vector Multiplication/Division)


When multiplying a Vector with a constant (as in the picture above, 'a') we get a Vector with a high/lower "magnitude". The magnitude of a Vector is, 3D-wise seen, it's length. When multiplying a constant with a Vector, only it's magnitude changes, not it's direction (unlike addition and subtraction, where mostly both changes). So, what use is magnitude? Well, for example, if we had a car with a Vector showing which direction it is going, the magnitude shows how fast it is going. For many Vector calculations, having a magnitude of 1 is very useful, as we will see later on. A vector with the magnitude of 1 is called a Normal-Vector.

Normal Vectors

As said above, Normal Vectors have the length of 1. To create a normal vector, we simply divide the vector by it's current length. So: V/length = |V|. There are many different ways of writing this, but many stick to writing a normal vector as |V| and it's length as ||V||. Normal Vectors are also called Unit Vectors.
Now you must be asking yourself, what they are used for? One important use is for Polygons. Each polygon has a Normal, which shows which way is "up".


(The normal of a polygon)


The normal is always perpendicular (90 degrees) to the polygon. Later i will show you how it is calculated, but for that, we need some other formulas.

Length of a Vector

To calculate the length of a vector, we use the formula by pytagoras. So, length=Sqr(X*X+Y*Y+Z*Z).

Dot Product (Scalar Product)

I won't go into how and why the next 2 formulas work, but i will explain what they can be used for and how to use them. For a Dot-Product we need 2 Vectors. The Dot-Product looks like this:

Result=V1.x*V2.x+V1.y*V2.y+V1.z*V2.z

This is one of the most important formulas for 3D, since it is so powerful. But what does this formula actually return? To answer this quickly, it returns a product of: The length of both vectors and the cosine of the angle between them ( ||V1||*||V2||*cos(a) ).


(Dot-Product)


You might think this formula doesn't seem all that usefull, but when looking closer, you might remember that with unit vectors, the length ( ||V1|| ) is equal to 1. This would mean, as a result of a Dot-product of 2 unit vectors we would receive this: 1*1*cos(a) or simply cos(a). So, with the Dot-product we can easily calculate the angle between 2 vectors.

Vector Product

Unlike the dot-product (scalar product), the Vector-product returns a vector instead of a scalar. Here is the formula:

V.x = V1.y * V2.z - v2.y * v1.z
V.y = v1.z * v2.x - v2.z * v1.x
V.z = v1.x * v2.y - v2.x * v1.y

THe resulting vector is very interesting. It is perpendicular to both Vector 1 and Vector 2, even if they both aren't unit-vectors.


(Vector-Product)


If both are unit-vectors, the result is a unit vector as well. If you are at the moment thinking if this could be a way of calculating the normal of a polygon, you are right.

Calculating the Normal of a polygon

This is fairly simple if you have understood the vector-product correctly. Let's say we have a polygon with these 3 points: P1,P2,P3. All we have to do is:

1. Create a Vector from P1 to P2 (P2-P1) = V1
2. Create a Vector from P1 to P3 (P3-P1) = V2
3. Normalize (create unit vectors) both the vectors ( V1/||V1|| and V2/||V2|| )
4. Apply Vector product on V1 and V2.

At this point we have all the need fundaments to do the most 3D related math. If we need new formulas in the future, i will introduce them as we need them.

Back to the Last Tutorial or on to the next one
Hosted by www.Geocities.ws

1