GAME PROGRAMMING RESOURCES

IT'S ALL ABOUT VECTORS

In this tutorial we assume you have read this document.

1. Understanding the concept of 3d vector is extremely helpful when programming a 3d engine like 3Impact.

Imagine a 3d vector as an arrow located somewhere in the space in front of you. The arrow has an origin, a length and an orientation.


In your .dll code, a vector is declared (created, allocated) with

D3DXVECTOR3 myvector;

where 'myvector' is a custom name you choose. In your code, you will use 'myvector' as a single variable, but keep in mind that it is made of three components really. Each component is a value. For example, you can set the three values with

myvector.x = 0.0f;
myvector.y = 1.0f;
myvector.z = 0.0f;

or with

myvector = D3DXVECTOR3(0.0f,1.0f,0.0f);

The three values define the arrow. In the code above, 'myvector' is set to an one-meter-long arrow that points upward. Given the three values, you can visualize the corresponding arrow as follows:
  • imagine the arrow origin in front of you
  • imagine that the arrow is null (zero length), with the origin and tip collapsed to one single point
  • imagine that you can pick the arrow tip and that you can stretch the arrow around, with the origin keeping its location
  • the first value (x) tells you how many meters you should shift the arrow tip to your right, from the origin
  • the second value (y) tells you how many meters you should lift the tip, upward, from the origin
  • the third value (z) tells you how many meters you should push the tip away from you, from the origin


  • The x,y,z values can be negative. If so, you will translate the arrow tip leftward, downward and backward respectively.

    2. The origin of a vector is always understood.

    For example, if the vector is used to express an absolute space location, its origin is understood to match the world center. Imagine the world center exactly between your eyes. The horizontal (ground) plane passes through your eyes and through the world center. The vector

    D3DXVECTOR3(-7.0f,2.5f,0.0f);

    is a point located 7 meters on your left, 2.5 meters above the ground plane.

    If a vector is used to express a force applied to a body, its origin is understood to match the point where the force is applied to (usually defined by a second vector). Its orientation defines the direction of the force, and its length defines the intensity of the force.

    If a vector is used to express a torque applied to a body, its origin is, for simplicity, assumed to match the center of mass of the body. A torque is a force that tends to rotate a body about a certain axis. So the vector direction is the axis of rotation, and vector length is the intensity of the torque.

    3. Vectors are passed to 3Impact functions as variables. For instance

    iBodyLocationSet(body,&myvector);

    Note that the vector variable name is always preceded by the & character, when vectors are passed to functions as parameters.

    The function above places the specified body to the specified absolute space location. Of course the absolute location (second parameter) is passed as a vector. You can visualize it as an arrow from the world center to the desired location.

    Some functions return vectors. For example

    iBodyLocation(body,&myvector);

    will set the three values of 'myvector' so that it expresses the absolute location of the specified body.

    4. Vectors can be transformed and added up.

    3Impact provides a number of functions that allow you to manage vectors. Names of vector-manipulation functions start with 'iVector...'.

    You can also compute vectors with common C language expressions. For example

    myvector = v1 + v2;

    where v1 and v2 are vector variables, is a valid expression. The resulting 'myvector' variable is filled with the sum of v1 and v2.

    REMEMBER: when you deal with vector variables, do your best to visualize them as 3d ARROWS. Mastering vectors alone means mastering half the power of 3Impact game engine!