[added] mathx.relative_angle replaced with mathx.angle_difference and direction "fixed"

Fixes issue #6 after discussion was in favour of change.
This commit is contained in:
Max Cahill 2020-05-01 12:59:58 +10:00
parent 9afbf08f34
commit 4a2505a04d

View File

@ -174,20 +174,31 @@ end
--alias for americans --alias for americans
mathx.normalize_angle = mathx.normalise_angle mathx.normalize_angle = mathx.normalise_angle
--get the relative turn between two angles --get the normalised difference between two angles
--note: cam considering reversing this, as the (b - a) version is more useful and intuitive... function mathx.angle_difference(a, b)
function mathx.relative_angle(a, b)
a = mathx.normalise_angle(a) a = mathx.normalise_angle(a)
b = mathx.normalise_angle(b) b = mathx.normalise_angle(b)
return mathx.normalise_angle(a - b) return mathx.normalise_angle(b - a)
end end
--lerp between two angles --mathx.lerp equivalent for angles
function mathx.lerp_angle(a, b, t) function mathx.lerp_angle(a, b, t)
--(note: if we change relative_angle this will need changing, local dif = mathx.angle_difference(a, b)
-- it's a good example of why relative_angle should probably change) return mathx.normalise_angle(a + dif * t)
local dif = mathx.relative_angle(a, b) end
return mathx.normalise_angle(a - dif * t)
--mathx.lerp_eps equivalent for angles
function mathx.lerp_angle_eps(a, b, t, eps)
--short circuit to avoid having to wrap so many angles
if a == b then
return a
end
--same logic as lerp_eps
local v = mathx.lerp_angle(a, b, t)
if math.abs(mathx.angle_difference(v, b)) < eps then
v = b
end
return v
end end
--geometric rotation with multi-return --geometric rotation with multi-return