leef-math-cd2025/spec/utils_spec.lua
David Briscoe 39aee9a421 Fix angle_to to produce signed angle
angle_to was producing the angle from +x to the difference between a,b
which is unexpected. Instead, it should produce the smallest absolute
angle between the two vectors and be signed to indicate the direction of
rotation.

By using the old angle_to implementation and modifying equal() to print
out the failures, you can see the numbers that it was producing before
didn't make much sense:

    right:angle_to(down) = 45.0
    right:angle_to(left) = 0.0
    right:angle_to(up)   = -45.0
    down:angle_to(right) = -135.0
    down:angle_to(left)  = -45.0
    down:angle_to(up)    = -90.0
    left:angle_to(down)  = 135.0
    left:angle_to(up)    = -135.0
    up:angle_to(right)   = 135.0
    up:angle_to(down)    = 90.0
    up:angle_to(left)    = 45.0

Now it produces numbers you'd expect:

    right:angle_to(down) = -90.0
    right:angle_to(left) = 180.0
    right:angle_to(up)   = 90.0
    down:angle_to(right) = 90.0
    down:angle_to(left)  = -90.0
    down:angle_to(up)    = 180.0
    left:angle_to(down)  = 90.0
    left:angle_to(up)    = -90.0
    up:angle_to(right)   = -90.0
    up:angle_to(down)    = 180.0
    up:angle_to(left)    = 90.0

See also https://stackoverflow.com/questions/21483999/using-atan2-to-find-angle-between-two-vectors

Also added tests for angle_between.
2021-06-15 11:56:04 -07:00

49 lines
1.1 KiB
Lua

local vec3 = require "modules.vec3"
local utils = require "modules.utils"
local constants = require "modules.constants"
local function tolerance(v, t)
return math.abs(v - t) < 1e-6
end
describe("utils:", function()
it("interpolates between two numbers", function()
assert.is_true(tolerance(utils.lerp(0, 1, 0.5), 0.5))
end)
it("interpolates between two vectors", function()
local a = vec3(0, 0, 0)
local b = vec3(1, 1, 1)
local c = vec3(0.5, 0.5, 0.5)
assert.is.equal(utils.lerp(a, b, 0.5), c)
a = vec3(5, 5, 5)
b = vec3(0, 0, 0)
c = vec3(2.5, 2.5, 2.5)
assert.is.equal(utils.lerp(a, b, 0.5), c)
end)
it("decays exponentially", function()
local v = utils.decay(0, 1, 0.5, 1)
assert.is_true(tolerance(v, 0.39346934028737))
end)
end)
--[[
clamp(value, min, max)
deadzone(value, size)
threshold(value, threshold)
tolerance(value, threshold)
map(value, min_in, max_in, min_out, max_out)
lerp(progress, low, high)
smoothstep(progress, low, high)
round(value, precision)
wrap(value, limit)
is_pot(value)
project_on(out, a, b)
project_from(out, a, b)
mirror_on(out, a, b)
reflect(out, i, n)
refract(out, i, n, ior)
--]]