Skip to main content

Vector2

class Vector2

Vector2 is a Vector with 2 positions, respectively, X and Y, generally used in 2d dimension space, if you need a vector for 3 positions, use Vector3 instead.They are used to map buttons positions on screen, touch position on screen, etc.


public class Vector2

β€” Constructors (6) β€”

ConstructorDescription
Vector2()Creates a new Vector2 with all values setted as 0.
Vector2(float a)Creates a new Vector2 with the two axes [X, Y] defined with the value of the argument "a".
Vector2(float x, float y)Creates a new Vector2 with X and Y setted as "x" and "y" arguments respectively.
Vector2(int x, int y)Creates a new Vector2 with X and Y setted as "x" and "y" arguments respectively.
Vector2(float x, int y)Creates a new Vector2 with X and Y setted as "x" and "y" arguments respectively.
Vector2(int x, float y)Creates a new Vector2 with X and Y setted as "x" and "y" arguments respectively.

β€” Methods (65) β€”

NameDescription
float getX()Return the current X value inside of this vector.
float getY()Return the current Y value inside of this vector.
void setX(float value)Sets the current X value inside of this vector with a new value.
void setY(float value)Sets the current Y value inside of this vector with a new value.
void set(Vector2 value)Sets the current value within this vector with a new vector of the argument.
void set(float a)Sets the current value within this vector to a new value.
void set(float x, float y)Sets the current x, and y value within this vector to a new value.
Vector2 mul(Vector2 value)Multiplies the current vector values with the input vector values and return a new instance with these multiplied values.
Vector2 mul(float x, float y)Multiplies the current vector values with the input X and Y values and return a new instance with these multiplied values.
Vector2 mul(float a)Multiplies the current vector values with the input "a" and return a new instance of these multiplied values.
void mulLocal(Vector3 value)Multiply the current values of the vector by the values of the input vector and apply to the vector itself.
void mulLocal(Vector2 value)Multiply the current values of the vector by the values of the input vector and apply to the vector itself.
void mulLocal(float a)Multiply the current values of the vector by the input "a" and apply to the vector itself.
void mulLocal(float x, float y)Multiplies the current values of the vector with the input X and Y values and applies it to the vector itself.
Vector2 div(Vector2 value)Divides the current vector values with the input vector values and return a new instance with these divided values.
Vector2 div(float x, float y)Divides the current vector values with the input X and Y values and return a new instance with these divided values.
Vector2 div(float a)Divides the current vector values with the input "a" and return a new instance with these divided values.
void divLocal(Vector3 value)Divides the values of the current vector with the values of the input vector and applies to the vector itself.
void divLocal(Vector2 value)Divides the values of the current vector with the values of the input vector and applies to the vector itself.
void divLocal(float a)Divides the current values of the vector with the input "a" and apply it to the vector itself.
void divLocal(float x, float y)Divides the current vector values with the X and Y input values and applies it to the vector itself.
Vector2 sum(Vector2 value)Increments the current vector values with the input vector values and return a new instance with these incremented values.
Vector2 sum(float x, float y)Increments the current vector values with the input X and Y values and return a new instance with these incremented values.
Vector2 sum(float a)Increments the current vector values with the input "a" and return a new instance with these incremented values.
void sumLocal(Vector3 value)Increments the current vector values with the input vector values and applies to the vector itself.
void sumLocal([Vector2 value])Increments the current vector values with the input vector values and applies to the vector itself.
void sumLocal(float a)Increments the current values of the vector with the input "a" and applies it to the vector itself.
void sumLocal(float x, float y)Increments the current vector values with the input X and Y values and applies it to the vector itself.
Vector2 sub(Vector2 value)Subtracts the current vector values with the input vector values and return a new instance with these subtracted values.
Vector2 sub(float x, float y)Subtracts the current vector values with the input X and Y and return a new instance with these subtracted values.
Vector2 sub(float a)Subtracts the current vector values with the input "a" and return a new instance with these subtracted values.
void subLocal(Vector3 value)Subtracts the current vector values from the input vector values and applies it to the vector itself.
void subLocal(Vector3 value)Subtracts the current vector values from the input vector values and applies it to the vector itself.
void subLocal(float a)Subtract the values from the current vector with the input "a" and apply it to the vector itself.
void subLocal(float x, float y)Subtracts the current values of the vector with the X and Y input and applies it to the vector itself.
boolean equals(Vector2 value)Compares the first vector with the one in the argument, returns true if the values match.
boolean equals(float a)Compares first vector with argument value, returns true if values match.
boolean equals(float x, float y)Compares the vector with the value of the arguments respectively x and y, returns true if the values match.
float length()Returns the length of the vector with the square root.
float sqrLength()Returns the length of the vector without square root.
float distance(Vector2 value)Returns the distance between two vectors with the square root.
float sqrDistance(Vector2 value)Returns the distance between two vectors without the square root.
Vector2 copy()Returns the perfect copy of a vector.
String toString()Returns vector values to a String.
String toString(int decimals)Defines the number of decimal places that the axes will have when creating the string.
Vector2 zero()Returns a new vector with its axes at 0.
Vector2 normalize()Makes the vector have a magnitude of 1, vector keeps the same direction but its length is 1.0, if the vector is too small to normalize, it is set to zero.
void normalizeLocal()Makes the vector have magnitude 1, the vector keeps the same direction, but its length is 1.0, the difference to "normalize" is that this method applies the modification to the vector itself, if the vector is too small to normalize , is set to zero.
float dot(Vector2 value)Calculates the dot product of this vector and the vector passed in the argument and returns the value.
float dot(float a)Calculates the dot product of this vector and the value passed in the argument and returns the value.
float dot(float x, float y)Calculates the dot product of this vector and the values passed, x, and y in the argument and returns the value
Vector2 cross(Vector2 value)Cross Product of two vectors.The cross product of two vectors results in a third vector which is perpendicular to the two input vectors.The result's magnitude is equal to the magnitudes of the two inputs multiplied together and then multiplied by the sine of the angle between the inputs.
Vector2 cross(float x, float y)Cross product of two vectors, respectively x, and y, the cross product of two vectors results in a third vector that is perpendicular to the two input vectors. The magnitude of the result is equal to the magnitudes of the two inputs multiplied together and then multiplied by the sine of the angle between the inputs.
void lerp(Vector2 value, float speed)Transforms a vector into a smoothed form vector based on a velocity.
void lerp(float a, float speed)Transforms a vector into a smoothed form vector based on a velocity.
void lerp(float x, float y, float speed)Smooths one vector into another, respectively x and y based on a velocity.
void lerpInSeconds(Vector2 value, float speed)Smooths one vector into another, based on a speed multiplied by "Math.bySecond(1)".
void lerpInSeconds(float a, float speed)Smooths one vector into another, based on a speed multiplied by "Math.bySecond(1)".
void lerpInSeconds(float x, float y, float speed)Smooths one vector into another, respectively x and y based on a velocity multiplied by "Math.bySecond(1)".
void blend(Vector2 value, float blend)Mix the vector with another.
void blend(float a, float blend)Mix the vector with another, given a 0-1 percentage.
void blend(float x, float y)Mixes the vector with another, respectively, x and y, given a 0-1 percentage.
Vector2 rotateAroundPivot(Vector2 vector, float angle)Rotates the vector the amount of degrees determined by the "angle" argument around the 0, 0 coordinates.
Vector2 rotateAroundPivot(Vector2 vector, Vector2 pivot, float angle)Rotates the vector the amount of degrees determined by the "angle" argument around the "pivot" coordinates.
void rotateLocal(float angle)Rotates the vector the amount of degrees determined by the "angle" argument and apply it to the vector itself.

