diff --git a/polymer/eduke32/source/lunatic/geom.lua b/polymer/eduke32/source/lunatic/geom.lua index 03c6b1a09..fb95ce153 100644 --- a/polymer/eduke32/source/lunatic/geom.lua +++ b/polymer/eduke32/source/lunatic/geom.lua @@ -133,19 +133,35 @@ local vec3_mt = { }, } +ffi.metatype(dvec2_t, vec2_mt) +ffi.metatype(dvec3_t, vec3_mt) + -- VEC2 user data constructor. --- * vec2(),
should be indexable with "x" and "y" --- * vec2(x, y), assuming that x and y are numbers -vec2 = ffi.metatype(dvec2_t, vec2_mt) -vec3 = ffi.metatype(dvec3_t, vec3_mt) +-- * vec2([x [, y]]), assuming that x and y are numbers. Vacant positions are +-- assumed to be 0. +-- * vec2(), can be anything indexable with "x" and "y" +function vec2(...) + local x, y = ... + if (type(x)=="number" or x==nil) then + return dvec2_t(...) + else + return dvec2_t(x.x, x.y) + end +end -ivec3 = ffi.metatype("vec3_t", vec3_mt) +-- VEC3 user data constructor. +-- Analogous to VEC2. +function vec3(...) + local x, y, z = ... + if (type(x)=="number" or x==nil) then + return dvec3_t(...) + else + return dvec3_t(x.x, x.y, x.z) + end +end --- Returns a vec2 from anything indexable with "x" and "y" --- (vec2(t) works if t is such a table, but not if it's a vec2 or a cdata of --- different type) -function tovec2(t) return dvec2_t(t.x, t.y) end -function tovec3(t) return dvec3_t(t.x, t.y, t.z) end +-- TODO: make a constructor like VEC3? +ivec3 = ffi.metatype(ivec3_t, vec3_mt) -- Two-element vector cross product. @@ -168,11 +184,11 @@ function intersect(a,v, b,w, retpoint_p) local vxw = cross2(v,w) if (vxw ~= 0) then - local btoa = tovec2(a)-tovec2(b) + local btoa = vec2(a) - vec2(b) local cv, cw = cross2(w, btoa)/vxw, cross2(v, btoa)/vxw if (retpoint_p) then - return tovec2(a)+cv*tovec2(v) + return vec2(a) + cv*vec2(v) else return cv, cw end diff --git a/polymer/eduke32/source/lunatic/m32/randwalk.lua b/polymer/eduke32/source/lunatic/m32/randwalk.lua index 01834f2b7..5b36688fd 100644 --- a/polymer/eduke32/source/lunatic/m32/randwalk.lua +++ b/polymer/eduke32/source/lunatic/m32/randwalk.lua @@ -33,7 +33,7 @@ function randwalk(N, spritenum, minlen, maxlen, randofs, funci, logfn) local times = {} local successp = {} - local pos = geom.tovec3(sprite[spritenum]) + local pos = geom.vec3(sprite[spritenum]) local sectnum = sprite[spritenum].sectnum for i=1,N do diff --git a/polymer/eduke32/source/lunatic/test.elua b/polymer/eduke32/source/lunatic/test.elua index 45af19214..891d1d043 100644 --- a/polymer/eduke32/source/lunatic/test.elua +++ b/polymer/eduke32/source/lunatic/test.elua @@ -381,6 +381,9 @@ gameactor -- NOTE: __add metamethod is called because of the RHS: local v = spr + geom.vec3(r(-d,d), r(-d,d)) spr:setpos(v) + + -- Test vec3 constructor with cdata. + local tempvec = geom.vec3(player[0].pos) end } diff --git a/polymer/eduke32/source/lunatic/xmath.lua b/polymer/eduke32/source/lunatic/xmath.lua index 0d1d958a3..83823241a 100644 --- a/polymer/eduke32/source/lunatic/xmath.lua +++ b/polymer/eduke32/source/lunatic/xmath.lua @@ -104,7 +104,7 @@ end -- XXX: passing mixed vec2/vec3 is problematic. Get rid of geom.vec2? -- : BUILD angle (0-2047 based) function rotate(pos, pivot, ang) - local p = geom.tovec3(pos)-pivot + local p = geom.vec3(pos)-pivot local c, s = cosb(ang), sinb(ang) local x, y = p.x, p.y p.x = pivot.x + (c*x - s*y)