SFSVector2

Represents a vector/point in a 2D space.


This class is used to represent 2D positions and vectors.

Constructor

new SFSVector2(x, y)

Creates a new instance of the SFSVector2 class.
Parameters:
NameTypeDescription
xnumberThe vector component along the X axis.
ynumberThe vector component along the Y axis.

Members

(readonly) angle :number

Angle of this vector with respect to the positive X axis, in radians.

The returned angle ranges from -PI to PI.

Type:
  • number

(readonly) aspect :number

Aspect ratio of this vector (ratio of x to y).
Type:
  • number

(readonly) isNormalized :boolean

Indicates if the vector is normalized, i.e. its length is approximately equal to 1.
Type:
  • boolean

(readonly) length :number

The length (magnitude) of the vector.
Type:
  • number

x :number

The vector component along the X axis.
Type:
  • number

y :number

The vector component along the Y axis.
Type:
  • number

Methods

abs() → {SFSVector2}

Returns a new vector with all components in absolute values.
Returns:
A new vector with absolute components.
Type: 
SFSVector2

ceil() → {SFSVector2}

Returns a new vector with all components rounded up to the nearest integer.
Returns:
A new vector with rounded up components.
Type: 
SFSVector2

cross(vec) → {number}

Returns the 2D cross product (also known as the perpendicular dot product or wedge product) of this vector and the passed vector.

This returns a scalar representing the z-component of the 3D cross product when treating the 2D vectors as 3D vectors with z equal to 0.

The magnitude represents the area of the parallelogram formed by the two vectors. The sign indicates orientation: positive if the passed vector is counterclockwise from this vector, negative if clockwise.

Parameters:
NameTypeDescription
vecSFSVector2The vector to compute the cross product with.
Returns:
The scalar cross product of the vectors.
Type: 
number

distanceSquaredTo(vec) → {number}

Returns the squared distance between this vector and the passed vector.

This method is more efficient than SFSVector2#distanceTo method when comparing distances.

Parameters:
NameTypeDescription
vecSFSVector2The vector to measure the squared distance from.
Returns:
The squared distance between the vectors.
Type: 
number

distanceTo(vec) → {number}

Returns the Euclidean distance between this vector and the passed vector.
Parameters:
NameTypeDescription
vecSFSVector2The vector to measure the distance from.
Returns:
The distance between the vectors.
Type: 
number

div(vec) → {SFSVector2}

Returns a new vector that is the component-wise division of this vector by the passed vector.
Parameters:
NameTypeDescription
vecSFSVector2The vector to divide this vector by.
Returns:
A new vector representing the component-wise division of the two vectors.
Type: 
SFSVector2

dot(vec) → {number}

Returns the dot product of this vector and the passed vector.

The dot product will be 0 for a right angle (90 degrees), greater than 0 for angles narrower than 90 degrees and lower than 0 for angles wider than 90 degrees.

When using unit (normalized) vectors, the result will always be between -1.0 (180 degrees angle), when the vectors are facing opposite directions, and 1.0 (0 degrees angle) when the vectors are aligned.

Parameters:
NameTypeDescription
vecSFSVector2The vector to compute the dot product with.
Returns:
The dot product of the vectors.
Type: 
number

floor() → {SFSVector2}

Returns a new vector with all components rounded down to the nearest integer.
Returns:
A new vector with rounded down components.
Type: 
SFSVector2

lerp(vec, weight) → {SFSVector2}

Returns the result of the linear interpolation between this vector and the passed vector by the passe weight.

The weight range is 0.0 to 1.0, representing the amount of interpolation. Values outside this range will extrapolate.

Parameters:
NameTypeDescription
vecSFSVector2The target vector to interpolate towards.
weightnumberThe interpolation weight (0.0 = this vector, 1.0 = target vector).
Returns:
A new interpolated vector.
Type: 
SFSVector2

max(vec) → {SFSVector2}

Returns a new vector with the component-wise maximum of this vector and the passed vector.
Parameters:
NameTypeDescription
vecSFSVector2The vector to compare to.
Returns:
A new vector with the maximum components of the vectors.
Type: 
SFSVector2

min(vec) → {SFSVector2}

Returns a new vector with the component-wise minimum of this vector and the passed vector.
Parameters:
NameTypeDescription
vecSFSVector2The vector to compare to.
Returns:
A new vector with the minimum components of the vectors.
Type: 
SFSVector2

mult(vec) → {SFSVector2}

Returns a new vector that is the component-wise multiplication of this vector and the passed vector.
Parameters:
NameTypeDescription
vecSFSVector2The vector to multiply this vector by.
Returns:
A new vector representing the component-wise product of the two vectors.
Type: 
SFSVector2

normalize() → {SFSVector2}

Returns a new vector that is the normalized (unit length) version of this vector.

If this vector has zero length, returns a zero vector.

Returns:
A new normalized vector.
Type: 
SFSVector2

rotate(angle) → {SFSVector2}

Returns a new vector that is the result of rotating this vector by the passed angle.
Parameters:
NameTypeDescription
anglenumberThe angle to rotate the vector by, in radians.
Returns:
A new rotated vector.
Type: 
SFSVector2

round() → {SFSVector2}

Returns a new vector with all components rounded to the nearest integer, with halfway cases rounded away from zero.
Returns:
A new vector with rounded components.
Type: 
SFSVector2

sub(vec) → {SFSVector2}

Returns a new vector that is the result of subtracting the passed vector from this vector.
Parameters:
NameTypeDescription
vecSFSVector2The vector to subtract from this vector.
Returns:
A new vector representing the difference between the two vectors.
Type: 
SFSVector2

sum(vec) → {SFSVector2}

Returns a new vector that is the sum of this vector and the passed vector.
Parameters:
NameTypeDescription
vecSFSVector2The vector to sum to this vector.
Returns:
A new vector representing the sum of the two vectors.
Type: 
SFSVector2

toString() → {string}

Returns a string containing the X and Y components of the vector.
Returns:
The string representation of the SFSVector2 object.
Type: 
string