From 2b87484805c4a52797ac6ed056113d523ae2b3b6 Mon Sep 17 00:00:00 2001 From: Kira Boom Date: Sat, 7 Oct 2023 11:21:18 -0400 Subject: [PATCH] fix quat(quat(1,2,3,4)) returning the identity quaternion under luajit I changed it to match the way vec3 does it. --- modules/quat.lua | 8 +++++--- spec/quat_spec.lua | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/modules/quat.lua b/modules/quat.lua index 999f970..1f9573a 100644 --- a/modules/quat.lua +++ b/modules/quat.lua @@ -67,7 +67,7 @@ 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") @@ -75,9 +75,11 @@ function quat.new(x, y, z, w) 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. diff --git a/spec/quat_spec.lua b/spec/quat_spec.lua index a85297b..95b857f 100644 --- a/spec/quat_spec.lua +++ b/spec/quat_spec.lua @@ -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)