Lua Vector (update 2)

mrsimb - Custom level - from Windows
PlayEdit7 players liked this.Log in to like this level.

To use Vector in your Lua project, just copy compressed version from this this level to end of Lua script code.
Or copy it from here: http://pastebin.com/gZSvYxLQ
Standart version: http://pastebin.com/gi4Q4pta
I would appreciate if you'll found any bugs or miscalculations.

Create new vector from given parameters
v1 = Vector(1, 2, 3)
array = {4, 5, 6}
v2 = Vector(array)

You can use skip some parameters
v1 = Vector() - [0, 0, 0]
v1 = Vector(1) - [1, 0, 0]

Create vector from given angle
Vector.fromAngle(math.pi)

Create 2D vector from random angle
Vector.random2d()

Create 3D vector from random angle
Vector.random3d()

You can use basic arithmetics operators
v3 = (v1 + v2) * 0.5

Access to vector components
v1:x or v1[1]
v1:y or v1[2]
v1:z or v1[3]

Set vector components
v1:set(0.5, 10, 2)
v1:set(array)

Create vector copy
v2 = v1:copy()

Get vector string representation
str = v1:toString()
str = tostring(v1)

Get vector array representation
array = v1:toArray()

Adds values to vector
v1:add(1, 1, 1)
v1:add(array)

Substract values from vector
v1:sub(1, 1, 1)
v1:sub(array)

Multpiply vector components
v1:mul(0.5)

Divide vector components
v1:div(3)

Raise vector components to a power
v1:pow(4)

Equality check againist another vector
v1:eq(v2)
v1:eq(array)

Vector magnitude is less than another's
v1:lt(v2)
v1:lt(array)

Vector magnitude is less or equals another's
v1:le(v2)
v1:le(array)

Get vector magnitude (length)
v1:mag()

Squared magnitude (faster)
v1:magSq()

Distance beetween vector points
v1:dist(v2)

Squared distance beetween vector points
v1:distSq(v2)

Cross product of vectors
v1:cross(v2)

Dot product of vectors
v1:dot(v2)

Normalize vector
v1:norm()

Limit magnitude of vector to max value
v1:limit(5)

Angle of rotation for vector in radians (only 2D vectors)
v1:angle()

Angle between vectors in radians
v1:angleBetween(v2)

Rotates vector by angle in radians (only 2D vectors)
v1:rotate(math.pi / 2)

Linear interpolation from one vector to another
v1:lerp(v2, 0.5)

Checks if variable is vector
Vector.isVector(v1)

Views: 605 Downloads: 230 Unique objects: 2 Total objects: 8

Discuss this level

Log in to comment on this level.
  • mrsimb: can someone make a beautiful logo for this level? with big "Vector" label
  • mrsimb: done.
  • mrsimb: i will also modify Vector metatable to have basic arithmetics without calling functions, like v1 + v2 (not v1.add(v2))
  • mrsimb: i'll add full reference in description later. for now you can open link for standart version and read comments for each function.

LEVEL ID: 25141