KaoSDlanor

Love2d rendering

Mar 1st, 2013
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.39 KB | None | 0 0
  1. local tShapes={}
  2. local tOrigin={400,1}
  3. local nTimeElapsed=0
  4. local nSecCount=0
  5. local tTemplates={}
  6. local shape_mt={}
  7.  
  8. local function rotatePoint(x,y,rad)
  9.     local ang=math.atan2(y,x)+rad
  10.     local len=(x^2+y^2)^0.5
  11.     return math.cos(ang)*len,math.sin(ang)*len
  12. end
  13.  
  14. shape_mt.__index=function(self,index)
  15.     return shape_mt[index]
  16. end
  17. local tAxis={{"x","y"},{"x","z"},{"z","y"}}
  18. shape_mt.rotate=function(self,tDegs,tOrigins)
  19.     self.rotated=self.rotated or {}
  20.     tOrigins=tOrigins or {}
  21.     local sIndex
  22.     for k,v in pairs(self.nodes) do sIndex=k break end
  23.     tOrigins.x,tOrigins.y,tOrigins.z=tOrigins.x or self.nodes[sIndex].x,tOrigins.y or self.nodes[sIndex].y,tOrigins.z or self.nodes[sIndex].z
  24.     for num,nDeg in pairs(tDegs) do
  25.         self.rotated[num]=(self.rotated[num]+nDeg)%360
  26.         local axis1=tAxis[num][1]
  27.         local axis2=tAxis[num][2]
  28.         for _,tNode in pairs(self.nodes) do
  29.             local rotatedx,rotatedy=rotatePoint(tNode[axis1]-tOrigins[axis1],tNode[axis2]-tOrigins[axis2],math.rad(nDeg))
  30.             tNode[axis1]=rotatedx+tOrigins[axis1]
  31.             tNode[axis2]=rotatedy+tOrigins[axis2]
  32.         end
  33.     end
  34. end
  35. tTemplates.rectangle=function(x1,y1,z1,x2,y2,z2)
  36.     local ref=#tShapes+1
  37.     tShapes[ref]=setmetatable({rotated={0,0,0},nodes={
  38.     bo={x=x1,y=y1,z=z1,links={{"bx",colors={255,0,0}},{"bz",colors={255,0,0}},"ro"}},
  39.     bx={x=x2,y=y1,z=z1},
  40.     bz={x=x1,y=y1,z=z2},
  41.     ro={x=x1,y=y2,z=z1},
  42.     rx={x=x2,y=y2,z=z1,links={"bx",{"ro",colors={0,0,255}},{"rb",colors={0,0,255}}}},
  43.     rz={x=x1,y=y2,z=z2,links={"bz",{"ro",colors={0,0,255}},{"rb",colors={0,0,255}}}},
  44.     rb={x=x2,y=y2,z=z2},
  45.     bb={x=x2,y=y1,z=z2,links={{"bx",colors={255,0,0}},{"bz",colors={255,0,0}},"rb"}},
  46.     center={x=(x2-x1)/2+x1,y=(y2-y1)/2+y1,z=(z2-z1)/2+z1},
  47.     }},shape_mt)
  48.     return tShapes[ref],ref
  49. end
  50.  
  51. local myRec=tTemplates.rectangle(30,30,30,90,60,60)
  52. local rec2=tTemplates.rectangle(0,30,0,20,40,20)
  53. rec2:rotate({5,0,0},rec2.nodes.center)
  54.  
  55. local function convert(x,y,z)
  56.     local c=math.cos(math.rad(30))
  57.     local s=math.sin(math.rad(30))
  58.     return z*c-x*c,y+z*s+x*s
  59. end
  60.  
  61. function love.load()
  62.     love.graphics.setBackgroundColor(0,0,0)
  63. end
  64.  
  65. local rotated=0
  66. function love.update(nTime)
  67.     rotated=(rotated)%216+1
  68.     --rec2:rotate({5,0,4},rec2.nodes.center)
  69.     rec2:rotate({5,0,4},{})
  70.     myRec:rotate(rotated>144 and {0,0,5} or rotated>72 and {0,5,0} or {5,0,0},myRec.nodes.center)
  71.     myRec:rotate({1,0,0},{x=60,y=250,z=45})
  72.     nTimeElapsed=nTimeElapsed+nTime
  73. end
  74.  
  75. function love.draw()
  76.     love.graphics.setColor(255,255,255)
  77.     love.graphics.print(table.concat(rec2.rotated,","),0,0)
  78.     local x1,y1=convert(60,250,45)
  79.     local x2,y2=convert(myRec.nodes.center.x,myRec.nodes.center.y,myRec.nodes.center.z)
  80.     love.graphics.line(tOrigin[1]+x1,tOrigin[2]+y1,tOrigin[1]+x2,tOrigin[2]+y2)
  81.     for _,tShape in pairs(tShapes) do
  82.         for _,tNode in pairs(tShape.nodes) do
  83.             local nodeX,nodeY
  84.             if tNode.z then
  85.                 nodeX,nodeY=convert(tNode.x,tNode.y,tNode.z)
  86.             else
  87.                 nodeX,nodeY=tNode.x,tNode.y
  88.             end
  89.             for _,nLink in pairs(tNode.links or {}) do
  90.                 local colors={255,0,255}
  91.                 if type(nLink)=="table" then
  92.                     colors=nLink.colors
  93.                     nLink=nLink[1]
  94.                 end
  95.                 local tLink=tShape.nodes[nLink]
  96.                 local linkX,linkY
  97.                 if tLink.z then
  98.                     linkX,linkY=convert(tLink.x,tLink.y,tLink.z)
  99.                 else
  100.                     linkX,linkY=tLink.x,tLink.y
  101.                 end
  102.                 love.graphics.setColor(unpack(colors))
  103.                 love.graphics.line(tOrigin[1]+nodeX,tOrigin[2]+nodeY,tOrigin[1]+linkX,tOrigin[2]+linkY)
  104.             end
  105.         end
  106.     end
  107. end
Advertisement
Add Comment
Please, Sign In to add comment