Page 1 of 1

[SOLVED] Scaling and camera issues :'D [im probably very stoopid]

Posted: Mon Dec 04, 2023 5:06 pm
by Nycleo
Hello hello!
I'm fairly new to this whole Love2D and lua stuff so please explain to me as simply as possible!

So i'm trying "to zoom in" with the camera system i have made for my little game project but i can't figure out how to do it even with some research.
The "y" doesn't do what i want it to do, as it shows in the two screenshots down here.
What did i mess up ? Help pretty please :'D !!!



Here's my very clumsy code:

Code: Select all

local Camera = {
    x = 0,
    y = 0,
    scale = 2,
 }

function Camera:apply()
    love.graphics.push()
    love.graphics.scale(self.scale,self.scale)
    love.graphics.translate(-self.x, -self.y)
end
 
 function Camera:clear()
    love.graphics.pop()
end
 
 function Camera:setPosition(x, y)
    self.x = x - love.graphics.getWidth() / 2 / self.scale
    self.y = y - love.graphics.getHeight() / 2 / self.scale
    local RX = self.x + love.graphics.getWidth() / 2
    local RY = self.y + love.graphics.getHeight() / 2
    local MapW, MapH = love.graphics.getDimensions() 

    if self.x < 0 then
       self.x = 0
    elseif RX > MapW then
       self.x = MapW - love.graphics.getWidth() / 2
    end

    if self.y > 0 then
        self.y = 0
    elseif RY > MapH then
    self.y = MapH + love.graphics.getHeight() / 2
    end 
end
 
return Camera

Note: First image is the normal camera scale and second is the changed one.
For the changed on i thought just changing "scale" would work but no lol
So i tried tinkering around to understand but haven't found anything...

Re: Scaling and camera issues :'D [im probably very stoopid]

Posted: Mon Dec 04, 2023 6:15 pm
by darkfrei
I am making translate and than scale, not in other order.

Re: Scaling and camera issues :'D [im probably very stoopid]

Posted: Mon Dec 04, 2023 7:22 pm
by Nycleo
darkfrei wrote: Mon Dec 04, 2023 6:15 pm I am making translate and than scale, not in other order.
What do you mean?

Re: Scaling and camera issues :'D [im probably very stoopid]

Posted: Mon Dec 04, 2023 8:29 pm
by dusoft
Nycleo wrote: Mon Dec 04, 2023 7:22 pm
darkfrei wrote: Mon Dec 04, 2023 6:15 pm I am making translate and than scale, not in other order.
What do you mean?
He probably means:
Scale and translate are not commutative operations, therefore, calling them in different orders will change the outcome.
See: https://love2d.org/wiki/love.graphics.scale

Re: Scaling and camera issues :'D [im probably very stoopid]

Posted: Mon Dec 04, 2023 11:10 pm
by pgimeno
I think you have made a mess of coordinate systems. According to the code, self.x and self.y are in screen coordinates, while it appears that Camera:setPosition(x, y) expects world coordinates. Your coordinate transformation formulas are messed up. My recommendation is that you use a camera library such as cam11 or gamera or hump.camera or stalker-x or whatever.

Re: Scaling and camera issues :'D [im probably very stoopid]

Posted: Mon Dec 11, 2023 8:10 pm
by Nycleo
pgimeno wrote: Mon Dec 04, 2023 11:10 pm I think you have made a mess of coordinate systems. According to the code, self.x and self.y are in screen coordinates, while it appears that Camera:setPosition(x, y) expects world coordinates. Your coordinate transformation formulas are messed up. My recommendation is that you use a camera library such as cam11 or gamera or hump.camera or stalker-x or whatever.
Yup! Im currently doing that. But thanks for the explanation! It makes so much sense now that i looked into it lmao