dreamworkAPI
Ctrl K
GitHub
classDocs / Vector2

Vector2

classsharedmenusource
Object → Vector2

A 2D vector object, e.g. a screen position or a size.

Arithmetic operators work component-wise:

  • vec2 + vec2, vec2 + number, number + vec2 ( same for - and * )
  • vec2 / vec2, vec2 / number
  • -vec2

Constructor

Vector2(x: number?, y: number?): Vector2

Fields

NameTypeDescription
xnumberThe X coordinate.
ynumberThe Y coordinate.
originVector2The shared zero vector ( 0, 0 ), must not be modified.

Operators

OperatorOperandResult
add Vector2 | number Vector2
sub Vector2 | number Vector2
mul Vector2 | number Vector2
div Vector2 | number Vector2
unmVector2

Static functions

copyCreates a new independent copy of the vector.
getAngleReturns the angle of the vector in degrees, measured clockwise from the +y axis in range ( 0, 360 ].
getLengthCalculates the length of the vector.
getLengthSqrCalculates the squared length of the vector.
getLerpedReturns a copy of the vector linearly interpolated to another vector or a number.
lerpLinear interpolation between the vector and another vector or a number.
normalizeScales the vector to the length of 1.
rotateRotates the vector counter-clockwise ( from +x towards +y ) by the given degrees.
scaleMultiplies all coordinates of the vector by the number.

#Vector2.copy

sharedmenunodiscardsource
function Vector2.copy(self: Vector2): Vector2

Creates a new independent copy of the vector.

Parameters
NameTypeDescription
selfVector2The vector.
Returns
TypeNameDescription
Vector2copyThe new vector.

#Vector2.getAngle

sharedmenunodiscardsource
function Vector2.getAngle(self: Vector2, up?: Vector2): number

Returns the angle of the vector in degrees, measured clockwise from the +y axis in range ( 0, 360 ].

If up is given, its angle is added to the result.

Parameters
NameTypeDescription
selfVector2The vector.
upoptionalVector2The direction of the angle.
Returns
TypeNameDescription
numberdegreesThe angle of the vector.

#Vector2.getLength

sharedmenunodiscardsource
function Vector2.getLength(self: Vector2): number

Calculates the length of the vector.

Parameters
NameTypeDescription
selfVector2The vector.
Returns
TypeNameDescription
numberlengthThe length of the vector.

#Vector2.getLengthSqr

sharedmenunodiscardsource
function Vector2.getLengthSqr(self: Vector2): number

Calculates the squared length of the vector.

Faster than getLength, use it to compare lengths.

Parameters
NameTypeDescription
selfVector2The vector.
Returns
TypeNameDescription
numberlengthThe squared length of the vector.

#Vector2.getLerped

sharedmenunodiscardsource
function Vector2.getLerped(self: Vector2, target: Vector2 | number, frac: number): Vector2

Returns a copy of the vector linearly interpolated to another vector or a number.

Parameters
NameTypeDescription
selfVector2The vector.
targetVector2 | numberThe target vector or a number ( used for every coordinate ).
fracnumberThe interpolation factor.
Returns
TypeNameDescription
Vector2lerped_vec2The new vector.

#Vector2.lerp

sharedmenusource
function Vector2.lerp(self: Vector2, target: Vector2 | number, frac: number): Vector2

Linear interpolation between the vector and another vector or a number.

frac of 0 keeps the vector, 1 sets it to the target, values outside [0, 1] extrapolate.

Parameters
NameTypeDescription
selfVector2The vector.
targetVector2 | numberThe target vector or a number ( used for every coordinate ).
fracnumberThe interpolation factor.
Returns
TypeNameDescription
Vector2vec2The same vector, interpolated.

#Vector2.normalize

sharedmenusource
function Vector2.normalize(self: Vector2): Vector2

Scales the vector to the length of 1.

Zero vector stays unchanged.

Parameters
NameTypeDescription
selfVector2The vector.
Returns
TypeNameDescription
Vector2vec2The same vector, normalized.

#Vector2.rotate

sharedmenusource
function Vector2.rotate(self: Vector2, degrees: number): Vector2

Rotates the vector counter-clockwise ( from +x towards +y ) by the given degrees.

Parameters
NameTypeDescription
selfVector2The vector.
degreesnumberThe rotation angle.
Returns
TypeNameDescription
Vector2vec2The same vector, rotated.

#Vector2.scale

sharedmenusource
function Vector2.scale(self: Vector2, scale: number): Vector2

Multiplies all coordinates of the vector by the number.

Parameters
NameTypeDescription
selfVector2The vector.
scalenumberThe scale factor.
Returns
TypeNameDescription
Vector2vec2The same vector, scaled.

Methods

addAdds another vector or a number ( to every coordinate ) to the vector.
crossCalculates the 2D cross product ( Z component of the 3D cross product ) of two vectors.
divDivides the vector by another vector ( component-wise ) or a number.
dotCalculates the dot product of two vectors.
getDistanceCalculates the distance between two vectors.
getDistanceSqrCalculates the squared distance between two vectors.
getNormalizedReturns a copy of the vector with the length of 1.
getRotatedReturns a copy of the vector rotated counter-clockwise ( from +x towards +y ) by the given degrees.
getScaledReturns a scaled copy of the vector.
isNearChecks if the vector is equal to the given vector with the given tolerance.
isZeroChecks if all coordinates of the vector are exactly zero.
mulMultiplies the vector by another vector ( component-wise ) or a number.
negateNegates all coordinates of the vector.
setCopies the coordinates of another vector into this vector.
setUnpackedSets the x and y coordinates of the vector.
subSubtracts another vector or a number ( from every coordinate ) from the vector.
unpackReturns the x and y coordinates of the vector.
zeroSets all coordinates of the vector to zero.

