Skip to content

olc::vf2d

Javidx9 edited this page Sep 15, 2020 · 1 revision

Description

For convenience olc::PixelGameEngine provides a 2D geometric vector type, which can have integer or floating point components:

  • olc::vf2d - uses float32 - "float"
  • olc::vi2d - uses signed integer32 - "int"
  • olc::vd2d - uses float64 - "double"

The vector class definition olc::v2d_generic can actually be templated with other numeric types, though that is up to you. In principle this vector class operates as you would expect geometrical vectors to behave, you can add them, scale them, work out various numerical properties, but they also exhibit some behaviour which is useful for 2D game scenarios.

olc::vf2d v1 = {5.0f, 6.0f};
olc::vf2d v2 = {7.0f, 8.0f};
olc::vf2d v3 = v1 + v2;   // {12.0f, 14.0f}
olc::vf2d v4 = v3 * 2.0f; // {24.0f, 28.0f}
olc::vf2d v5 = v1 * v2;   // {35.0f, 48.0f} <- Element wise multiplication

Caution! Mixing the types of the vectors requires thinking about by the user, in terms of what happens in terms of type priority. It is recommended that you do not mix types. You can, but double check you get the results you expect (as with all inter type operations in C++).

All vector types are initialised to {0, 0} reagrdless of type.

Methods

T olc::v2d_generic::mag()

Returns magnitude, or the length, of vector

olc::vf2d v1 = {3.0f, 4.0f};
float length = v1.mag(); // length = 5.0

T olc::v2d_generic::mag2()

Returns (magnitude * magnitude) of vector. Useful for certain optimistaions.

olc::vf2d v1 = {3.0f, 4.0f};
float length = v1.mag2(); // length = 25.0

olc::v2d_generic olc::v2d_generic::norm()

Returns a normalised form of the vector, i.e. a unit vector.

olc::vf2d v1 = {3.0f, 4.0f};
olc::vf2d v2 = v1.norm(); // v2 = {0.6, 0.8}

olc::v2d_generic olc::v2d_generic::perp()

Returns a vector orthogonal to this one, i.e. 90 degrees, or perpendicular.

olc::vf2d v1 = {3.0f, 4.0f};
olc::vf2d v2 = v1.perp(); // v2 = {-4.0, 3.0}

T olc::v2d_generic::dot(const v2d_generic& rhs)

Calculates the dot product between the two vectors.

olc::vf2d v1 = {3.0f, 4.0f};
olc::vf2d v2 = {5.0f, 6.0f};
float d = v1.dot(v2); // d = 3*5 + 4*6 = 39

T olc::v2d_generic::cross(const v2d_generic& rhs)

Calculates the 2D cross product between the two vectors, i.e. the length of a vector orthogonal to both input vectors. In this instance the size of the number may be useful but far less so than the sign of the number, which indicates how the two vectors are related in space.

olc::vf2d v1 = {3.0f, 4.0f};
olc::vf2d v2 = {5.0f, 6.0f};
float d = v1.cross(v2); // d = 3*6 - 4*5 = -27

std::string olc::v2d_generic::str()

Returns a string representing the vector in the form "(x, y)".

Equality Operators

Both == and != operators are provided for the vector class, and are based upon binary exactness, so be cautious when comparing vectors that are fundamentally floating point types for equality.

std::ostream Operator

You can use the vector type directly with an output stream, for example:

olc::vf2d v1 = {10.0f, 5.0f};
std::cout << "Vector: " << v1 << "\n";

will output: Vector: (10.0, 5.0)