Category:Physics Engine/Vectors
A vector is a 2-dimensional object with x and y components. Its type is TI.cpVect.
Vect
vector = physics.Vect(x, y) vector = physics.Vect(angle) vector = physics.Vect(vect)
Parameter | Type | Description |
---|---|---|
x | in number | The _x_ component of the vector |
y | in number | The _y_ component of the vector |
angle | in number | An angle in radians |
vect | in physics.Vect | A vector |
vector | out physics.Vect | A vector |
Creates a vector with initial x and y component values. The second form creates a unit vector pointing in direction angle. The third form creates a copy of the input vector.
Introduced in platform.apiLevel = '2.0'
add
sum = physics.Vect:add(vec)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | A vector to add to self |
sum | out physics.Vect | The vector sum of self and vec |
Returns the vector sum of self and vec.
The Vect class also implements the addition operator (+). Therefore vectors v1 and v2 can be added with the expression v1 + v2.
Introduced in platform.apiLevel = '2.0'
clamp
clamped = physics.Vect:clamp(len)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
len | in number | The maximum length of the vector |
clamped | out physics.Vect | A new vector with a length no longer than len |
Returns a copy of self clamped to length len.
Introduced in platform.apiLevel = '2.0'
cross
crossprod = physics.Vect:cross(vec)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The vector to cross with self |
zmag | out number | The z magnitude of the cross product of self and vec |
Returns the z magnitude of the cross product of self and vec.
Introduced in platform.apiLevel = '2.0'
dist
dist = physics.Vect:dist(vec)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The vector to which to find the distance from self |
dist | out number | The distance from self to vec |
Returns the distance between self and vec.
Introduced in platform.apiLevel = '2.0'
distsq
distsq = physics.Vect:distsq(vec)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The vector to which to find the distance squared from self |
distsq | out number | The distance squared from self to vec |
Returns the distance squared between self and vec. This routine is faster than physics.Vect:dist when you only need to compare distances.
Introduced in platform.apiLevel = '2.0'
dot
dotprod = physics.Vect:dot(vec)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The other vector |
dotprod | out number | The scalar dot product of self and vec |
Returns the scalar dot product of self and vec.
Introduced in platform.apiLevel = '2.0'
eql
isequ = physics.Vect:eql(vec)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The vector against which to compare with self |
isequ | out boolean | True if the components of self equal the components of vec |
Returns true if the x and y components of self equal those of vec. Take the usual precautions when comparing floating point numbers for equality.
The Vect class also implements the equal comparison operator (==). Therefore vectors v1 and v2 can be compared with the expression v1 == v2.
Introduced in platform.apiLevel = '2.0'
length
len = physics.Vect:length()
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
len | out number | The length of vector self |
Returns the magnitude of self.
Introduced in platform.apiLevel = '2.0'
lengthsq
lensq = physics.Vect:lengthsq()
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
lensq | out number | The length squared of vector self |
Returns the length squared of self. This routine is faster than Vect:length() when you only need to compare lengths.
Introduced in platform.apiLevel = '2.0'
lerp
v = physics.Vect:lerp(vec, f)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The other vector |
f | in number | f_ is a fractional number from 0 to 1 representing the proportion of distance between _self and vec |
v | out physics.Vect | A vector interpolated between self and vec |
Returns the linear interpolation between self and vec as a vector. f_ is the fraction of distance between _self and vec.
May not behave as expected for f larger than 1.0 or less than 0.
Introduced in platform.apiLevel = '2.0'
lerpconst
v = physics.Vect:lerpconst(vec, d)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The other vector |
d | in number | The distance from self to vec to interpolate a new vector |
v | out physics.Vect |
Returns a vector interpolated from self towards vec with length _d_.
May not behave as expected for d larger than 1.0 or less than 0.
Introduced in platform.apiLevel = '2.0'
mult
v = physics.Vect:mult(factor)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
factor | in number | The value to multiply by self |
v | out physics.Vect | The resulting scaled vector |
Multiplies a vector by a factor.
Introduced in platform.apiLevel = '2.0'
near
isnear = physics.Vect:near(vec, distance)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The value to multiply by self |
distance | in number | The distance from vec |
isnear | out boolean | True if self is within distance of vec |
Determines if self is near another vector.
Introduced in platform.apiLevel = '2.0'
neg
v = physics.Vect:neg()
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
v | out physics.Vect | The resulting negated vector |
Returns the negative of self.
The Vect class also implements the unary minus operator (-self).
Introduced in platform.apiLevel = '2.0'
normalize
normvec = physics.Vect:normalize()
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
normvec | out physics.Vect | The resulting normalized vector |
Returns a normalized copy of self. The length of a normal vector is 1.
Introduced in platform.apiLevel = '2.0'
normalizeSafe
normvec = physics.Vect:normalizeSafe()
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
normvec | out physics.Vect | The resulting normalized vector |
Returns a normalized copy of self. Protects against division by zero.
Introduced in platform.apiLevel = '2.0'
perp
perpvec = physics.Vect:perp()
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
perpvec | out physics.Vect | The resulting perpendicular vector |
Returns a vector perpendicular to self. (90 degree rotation)
Introduced in platform.apiLevel = '2.0'
project
pvec = physics.Vect:project(vec)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The other vector |
pvec | out physics.Vect | The vector of self projected onto vec |
Computes the projection of self onto another vector.
Introduced in platform.apiLevel = '2.0'
rotate
rvec = physics.Vect:rotate(vec)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The other vector |
rvec | out physics.Vect | The resulting rotated vector |
Uses complex multiplication to rotate self by vec. Scaling will occur if self is not a unit vector.
Introduced in platform.apiLevel = '2.0'
rperp
perpvec = physics.Vect:rperp()
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
perpvec | out physics.Vect | The resulting perpendicular vector |
Returns a vector perpendicular to self. (90 degree rotation)
Introduced in platform.apiLevel = '2.0'
setx
self = physics.Vect:setx(x)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The vector to modify |
x | in number | The new value of the _x_ component of the vector |
self | out physics.Vect | The input vector is returned as the output |
Changes the value of the x_ component of _self. Returns self.
Introduced in platform.apiLevel = '2.0'
sety
self = physics.Vect:sety(y)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The vector to modify |
y | in number | The new value of the _y_ component of the vector |
self | out physics.Vect | The input vector is returned as the output |
Changes the value of the y_ component of _self. Returns self.
Introduced in platform.apiLevel = '2.0'
slerp
v = physics.Vect:slerp(vec, f)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | A unit vector |
vec | in physics.Vect | The other unit vector |
f | in number | f_ is a fractional number from 0 to 1 representing the proportion of distance between _self and vec |
v | out physics.Vect | A vector interpolated between self and vec |
Computes a spherical linear interpolation between unit vectors self and vec.
See [http://en.wikipedia.org/wiki/Slerp] for a discussion of the meaning, value, and usage of spherical linear interpolation.
slerp
local vect1 = physics.Vect(math.pi/3) -- unit vector with angle pi/3 radians local vect2 = physics.Vect(math.pi/2) -- unit vector with angle pi/2 radians local result = vect1:slerp(vect2, 0.55) -- compute spherical linear interpolation
This routine computes meaningful results only when the two inputs are unit vectors.
May not behave as expected for f larger than 1.0 or less than 0.
Introduced in platform.apiLevel = '2.0'
slerpconst
v = physics.Vect:slerpconst(vec, angle)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | A unit vector |
vec | in physics.Vect | The other unit vector |
angle | in number | The maximum angle between self and vec to interpolate a new vector |
v | out physics.Vect |
Returns the spherical linear interpolation from self towards vec but by no more than angle in radians.
See [http://en.wikipedia.org/wiki/Slerp] for a discussion of the meaning, value, and usage of spherical linear interpolation.
This routine computes meaningful results only when the two inputs are unit vectors.
NOTE: NOTE
|
Introduced in platform.apiLevel = '2.0'
sub
diff = physics.Vect:sub(vec)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | A vector to subtract from self |
diff | out physics.Vect | The vector difference between self and vec |
Returns the vector difference of self and vec.
The Vect class also implements the subtraction operator (-). Therefore vector v2 can be subtracted from v1 with the expression v1 - v2.
Introduced in platform.apiLevel = '2.0'
toangle
angle = physics.Vect:toangle()
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
angle | out number | The angle of self |
Returns the angle in radians of self.
Introduced in platform.apiLevel = '2.0'
unrotate
uvec = physics.Vect:unrotate(vec)
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
vec | in physics.Vect | The other vector |
uvec | out physics.Vect | The resulting unrotated vector |
Inverse of physics.Vect:rotate(vec).
Introduced in platform.apiLevel = '2.0'
x
x = physics.Vect:x()
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
x | out number | The value of the _x_ component of the vector |
Returns the value of the _x_ component of the input vector.
Introduced in platform.apiLevel = '2.0'
y
y = physics.Vect:y()
Parameter | Type | Description |
---|---|---|
self | in physics.Vect | The input vector |
y | out number | The value of the _y_ component of the vector |
Returns the value of the _y_ component of the input vector.
Introduced in platform.apiLevel = '2.0'
This category currently contains no pages or media.