PicoPicoGameEngine

Quick Links

ピコピココード

g=ppgraph

sqrt=math.sqrt
tan=math.tan
sin=math.sin
cos=math.cos
rand=math.random

function v3print(s)
 print(s.x..","..s.y..","..s.z)
end

function v3(x,y,z)
local q={}
q.x=x
q.y=y
q.z=z
q.print=v3print
return q
end

function nml(p)
local q={}
local l
l=p.x*p.x+p.y*p.y+p.z*p.z
l=sqrt(l)
q.x=p.x/q
q.y=p.y/q
q.z=p.z/q
q.print=v3print
return q
end

function cross(a,b)
local q={}
q.x=a.x*b.x
q.y=a.y*b.y
q.z=a.z*b.z
q.print=v3print
return q
end

function
pmap(fov_h,fov_v,near,far)
local t={}
t.w=1/tan(fov_h*0.5)
t.h=1/tan(fov_v*0.5)
t.q=far/(far-near)
t.u=-t.q*near
t.mul=function(s,p)
local t={}
t.x=s.w*p.x
t.y=s.h*p.y
t.z=s.q*p.z+s.u
t.print=v3print
return t
end
return t
end

function rad(r)
r=math.pi*r/180
return r
end

function trans(v,p)
 local a={}
 a.x=v.x+p.x
 a.y=v.y+p.y
 a.z=v.z+p.z
 return a
end

function rotx(v,r)
local a={}
a.x=v.x
a.y=v.y*cos(r)+v.z*sin(r)
a.z=-v.y*sin(r)+v.z*cos(r)
return a
end

function roty(v,r)
local a={}
a.x=v.x*cos(r)-v.z*sin(r)
a.y=v.y
a.z=v.x*sin(r)+v.z*cos(r)
return a
end

function rotz(v,r)
local a={}
a.x=v.x*cos(r)+v.y*sin(r)
a.y=-v.x*sin(r)+v.y*cos(r)
a.z=v.z
return a
end

proj=pmap(
 rad(120),rad(120),
 100,1000)

cube={
 v={
  v3(100,100,100),
  v3(-100,100,100),
  v3(100,-100,100),
  v3(-100,-100,100),
  v3(100,100,-100),
  v3(-100,100,-100),
  v3(100,-100,-100),
  v3(-100,-100,-100)
 },
 p={
 {1,2},
 {2,4},
 {4,3},
 {3,1},
 {5,6},
 {6,8},
 {8,7},
 {7,5},
 {1,5},
 {2,6},
 {3,7},
 {4,8}}
}

star={
 v={
  v3(0,0,0)
 }
}

c=ppoffscreen.new()
c:create(1,1)
c.x=160
c.y=280

s={}
s.x=500
s.y=500

function scr(v)
local t={}
t.x=(v.x/v.z)*s.x+c.x
t.y=(v.y/v.z)*s.y+c.y
return t
end

function drawpos(v)
 g:fill(v.p.x,
        v.p.y,2,2)
end

function drawshape(p,v)
for i=1,#p do
local q=p[i]
g:line(
 v[q[1]].p,v[q[2]].p)
end
end

function obj(shape,x,y,z)
local t={}
t.mv=v3(x,y,z)
t.r=v3(0,0,0)
t.shape=shape
t.idle=function(s)
local r=s.r
r.x=r.x+0.02
r.y=r.y+0.03
r.z=r.z+0.01
s.r=r
s.mv.z=s.mv.z-30
if s.mv.z<300 then
 s.mv.x=rand(-600,600)
 s.mv.y=rand(-600,600)
 s.mv.z=2000
end
end
t.draw=function(s)
local v=s.shape.v
local q=s.shape.p
local rx=s.r.x
local ry=s.r.y
local rz=s.r.z
local mv=s.mv
for i=1,#v do
local p
p=rotx(v[i],rx)
p=roty(p,ry)
p=rotz(p,rz)
p=trans(p,mv)
p=proj:mul(p)
v[i].p=scr(p)
if not q then
drawpos(v[i])
end
end
if q then
drawshape(q,v)
end
end
return t
end

prim={}
for i=1,10 do
local t
local x,y,z
x=rand(-600,600)
y=rand(-600,600)
z=rand(500,5000)
t=obj(cube,x,y,z)
prim[#prim+1]=t
end
for i=1,50 do
local t
local x,y,z
x=rand(-600,600)
y=rand(-600,600)
z=rand(500,2000)
t=obj(star,x,y,z)
prim[#prim+1]=t
end

function start()
 c:drag(pptouch())
 for i=1,#prim do
 local c
 c=prim[i]
 c:idle()
 c:draw()
 end
end