20. Table vec2

Table vec2 is a vector 2d (x,y) class for the most known operations using vector.

20.1. vec2 methods

20.1.1. vec2 new

new

Create a new instance of vec2 with no argument.

Returns

vec2 table.

Example:

local v = vec2:new()
print(v.x,v.y) -- 0,0
new(number x, number * y)

Create a new instance of vec2 passing two arguments.

Parameters
  • numberx value.

  • numbery value (optional).

Returns

vec2 table.

Example:

local v = vec2:new(15,66)
print(v.x,v.y) -- 15,66

v = vec2:new(15)
print(v.x,v.y) -- 15,0
new(vec2 other)

Create a new instance of vec2 passing other vec2 as parameter.

Parameters

vec2other to copy values.

Returns

vec2 table.

Example:

local v1 = vec2:new(15,66)
print(v1.x,v1.y) -- 15,66

local v2 = vec2:new(v1)
print(v2.x,v2.y) -- 15,66
new(vec3 other)

Create a new instance of vec2 passing other vec3 as parameter.

Parameters

vec3other to copy values (x and y).

Returns

vec2 table.

Example:

local v1 = vec3:new(1,2,3)
print(v1.x,v1.y,v1.z) -- 1,2,3

local v2 = vec2:new(v1)
print(v2.x,v2.y) -- 1,2
new(renderizable render)

Create a new instance of vec2 passing a renderizable as parameter.

Parameters

renderizablerender to copy values from position.

Returns

vec2 table.

Example:

local tTexture =  texture:new('2dw')
tTexture:setPos(88,99)

local v2 = vec2:new(tTexture)
print(v2.x,v2.y) -- 88,99

20.1.2. vec2 get

get

Retrieve the values x and y from vec2.

Returns

number x, number y

Example:

local v2 = vec2:new(415,669)
local x,y = v2:get()
print(x,y) -- 415,669

20.1.3. vec2 set

set(number x, number y)

Set new values to vec2 passing two number arguments (x and y).

Parameters
  • numberx value.

  • numbery value (optional).

Example:

local v = vec2:new(15,66)
v:set(88,74)
print(v:get()) -- 88,74

v:set(15) -- Note only x change
print(v:get()) -- 15,74
set(vec2 other)

Set new values to vec2 passing other vec2 as parameter.

Parameters

vec2other to set values.

Example:

local v1 = vec2:new(15,66)
local v2 = vec2:new()
print(v1) -- x:15 y:66
print(v2) -- x:0 y:0

v2:set(v1)
print(v2) -- x:15 y:66
set(vec3 other)

Set new values to vec2 passing other vec3 as parameter.

Parameters

vec3other to set values (x and y).

Example:

local v1 = vec3:new(44,55,33)
local v2 = vec2:new()
print(v1:get()) -- 44,55,33
print(v2:get()) -- 0,0

v2:set(v1)
print(v2) -- x:44 y:55
set(renderizable render)

Set new values to vec2 passing a renderizable as parameter.

Parameters

renderizablerender to set values from position.

Example:

local tTexture =  texture:new('2dw')
tTexture:setPos(12,15)

local v2 = vec2:new()
v2:set(tTexture)
print(v2) --x:12 y:15

20.1.4. vec2 add

add(number x, number y)

Add values to vec2 passing two number arguments (x and y).

Parameters
  • numberx value.

  • numbery value (optional).

Example:

local v = vec2:new(15,66)
v:add(88,74)
print(v:get()) -- 103,140

v:add(15) -- Note.: 103 + 15 and 140 + 15
print(v:get()) -- 118,155
add(vec2 other)

Add values to vec2 passing other vec2 as parameter.

Parameters

vec2other to add values.

Example:

local v1 = vec2:new(15,66)
local v2 = vec2:new(1,2)
print(v1) -- x:15 y:66
print(v2) -- x:1 y:2

v2:add(v1)
print(v2) -- x:16 y:68
add(vec3 other)

Add values to vec2 passing other vec3 as parameter.

Parameters

vec3other to add values (x and y).

Example:

local v1 = vec3:new(44,55,33)
local v2 = vec2:new(5,8)
print(v1:get()) -- 44,55,33
print(v2:get()) -- 5,8

v2:add(v1)
print(v2) -- x:49 y:63
add(renderizable render)

Add values to vec2 passing a renderizable as parameter.

Parameters

renderizablerender to add values from position.

Example:

local tTexture =  texture:new('2dw')
tTexture:setPos(12,15)

local v2 = vec2:new(5,5)
v2:add(tTexture)
print(v2) --x:17 y:20

20.1.5. vec2 sub

