swap lerp arg order, fix tests

This commit is contained in:
Colby Klein 2017-03-29 07:42:40 -07:00
parent c7622dd91b
commit 65e3676af5
5 changed files with 25 additions and 23 deletions

View File

@ -3,6 +3,7 @@
local modules = (...):gsub('%.[^%.]+$', '') .. "."
local constants = require(modules .. "constants")
local mat4 = require(modules .. "mat4")
local vec3 = require(modules .. "vec3")
local utils = require(modules .. "utils")
local DBL_EPSILON = constants.DBL_EPSILON
@ -358,7 +359,7 @@ function intersect.aabb_obb(aabb, obb)
local a = aabb.extent
local b = obb.extent
local T = obb.position - aabb.position
local rot = obb.rotation:transpose()
local rot = mat4():transpose(obb.rotation)
local B = {}
local t

View File

@ -69,22 +69,22 @@ end
--- Linear interpolation.
-- Performs linear interpolation between 0 and 1 when `low` < `progress` < `high`.
-- @param progress (0-1)
-- @param low value to return when `progress` is 0
-- @param high value to return when `progress` is 1
-- @param progress (0-1)
-- @return number
function utils.lerp(progress, low, high)
function utils.lerp(low, high, progress)
return ((high - low) + low) * progress
end
-- Exponential decay
-- @param rate portion of the original value remaining per second
-- @param low initial value
-- @param high target value
-- @param rate portion of the original value remaining per second
-- @param dt time delta
-- @return number
function utils.decay(rate, low, high, dt)
return utils.lerp(1.0 - math.exp(-rate * dt), low, high)
function utils.decay(low, high, rate, dt)
return utils.lerp(low, high, 1.0 - math.exp(-rate * dt))
end
--- Hermite interpolation.

View File

@ -170,7 +170,7 @@ describe("intersect:", function()
end)
it("intersects an aabb with an obb", function()
local r = mat4():rotate(math.pi / 4, vec3.unit_z)
local r = mat4():rotate(mat4(), math.pi / 4, vec3.unit_z)
local a = {
position = vec3(),

View File

@ -142,7 +142,7 @@ describe("mat4:", function()
3, 7, 11, 15,
4, 8, 12, 16
}
local c = a:mul(b)
local c = mat4():mul(a, b)
local d = a * b
assert.is.equal(30, c[1])
assert.is.equal(70, c[2])
@ -171,7 +171,7 @@ describe("mat4:", function()
13, 14, 15, 16
}
local b = { 10, 20, 30, 40 }
local c = mat4.mul_vec4(a, b)
local c = mat4.mul_vec4(mat4(), a, b)
local d = a * b
assert.is.equal(900, c[1])
assert.is.equal(1000, c[2])
@ -185,14 +185,14 @@ describe("mat4:", function()
end)
it("scales a matrix", function()
local a = mat4():scale(vec3(5, 5, 5))
local a = mat4():scale(mat4(), vec3(5, 5, 5))
assert.is.equal(5, a[1])
assert.is.equal(5, a[6])
assert.is.equal(5, a[11])
end)
it("rotates a matrix", function()
local a = mat4():rotate(math.rad(45), vec3.unit_z)
local a = mat4():rotate(mat4(), math.rad(45), vec3.unit_z)
assert.is_true(utils.tolerance( 0.7071-a[1], 0.001))
assert.is_true(utils.tolerance( 0.7071-a[2], 0.001))
assert.is_true(utils.tolerance(-0.7071-a[5], 0.001))
@ -200,7 +200,7 @@ describe("mat4:", function()
end)
it("translates a matrix", function()
local a = mat4():translate(vec3(5, 5, 5))
local a = mat4():translate(mat4(), vec3(5, 5, 5))
assert.is.equal(5, a[13])
assert.is.equal(5, a[14])
assert.is.equal(5, a[15])
@ -208,16 +208,16 @@ describe("mat4:", function()
it("inverts a matrix", function()
local a = mat4()
:rotate(math.pi/4, vec3.unit_y)
:translate(vec3(4, 5, 6))
a = a:rotate(a, math.pi/4, vec3.unit_y)
a = a:translate(a, vec3(4, 5, 6))
local b = a:invert()
local b = mat4.invert(mat4(), a)
local c = a * b
assert.is.equal(mat4(), c)
local d = mat4()
:rotate(math.pi/4, vec3.unit_y)
:translate(vec3(4, 5, 6))
d:rotate(d, math.pi/4, vec3.unit_y)
d:translate(d, vec3(4, 5, 6))
local e = -d
local f = d * e
@ -230,7 +230,8 @@ describe("mat4:", function()
2, 2, 2, 2,
3, 3, 3, 3,
4, 4, 4, 4
}):transpose()
})
a = a:transpose(a)
assert.is.equal(1, a[1])
assert.is.equal(2, a[2])
assert.is.equal(3, a[3])
@ -251,7 +252,7 @@ describe("mat4:", function()
it("shears a matrix", function()
local yx, zx, xy, zy, xz, yz = 1, 1, 1, -1, -1, -1
local a = mat4():shear(yx, zx, xy, zy, xz, yz)
local a = mat4():shear(mat4(), yx, zx, xy, zy, xz, yz)
assert.is.equal( 1, a[2])
assert.is.equal( 1, a[3])
assert.is.equal( 1, a[5])
@ -287,7 +288,7 @@ describe("mat4:", function()
local e = vec3(0, 0, 1.55)
local c = vec3(4, 7, 1)
local u = vec3(0, 0, 1)
local a = mat4().look_at(e, c, u)
local a = mat4():look_at(mat4(), e, c, u)
assert.is_true(utils.tolerance( 0.868-a[1], 0.001))
assert.is_true(utils.tolerance( 0.034-a[2], 0.001))

View File

@ -8,18 +8,18 @@ end
describe("utils:", function()
it("interpolates between two numbers", function()
assert.is_true(tolerance(utils.lerp(0.5, 0, 1), 0.5))
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(0.5, a, b), c)
assert.is.equal(utils.lerp(a, b, 0.5), c)
end)
it("decays exponentially", function()
local v = utils.decay(0.5, 0, 1, 1)
local v = utils.decay(0, 1, 0.5, 1)
assert.is_true(tolerance(v, 0.39346934028737))
end)
end)