Vectors, blah, blah, blah.

Vectors Part 1

By Alexei Novikov

I just thought that many of you, COGgers, potential JED Plugin writers and just people interested in some 3D stuff might find it useful to learn some vector math (if you don't know it already). I myself actually almost loathe math, but in this case it's so useful and intuitive, that I even got sort of attached to it. Besides, to my shame I learned this stuff not that long ago (believe it or not, I had next to no knowledge of 3D geometry when I got started with JED) so I freshly remember how much pain it was to do any 3D geometry without it.
The vector math is pretty trivial, but it's INCREDIBLY useful in just about any 3D operations. As a matter of fact, I doubt that there's a single level geometry manipulating procedure in JED that doesn't use some vector math.
First, the basics. A vector in 3D space is a piece of line. It's defined by 3 numbers, let's call them dx,dy,dz. dx its length along X axis, dx - along Y, dz - along Z. It's important to understand that a vector doesn't have a fixed location in space, but its slope and lengths are fixed.

Well, some conventions first. I will write a vector defined by dx,dy,dz as (dx,dy,dz). If a vector is defined by a letter (say V), it's dx,dy,dz components will be V.dx, V.dy and V.dz.The point with coordinate X,Y and Z will be written as X,Y,Z and P.x, P.y,P.z, correspondingly. Sqrt is square root, sqr - square (that Pascal names for those functions). Notice that a vector going from the point X1,Y1,Z1 to X2,Y2,Z2 will have coordinates (X2-X1,Y2-Y1,Z2-Z1). So you subtract source from destination.

OK, here's the key operations on vectors:
The length of a vector:

|V| = sqrt( sqr(V.dx)+sqr(V.dy)+sqr(V.dz)).

In geometrical sense it's the length of the vector in space.

Number*V = (number*v.dx,number*v.dy,number*v.dz)

In geometrical sense Number*V is the vector with the same slope, but number times longer.

V1+V2 = (V1.dx+V2.dx,V1.dy+V2.dy,V1.dz+V2.dz)

That's a sum of two vectors. A bit later about its geometrical meaning.

Now, the cool part. You can define points, lines and planes through vectors. What does it give us? A universal way to operate them to find the things you need.

Points:
A point can be regarded as a vector from the origin to X,Y,Z (coordinates of the point). That might come in handy sometimes. Another important thing- if you define coordinate system axis as 3 normalized vectors XV -(1,0,0), YV (0,1,0) and ZV (0,01), you can write a point as

P = X*XV+Y*YV+Z*ZV

That is, in expanded form:

P.x=X*XV.dx+Y*YV.dx+Z*ZV.dx
P.y=X*XV.dy+Y*YV.dy+Z*ZV.dy
P.z=X*XV.dz+Y*YV.dz+Z*ZV.dz

What for? Because if, say, you rotate, scale or flip the coordinate system(i.e. - the whole scene), the above equation will give you the coordinates of the point after transformation.

Lines:
A line can be defined by a point and a vector. The vector would define the slope of the line and the point - its location in space - its "starting" point. Geometrically, if you put the end of the vector in the starting point, the vector will lie on the line. The line equation can be written as

P = X1,Y1,Z1+N*LV

Or, in expanded form

P.x = X1+N*LV.dx
P.y = Y1+N*LV.dy
P.z = Z1+N*LV.dz

Are you starting to get the hang of these short form equations?

Where X1,Y1,Z1 is the point where the line "starts", LV is the vector that defines the line's slope and N is some number. But using different numbers for N you get different points on the line. Here's something to illustrate. Say you have an edge that begins at point X1,Y1,Z1 and ends and point X2,Y2,Z2. That piece of line will be defined by a starting point X1,Y1,Z1and vector (X2-X1,Y2-Y1,Z2-Z1). So the equation will be:

P = X1,Y1,Z1+N*(X2-X1,Y2-Y1,Z2-Z1)

Important thing to note is, in this case at N=0 , P will be X1,Y1,Z1(beginning of the edge). At N=1, P will be X2,Y2,Z2 (end of the edge). The values of N from 0 to 1 will give you points on the edge.


Planes:

OK, now it's the time to introduce another vector operations - dot product. The dot product of two vectors is a number. It is:

V1*V2=V1.dx*V2.dx+V1.dy*V2.dy+V1.dz*V2.dz

It has this property:

V1*V2 = |V1| * |V2| * cos (angle between V1 and V2)

I.e. it's the cosine of the angle between two vectors multiplied by the length of both.

Now back to the planes. the plane can be defined by a vector and a point as well. Like in case of the line the vector defines the slope of the plane and point - it's position, but it's a bit different than in case of the line. Geometrically, the vector is PERPENDICULAR to the plane. The plane equation in this case is

(X-X1,Y-Y1,Z-Z1)*NV=0

Where X1,Y1,Z1 is the point that defines the plane and NV is the vector that defines the plane. X, Y and Z are the points on the plane. This equation can also be written as

(X,Y,Z)*NV=D

Where D is

D=(X1,Y1,Z1)*NV

The vector NV is typically normalized (has a length of 1) and called plane's NORMAL. It's used in just about every operation involving planes.


Now, one more vector operation (the last one), called cross product. It's defined as

V1 x V2 = (V1.dy*V2.dz-V2.dy*V1.dz, V2.dx*V1.dz-V1.dx*V2.dz, V1.dx*V2.dy-V2.dx*V1.dy)

Or, a in a bit clearer form:

V.dx=V1.dy*V2.dz-V2.dy*V1.dz
V.dy=V2.dx*V1.dz-V1.dx*V2.dz
V.dz:=V1.dx*V2.dy-V2.dx*V1.dy

The cross product of two vectors is a vector. Geometrically it is a vector, perpendicular to both of the starting vectors and oriented in such a way that three vectors V1, V2 and V1XV2 have "right" orientation. "Right" means- if you make a fist with your right hand and then straighten thumb, index and middle finger, if thumb, index and middle fingers were vectors, they would have "right" orientation. This term also applies for a coordinate systems. The "right" coordinate system is such system in which X x Y = Z. That's the coordinate system used everywhere.

The cross product has these properties -V2 x V1 = - V1 x V2 the length of the cross-product vector is|V1xV2|=|V1|*|V2|*sin(angle between vectors)

Some of the useful things that come from the above stuff. For instance you want to find a normal of a plane that comes through two vectors (let's say that's two edges of a surface). What would you do? Simply take their cross product and normalize it. Normalization is done simply by dividing the vector by its length. I.e.

norm(V)=V/|V|

Or in expanded form

norm(V).dx=V.dx/|V|
norm(V).dy=V.dy/|V|
norm(V).dz=V.dz/|V|

And |V|, as defined above

|V| = sqrt( sqr(V.dx)+sqr(V.dy)+sqr(V.dz) )

And here's a couple of examples for using this stuff for some real tasks. For instance you need to tell if a give surface has a slope below 45 degrees. Using the property of dot product that

Take the normal of the surface (its length is 1 by definition) and find a dot product with vector (0,0,1) (its length is also 1). The dot product will be the cosine of the slope. So basically if you dot product is more than cos(45) (which is about 0.7), your surface slope is below 45 degrees

Or, for instance, you need to find the shortest distance from a given point to a plane (surface). Defined by a point PP and a normal NV. That'll be a distance from P to P' (on the picture), where P-P' is perpendicular to the plane. Obviously, it's equal to the distance PP-P''. From the triangle P-P''-PP:

Dist=cos(angle between PP-P and PP-P'')*length(P-PP)

The angle between PP-P and PP-P'' is equal to angle between PP-P and NV (as normal is perpendicular to the surface by definition, thus it lies on PP-P''). Now let's see, the dot product between NV and vector PP-P will be (note that you get the vector P-PP by subtracting of PP from P. So it's P-PP where - is a minus)

NV*(P-PP)=|NV|*|P-PP|*cos(angle between P-PP and NV)

|P-PP| is the length of the P-PP. |NV| is 1 (since it's a normal). Now look at the previous expression. It turns out to be exactly it. So the distance will be:

Dist=NV*(P-PP)

Actually, even better. This dot product will be positive if the point is "in front" of the plane (on the side where normal points), negative if it's on the back of the plane and 0 if the point lies on the plane.


So, you see, some scary at the first glance operations turn out rather trivial with the use of vector math. In fact, for instance, lighting calculation in JED doesn't go beyond .the vector math explained above. If someone's interested, I can explain it in full in the next article. Alex.