sub(number x, number y)

Subtract values in the vec2 passing two number arguments (x and y).

Parameters
  • numberx value.

  • numbery value (optional).

Example:

local v = vec2:new(150,660)
v:sub(88,74)
print(v:get()) -- 62 586

v:sub(15) --note.: 62 - 15, 586 - 15
print(v:get()) -- 47 571
sub(vec2 other)

Subtract values in the vec2 passing other vec2 as parameter.

Parameters

vec2other to subtract values.

Example:

local v1 = vec2:new(15,66)
local v2 = vec2:new(1,2)
print(v1) -- x:15 y:66
print(v2) -- x:1 y:2

v2:sub(v1)
print(v2) -- x:-14 y:-64
sub(vec3 other)

Subtract values in the vec2 passing other vec3 as parameter.

Parameters

vec3other to subtract values (x and y).

Example:

local v1 = vec3:new(44,55,33)
local v2 = vec2:new(5,8)
print(v1:get()) -- 44,55,33
print(v2:get()) -- 5,8

v2:sub(v1)
print(v2) --  x:-39 y:-47
sub(renderizable render)

Subtract values in the vec2 passing a renderizable as parameter.

Parameters

renderizablerender to subtract values from position.

Example:

local spt =  sprite:new('2dw')
spt:setPos(12,15)

local v2 = vec2:new(5,5)
v2:sub(spt)
print(v2) --   x:-7 y:-10

20.1.6. vec2 mul

mul(number x, number y)

Multiply (scalar) values to vec2 passing two number arguments (x and y).

Parameters
  • numberx value.

  • numbery value (optional).

Example:

local v = vec2:new(2,3)
v:mul(5,2)
print(v:get()) -- 10 6

v:mul(2) -- Note.: 10 * 2 and 6 * 2
print(v:get()) -- 20 12
mul(vec2 other)

Multiply (scalar) values to vec2 passing other vec2 as parameter.

Parameters

vec2other to multiply values.

Example:

local v1 = vec2:new(2,3)
local v2 = vec2:new(3,4)
print(v1) -- x:2 y:3
print(v2) -- x:3 y:4

v2:mul(v1)
print(v2) -- 6 12
mul(vec3 other)

Multiply (scalar) values by vec2 passing other vec3 as parameter.

Parameters

vec3other to multiply values (x and y).

Example:

local v1 = vec3:new(2,3,4)
local v2 = vec2:new(2,1.5)
print(v1:get()) -- 2,3,4
print(v2:get()) -- 2,1.5

v2:mul(v1)
print(v2) --  4, 4.5
mul(renderizable render)

Multiply (scalar) values by vec2 passing a renderizable as parameter.

Parameters

renderizablerender to multiply values by position.

Example:

local spt =  sprite:new('2dw')
spt:setPos(2.2,3.3)

local v2 = vec2:new(3,4)
v2:mul(spt)
print(v2) --   x:6.6 y:13.2

20.1.7. vec2 div

Note

Any number divided by zero is an error however the engine set the result to 0.

div(number x, number y)

Divide values by vec2 passing two number arguments (x and y).

Parameters
  • numberx value.

  • numbery value (optional).

Example:

local v = vec2:new(2,3)
v:div(2,1.5)
print(v:get()) -- 1 2

v:div(2) -- Note.: 1 / 2 and 2 / 2
print(v:get()) -- 0.5    1

v:div(0) -- Note.: 0.5 / 0 and 1 / 0 not throw an exception and the result is set to 0
print(v) -- x:0 y:0
div(vec2 other)

Divide values by vec2 passing other vec2 as parameter.

Parameters

vec2other to divide values.

Example:

local v1 = vec2:new(20,30)
local v2 = vec2:new(2,4)
print(v1) -- x:20 y:30
print(v2) -- x:2 y:4

v1:div(v2)
print(v1) -- x:10 y:7.5

v2.y = 0
v1:div(v2)
print(v1) -- x:5 y:0

v1:set(10,20)
v2:set(0,10)
v1:div(v2)-- Note.: 10 / 0 not throw an exception and the result is set to 0
print(v1) -- x:0 y:2
div(vec3 other)

Divide values by vec2 passing other vec3 as parameter.

Parameters

vec3other to divide values by (x and y).

Example:

local v1 = vec3:new(2,3,4)
local v2 = vec2:new(2,1.5)
print(v1:get()) -- 2,3,4
print(v2:get()) -- 2,1.5

v2:div(v1)
print(v2) --  x:1 y:0.5
div(renderizable render)

Divide values by vec2 passing a renderizable as parameter.

Parameters

renderizablerender to divide values by position.

