Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local screen = platform.window
- Polygon = class()
- Rectangle = class()
- Cube = class()
- DVector = class()
- cameraPoint = {40,40,10}
- cameraViewPoint = {0,1,10}
- degreesx = 500
- radius = 140
- --0 = Nothing
- --1 = W
- --2 = S
- --3 = Left
- --4 = Right
- --5 = Up
- --6 = Down
- --0 = Nothing
- --1 = x
- --2 = y
- --3 = z
- --4 = -x
- --5 = -y
- --6 = -z
- currentInput = 1
- inputI = {4,4,6,5,0,0,3,3}
- inputI2 = {3,0,5,1,0,5,1,3}
- inputI3 = {4,4,4,3,3,2,0,0}
- inputI4 = {6,0,5,0,3,3,6,6}
- inputR = {41,32,44,20,80,40,40,40}
- inputI = {0}
- inputI2 = {0}
- inputI3 = {0}
- inputI4 = {0}
- inputR = {1}
- screenWidth = 318
- screenHeight = 212
- varGlobal = 0
- polygons = {}
- polyCount = 1
- function DVector:init(x, y, z)
- local length = math.sqrt(x*x + y*y + z*z)
- self.x = 0
- self.y = 0
- self.z = 0
- if length>0 then
- self.x = x / length
- self.y = y / length
- self.z = z / length
- end
- end
- function DVector:cross(v)
- local retvec = DVector(self.y * v.z - self.z * v.y, self.z * v.x - self.x * v.z, self.x * v.y - self.y * v.x)
- return retvec
- end
- function ReCalculation()
- ViewVector = DVector(cameraViewPoint[1] - cameraPoint[1], cameraViewPoint[2] - cameraPoint[2], cameraViewPoint[3] - cameraPoint[3])
- local DirectionVector = DVector(1,1,1)
- local PlaneVector1 = ViewVector:cross(DirectionVector)
- local PlaneVector2 = ViewVector:cross(PlaneVector1)
- local tx = math.abs(cameraPoint[1]-cameraViewPoint[1])
- local ty = math.abs(cameraPoint[2]-cameraViewPoint[2])
- local xrot = ty/(tx+ty)
- local yrot = tx/(ty+tx)
- if cameraPoint[2]>cameraViewPoint[2] then
- xrot = xrot * -1
- end
- if cameraPoint[1]<cameraViewPoint[1] then
- yrot = yrot * -1
- end
- local RotationVector = DVector(xrot,yrot,0)
- WVector1 = ViewVector:cross(RotationVector)
- WVector2 = ViewVector:cross(WVector1)
- end
- function CalculatePosition(x, y, z)
- local ViewToPoint = DVector(x - cameraPoint[1], y - cameraPoint[2], z - cameraPoint[3])
- t = (ViewVector.x*cameraViewPoint[1] + ViewVector.y*cameraViewPoint[2] + ViewVector.z*cameraViewPoint[3] - (ViewVector.x*cameraPoint[1] + ViewVector.y*cameraPoint[2] + ViewVector.z*cameraPoint[3])) / (ViewVector.x*ViewToPoint.x + ViewVector.y*ViewToPoint.y + ViewVector.z*ViewToPoint.z)
- x = cameraPoint[1] + ViewToPoint.x * t
- y = cameraPoint[2] + ViewToPoint.y * t
- z = cameraPoint[3] + ViewToPoint.z * t
- local retx
- local rety
- if t >= 0 then
- retx = WVector2.x * x + WVector2.y * y + WVector2.z * z
- rety = WVector1.x * x + WVector1.y * y + WVector1.z * z
- end
- return retx, rety
- end
- function Polygon:init(x1, y1, z1, x2, y2, z2, x3, y3, z3, c)
- polyCount = polyCount + 1
- self.c = c
- self.x1 = x1
- self.y1 = y1
- self.z1 = z1
- self.x2 = x2
- self.y2 = y2
- self.z2 = z2
- self.x3 = x3
- self.y3 = y3
- self.z3 = z3
- end
- function Polygon:draw(gc)
- local viewLimit = 800
- local zoom = 2
- local vx, vy = CalculatePosition(cameraViewPoint[1],cameraViewPoint[2],cameraViewPoint[3])
- vx = vx * -zoom
- vy = vy * -zoom
- local dx1, dy1 = CalculatePosition(self.x1,self.y1,self.z1)
- local dx2, dy2 = CalculatePosition(self.x2,self.y2,self.z2)
- local dx3, dy3 = CalculatePosition(self.x3,self.y3,self.z3)
- gc:setColorRGB(self.c[1], self.c[2], self.c[3])
- if dx3 ~= nil and dx2 ~= nil and dx1 ~= nil and dy3 ~= nil and dy2 ~= nil and dy1 ~= nil then
- dx1 = vx + (dx1 * 2) + (screenWidth/2)
- dy1 = vy + (dy1 * zoom) + (screenHeight/2)
- dx2 = vx + (dx2 * zoom) + (screenWidth/2)
- dy2 = vy + (dy2 * zoom) + (screenHeight/2)
- dx3 = vx + (dx3 * zoom) + (screenWidth/2)
- dy3 = vy + (dy3 * zoom) + (screenHeight/2)
- changed = 0
- if dx1 > -viewLimit and dx1 < screenWidth+viewLimit and dx2 > -viewLimit and dx2 < screenWidth+viewLimit and dx3 > -viewLimit and dx3 < screenWidth+viewLimit then
- if dy1 > -viewLimit and dy1 < screenHeight+viewLimit and dy2 > -viewLimit and dy2 < screenHeight+viewLimit and dy3 > -viewLimit and dy3 < screenHeight+viewLimit then
- gc:fillPolygon({dx1, dy1, dx2, dy2, dx3, dy3})
- end
- end
- end
- end
- function Polygon:getDistance()
- local sdistance = 0
- local tx = cameraPoint[1] - self.x1
- local ty = cameraPoint[2] - self.y1
- local tz = cameraPoint[3] - self.z1
- local sdistance1 = math.sqrt(tx * tx + ty * ty + tz * tz)
- tx = cameraPoint[1] - self.x2
- ty = cameraPoint[2] - self.y2
- tz = cameraPoint[3] - self.z2
- local sdistance2 = math.sqrt(tx * tx + ty * ty + tz * tz)
- tx = cameraPoint[1] - self.x3
- ty = cameraPoint[2] - self.y3
- tz = cameraPoint[3] - self.z3
- local sdistance3 = math.sqrt(tx * tx + ty * ty + tz * tz)
- sdistance = (sdistance1 + sdistance2 + sdistance3) / 3
- return sdistance
- end
- function Plane(x1,y1,z1,x2,y2, c)
- polygons[polyCount] = Polygon(x1,y1,z1,x1 + x2,y1,z1,x1 + x2,y1 + y2,z1, c)
- polygons[polyCount] = Polygon(x1,y1,z1,x1,y1 + y2,z1,x1 + x2,y1 + y2,z1, c)
- end
- function getPointOnCircle(r,a,x,y)
- x = x + r * math.cos(a)
- y = y + r * math.sin(a)
- return x,y
- end
- function Floor(x1,y1,x2,y2,z, c)
- for i = x1, x2, 20 do
- for j = y1, y2, 20 do
- Plane(i,j,z,20,20, c)
- end
- end
- end
- function DrawSky(gc, c)
- dx, dy = getPointOnCircle(1000,degreesx,cameraPoint[1],cameraPoint[2])
- tx, ty = CalculatePosition(dx,dy,cameraPoint[3])
- gc:setColorRGB(c[1],c[2], c[3])
- if dy ~= nil then
- gc:fillRect(0,(ty * 2) - (screenHeight / 2) + 20,screenWidth,screenHeight)
- end
- end
- function DrawFloor(gc, c)
- gc:setColorRGB(c[1],c[2], c[3])
- gc:fillRect(0,0,screenWidth,screenHeight)
- end
- function Cube:init(x1, y1, z1, x2, y2, z2, c)
- x2 = x1 + x2
- y2 = y1 + y2
- z2 = z1 + z2
- c1 = {c[1], c[2], c[3]}
- c2 = {c[1] - 40, c[2] - 40, c[3] - 40}
- c3 = {c[1] - 20, c[2] - 20, c[3] - 20}
- for i = 1, 3, 1 do if c1[i]<0 then c1[i]=0 end end
- for i = 1, 3, 1 do if c2[i]<0 then c2[i]=0 end end
- for i = 1, 3, 1 do if c3[i]<0 then c3[i]=0 end end
- polygons[polyCount] = Polygon(x1,y1,z1,x2,y2,z1,x1,y2,z1, c1)
- polygons[polyCount] = Polygon(x1,y1,z1,x2,y2,z1,x2,y1,z1, c1)
- polygons[polyCount] = Polygon(x1,y1,z1,x1,y2,z2,x1,y2,z1, c2)
- polygons[polyCount] = Polygon(x1,y1,z1,x1,y2,z2,x1,y1,z2, c2)
- polygons[polyCount] = Polygon(x2,y2,z2,x1,y1,z2,x2,y1,z2, c1)
- polygons[polyCount] = Polygon(x2,y2,z2,x1,y1,z2,x1,y2,z2, c1)
- polygons[polyCount] = Polygon(x2,y2,z2,x2,y1,z1,x2,y1,z2, c2)
- polygons[polyCount] = Polygon(x2,y2,z2,x2,y1,z1,x2,y2,z1, c2)
- polygons[polyCount] = Polygon(x1,y1,z1,x2,y1,z1,x2,y1,z2, c3)
- polygons[polyCount] = Polygon(x1,y1,z1,x1,y1,z2,x2,y1,z2, c3)
- polygons[polyCount] = Polygon(x1,y2,z1,x2,y2,z1,x2,y2,z2, c3)
- polygons[polyCount] = Polygon(x1,y2,z1,x1,y2,z2,x2,y2,z2, c3)
- end
- cube = Cube(0,0,0,20,20,20,{0,0,128})
- cube = Cube(0,20,0,20,20,20,{0,0,100})
- cube = Cube(0,40,0,20,20,20,{0,0,128})
- cube = Cube(20,-20,0,20,20,20,{0,0,128})
- --cube = Cube(40,-20,0,20,20,20,{0,0,100})
- Cube(40,-20,0,20,2,20,{95,60,0})
- cube = Cube(60,-20,0,20,20,20,{0,0,128})
- cube = Cube(80,0,0,20,20,20,{0,0,128})
- cube = Cube(80,20,0,20,20,20,{0,0,100})
- cube = Cube(80,40,0,20,20,20,{0,0,128})
- cube = Cube(60,60,0,20,20,20,{0,0,128})
- cube = Cube(40,60,0,20,20,20,{0,0,100})
- cube = Cube(20,60,0,20,20,20,{0,0,128})
- function on.paint(gc)
- cameraViewPoint[1], cameraViewPoint[2] = getPointOnCircle(radius,degreesx,cameraPoint[1],cameraPoint[2])
- ReCalculation()
- DrawFloor(gc,{128,128,128})
- DrawSky(gc, {64,64,64})
- highest = polygons[1]:getDistance()
- highesti = 1
- order = {}
- done = {}
- distances = {}
- for i = 1, polyCount - 1, 1 do
- distances[i] = polygons[i]:getDistance()
- if polygons[i]:getDistance() < highest then
- highest = polygons[i]:getDistance()
- end
- end
- lowest = highest
- for i = 1, polyCount - 1, 1 do
- done[i] = 0
- end
- for k = 1, polyCount - 1, 1 do
- for i = 1, polyCount - 1, 1 do
- if polygons[i]:getDistance() >= highest then
- if done[i]==0 then
- highest = polygons[i]:getDistance()
- highesti = i
- end
- end
- end
- done[highesti] = 1
- order[k] = highesti
- highest = lowest
- end
- for i = 1, polyCount - 1, 1 do
- polygons[order[i]]:draw(gc)
- end
- end
- function on.charIn(char)
- if char == "w" then
- x1,y1 = getPointOnCircle(1,degreesx,cameraPoint[1],cameraPoint[2])
- cameraPoint[1] = x1
- cameraPoint[2] = y1
- end
- if char == "s" then
- x1,y1 = getPointOnCircle(1,degreesx,cameraPoint[1],cameraPoint[2])
- cameraViewPoint[1] = cameraViewPoint[1] + cameraPoint[1] - x1
- cameraViewPoint[2] = cameraViewPoint[2] + cameraPoint[2] - y1
- cameraPoint[1] = cameraPoint[1] + cameraPoint[1] - x1
- cameraPoint[2] = cameraPoint[2] + cameraPoint[2] - y1
- end
- end
- function on.arrowDown()
- cameraViewPoint[3] = cameraViewPoint[3] - 2
- end
- function on.arrowUp()
- cameraViewPoint[3] = cameraViewPoint[3] + 2
- end
- function on.arrowLeft()
- degreesx = degreesx - .06
- end
- function on.arrowRight()
- degreesx = degreesx + .06
- end
- function goToRelative(x,y,z)
- cameraViewPoint[1] = cameraViewPoint[1] + x
- cameraViewPoint[2] = cameraViewPoint[2] + y
- cameraViewPoint[3] = cameraViewPoint[3] + z
- cameraPoint[1] = cameraPoint[1] + x
- cameraPoint[2] = cameraPoint[2] + y
- cameraPoint[3] = cameraPoint[3] + z
- end
- timer.start(1)
- function on.timer()
- if inputI[currentInput]==1 then
- goToRelative(1,0,0)
- elseif inputI[currentInput]==2 then
- goToRelative(0,1,0)
- elseif inputI[currentInput]==3 then
- goToRelative(0,0,1)
- elseif inputI[currentInput]==4 then
- goToRelative(-1,0,0)
- elseif inputI[currentInput]==5 then
- goToRelative(0,-1,0)
- elseif inputI[currentInput]==6 then
- goToRelative(0,0,-1)
- end
- if inputI2[currentInput]==1 then
- goToRelative(1,0,0)
- elseif inputI2[currentInput]==2 then
- goToRelative(0,1,0)
- elseif inputI2[currentInput]==3 then
- goToRelative(0,0,1)
- elseif inputI2[currentInput]==4 then
- goToRelative(-1,0,0)
- elseif inputI2[currentInput]==5 then
- goToRelative(0,-1,0)
- elseif inputI2[currentInput]==6 then
- goToRelative(0,0,-1)
- end
- if inputI3[currentInput]==1 then
- on.charIn("w")
- elseif inputI3[currentInput]==2 then
- on.charIn("s")
- elseif inputI3[currentInput]==3 then
- on.arrowLeft()
- elseif inputI3[currentInput]==4 then
- on.arrowRight()
- elseif inputI3[currentInput]==5 then
- on.arrowUp()
- elseif inputI3[currentInput]==6 then
- on.arrowDown()
- end
- if inputI4[currentInput]==1 then
- on.charIn("w")
- elseif inputI4[currentInput]==2 then
- on.charIn("s")
- elseif inputI4[currentInput]==3 then
- on.arrowLeft()
- elseif inputI4[currentInput]==4 then
- on.arrowRight()
- elseif inputI4[currentInput]==5 then
- on.arrowUp()
- elseif inputI4[currentInput]==6 then
- on.arrowDown()
- end
- screen:invalidate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement