All Classes Files Functions Variables Typedefs Pages
Work with Vectors

A vector is one of the most important data structure for games.

A N-dimensional vector contains N elements and can perform various tasks. The Vector class is templated with two parameters: A type parameter T and the dimension N. So Vector<float, 3> is a 3D vector with float elements.

You can have every vector you like, but in most cases you just need a small number of vector types. Those types have typename aliases for easy use:

Vec3f // Vector<float, 3>
Vec2f // Vector<float, 2>
Vec4i // Vector<int, 4>

There are a lot of other short names, for the complete list see: util/VectorCore.hpp

There are some different ways to create a Vector:

Vec3f vec(3, 3.0, 2);
Vec3f vec = Vec3f(1, 1, 1);
Vec3f vec{3, 2, 1};
Vec3f vec = {1, 2, 3};

The last two methods come very handy when passing a Vector to a function, because its less to type ;)

You can access the elements of an vector in two ways: With a letter x, y, z, w (just for dimensions 2, 3 and 4) or via [] operator:

Vec4f v{1,2,3,4};
v.x; // 1
v.z; // 3
v[1]; // 1
v.w; // 4

You can calculate with Vectors just like you would write on paper (thanks to the power of operator overloading):

Vec3f a{1,2,3};
Vec3f b{10, 1, 0};
// calculations
a + b; // 11, 3, 3
a - b; // -9, 1, 3
a * 3; // 3, 6, 9
a / 2; // 0.5, 1, 1.5
-a; // -1, -2, -3
// the above operators works also as compound assignment: +=, -=, *=, /=
// comparison
a == b; // false
a != b; // true

If you want to multiply two vectors you have two possibilities:

// a and b from above
cross(a, b); // cross product = {-3, 30, -19}
dot(a, b); // dot product = 12

The vector also has a lot of methods for manipuling it. Actually there often are pairs of methods: One method that manipulates the current object and one that returns a manipulated copy of the object. For example: normalize() changes the object (every component /= length) and normalized() returns the normalized vector instead of changing the object. For all methods see: lumina::Vector