Przemator wrote:@Roland: yes, it did help. However, it is extremely hard to learn how it works
.
Be sure to check out the
documentation: Every function has at least one example how to use it.
Przemator wrote:only makes me wonder why was the vector.dist(a, b) implemented in the code, and not directly vector:dist(b). the vector.dist function cannot be used anyway.
It is really just a renaming of the variables.
vector:dist(b) is the same as writing
vector.dist(self, b).
As to why you can't use the
vector.dist(a,b),
vector.clone(v), etc notation, see
how the vector-table is defined and
how the module is returned.
If you really want to, you can use this to get access to the vector table:
Code: Select all
vector = require 'hump.vector'
vector = getmetatable(vector(0,0))
Drawback: you need to use
vector.new() instead of the shortcut notation:
Code: Select all
a = vector.new(10, 20)
b = vector.new(30, 40)
dst = vector.dist(a, b)
Przemator wrote:if I wanted to make this work, would i have to write dist = dist in the return statement?
Could you rephrase that question?
Przemator wrote:EDIT: BTW I have a very CPU-intensive function for calculating the distance between a point and a curve. It is run for every segment of the curve, which has about 1000 segments, in every love.update, so the segment function is run 6000 times per second. I made 2 versions of it:
[snip]
Using the function based on hump.vector, it takes 0.01 second to calculate (almost the whole dt!). The segdist2 takes 0.0007 second, so it is 15 times faster! So I guess the vector class is REALLY heavy.
Well, (nearly) each vector operation creates a new table, and while that's ok in most cases, it adds up if you do it very (
very) often per frame. That's why there is
hump.vector-light.
But your comparison is a little bit unfair, as the second function is a bit more optimized than the first one. Also, the first variant creates about 9 vectors in the worst case, so thats about 9000 new tables
per frame.
Here are three alternatives, one using vector and one using vector-light and one using neither (all untested):
Code: Select all
-- using hump.vector
function segdist(p, a, b) -- creates 3 vectors
local pa, ba = p - a, b - a
local l2 = ba:len2()
if l2 == 0 then return pa:len() end -- or p:dist(a)
local t = pa * ba / l2 -- basically already pa:projectOn(ba)
if t < 0 then return pa:len() end
if t > 1 then return p:dist(b) end
local proj = a + ba * t / l2
return p:dist(proj)
end
-- using hump.vector-light
function segdist(px, py, ax, ay, bx, by)
local pax, pay = px - ax, py - ay
local bax, bay = bx - ax, by - ay
local l2 = vector.len2(bax, bay)
if l2 == 0 then return vector.len(pax, pay) end -- or vector.dist(ax, ay, px, py)
local t = vector.dot(pax, pay, bax, bay) / l2
if t < 0 then return vector.len(pax, pay) end -- or vector.dist(ax, ay, px, py)
if t > 1 then return vector.dist(bx, by, px, py) end
local qx, qy = vector.add(ax,ay, vector.mul(bax, bay, t / l2))
return vector.dist(px, py, qx, py)
end
-- using neither
function segdist(px, py, ax, ay, bx, by)
local pax, pay = px - ax, py - ay
local bax, bay = bx - ax, by - ay
local l2 = bax*bax + bay*bay
if l2 == 0 then return math.sqrt(pax*pax, pay*pay) end
local t = (pax * bax + pay * bay) / l2
if t < 0 then return math.sqrt(pax*pax + pay*pay) end
if t > 1 then return math.sqrt((px-bx)^2 + (py-by)^2) end
local qx, qy = ax + (bax * t / l2), ay + bay * t / l2
return math.sqrt((px - qx)^2 + (py - qy)^2)
end
I am not sure whether the vector-light or no-library version is faster, but I think the former is more readable than the latter - which is also important