Advertisement
Guest User

Codea - vanishing point demo with tilt controls

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