Hi again. The code looks better although I still think it's way more complicated than it needs to be.
For example:
Code: Select all
function vlength(x,y)
x = math.abs(x)
y = math.abs(y)
if x ~= 0 and y ~= 0 then
return math.sqrt(x^2 + y^2)
else
return x+y
end
end
Could be simplified to:
Code: Select all
function vlength(x, y)
return math.sqrt(x * x + y * y)
end
I personally write n*n instead of n^2 since the latter probably does a ''pow' function call which is unnecessary in this case.
Code: Select all
function setvlength(x, y, length, newlength)
newlength = newlength / length
return x*newlength, y*newlength
end
I think this function might be better if you didn't have to supply the old length of the vector:
Code: Select all
function setvlength(x, y, len)
local d = math.sqrt(x * x + y * y)
d = 1/d * len
return x*d, y*d
end
BTW, you have to be careful not to call the above func with a 0 length lector or you'll get a division by 0.
Code: Select all
local xspdc = math.abs(xspd)
local yspdc = math.abs(yspd)
if xspdc == yspdc then
xspd = xspd * circle
yspd = yspd * circle
end
Here, I would have used the 'clamp_vec' function from my last post.
Or perhaps you can just normalize the vector by hand which I think is much more clear
(primarily because it doesn't need the 'circle' global):
Code: Select all
local d = vlength(xspd,yspd)
if d > 1 then
xspd, yspd = xspd/d, yspd/d
end
Regarding 'friction': you are applying it in a linear manner:
Can't say for sure but I think the Box2D approach that I mentioned is probably smoother:
Code: Select all
v = v * clamp(1 - damping * dt, 0, 1)
Lastly, I would advise against using table to represent vectors.
In my experience, it's an unnecessary strain on the Lua garbage collector.
Hope this helps