Lunatic 'geom': retire tovec2 and tovec3, make vec2 and vec3 accept cdata, too.

git-svn-id: https://svn.eduke32.com/eduke32@3890 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-06-20 18:31:45 +00:00
parent 4a4f90e5e7
commit 10379910c1
4 changed files with 33 additions and 14 deletions

View file

@ -133,19 +133,35 @@ local vec3_mt = {
}, },
} }
ffi.metatype(dvec2_t, vec2_mt)
ffi.metatype(dvec3_t, vec3_mt)
-- VEC2 user data constructor. -- VEC2 user data constructor.
-- * vec2(<table>), <table> should be indexable with "x" and "y" -- * vec2([x [, y]]), assuming that x and y are numbers. Vacant positions are
-- * vec2(x, y), assuming that x and y are numbers -- assumed to be 0.
vec2 = ffi.metatype(dvec2_t, vec2_mt) -- * vec2(<compound>), <compound> can be anything indexable with "x" and "y"
vec3 = ffi.metatype(dvec3_t, vec3_mt) 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" -- TODO: make a constructor like VEC3?
-- (vec2(t) works if t is such a table, but not if it's a vec2 or a cdata of ivec3 = ffi.metatype(ivec3_t, vec3_mt)
-- 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
-- Two-element vector cross product. -- Two-element vector cross product.
@ -168,11 +184,11 @@ function intersect(a,v, b,w, retpoint_p)
local vxw = cross2(v,w) local vxw = cross2(v,w)
if (vxw ~= 0) then 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 local cv, cw = cross2(w, btoa)/vxw, cross2(v, btoa)/vxw
if (retpoint_p) then if (retpoint_p) then
return tovec2(a)+cv*tovec2(v) return vec2(a) + cv*vec2(v)
else else
return cv, cw return cv, cw
end end

View file

@ -33,7 +33,7 @@ function randwalk(N, spritenum, minlen, maxlen, randofs, funci, logfn)
local times = {} local times = {}
local successp = {} local successp = {}
local pos = geom.tovec3(sprite[spritenum]) local pos = geom.vec3(sprite[spritenum])
local sectnum = sprite[spritenum].sectnum local sectnum = sprite[spritenum].sectnum
for i=1,N do for i=1,N do

View file

@ -381,6 +381,9 @@ gameactor
-- NOTE: __add metamethod is called because of the RHS: -- NOTE: __add metamethod is called because of the RHS:
local v = spr + geom.vec3(r(-d,d), r(-d,d)) local v = spr + geom.vec3(r(-d,d), r(-d,d))
spr:setpos(v) spr:setpos(v)
-- Test vec3 constructor with cdata.
local tempvec = geom.vec3(player[0].pos)
end end
} }

View file

@ -104,7 +104,7 @@ end
-- XXX: passing mixed vec2/vec3 is problematic. Get rid of geom.vec2? -- XXX: passing mixed vec2/vec3 is problematic. Get rid of geom.vec2?
-- <ang>: BUILD angle (0-2047 based) -- <ang>: BUILD angle (0-2047 based)
function rotate(pos, pivot, ang) 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 c, s = cosb(ang), sinb(ang)
local x, y = p.x, p.y local x, y = p.x, p.y
p.x = pivot.x + (c*x - s*y) p.x = pivot.x + (c*x - s*y)