API reference
API reference
Select your platform
No SDKs available
No versions available

Vector2 Class

Modifiers: open
Represents a 2D vector with x and y components.

Signature

open class Vector2(var x: Float = 0.0f, var y: Float = 0.0f)

Constructors

Vector2 ( v ) : Vector2
Secondary constructor to create a vector where both components are the same.
Signature
constructor(v: Float)
Parameters
v: Float
The value to use for both components.
Returns
Vector2 ( x , y ) : Vector2
Signature
constructor(x: Float = 0.0f, y: Float = 0.0f)
Parameters
x: Float
The x component of the vector.
y: Float
The y component of the vector.
Returns

Properties

x : Float
[Get][Set]
Signature
open var x: Float
y : Float
[Get][Set]
Signature
open var y: Float

Functions

add ( v ) : Vector2
Adds the specified vector to this vector.
Signature
fun add(v: Vector2): Vector2
Parameters
The other vector to add.
Returns
A new vector representing the sum of this vector and the other vector.
component1 () : Float
Signature
operator fun component1(): Float
Returns
Float
component2 () : Float
Signature
operator fun component2(): Float
Returns
Float
copy () : Vector2
Returns a copy of this vector.
Signature
fun copy(): Vector2
Returns
A copy of this vector.
cross ( other ) : Float
Calculates the cross product of this vector with another vector.
Signature
fun cross(other: Vector2): Float
Parameters
other: Vector2
The other vector to calculate the cross product with.
Returns
Float
The cross product of this vector and the other vector.
distanceTo ( v ) : Float
Calculates the distance to another vector.
Signature
fun distanceTo(v: Vector2): Float
Parameters
The other vector to calculate the distance to.
Returns
Float
The distance between this vector and the other vector.
div ( v ) : Vector2
Divides each component of this vector by a scalar.
Signature
operator fun div(v: Float): Vector2
Parameters
v: Float
The scalar to divide by.
Returns
A new vector representing the division of this vector by the scalar.
divide ( v ) : Vector2
Divides each component of this vector by a scalar.
Signature
fun divide(v: Float): Vector2
Parameters
v: Float
The scalar to divide by.
Returns
A new vector representing the division of this vector by the scalar.
dot ( v ) : Float
Calculates the dot product of this vector with another vector.
Signature
fun dot(v: Vector2): Float
Parameters
The other vector to calculate the dot product with.
Returns
Float
The dot product of this vector and the other vector.
equals ( other ) : Boolean
Signature
open operator override fun equals(other: Any?): Boolean
Parameters
other: Any?
Returns
Boolean
hashCode () : Int
Signature
open override fun hashCode(): Int
Returns
Int
length () : Float
Returns the length (magnitude) of this vector.
Signature
fun length(): Float
Returns
Float
The length of this vector.
lerp ( dest , ratio ) : Vector2
Linearly interpolates between this vector and another vector by the given ratio.
Signature
fun lerp(dest: Vector2, ratio: Float): Vector2
Parameters
dest: Vector2
The destination vector to interpolate towards.
ratio: Float
The interpolation ratio, where 0 means no interpolation (i.e., the current vector) and 1 means full interpolation (i.e., the destination vector).
Returns
A new vector representing the linear interpolation between this vector and the destination vector by the given ratio.
max ( v ) : Vector2
Returns a vector with the maximum components of this vector and another vector.
Signature
fun max(v: Vector2): Vector2
Parameters
The other vector to compare with.
Returns
A new vector with the maximum components.
min ( v ) : Vector2
Returns a vector with the minimum components of this vector and another vector.
Signature
fun min(v: Vector2): Vector2
Parameters
The other vector to compare with.
Returns
A new vector with the minimum components.
minus ( v ) : Vector2
Subtracts the other vector from this vector.
Signature
operator fun minus(v: Vector2): Vector2
Parameters
The other vector to subtract.
Returns
A new vector representing the difference between this vector and the other vector.
multiply ( v ) : Vector2
Multiplies this vector by another vector component-wise.
Signature
fun multiply(v: Vector2): Vector2
Parameters
The other vector to multiply.
Returns
A new vector representing the product of this vector and the other vector.
multiply ( v ) : Vector2
Multiplies each component of this vector by a scalar.
Signature
fun multiply(v: Float): Vector2
Parameters
v: Float
The scalar to multiply by.
Returns
A new vector representing the product of this vector and the scalar.
negate () : Vector2
Negates this vector.
Signature
fun negate(): Vector2
Returns
A new vector representing the negation of this vector.
normalize () : Vector2
Normalizes this vector.
Signature
fun normalize(): Vector2
Returns
A new vector representing the normalization of this vector, or (0, 0) if the length of this vector is 0.
plus ( v ) : Vector2
Adds two vectors.
Signature
operator fun plus(v: Vector2): Vector2
Parameters
The other vector to add.
Returns
A new vector representing the sum of this vector and the other vector.
setX ( x ) : Vector2
Sets the x component of this vector.
Signature
fun setX(x: Float): Vector2
Parameters
x: Float
The new value for the x component.
Returns
This vector with the updated x component.
setY ( y ) : Vector2
Sets the y component of this vector.
Signature
fun setY(y: Float): Vector2
Parameters
y: Float
The new value for the y component.
Returns
This vector with the updated y component.
sub ( v ) : Vector2
Subtracts the specified vector from this vector.
Signature
fun sub(v: Vector2): Vector2
Parameters
The other vector to subtract.
Returns
A new vector representing the difference between this vector and the other vector.
times ( v ) : Vector2
Multiplies two vectors component-wise.
Signature
operator fun times(v: Vector2): Vector2
Parameters
The other vector to multiply.
Returns
A new vector representing the product of this vector and the other vector.
times ( v ) : Vector2
Multiplies each component of this vector by a scalar.
Signature
operator fun times(v: Float): Vector2
Parameters
v: Float
The scalar to multiply by.
Returns
A new vector representing the product of this vector and the scalar.
toString () : String
Returns a string representation of the vector.
Signature
open override fun toString(): String
Returns
String
A string representation of the vector in the format "(x, y)".
unaryMinus () : Vector2
Negates this vector.
Signature
operator fun unaryMinus(): Vector2
Returns
A new vector representing the negation of this vector.
Did you find this page helpful?
Thumbs up icon
Thumbs down icon