#Vector2:add

sharedmenusource
function Vector2:add(value: Vector2 | number): Vector2

Adds another vector or a number ( to every coordinate ) to the vector.

Parameters
NameTypeDescription
valueVector2 | numberThe other vector or a number.
Returns
TypeNameDescription
Vector2vec2The same vector, with the sum.

#Vector2:cross

sharedmenunodiscardsource
function Vector2:cross(vector: Vector2): number

Calculates the 2D cross product ( Z component of the 3D cross product ) of two vectors.

Positive if vector is counter-clockwise from the vector, negative if clockwise, zero if parallel.

Parameters
NameTypeDescription
vectorVector2The other vector.
Returns
TypeNameDescription
numbercross_productThe cross product of two vectors.

#Vector2:div

sharedmenusource
function Vector2:div(value: Vector2 | number): Vector2

Divides the vector by another vector ( component-wise ) or a number.

Modifies the vector in place, division by zero is not checked and results in inf / nan components.

Parameters
NameTypeDescription
valueVector2 | numberThe divisor vector or number.
Returns
TypeNameDescription
Vector2vec2The same vector, divided.

#Vector2:dot

sharedmenunodiscardsource
function Vector2:dot(vector: Vector2): number

Calculates the dot product of two vectors.

Parameters
NameTypeDescription
vectorVector2The other vector.
Returns
TypeNameDescription
numberdot_productThe dot product of two vectors.

#Vector2:getDistance

sharedmenunodiscardsource
function Vector2:getDistance(vector: Vector2): number

Calculates the distance between two vectors.

Parameters
NameTypeDescription
vectorVector2The other vector.
Returns
TypeNameDescription
numberdistanceThe distance between the two vectors.

#Vector2:getDistanceSqr

sharedmenunodiscardsource
function Vector2:getDistanceSqr(vector: Vector2): number

Calculates the squared distance between two vectors.

Faster than getDistance, use it to compare distances.

Parameters
NameTypeDescription
vectorVector2The other vector.
Returns
TypeNameDescription
numberdistanceThe squared distance between the two vectors.

#Vector2:getNormalized

sharedmenunodiscardsource
function Vector2:getNormalized(): Vector2

Returns a copy of the vector with the length of 1.

Zero vector returns a zero copy.

Returns
TypeNameDescription
Vector2normalized_vec2The new vector.

#Vector2:getRotated

sharedmenunodiscardsource
function Vector2:getRotated(degrees: number): Vector2

Returns a copy of the vector rotated counter-clockwise ( from +x towards +y ) by the given degrees.

Parameters
NameTypeDescription
degreesnumberThe rotation angle.
Returns
TypeNameDescription
Vector2rotated_vec2The new vector.

#Vector2:getScaled

sharedmenunodiscardsource
function Vector2:getScaled(scale: number): Vector2

Returns a scaled copy of the vector.

Parameters
NameTypeDescription
scalenumberThe scale factor.
Returns
TypeNameDescription
Vector2scaled_vec2The new vector.

#Vector2:isNear

sharedmenunodiscardsource
function Vector2:isNear(vector: Vector2, tolerance: number): boolean

Checks if the vector is equal to the given vector with the given tolerance.

Each coordinate is compared separately: |a.x - b.x| <= tolerance and so on.

Parameters
NameTypeDescription
vectorVector2The vector to check.
tolerancenumberThe maximum allowed difference per coordinate.
Returns
TypeNameDescription
booleanis_neartrue if the vectors are equal within the tolerance, otherwise false.

#Vector2:isZero

sharedmenunodiscardsource
function Vector2:isZero(): boolean

Checks if all coordinates of the vector are exactly zero.

Returns
TypeNameDescription
booleanis_zerotrue if the vector is zero, false otherwise.

#Vector2:mul

sharedmenusource
function Vector2:mul(value: Vector2 | number): Vector2

Multiplies the vector by another vector ( component-wise ) or a number.

Parameters
NameTypeDescription
valueVector2 | numberThe other vector or a number.
Returns
TypeNameDescription
Vector2vec2The same vector, with the product.

#Vector2:negate

sharedmenusource
function Vector2:negate(): Vector2

Negates all coordinates of the vector.

Returns
TypeNameDescription
Vector2vec2The same vector, negated.

#Vector2:set

sharedmenusource
function Vector2:set(vector: Vector2): Vector2

Copies the coordinates of another vector into this vector.

Parameters
NameTypeDescription
vectorVector2The vector to copy from.
Returns
TypeNameDescription
Vector2vec2The same vector.

#Vector2:setUnpacked

sharedmenusource
function Vector2:setUnpacked(x: number, y: number): Vector2

Sets the x and y coordinates of the vector.

Parameters
NameTypeDescription
xnumberThe x coordinate of the vector.
ynumberThe y coordinate of the vector.
Returns
TypeNameDescription
Vector2vec2The same vector.

#Vector2:sub

sharedmenusource
function Vector2:sub(value: Vector2 | number): Vector2

Subtracts another vector or a number ( from every coordinate ) from the vector.

Parameters
NameTypeDescription
valueVector2 | numberThe other vector or a number.
Returns
TypeNameDescription
Vector2vec2The same vector, with the difference.

#Vector2:unpack

sharedmenunodiscardsource
function Vector2:unpack(): number, number

Returns the x and y coordinates of the vector.

Returns
TypeNameDescription
numberxThe x coordinate of the vector.
numberyThe y coordinate of the vector.

#Vector2:zero

sharedmenusource
function Vector2:zero(): Vector2

Sets all coordinates of the vector to zero.

Returns
TypeNameDescription
Vector2vec2The same vector.
Generated from the dreamwork meta branch.