β€” Operators (10) β€”

+ Sum 2 vectors and return a new with the result.

Vector2 a = new Vector2(1, 0);  
Vector2 b = new Vector2(1, 0);

a + b;

// a = {2, 0}

- Decreases 2 vectors and returns a new with the result.

Vector2 a = new Vector2(6, 5);  
Vector2 b = new Vector2(6, 2);

a - b;

// a = {0, 3}

* Multiplies 2 vectors and returns a new with the result.

Vector2 a = new Vector2(5, 2);  
Vector2 b = new Vector2(5, 2);

a * b;

// a = {25, 4}

/ Divide 2 vectors and returns a new with the result.

Vector2 a = new Vector2(1, 2);  
Vector2 b = new Vector(1, 2);

a / b;

// a = {1, 1}

+= Sum vectors and apply the result to the vector itself.

Vector2 a = new Vector2(5, 5);  
Vector2 b = new Vector3(2, 3);

a += b;

// a = {7, 8}

-= Decreases 2 vectors and apply the result to the vector itself.

Vector2 a = new Vector2(5, 5);  
Vector2 b = new Vector2(4, 5);

a -= b;

// a = {1, 0}

/= Divide 2 vectors and apply the result to the vector itself.

Vector2 a = new Vector2(10, 0);  
Vector2 b = new Vector2(5, 0);

a /= b;

// a = {2, 0}

*= Multiplies 2 vectors and apply the result to the vector itself.

Vector2 a = new Vector2(2, 2);  
Vector2 b = new Vector2(4, 3);

a *= b;

// a = {8, 6}

++ Sum 1 to the vector.

Vector2 a = new Vector2(5, 0);

a++;

// a = {6, 0}

-- Decreases 1 from the vector.

Vector2 a = new Vector2(5, 0);  

a--;

// a = {4, 0}

β€” Comparators (5) β€”

>= Compares whether the length of the vector is greater than or equal to the other.

Vector2 a = new Vector2(10,0);  

if(a >= 10){

// TRUE because the length of the vector "a" is 10 or greater

}

<= Compares whether the length of the vector is less than or equal to the other.

Vector2 a = new Vector2(10,0);  

if(a <= 15){

// TRUE because the length is less than "15"

}

> Compares if the length of the vector is greater than the other.

Vector2 a = new Vector2(10,0);  

if(a > 10){

// FALSE because the length of the vector "a" is 10 not greater

}

< Compares if the length of the vector is greater than the other.

Vector2 a = new Vector2(10,0);  

if(a < 10){

// FALSE because the length of vector "a" is not less than 10

}

== Compares if the 2 axes of Vector3 are equal.

Vector2 a = new Vector3(10, 10);  
Vector2 b = new Vector3(10, 10);

if(a == b){

// TRUE because the length of vector "a" is equal to that of vector "b"

}