Skip to content

class Vec2 (shared-side)

Available since version: 0.2

This class represents 2d vector.

Constructor


Parameters:

Constructor

float value

Parameters:

  • float value: the initial value for x, y components.

Constructor

float x, float y

Parameters:

  • float x: the initial value for x component.
  • float y: the initial value for y component.

Properties

float x

Available since version: 0.2

Represents the x component of the vector.


float y

Available since version: 0.2

Represents the y component of the vector.


Methods

len

Available since version: 0.2

This method will return the vector length.
The length of the vector is square root of (x*x + y*y).

float len()

Returns float:

the squared length of the vector.


len2

Available since version: 0.2

This method will get the squared vector length.
The length of the vector is (x*x + y*y).
Calculating the squared length instead of the len is much faster.

float len2()

Returns float:

the length of the vector.


lenApprox

Available since version: 0.2

This method will get the approximate vector length.
The length of the vector is approximation square root of (x*x + y*y).
This method is faster than len but less accurate.

float lenApprox()

Returns float:

the length of the vector.


distance

Available since version: 0.2

This method will get distance between two vectors.

float distance(Vec2 vec)

Parameters:

  • Vec2 vec: the vector that will be used in distance calculation.

Returns float:

the distance between vectors.


normalize

Available since version: 0.2

Note

Avoid normalizing a vector with length = 0.

This method will normalize the vector.
Normalizing a vector will cause it's length to be equal to 1.
The direction of the vector won't be affected.

Vec2 normalize()

Returns Vec2:

this reference.


normalizeSafe

Available since version: 0.2

This method will normalize the vector. It works almost exactly the same as normalize with the difference that zero length vector won't cause any issues.

Vec2 normalizeSafe()

Returns Vec2:

this reference.


normalizeApprox

Available since version: 0.2

Note

Avoid normalizing a vector with length = 0.

This method will normalize the vector with small approximation.
It works almost the same as normalize, but it's faster and less accurate.
It uses the fast inverse square root algorithm as the base of calculating square root.

Vec2 normalizeApprox()

Returns Vec2:

this reference.


set

Available since version: 0.2

This method will set all of the vector components.

void set(float x, float y)

Parameters:

  • float x: the new vector x component.
  • float y: the new vector y component.

isEqualEps

Available since version: 0.2

This method will return true if vectors are approximately the same.
The minimal difference used for comparison is 0.001.

bool isEqualEps(Vec2 vec)

Parameters:

  • Vec2 vec: the vector that will be used in comparision.

Returns bool:

true if vectors are approximately the same, otherwise false.


abs

Available since version: 0.2

This method will return vector with absolute x,y components.

Vec2 abs()

Returns Vec2:

the new vector result.


static swap

Available since version: 0.2

This method will swap x,y components between two vectors.

void swap(Vec2 vec1, Vec2 vec2)

Parameters:

  • Vec2 vec1: the vector that will be used in swap operation.
  • Vec2 vec2: the vector that will be used in swap operation.

static min

Available since version: 0.2

This method will return vector with the smallest x,y components from two vectors.

Vec2 min(Vec2 vec1, Vec2 vec2)

Parameters:

  • Vec2 vec1: the vector that will be used in min operation.
  • Vec2 vec2: the vector that will be used in min operation.

Returns Vec2:

the new vector result.


static max

Available since version: 0.2

This method will return vector with the largest x,y components from two vectors.

Vec2 max(Vec2 vec1, Vec2 vec2)

Parameters:

  • Vec2 vec1: the vector that will be used in max operation.
  • Vec2 vec2: the vector that will be used in max operation.

Returns Vec2:

the new vector result.


static prod

Available since version: 0.2

This method will multiply two vectors and return the result as a new vector.

Vec2 prod(Vec2 vec1, Vec2 vec2)

Parameters:

  • Vec2 vec1: the vector that will be used in prod operation.
  • Vec2 vec2: the vector that will be used in prod operation.

Returns Vec2:

the new vector result.


static dot

Available since version: 0.2

This method will return the dot product between two vectors.
For normalized vectors dot returns:
- 1 if they point in exactly the same direction - -1 if they point in completely opposite directions and a number in between for other cases (e.g. Dot returns zero if vectors are perpendicular).
For vectors of arbitrary length the Dot return values are similar: they get larger when the angle between vectors decreases.

float dot(Vec2 vec1, Vec2 vec2)

Parameters:

  • Vec2 vec1: the vector that will be used in dot operation.
  • Vec2 vec2: the vector that will be used in dot operation.

Returns float:

the dot product.


static lerp

Available since version: 0.2

This method will return interpolated vector between vectors a and b by ratio t.

Vec2 lerp(float t, Vec2 v1, Vec2 v2)

Parameters:

  • float t: interpolation ratio.
  • Vec2 v1: the vector used in lerp operation.
  • Vec2 v2: the vector used in lerp operation.

Returns Vec2:

the lerp vector result.