In its update function, given the world's and the viewport's dimensions, and the camera's next calculated position, orientation and scale, check whether the viewport is wholly inside of the world, and if not, set it so it stays inside of the world, and does not show anything (the void) outside.
Here's the relevant code that i've been trying to append with the orientation detection:
Code: Select all
-- cam.update(cam,dt) -- uses cam instead of implicit self
-- Limit the camera to the world's boundary, if checks are enabled
-- cam.x and cam.y are the position of the camera, that is, the middle of the viewport; cam.o is its orientation, and cam.s is the scale.
if cam.checkBounds then
-- boundary checking helpers
local topLeft = {}
topLeft.x = cam.x - (viewport.width / 2) / cam.s
topLeft.y = cam.y - (viewport.height / 2) / cam.s
-- check if coords are inside the world boundary
if topLeft.x < 0 then
cam.x = (viewport.width / 2) / cam.s
end
if topLeft.x > world.width - (viewport.width / cam.s) then
cam.x = world.width - (viewport.width / 2) / cam.s
end
if topLeft.y < 0 then
cam.y = (viewport.height / 2) / cam.s
end
if topLeft.y > world.height - (viewport.height / cam.s) then
cam.y = world.height - (viewport.height / 2) / cam.s
end
end