J'ai essaye de creer mon petit moteur 3D, je ne pense pas m'etre trompe dans les formules, c'est plus dans le code en lui meme.
Lorsque je le lance, il tourne indefiniment.
Voila mon code:
Show/Hide spoilerAfficher/Masquer le spoiler
- Code: Tout sélectionner
local screen = platform.window
local RGB = unpack
function addToGC(name, func)
local tab = platform.withGC(getmetatable)
tab[name] = func
end
Point2D = class()
function Point2D:init(x,y)
self.x = x
self.y = y
self.zoom = 1
self.colour = {0,0,0}
self.inscreen = true
end
function Point2D:isInScreen()
if self.x >= 0 and self.x < screen.width() and self.y >= 0 and self.y < screen.height() then
self.inscreen = true
else
self.inscreen = false
end
end
function Point2D:disp(gc)
self:isInScreen()
if self.inscreen then
gc:setColorRGB(RGB(self.colour))
gc:fillRect(self.x - ((self.zoom - 1) / 2), self.y - ((self.zoom - 1) / 2), self.zoom, self.zoom)
end
end
Line2D = class()
function Line2D:init(x1,y1,x2.y2)
self.p1 = Point2D(x1,y1)
self.p2 = Point2D(x2,y2)
self.colour = {0,0,0}
self.inscreen = true
end
function Line2D:isInScreen()
--a faire
end
function Line2D:disp(gc)
local dx,dy,cumul,point,i
point = Point2D(self.p1.x,self.p1.y)
dx = self.p2.x - self.p1.x
dy = self.p2.y - self.p1.y
point:disp(gc)
cumul = dx / 2
point.x = point.x + 1
while point.x <= self.p2.x do
cumul = cumul + dy
if cumul >= dx then
cumul = cumul - dx
point.y = point.y + 1
end
point:disp(gc)
end
end
Point3D = class()
function Point3D:init(x,y,z)
self.x = x
self.y = y
self.z = z
self.p2d = Point2D(0,0)
self.zoom = 1
self.inscreen = true
self.colour = {0,0,0}
end
function Point3D:to2D()
local t = (screen:width() / 2) / (self.z + 5)
self.p2d.x = self.x * t + (screen:width() / 2)
self.p2d.y = self.y * t + (screen:height() / 2)
end
function Point3D:isInScreen()
self.p2d:isInScreen()
self.inscreen = self.p2d.inscreen
end
function Point3D:disp(gc)
self:isInScreen()
self:to2D()
if self.inscreen then
gc:setColorRGB(RGB(self.colour))
gc:fillRect(self.p2d.x - ((self.zoom - 1) / 2),self.p2d.y - ((self.zoom - 1) / 2),self.zoom,self.zoom)
end
end
Line3D = class()
function Line3D:init(x1,y1,z1,x2,y2,z2)
self.point1 = Point3D(x1,y1,z1)
self.point2 = Point3D(x2,y2,z2)
self.inscreen = true
self.colour = {0,0,0}
self.zoom = 1
end
function Line3D:to2D()
self.point1:to2D()
self.point2:to2D()
end
function Line3D:disp(gc)
self:to2D()
line2d = Line2D(self.point1.p2d.x, self.point1.p2d.y, self.point2.p2d.x, self.point2.p2d.y)
line2d:disp(gc)
end
--main
lx = {-1,-1,-1,1,1,1,1,-1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,1,1,1,1,-1}
ly = {-1,-1,-1,-1,-1,-1,-1,-1,-1,1,-1,1,-1,1,-1,1,1,1,1,1,1,1,1,1}
lz = {-1,1,1,1,1,-1,-1,-1,-1,-1,1,1,1,1,-1,-1,-1,1,1,1,1,-1,-1,-1}
local k = 1
function on.paint(gc)
local line = Line3D(lx[k],ly[k],lz[k],lx[k+1],ly[k+1],lz[k+1])
line:disp(gc)
end
function on.charIn(ch)
if ch == '+' and k < 23 then
k = k + 2
end
screen:invalidate()
end
et le lien vers le code pour ceux sur smartphone : https://pastebin.com/4fTdP69w
Voila, si une ame charitable voulait bien m'aider, je lui en serai reconnaissant
Merci d'avance