Advertisement
Guest User

Vanishing Point Codea Codify

a guest
Nov 17th, 2011
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.75 KB | None | 0 0
  1.  
  2.  
  3. -- Use this function to perform your initial setup
  4. function setup()
  5.     iparameter("VanishPointx",0,WIDTH,WIDTH/2)
  6.     iparameter("VanishPointy",0,WIDTH,WIDTH/2)
  7.     iparameter("VanishingPoint",0,1,0)
  8.     iparameter("Horizon",0,1,0)
  9.     iparameter("Midline",0,1,0)
  10.     iparameter("HitBox",0,100,0)
  11.     iparameter("Sticker",0,1,0)    
  12.     iparameter("Mobs",0,1,0)
  13.     iparameter("zSpeed",0,30,0)
  14.     iparameter("Mobtype",0,1,0)
  15.     Mobz = {}
  16.     print("Move the sliders to make something appear") 
  17. end
  18.  
  19. -- This function gets called once every frame
  20. function draw()
  21.     vp = vec2(VanishPointx,VanishPointy) --vanishing point
  22.     background(0,0,0,0)
  23.     stroke(0, 255, 39, 255)
  24.     strokeWidth(3)
  25.     fill(3, 253, 15, 255)
  26.     if HitBox > 0 then
  27.         ShowHitBox(HitBox)
  28.     end
  29.      if Midline == 1 then
  30.         line(vp.x,0,vp.x,HEIGHT)
  31.     end    
  32.     if Horizon == 1 then
  33.         line(0,vp.y,WIDTH,vp.y)
  34.     end    
  35.     if VanishingPoint == 1 then
  36.         ellipse(vp.x,vp.y,5,5)
  37.     end  
  38.     if Mobs == 0 then
  39.         for k, v in pairs(Mobz) do
  40.             table.remove(Mobz,k)
  41.         end
  42.     else
  43.         nMobz = table.maxn(Mobz) 
  44.         for i = 1,10-nMobz do  
  45.             x=math.random()*WIDTH
  46.             y=math.random()*HEIGHT
  47.             z=math.random()*WIDTH*10
  48.             d=256
  49.             table.insert(Mobz,nMobz+1,{x=x,y=y,z=z,d=d})            
  50.         end
  51.         for k, v in pairs(Mobz) do
  52.             zd = l3Dto2d(Mobz[k].d,Mobz[k].z)
  53.             e = c3Dto2d(vec3(Mobz[k].x,Mobz[k].y,Mobz[k].z))
  54.             if Mobtype == 0 then
  55.                 ellipse(e.x,e.y,zd)
  56.             else
  57.                 sprite("SpaceCute:Planet",e.x,e.y,zd)    
  58.             end    
  59.         end     
  60.     end 
  61.     if zSpeed > 0 and table.maxn(Mobz) > 0 then
  62.         for k, v in pairs(Mobz) do
  63.             Mobz[k].z = Mobz[k].z - zSpeed
  64.             if Mobz[k].z < 0 then
  65.                 table.remove(Mobz,k)
  66.             end
  67.         end     
  68.     end
  69.     
  70.     if Sticker == 1 then --draw something full size 
  71.         --like a sticker on the window
  72.         ellipse(100,100,100) 
  73.         --sprite("SpaceCute:Planet",300,300,256)
  74.     end    
  75. end
  76.  
  77. function l3Dto2d(d,z)
  78.     if z == 0 then
  79.         --depth is zero, we're on the screen
  80.         return (d)
  81.     elseif z < 0 then
  82.         --depth is negative
  83.         --look out it's behind you!
  84.         --now crash and burn them for passing this to us
  85.         return (nil) 
  86.         --maybe after some thought this could be handled    
  87.     else --if z > 0
  88.         --depth is positive
  89.         --it's ahead of us (doesn't mean we can see it)
  90.         dz = d * WIDTH / ( WIDTH + z)
  91.         return(dz)
  92.     end -- z depth
  93. end
  94.  
  95. function c3Dto2d(p)
  96.     --print(p)
  97.     --convert 3D to 2D
  98.     --accepts p a vec3 -- x,y,z
  99.     --returns a vec2 --x,y
  100.     -- p.x + (vp.x / 2)/WIDTH/p.z
  101.     -- p.x - ((WIDTH - vp.x) /2)/WIDTH/p.z
  102.     local x2, y2
  103.     if p.z == 0 then
  104.         --depth is zero, we're on the screen
  105.         return (vec2(p.x,p.y))
  106.     elseif p.z < 0 then
  107.         --depth is negative
  108.         --look out it's behind you!
  109.         --now crash and burn them for passing this to us
  110.         return vec2(nil,nil) 
  111.         --maybe after some thought this could be handled    
  112.     else --if p.z > 0
  113.         --depth is positive
  114.         --it's ahead of us (doesn't mean we can see it)
  115.         x2 = p.x + ((vp.x - p.x)* (p.z/WIDTH) / ((p.z/WIDTH)+1) )
  116.         y2 = p.y + ((vp.y - p.y)* (p.z/WIDTH) / ((p.z/WIDTH)+1) )
  117.         --print(x2,y2)
  118.         return(vec2(x2,y2))
  119.     end -- z depth
  120. end    
  121.  
  122. function ShowHitBox(boxes)
  123.     for i = 1,boxes do
  124.         --the hit box is a box in front of the viewer
  125.         --objects in the hit box will hit the screen
  126.         sll = vec3(0,0,(i-1)*WIDTH)          -- screen lower left point
  127.         sul = vec3(0,HEIGHT,(i-1)*WIDTH)     -- screen upper left point
  128.         slr = vec3(WIDTH,0,(i-1)*WIDTH)      -- screen lower right point
  129.         sur = vec3(WIDTH,HEIGHT,(i-1)*WIDTH) -- screen upper right point
  130.         -- depth as in the depth of the box
  131.         -- the box depth is set to it's width
  132.         dll = vec3(0,0,i*WIDTH)          -- depth lower left point
  133.         dul = vec3(0,HEIGHT,i*WIDTH)     -- depth upper left point
  134.         dlr = vec3(WIDTH,0,i*WIDTH)      -- depth screen lower right
  135.         dur = vec3(WIDTH,HEIGHT,i*WIDTH) -- depth screen upper right    
  136.         sll2 = c3Dto2d(sll)
  137.         sul2 = c3Dto2d(sul)
  138.         slr2 = c3Dto2d(slr)
  139.         sur2 = c3Dto2d(sur)
  140.         dll2 = c3Dto2d(dll)
  141.         dul2 = c3Dto2d(dul)
  142.         dlr2 = c3Dto2d(dlr)
  143.         dur2 = c3Dto2d(dur)
  144.         DrawBox(sll2,sul2,slr2,sur2,dll2,dul2,dlr2,dur2)
  145.     end
  146. end
  147.  
  148. function DrawBox(sll2,sul2,slr2,sur2,dll2,dul2,dlr2,dur2)
  149.     --back
  150.     line(dll2.x,dll2.y,dul2.x,dul2.y)
  151.     line(dul2.x,dul2.y,dur2.x,dur2.y)
  152.     line(dur2.x,dur2.y,dlr2.x,dlr2.y)
  153.     line(dlr2.x,dlr2.y,dll2.x,dll2.y)
  154.     --sides
  155.     line(sll2.x,sll2.y,dll2.x,dll2.y)
  156.     line(sul2.x,sul2.y,dul2.x,dul2.y)
  157.     line(sur2.x,sur2.y,dur2.x,dur2.y)
  158.     line(slr2.x,slr2.y,dlr2.x,dlr2.y)
  159.     --front
  160.     line(sll2.x,sll2.y,sul2.x,sul2.y)
  161.     line(sul2.x,sul2.y,sur2.x,sur2.y)
  162.     line(sur2.x,sur2.y,slr2.x,slr2.y)
  163.     line(slr2.x,slr2.y,sll2.x,sll2.y)
  164. end    
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement