2020-01-29 03:26:28 +00:00
|
|
|
--[[
|
|
|
|
extra mathematical functions
|
|
|
|
]]
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
local mathx = setmetatable({}, {
|
|
|
|
__index = math,
|
|
|
|
})
|
2020-01-29 03:26:28 +00:00
|
|
|
|
|
|
|
--wrap v around range [lo, hi)
|
2020-04-07 03:49:10 +00:00
|
|
|
function mathx.wrap(v, lo, hi)
|
2020-07-09 05:32:42 +00:00
|
|
|
return (v - lo) % (hi - lo) + lo
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2020-04-09 07:39:46 +00:00
|
|
|
--wrap i around the indices of t
|
|
|
|
function mathx.wrap_index(i, t)
|
|
|
|
return math.floor(mathx.wrap(i, 1, #t + 1))
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--clamp v to range [lo, hi]
|
2020-04-07 03:49:10 +00:00
|
|
|
function mathx.clamp(v, lo, hi)
|
2020-01-29 03:26:28 +00:00
|
|
|
return math.max(lo, math.min(v, hi))
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
--clamp v to range [0, 1]
|
|
|
|
function mathx.clamp01(v)
|
|
|
|
return mathx.clamp(v, 0, 1)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2020-07-09 05:32:42 +00:00
|
|
|
--round v to nearest whole, away from zero
|
2020-04-07 03:49:10 +00:00
|
|
|
function mathx.round(v)
|
2020-07-09 05:32:42 +00:00
|
|
|
if v < 0 then
|
|
|
|
return math.ceil(v - 0.5)
|
|
|
|
end
|
2020-01-29 03:26:28 +00:00
|
|
|
return math.floor(v + 0.5)
|
|
|
|
end
|
|
|
|
|
|
|
|
--round v to one-in x
|
|
|
|
-- (eg x = 2, v rounded to increments of 0.5)
|
2020-04-07 03:49:10 +00:00
|
|
|
function mathx.to_one_in(v, x)
|
|
|
|
return mathx.round(v * x) / x
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--round v to a given decimal precision
|
2020-04-07 03:49:10 +00:00
|
|
|
function mathx.to_precision(v, decimal_points)
|
|
|
|
return mathx.to_one_in(v, math.pow(10, decimal_points))
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--0, 1, -1 sign of a scalar
|
2021-06-29 04:44:09 +00:00
|
|
|
--todo: investigate if a branchless or `/abs` approach is faster in general case
|
2020-04-07 03:49:10 +00:00
|
|
|
function mathx.sign(v)
|
2020-01-29 03:26:28 +00:00
|
|
|
if v < 0 then return -1 end
|
|
|
|
if v > 0 then return 1 end
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
|
|
|
--linear interpolation between a and b
|
2020-04-07 03:49:10 +00:00
|
|
|
function mathx.lerp(a, b, t)
|
2020-01-29 03:26:28 +00:00
|
|
|
return a * (1.0 - t) + b * t
|
|
|
|
end
|
|
|
|
|
2020-04-01 09:58:18 +00:00
|
|
|
--linear interpolation with a minimum "final step" distance
|
|
|
|
--useful for making sure dynamic lerps do actually reach their final destination
|
2020-04-07 03:49:10 +00:00
|
|
|
function mathx.lerp_eps(a, b, t, eps)
|
|
|
|
local v = mathx.lerp(a, b, t)
|
2020-04-01 09:58:18 +00:00
|
|
|
if math.abs(v - b) < eps then
|
|
|
|
v = b
|
|
|
|
end
|
|
|
|
return v
|
|
|
|
end
|
|
|
|
|
2020-07-27 11:47:06 +00:00
|
|
|
--bilinear interpolation between 4 samples
|
|
|
|
function mathx.bilerp(a, b, c, d, u, v)
|
|
|
|
return math.lerp(
|
|
|
|
math.lerp(a, b, u),
|
|
|
|
math.lerp(c, d, u),
|
|
|
|
v
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-24 01:44:53 +00:00
|
|
|
--easing curves
|
|
|
|
--(generally only "safe" for 0-1 range, see mathx.clamp01)
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--classic smoothstep
|
2021-09-24 01:44:53 +00:00
|
|
|
function mathx.smoothstep(f)
|
|
|
|
return f * f * (3 - 2 * f)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
--classic smootherstep; zero 2nd order derivatives at 0 and 1
|
2021-09-24 01:44:53 +00:00
|
|
|
function mathx.smootherstep(f)
|
|
|
|
return f * f * f * (f * (f * 6 - 15) + 10)
|
|
|
|
end
|
|
|
|
|
|
|
|
--pingpong from 0 to 1 and back again
|
|
|
|
function mathx.pingpong(f)
|
2021-09-24 01:48:25 +00:00
|
|
|
return 1 - math.abs(f - 0.5) * 2
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2021-09-24 01:44:53 +00:00
|
|
|
--quadratic ease in
|
|
|
|
function mathx.ease_in(f)
|
|
|
|
return f * f
|
|
|
|
end
|
|
|
|
|
|
|
|
--quadratic ease out
|
|
|
|
function mathx.ease_out(f)
|
|
|
|
local oneminus = (1 - f)
|
|
|
|
return 1 - oneminus * oneminus
|
|
|
|
end
|
|
|
|
|
|
|
|
--quadratic ease in and out
|
|
|
|
--(a lot like smoothstep)
|
|
|
|
function mathx.ease_inout(f)
|
|
|
|
if t < 0.5 then
|
|
|
|
return t * t * 2
|
|
|
|
end
|
|
|
|
local oneminus = (1 - f)
|
|
|
|
return 1 - 2 * oneminus * oneminus
|
|
|
|
end
|
|
|
|
|
|
|
|
--branchless but imperfect quartic in/out
|
|
|
|
--either smooth or smootherstep are usually a better alternative
|
|
|
|
function mathx.ease_inout_branchless(f)
|
|
|
|
local halfsquared = f * f / 2
|
|
|
|
return halfsquared * (1 - halfsquared) * 4
|
|
|
|
end
|
2021-05-04 01:41:22 +00:00
|
|
|
|
2021-09-24 01:44:53 +00:00
|
|
|
--todo: more easings - back, bounce, elastic
|
2021-05-04 01:41:22 +00:00
|
|
|
|
|
|
|
--(internal; use a provided random generator object, or not)
|
|
|
|
local function _random(_r, ...)
|
|
|
|
if _r then return _r:random(...) end
|
|
|
|
if love then return love.math.random(...) end
|
|
|
|
return math.random(...)
|
|
|
|
end
|
|
|
|
|
|
|
|
--return a random sign
|
|
|
|
function mathx.random_sign(_r)
|
|
|
|
return _random(_r) < 0.5 and -1 or 1
|
|
|
|
end
|
|
|
|
|
|
|
|
--return a random value between two numbers (continuous)
|
|
|
|
function mathx.random_lerp(min, max, _r)
|
|
|
|
return math.lerp(min, max, _random(_r))
|
|
|
|
end
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2020-05-20 11:02:51 +00:00
|
|
|
--nan checking
|
|
|
|
function mathx.isnan(v)
|
|
|
|
return v ~= v
|
|
|
|
end
|
|
|
|
|
2020-01-29 03:26:28 +00:00
|
|
|
--angle handling stuff
|
2020-04-09 07:39:46 +00:00
|
|
|
--superior constant handy for expressing things in turns
|
|
|
|
mathx.tau = math.pi * 2
|
|
|
|
|
|
|
|
--normalise angle onto the interval [-math.pi, math.pi)
|
|
|
|
--so each angle only has a single value representing it
|
2020-04-07 03:49:10 +00:00
|
|
|
function mathx.normalise_angle(a)
|
|
|
|
return mathx.wrap(a, -math.pi, math.pi)
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2020-04-09 07:39:46 +00:00
|
|
|
--alias for americans
|
|
|
|
mathx.normalize_angle = mathx.normalise_angle
|
|
|
|
|
2020-05-01 02:59:58 +00:00
|
|
|
--get the normalised difference between two angles
|
|
|
|
function mathx.angle_difference(a, b)
|
2020-04-30 06:38:59 +00:00
|
|
|
a = mathx.normalise_angle(a)
|
|
|
|
b = mathx.normalise_angle(b)
|
2020-05-01 02:59:58 +00:00
|
|
|
return mathx.normalise_angle(b - a)
|
2020-04-30 06:38:59 +00:00
|
|
|
end
|
|
|
|
|
2020-05-01 02:59:58 +00:00
|
|
|
--mathx.lerp equivalent for angles
|
2020-04-30 06:38:59 +00:00
|
|
|
function mathx.lerp_angle(a, b, t)
|
2020-05-01 02:59:58 +00:00
|
|
|
local dif = mathx.angle_difference(a, b)
|
|
|
|
return mathx.normalise_angle(a + dif * t)
|
|
|
|
end
|
|
|
|
|
|
|
|
--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
|
2020-01-29 03:26:28 +00:00
|
|
|
end
|
|
|
|
|
2020-09-02 03:34:10 +00:00
|
|
|
--geometric functions standalone/"unpacked" components and multi-return
|
|
|
|
--consider using vec2 if you need anything complex!
|
|
|
|
|
|
|
|
--rotate a point around the origin by an angle
|
2020-04-07 03:49:10 +00:00
|
|
|
function mathx.rotate(x, y, r)
|
2020-01-29 03:26:28 +00:00
|
|
|
local s = math.sin(r)
|
|
|
|
local c = math.cos(r)
|
|
|
|
return c * x - s * y, s * x + c * y
|
|
|
|
end
|
2020-03-15 09:28:50 +00:00
|
|
|
|
2020-09-02 03:34:10 +00:00
|
|
|
--get the length of a vector from the origin
|
|
|
|
function mathx.length(x, y)
|
|
|
|
return math.sqrt(x * x + y * y)
|
|
|
|
end
|
|
|
|
|
|
|
|
--get the distance between two points
|
|
|
|
function mathx.distance(x1, y1, x2, y2)
|
|
|
|
local dx = x1 - x2
|
|
|
|
local dy = y1 - y2
|
|
|
|
return mathx.length(dx, dy)
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
return mathx
|