Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions modules/quat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,19 @@ function quat.new(x, y, z, w)
return new(x, y, z, w)

-- {x, y, z, w} or {x=x, y=y, z=z, w=w}
elseif type(x) == "table" then
elseif type(x) == "table" or type (x) == "cdata" then
local xx, yy, zz, ww = x.x or x[1], x.y or x[2], x.z or x[3], x.w or x[4]
precond.typeof(xx, "number", "new: Wrong argument type for x")
precond.typeof(yy, "number", "new: Wrong argument type for y")
precond.typeof(zz, "number", "new: Wrong argument type for z")
precond.typeof(ww, "number", "new: Wrong argument type for w")

return new(xx, yy, zz, ww)
end

return new(0, 0, 0, 1)
else
precond.assert(x == nil, "new: Wrong arguments")
return new(0, 0, 0, 1)
end
end

--- Create a quaternion from an angle/axis pair.
Expand Down
8 changes: 8 additions & 0 deletions spec/quat_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ describe("quat:", function()
assert.is.equal(1, a.w)
end)

it("creates a quaternion from a quaternion", function()
local a = quat (quat(2, 3, 4, 1))
assert.is.equal(2, a.x)
assert.is.equal(3, a.y)
assert.is.equal(4, a.z)
assert.is.equal(1, a.w)
end)

it("creates a quaternion from a direction", function()
local v = vec3(-80, 80, -80):normalize()
local a = quat.from_direction(v, vec3.unit_z)
Expand Down