diff --git a/init.lua b/init.lua index eca3de2..d93c2b0 100644 --- a/init.lua +++ b/init.lua @@ -31,7 +31,7 @@ local current_folder = (...):gsub('%.init$', '') .. "." local cpml = { _LICENSE = "CPML is distributed under the terms of the MIT license. See LICENSE.md.", _URL = "https://github.com/shakesoda/cpml", - _VERSION = "0.0.1", + _VERSION = "0.0.9", _DESCRIPTION = "Cirno's Perfect Math Library: Just about everything you need for 3D games. Hopefully." } diff --git a/modules/quat.lua b/modules/quat.lua index 6aa8c49..62a957a 100644 --- a/modules/quat.lua +++ b/modules/quat.lua @@ -500,26 +500,29 @@ function quaternion.unit() end -- http://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/ -function quaternion.lerp(a, b, dt) - return a + dt * (b - a) -end - --- http://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/ -function quaternion.nlerp(a, b, dt) - return quaternion.lerp(a, b, dt):normalize() +-- non-normalized rotations do not work out for quats! +function quaternion.lerp(a, b, s) + local v = a + (b - a) * s + return v:normalize() end -- http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/ -function quaternion.slerp(a, b, dt) +function quaternion.slerp(a, b, s) local function clamp(n, low, high) return math.min(math.max(n, low), high) end local dot = a:dot(b) + -- http://www.gamedev.net/topic/312067-shortest-slerp-path/#entry2995591 + if dot < 0 then + a = -a + dot = -dot + end + if dot > constants.DOT_THRESHOLD then - return quaternion.nlerp(a, b, dt) + return quaternion.lerp(a, b, s) end clamp(dot, -1, 1) - local theta = math.acos(dot) * dt + local theta = math.acos(dot) * s local c = (b - a * dot):normalize() return a * math.cos(theta) + c * math.sin(theta) diff --git a/modules/vec3.lua b/modules/vec3.lua index 57a2cd5..509fef2 100644 --- a/modules/vec3.lua +++ b/modules/vec3.lua @@ -233,6 +233,11 @@ function vector:orientation_to_direction(orientation) :rotated(self.x, new(1, 0, 0)) end +-- http://keithmaggio.wordpress.com/2011/02/15/math-magician-lerp-slerp-and-nlerp/ +function vector.lerp(a, b, s) + return a + s * (b - a) +end + -- the module return setmetatable({new = new, isvector = isvector, zero = zero}, {__call = function(_, ...) return new(...) end})