Another Lua Vector library

Showcase your libraries, tools and other projects that help your fellow love users.
damv
Prole
Posts: 18
Joined: Thu Oct 11, 2018 9:09 pm

Re: Another Lua Vector library

Post by damv »

User avatar
pgimeno
Party member
Posts: 3544
Joined: Sun Oct 18, 2015 2:58 pm

Re: Another Lua Vector library

Post by pgimeno »

damv wrote: Tue Jul 30, 2019 10:55 pm Here is the improved version: https://github.com/DeybisMelendez/lua-v ... tag/v0.7.0
github wrote:

Code: Select all

vecMt.__index = vecMt
local mt = { -- Metatable of vector
    __call = function(_, x, y)
...
I think you want:

Code: Select all

vecMt.__index = vecMt
vecMt.__call = function(_, x, y)
...
Also, you seem to have mixed tabs and spaces. If you look at https://github.com/DeybisMelendez/lua-v ... vector.lua you'll see the difference between lines 67 and before, and 68 and after, because GitHub uses a tab size of 8.

Personally I'm more of the "no tabs ever" school but I know that's not too popular with Lua.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Another Lua Vector library

Post by ReFreezed »

Nelvin wrote: Tue Jul 30, 2019 9:09 pm If you really care about performance (maybe just in special areas of some innerloops where you need the best possible performance) you could easily localize the methods in question, shortcutting all the overhead described above and directly call them with your tables.
I just want to add that the absolutely most performant code does not use tables at all. The fastest vector library would simply treat 2D vectors as two numbers:

Code: Select all

local vec1x, vec1y = 4, 2
local vec2x, vec2y = 2, 4

vec1x, vec1y = vectorRotate( vec1x,vec1y, math.pi/3 )

local dotResult = vectorDot( vec1x,vec1y, vec2x,vec2y )
It's a bit less elegant, but it's way faster and more memory efficient.
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
damv
Prole
Posts: 18
Joined: Thu Oct 11, 2018 9:09 pm

Re: Another Lua Vector library

Post by damv »

ReFreezed wrote: Sat Aug 03, 2019 12:21 pm
Nelvin wrote: Tue Jul 30, 2019 9:09 pm If you really care about performance (maybe just in special areas of some innerloops where you need the best possible performance) you could easily localize the methods in question, shortcutting all the overhead described above and directly call them with your tables.
I just want to add that the absolutely most performant code does not use tables at all. The fastest vector library would simply treat 2D vectors as two numbers:

Code: Select all

local vec1x, vec1y = 4, 2
local vec2x, vec2y = 2, 4

vec1x, vec1y = vectorRotate( vec1x,vec1y, math.pi/3 )

local dotResult = vectorDot( vec1x,vec1y, vec2x,vec2y )
It's a bit less elegant, but it's way faster and more memory efficient.
What do you think if I change it as follows:

Code: Select all

vector = require "vector"
--variables
vec1 = vector(5, 5)
vec2 = vector(3, 6)
-- The vector class contains the methods...
local distance = vector.distanceTo(vec1, vec2) --get the distance between 2 vectors...
vec1 = vector.rotated(vec1, math.pi)
-- The vector instance contains the metatable methods
print(tostring(vec1 + vec2))
damv
Prole
Posts: 18
Joined: Thu Oct 11, 2018 9:09 pm

Re: Another Lua Vector library

Post by damv »

pgimeno wrote: Tue Jul 30, 2019 11:42 pm
Also, you seem to have mixed tabs and spaces. If you look at https://github.com/DeybisMelendez/lua-v ... vector.lua you'll see the difference between lines 67 and before, and 68 and after, because GitHub uses a tab size of 8.

Personally I'm more of the "no tabs ever" school but I know that's not too popular with Lua.
As for the spaces and tabs, I have that problem with Atom, I think I do not have it well configured, I will have to see how to fix it, although Travis gives me the "build: passing".
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 36 guests