Table of content

Previous: 1.b Particle Creation

c. Essentials of vector algebra

Figure 1.1: A point M (Mx,My) defines also a vector, M, which is simply an arrow going from the origin O (0,0) to the point M.

Let us consider a Cartesian coordinate system, exactly like the one we find when we open WED: it contains a point O and two perpendicular lines, the axes x and y (I will neglect the z axis here but it does not change any of the following arguments). O is the point of reference in space of the coordinate system, and it is called the origin. The two axes x and y define directions along which we measure distances from the point O. These 3 objects allow us to define a position M anywhere in space by saying "starting from point O we go Mx quants in the x direction, and then we go My quants in the y direction". In other words, a point M or a position in the plane can be defined by two numbers, Mx and My, which represent distances measured along the axes of the system and which we call the coordinates of the point M (see Figure 1.1). Since O is the starting point, or origin, of the coordinate system, it has coordinates (0,0).

Now, let us draw an arrow which starts at point O and ends at the position occupied by point M. This defines what is called in mathematical terms the vector M: it points from the origin to the point M (I will use the convention of writing vectors using bold font in this tutorial). A vector can also be described by coordinates: they are defined by subtracting the coordinates of the point at its base (the origin O here) from those of the point at its tip (point M):

M.x = Mx - Ox = Mx - 0 = Mx	(1.3)
M.y = My - Oy = My - 0 = My	(1.4)

since the coordinates of the base point, O, are (Ox,Oy) = (0,0). The vector M therefore has the same coordinates (M.x = Mx, M.y = My) as point M, and talking about point M(Mx,My) or about the vector M with coordinates M.x = Mx and M.y = My is exactly equivalent (some mathematicians might not agree here, but for all practical uses, the above is correct).

Figure 1.2: Illustration of vector addition. When adding vector B(Bx,By) and R(Rx,Ry) we get the vector G(Gx,Gy). Graphically, vector G is the arrow pointing from the beginning of B to the tip of R, if R is translated so that it starts at the tip of B (drawn in light red).

Vectors are more useful than points because they can be added and subtracted very easily. Figure 1.2 shows graphically that when adding the blue B and red R vectors, we get the green G vector: the green vector just points from the beginning of the blue vector to the tip of the red one. Intuitively, adding vectors B and R is just like moving by following successive arrows: first, you go from O to B by following the blue arrow B, and then you go from B to G by following the red arrow R. This is of course the same thing as going directly from O to the last point G by moving along the straight line G.

This addition can be written in terms of coordinates also: if (B.x,B.y) and (R.x,R.y) are the coordinates of the blue and red vectors, then the coordinates of the green vector are given by:

G.x = B.x + R.x		(1.5)
G.y = B.y + R.y		(1.6)

To subtract vector R from vector B, we just replace the '+' by a '-' (or, on Figure 1.2, we add to vector B the vector -R, which is identical to R but points in the opposite direction). Conitec has provided us with the functions vec_add and vec_sub to perform these operations directly on vectors, instead of having to add their coordinates (see manual).

So we know now that a vector M is specified by its coordinates M.x and M.y. However, it can equally well be described by its length and direction. Its length is just the length of the arrow itself, which is also equal to the distance between the origin O and point M. It's direction is just, well, the direction along which the arrow points. It is usually represented by a vector m (written in lower case) with length 1, which points in the same direction as vector M.

In mathematical terms, the length |M| of vector M is defined by

|M| = sqrt(M.x2 + M.y2)		(1.7)

so it is just the square root of the sum of the squares of the coordinates of M. This is given more conveniently by vec_length(M.x) using the 3DGS function vec_length (see manual). The vector direction m of M is obtained by dividing all the coordinates of M by its length |M|:

m.x = M.x/|M|		(1.8)
m.y = M.y/|M|.		(1.9)

m can also be obtained using the 3DGS command vec_normalize(M.x,1) (see manual).

Specifying a vector with its coordinates or using its length and direction are both correct and equivalent ways to determine uniquely that vector: the two procedures give the same result. However, depending on the situation as we will see later on, one way is sometimes more practical to use than the other. It is just like giving directions to a person to go some place. You can say: "just go north for 3 km and then 1 km east" or you can say "just go 3.16 km in the direction north-east". In the first method, you give the person the coordinates of the location where he/she wants to go. In the second, you point in the direction where to go (north-east) and tell him/her the distance to travel along that direction (3.16 is equal to sqrt(10) = sqrt(32+12) which, according to equation (1.7), is the length of a vector with coordinates (3,1)).

Separating vectors into their length and direction allows us to manipulate separately one or the other. The most common operation on a vector's direction is a rotation by some angle.

For instance, vec_rotate(M.x,player.pan) rotates the direction of M by the pan, tilt and roll angles of the entity player. This is useful when programming a light saber, for instance, where M represents the direction of the laser blade which has to follow when the player turns left or right.

vec_scale(M.x,a) multiplies the length of vector M by the factor a, lengthening or shorting the vector, without changing its direction. A handy operation if one wants to lengthen or shorten the mentioned light saber's blade without changing its orientation.

Finally, one can turn an entity so that it faces in the direction specified by a vector. For instance, vec_to_angle(camera.pan,M.x) will make the camera entity face toward the point M by changing the direction of the vector camera.pan. The reader is referred to the manual and Acknex Magazine for instructions and examples.

We will see many instances of the usefulness of modifying the length and direction of vectors in the following lessons.

Now that we know what vectors are and how to use them, what do they have to do with position, speed and forces? We just saw that vectors are natural tools to specify a location in space. What about the rest? It has been shown a few centuries ago by Sir Isaac Newton and others that the sum of the forces acting on a body produces an acceleration of that body in the direction proportional to the sum of forces. This implies that, in other words, we live in a world where forces, acceleration, and velocity (which is related to acceleration) can all be represented by vectors, and therefore follow vector algebra. This is a good thing since vectors, as we saw above, obey a very simple algebra which allows us to readily compute a lot of things.

Figure 1.3: A stone (dark circle) has been thrown and flies through the air before hitting the ground. Its initial velocity is represented by the blue vector v. The force of gravity which pulls it down toward the earth is represented by a green downward vector F. Finally, the position of the stone at any time it represented by the red vector d.

Figure 1.3 shows examples of vectors used to represent the position, velocity and forces acting on an object. In the case of a velocity vector, the length of the vector indicates how fast the object is traveling, and its direction shows where it's heading. The position vector points to where the object is at each instant. Finally, if the vector represents a force, the length of the vector indicates the magnitude of the force and it direction specifies the angle with which the force is applied on the object.

Now let's get back to particles and video games!

Next: 1.d Finally, some particle programming