• 3D geometry , code giving weird errors.
    1 replies, posted
Here is my code : [LUA] -- Point3D Point3D = { } function Point3D.__construct( x , y , z ) local New = table.Copy( Point3D ) New.X = x New.Y = y New.Z = z return New end function Point3D:RotateX( Angle ) local rad = Angle * math.pi / 180 local cosa = math.cos( rad ) local sina = math.sin( rad ) self.Y = self.Y * cosa - self.Z * sina; self.Z = self.Y * sina + self.Z * cosa; return Point3D.__construct( self.X , self.Y , self.Z ) end function Point3D:RotateY( Angle ) local rad = Angle * math.pi / 180; local cosa = math.cos( rad ); local sina = math.sin( rad ); self.Z = self.Z * cosa - self.X * sina; self.X = self.Z * sina + self.X * cosa; return Point3D.__construct( self.X , self.Y , self.Z ) end function Point3D:RotateZ( Angle ) local rad = Angle * math.pi / 180; local cosa = math.cos( rad ); local sina = math.sin( rad ); self.X = self.X * cosa - self.Y * sina; self.Y = self.X * sina + self.Y * cosa; return Point3D.__construct( self.X , self.Y , self.Z ) end function Point3D:Project( Width , Height , FOV , ViewerDistance ) local factor = FOV / (ViwerDistance + self.Z); self.X = self.X * factor + Width / 2; self.Y = -self.Y * factor + Height / 2; return Point3D.__construct( self.X , self.Y , self.Z ) end setmetatable( Point3D , { __call = Point3D.__construct } ) ------ Test local Vertices = { Point3D(-1,1,-1), Point3D(1,1,-1), Point3D(1,-1,-1), Point3D(-1,-1,-1), Point3D(-1,1,1), Point3D(1,1,1), Point3D(1,-1,1), Point3D(-1,-1,1) } local Faces = { { 1 , 2 , 3 , 4 }, -- Front Side { 2 , 6 , 7 , 3 }, -- Right Side { 6 , 5 , 8 , 7 }, -- Back Side { 5 , 1 , 4 , 8 }, -- Left Side { 4 , 3 , 7 , 8 }, -- Botom Side { 1 , 5 , 6 , 2 } -- Top Side } local angleX = 15; local angleZ = -30; local angleY = -30; local t = {} for k , v in pairs( Vertices ) do t[ k ] = v:RotateX(angleX):RotateY(angleY):RotateZ(angleZ):Project(400,300,256,4); end /* When drawing the cube we must be careful to draw only the faces that are visible. We do that by using the Painter's Algorithm, which consists in drawing the faces from back to front. Note that other algorithms do exist, and are classified as HIDDEN SURFACE REMOVAL ALGORITHMS. */ local avgZ = {} for index , f in pairs( Faces ) do avgZ[ index ] = ( t[ f[1] ].Z + t[ f[2] ].Z + t[ f[3] ].Z + t[ f[4] ].Z) / 4.0; end table.sort(avgZ, function(a, b) return a > b end) //hook.Add( "HUDPaint" , "TestDrawCube" , function( ) for index , z in ipairs( avgZ ) do local f = Faces[ index ]; local points = { t[ f[1] ].X, t[ f [1] ].Y, t[ f[2] ].X, t[ f [2] ].Y, t[ f[3] ].X, t[ f [3] ].Y, t[ f[4] ].X, t[ f [4] ].Y }; //imagefilledpolygon($im,$points,4,$im_colors[$index]); PrintTable( points ) end //end) [/LUA] I got it by translating this PHP code : [url]http://codentronix.com/2011/06/06/how-to-draw-a-cube-using-php/[/url] For some reason i am getting this error : [CODE] [lua\autorun\client\vspace2.lua:37] attempt to perform arithmetic on field 'X' (a table value) [/CODE] I have tried printing X and indeed it has become a table, except there is nothing ( that i know of ) in my code that would turn it into a table. Help would be appreciated. EDIT : I fixed it , metatables are evil :)
What line is the actual line 37? In this snippet it's an empty line.
Sorry, you need to Log In to post a reply to this thread.