If the direction is a unit vector instead of an angle, as it should, transposition can be as simple as:
Code: Select all
function transposition(x, y, direction_x, direction_y, distance)
return distance*direction_x+x, distance*direction_y+y
end
In fact, it's intuitive enough for not needing a function for it, you can just place the formulas inline where needed. IMO the function would be counter-productive clarity wise.
You would find direction by normalizing the difference between target and origin:
Code: Select all
function normalize(x, y)
local length = distance(0, 0, x, y)
if length == 0 then
return 1, 0
end
length = 1 / length
return x * length, y * length
end
function direction(x1, y1, x2, y2)
return normalize(x2 - x1, y2 - y1)
end
That saves a sine and cosine every time you need the direction vector.
You only need atan2 because unfortunately, Löve doesn't accept a direction vector as input for the rotation argument in love.draw & co.