Skip to content

Vector2

Imagment edited this page Mar 27, 2025 · 14 revisions

Explanation

Representation of 2D vectors and points. This class is used in some places to represent 2D positions and vectors.

Constructors

Constructor Type Description
Vector2(double x_, double y_) Default constructor. Initializes x and y, with default values of 0.0.
Vector2(std::initializer_list<double> list) Allows initialization using {x, y} format. If fewer elements are provided, missing values default to 0.0.
Vector2(const Vector3 &v3) Constructs a Vector2 from a Vector3, taking only the x and y components.

Constants

Name Description
Vector2Up Shorthand for writing Vector2(0, -1)
Vector2Down Shorthand for writing Vector2(0, 1)
Vector2Left Shorthand for writing Vector2(-1, 0)
Vector2Right Shorthand for writing Vector2(1, 0)
Vector2One Shorthand for writing Vector2(1, 1)
Vector2Zero Shorthand for writing Vector2(0, 0)

Variables

Name Description
x The x-component of the vector.
y The y-component of the vector.

Functions

Function Name (with Type) Description
double magnitude() Returns the length (magnitude) of the vector.
double magnitudeSqr() Returns the squared length (magnitude squared) of the vector.
Vector2 normalized() Returns a unit vector in the same direction as the original vector (with magnitude 1). If the magnitude is 0, it returns the zero vector.
double dotProduct(Vector2 v1, Vector2 v2) Returns the dot product of two Vector2 objects.
double crossProduct(Vector2 v1, Vector2 v2) Returns the cross product of two Vector2 objects (as a scalar in 2D).
double VectorAngle(Vector2 v1, Vector2 v2) Returns the angle between two Vector2 objects.
double lineAngle(Vector2 start, Vector2 end) Returns the angle of a line defined by two Vector2 objects.
Vector2 VectorLerp(Vector2 v1, Vector2 v2, double amount) Returns the linear interpolation between two Vector2 objects.
Vector2 VectorReflect(Vector2 v, Vector2 normal) Returns the reflection of a Vector2 off a surface with a normal.
Vector2 VectorMin(Vector2 v1, Vector2 v2) Returns the minimum of two Vector2 components.
Vector2 VectorMax(Vector2 v1, Vector2 v2) Returns the maximum of two Vector2 components.
Vector2 VectorRotate(Vector2 v, double angle) Returns a Vector2 rotated by a given angle.
Vector2 VectorMoveTowards(Vector2 v, Vector2 target, double maxDistance) Moves a Vector2 towards a target with a maximum distance.
Vector2 VectorClamp(Vector2 v, Vector2 min, Vector2 max) Clamps a Vector2 between two other Vector2 objects.
Vector2 VectorClampValue(Vector2 v, double min, double max) Clamps the magnitude of a Vector2 between minimum and maximum values.
Vector2 VectorRefract(Vector2 v, Vector2 n, double r) Returns the refraction of a Vector2 through a surface with a normal and refraction ratio.

Operators

Operator Description
operator+ Adds two Vector2 objects component-wise.
operator- Subtracts one Vector2 object from another component-wise.
operator* Multiplies the vector by a scalar value.
operator/ Divides the vector by a scalar value.
operator+= Adds another Vector2 to the current vector and assigns the result back.
operator-= Subtracts another Vector2 from the current vector and assigns the result back.
operator/= Divides the current vector by a scalar and assigns the result back.
operator== Checks if two Vector2 objects are equal by comparing x and y components.
operator!= Checks if two Vector2 objects are not equal by negating the result of the equality operator.
operator[] (Accessor) Allows access to the x or y component of the vector by index (0 for x, 1 for y). Returns -1 for invalid indices.
operator[] (Mutator) Allows modification of the x or y component by index (0 for x, 1 for y). Throws an exception for invalid indices.

Overview

Cheatsheets

Structure of the engine

Silver Mathematics

Components

Clone this wiki locally