Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local tShapes={}
- local tOrigin={400,1}
- local nTimeElapsed=0
- local nSecCount=0
- local tTemplates={}
- local shape_mt={}
- local function rotatePoint(x,y,rad)
- local ang=math.atan2(y,x)+rad
- local len=(x^2+y^2)^0.5
- return math.cos(ang)*len,math.sin(ang)*len
- end
- shape_mt.__index=function(self,index)
- return shape_mt[index]
- end
- local tAxis={{"x","y"},{"x","z"},{"z","y"}}
- shape_mt.rotate=function(self,tDegs,tOrigins)
- self.rotated=self.rotated or {}
- tOrigins=tOrigins or {}
- local sIndex
- for k,v in pairs(self.nodes) do sIndex=k break end
- 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
- for num,nDeg in pairs(tDegs) do
- self.rotated[num]=(self.rotated[num]+nDeg)%360
- local axis1=tAxis[num][1]
- local axis2=tAxis[num][2]
- for _,tNode in pairs(self.nodes) do
- local rotatedx,rotatedy=rotatePoint(tNode[axis1]-tOrigins[axis1],tNode[axis2]-tOrigins[axis2],math.rad(nDeg))
- tNode[axis1]=rotatedx+tOrigins[axis1]
- tNode[axis2]=rotatedy+tOrigins[axis2]
- end
- end
- end
- tTemplates.rectangle=function(x1,y1,z1,x2,y2,z2)
- local ref=#tShapes+1
- tShapes[ref]=setmetatable({rotated={0,0,0},nodes={
- bo={x=x1,y=y1,z=z1,links={{"bx",colors={255,0,0}},{"bz",colors={255,0,0}},"ro"}},
- bx={x=x2,y=y1,z=z1},
- bz={x=x1,y=y1,z=z2},
- ro={x=x1,y=y2,z=z1},
- rx={x=x2,y=y2,z=z1,links={"bx",{"ro",colors={0,0,255}},{"rb",colors={0,0,255}}}},
- rz={x=x1,y=y2,z=z2,links={"bz",{"ro",colors={0,0,255}},{"rb",colors={0,0,255}}}},
- rb={x=x2,y=y2,z=z2},
- bb={x=x2,y=y1,z=z2,links={{"bx",colors={255,0,0}},{"bz",colors={255,0,0}},"rb"}},
- center={x=(x2-x1)/2+x1,y=(y2-y1)/2+y1,z=(z2-z1)/2+z1},
- }},shape_mt)
- return tShapes[ref],ref
- end
- local myRec=tTemplates.rectangle(30,30,30,90,60,60)
- local rec2=tTemplates.rectangle(0,30,0,20,40,20)
- rec2:rotate({5,0,0},rec2.nodes.center)
- local function convert(x,y,z)
- local c=math.cos(math.rad(30))
- local s=math.sin(math.rad(30))
- return z*c-x*c,y+z*s+x*s
- end
- function love.load()
- love.graphics.setBackgroundColor(0,0,0)
- end
- local rotated=0
- function love.update(nTime)
- rotated=(rotated)%216+1
- --rec2:rotate({5,0,4},rec2.nodes.center)
- rec2:rotate({5,0,4},{})
- myRec:rotate(rotated>144 and {0,0,5} or rotated>72 and {0,5,0} or {5,0,0},myRec.nodes.center)
- myRec:rotate({1,0,0},{x=60,y=250,z=45})
- nTimeElapsed=nTimeElapsed+nTime
- end
- function love.draw()
- love.graphics.setColor(255,255,255)
- love.graphics.print(table.concat(rec2.rotated,","),0,0)
- local x1,y1=convert(60,250,45)
- local x2,y2=convert(myRec.nodes.center.x,myRec.nodes.center.y,myRec.nodes.center.z)
- love.graphics.line(tOrigin[1]+x1,tOrigin[2]+y1,tOrigin[1]+x2,tOrigin[2]+y2)
- for _,tShape in pairs(tShapes) do
- for _,tNode in pairs(tShape.nodes) do
- local nodeX,nodeY
- if tNode.z then
- nodeX,nodeY=convert(tNode.x,tNode.y,tNode.z)
- else
- nodeX,nodeY=tNode.x,tNode.y
- end
- for _,nLink in pairs(tNode.links or {}) do
- local colors={255,0,255}
- if type(nLink)=="table" then
- colors=nLink.colors
- nLink=nLink[1]
- end
- local tLink=tShape.nodes[nLink]
- local linkX,linkY
- if tLink.z then
- linkX,linkY=convert(tLink.x,tLink.y,tLink.z)
- else
- linkX,linkY=tLink.x,tLink.y
- end
- love.graphics.setColor(unpack(colors))
- love.graphics.line(tOrigin[1]+nodeX,tOrigin[2]+nodeY,tOrigin[1]+linkX,tOrigin[2]+linkY)
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment