float2
float2 describes a vector in 2D spaces.
It contains functions for doing common vector operations.Properties
float2 zero
float2 one
float2 up
float2 down
float2 right
float2 left
number magnitude
float2 normalized
number sqrMagnitude
number x
number y
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)
from and to.
The angle returned is the unsigned angle between the two vectors.fromThe vector from which the angular difference is measured.toThe 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.
aStart value, returned when t = 0.bEnd 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.
currentThe position to move from.targetThe position to move towards.maxDistanceDeltaDistance to movecurrent 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)
* to create a new float2 value without self modification.valueA number.
local a = float2.New(1,1)
a:Mul(2)
print(tostring(a)) -- output: [2,2]
local b = float2.New(3, 5)
local c = b * 2
print(tostring(b)) -- output: [3,5]
print(tostring(c)) -- output: [6,10]
In this example, Mul modifies the value of a to [2,2], while b*2 creates a new float2 without modifying b.
float2 Div(number value)
/ to create a new float2 value without self modification.valueA number.
float2 Add(float2 value)
Add will modify the value of self. Instead, apply operator + to create a new float2 value without modification.valueA float2 value.
local a = float2.New(1, 2)
a:Add(float2.New(3, 4))
print(tostring(a)) -- output: [4,6]
local b = float2.New(8, 10)
local c = float2.New(9, 11)
local d = b+c
print(tostring(b)) -- output: [8,10]
print(tostring(c)) -- output: [9,11]
print(tostring(d)) -- output: [17,21]
In this example, function Add modifies the value of a to [4, 6], while b+c creates a new float2 without modification.
float2 Sub(float2 value)
Sub will modify the value of self. Instead, apply operator - to create a new float2 value without modification.valueA float2.
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 (xx+yy).
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.