Skip to content

float2

float2 describes a vector in 2D spaces. It contains functions for doing common vector operations.

Properties

float2 zero
A float2 with the magnitude of zero
float2 one
A float2 with a value of 1 on every axis
A float2 with a value of 1 on the Y axis
float2 down
A float2 with a value of -1 on the Y axis
float2 right
A float2 with a value of 1 on the X axis
float2 left
A float2 with a value of -1 on the X axis
number magnitude
Returns the length of this vector
float2 normalized
Returns this vector with a magnitude of 1
number sqrMagnitude
Returns the squared length of this vector
number x
The x-coordinate of the float2
number y
The y-coordinate of the float2

Functions

float2 float2.New(number x, number y)
Constructs a new float2 using the given x, and y components.
number float2.Angle(float2 from, float2 to)
Gets the unsigned angle in degrees between from and to. The angle returned is the unsigned angle between the two vectors.
  • from The vector from which the angular difference is measured.
  • to The vector to which the angular difference is measured.
float2 float2.ClampMagnitude(float2 value, number maxLength)
Returns a copy of value with its magnitude clamped to maxLength.
number float2.Distance(float2 a, float2 b)
Returns the distance between a and b.
local other = float2.New(1,1)
local _self = float2.New(0,0)
local dist = float2.Distance(other, _self)
print("distance to other: " .. dist)
number float2.Dot(float2 a, float2 b)
Returns dot Product of two vectors.
float2 float2.Lerp(float2 a, float2 b, number t)
Linearly interpolates between two points. Interpolates between the points a and b by t. The parameter t is clamped to the range [0, 1]. When t = 0, returns a. When t = 1, returns b. When t = 0.5, returns the midpoint of between a and b.
  • a Start value, returned when t = 0.
  • b End value, returned when t = 1.
float2 float2.LerpUnclamped(float2 a, float2 b, number t)
Linearly interpolates between vectors a and b by t. When t = 0, returns a. When t = 1, returns b. When t = 0.5, returns the midpoint of between a and b.
float2 float2.Max(float2 a, float2 b)
Returns a vector that is made from the largest components of two vectors.
local a = float2.New(1,2)
local b = float2.New(3,1)
-- (3,2)
print(float2.Max(a,b))
float2 float2.Min(float2 a, float2 b)
Returns a vector that is made from the smallest components of two vectors.
float2 float2.MoveTowards(float2 current, float2 target, number maxDistanceDelta)
Moves a point current towards target.
  • current The position to move from.
  • target The position to move towards.
  • maxDistanceDelta Distance to move current per call.
float2 float2.Normalize(float2 value)
Makes vector have a magnitude of 1. When normalized, a vector keeps the same direction but its length is 1.0.
float2 float2.Reflect(float2 inDirection, float2 inNormal)
Reflects a vector off the plane defined by a normal.
float2 float2.Scale(float2 a, float2 b)
Multiplies two vectors component-wise. Every component in the result is a component of a multiplied by the same component of b.
float2 Mul(number value)
Multiplies self by a number, same as operator *.
local vector = float2.New(1,1)
-- same as result = vector * 2
local result = vector:Mul(2)
-- (2,2)
print(result)
float2 Div(number value)
Divides self by a number, same as operator /.
float2 Add(float2 value)
Adds other vector, same as operator +.
float2 Sub(float2 value)
Subtracts self from another.
number SqrMagnitude()
Returns the squared length of this vector.
float2 Clone()
Returns a deep copy of this vector.
float2 Magnitude()
Returns the length of this vector. The length of the vector is square root of (x*x+y*y).
void Set(number x, number y)
Set x and y components of an existing float2.
float2 SetNormalize()
Makes this vector have a magnitude of 1.