I'm developing a game where all objects have a getExtents() method which returns the minimum and maximum coordinates occupied by the object. I'm trying to develop a camera that will get the extents of all visible objects and scale and translate things appropriately to have them all appear on the screen. I've tried many things but can't seem to make it work:
Code: Select all
function love.draw()
-- the background is not scaled nor translated
love.graphics.draw( back, 0, 0 )
-- scale and translate the coordinate system
local minx, miny, maxx, maxy = self.visible[ 1 ]:getExtents()
local len = #self.tracked
for i = 2, len do
local ominx, ominy, omaxx, omaxy = self.visible[ i ]:getExtents()
if ominx < minx then
minx = ominx
end
if ominy < miny then
miny = ominy
end
if omaxx > maxx then
maxx = omaxx
end
if omaxy > maxy then
maxy = omaxy
end
end
local dx = maxx - minx
local dy = maxy - miny
local scale
if dx > dy then
scale = love.graphics.getWidth() / dx
else
scale = love.graphics.getHeight() / dy
end
love.graphics.translate( -minx, -miny )
love.graphics.scale( scale )
-- draw the objects
for i = 1, len do
self.visible[ i ]:draw()
end
end
Thanks,
Andre
EDIT: I've found that if I swap the translate and scale calls it works, I have no idea how I missed it. The problem that I'm having now is to centralize the objects on the screen, i.e. if the objects are spread along the horizontal ax I want to make them appear with the same space left on the top and on the bottom.