Add cpml.quat.from_direction

This commit is contained in:
Colby Klein 2015-11-20 17:03:23 -08:00
parent e5d0dc799c
commit c4a33434b2

View File

@ -279,6 +279,16 @@ local function rotate(angle, axis)
return new(axis.x * sin, axis.y * sin, axis.z * sin, cos)
end
--- Create a quaternion from a direction + up vector.
-- @param normal
-- @param up
-- @return quat
local function from_direction(normal, up)
local a = up:cross(normal)
local d = up:dot(normal)
return new(a.x, a.y, a.z, d + 1)
end
function quaternion:to_euler()
local sqx = self.x*self.x
local sqy = self.y*self.y
@ -345,5 +355,5 @@ end
-- return quaternion
-- the module
return setmetatable({ new = new, rotate = rotate },
return setmetatable({ new = new, rotate = rotate, from_direction = from_direction },
{ __call = function(_, ...) return new(...) end })