Example:

local spt =  sprite:new('2dw')
spt:setPos(2,50)

local v2 = vec2:new(200,300)
v2:div(spt)
print(v2) --   x:100 y:6

20.1.8. vec2 length

The formula to length is:

\sqrt{(x * x) + (y * y)}

length

Get the length from vec2. Same as len attribute.

Returns

number - length of vector.

Example:

local v = vec2:new(100,300)
print(v:length()) -- 316.2278

20.1.9. vec2 azimuth

azimuth

Get the azimuth from vec2.

Returns

number - azimuth of vector (radian).

Example:

local v = vec2:new(1,0)
print(math.deg(v:azimuth())) -- 90
azimuth(vec2 v2)

Get the azimuth between two vec2.

Returns

number - azimuth between two vectors (radian).

Example:

local v1 = vec2:new(1,0)
local v2 = vec2:new(0,1)
print(math.deg(v1:azimuth(v2))) -- 135

20.1.10. vec2 move

move(number x, number * y)

Move x and y units frames by seconds. This method consider the FPS.

Parameters
  • numberx position.

  • numbery position (optional).

Example:

local v = vec2:new(5,55)
v:move(100,50)

--this is equivalent
function loop(delta)
     v.x = v.x + (delta * 100)
     v.y = v.y + (delta * 50)
end

20.1.11. vec2 dot

A dot product is an algebraic operation that takes two equal-length sequences of numbers and returns a single number.
Learn more at wiki dot product .
The formula to vec2 dot is:

(x * x) + (y * y)

dot(vec2 b)

Calculate a dot product value to vec2 passing other vec2 as parameter.

Parameters

vec2parameter.

Returns

number dot calculated.

Example:

a = vec2:new(3,4)            --x = 3      y = 4
b = vec2:new(10,25)          --x = 10     y = 25
local dot_result = a:dot(b)  --3 * 10 + 4 * 25 = 130

print(dot_result)            -- 130
dot(vec2 a, vec2 b)

Calculate a dot product value to vec2 between a and b vec2 as parameter.

Parameters
  • vec2a a parameter.

  • vec2b b parameter.

Returns

number dot calculated.

Example:

a = vec2:new(3,4)              --x = 3      y = 4
b = vec2:new(10,25)            --x = 10     y = 25
c = vec2:new(1,2)              --x = 1      y = 2
local dot_result = c:dot(a,b)  --3 * 10 + 4 * 25 = 130

print(dot_result)              -- 130

20.1.12. vec2 lerp

Linear interpolation is a method of curve fitting using linear polynomials to construct new data points within the range of a discrete set of known data points.
The formula to vec2 lerp is:

v = (1 - s) * (v1) + s * (v2)
lerp(vec2 a, vec2 b, number s)

Calculate a linear interpolation between two vec2. Store the result on itself.

Parameters
  • vec2a a parameter.

  • vec2b b parameter.

  • numbers parameter.

Example:

local a = vec2:new(20,30)
local b = vec2:new(10,15)
local c = vec2:new()       --x = 0     y = 0
c:lerp(a,b,0.5)            --Keep the result on 'c' vector
print(c)                   --x:15 y:22.5

20.1.13. vec2 normalize

A normal vector is a vector that is perpendicular to a given object.
The formula to vec2 normal is:

\Hat{U} = \frac{U}{|U|}

where |u| is the length of u.
normalize

Normalize a vec2. Store the result on itself.

Example:

local a = vec2:new(500,100)
a:normalize()
print(a)   -- x:0.9805807 y:0.1961161
normalize(vec2 out)

Normalize a vec2 and store the result in the vec2 out.

Parameters

vec2out as parameter.

Example:

local a = vec2:new(500,100)
local b = vec2:new()
a:normalize(b)
print(a)   -- x:500 y:100
print(b)   -- x:0.9805807 y:0.1961161

20.2. vec2 attributes

x, y [read / write]

Access the position x and y member from vec2.

Example:

local v = vec2:new()
v.x = 100
v.y = 200

print(v) -- x:100 y:200
len [ read only ]
Call len method as member from vec2 for read only.
Same as length

Example:

local v = vec2:new()
v.x = 100
v.y = 200

print(v.len) -- 223.6068

20.3. User data acknowledgment

This table uses the first index to store the userdata C++ class internally, and is not allowed for vec2 to write / read to index.

Listing 20.1 Do not do this:
1 v = vec2:new()
2 v:set(10,10)
3 
4 print(v[1]) -- read, okay will print userdata: 0x44958588a6a8 for example
5 
6 v[1] = 5 -- Error, not allowed for this table. program will crash!