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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Nycleo
Prole
Posts: 3
Joined: Mon Dec 04, 2023 4:56 pm
Location: Latvia

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

Post 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...
Attachments
Messed up cheery!!! help D:
Messed up cheery!!! help D:
image_2023-12-04_190544974.png (24.36 KiB) Viewed 20119 times
Normal cheery
Normal cheery
image_2023-12-04_190358428.png (42.14 KiB) Viewed 20119 times
Last edited by Nycleo on Mon Dec 11, 2023 8:11 pm, edited 1 time in total.
Kindly, your average everyday purple scarf monster.
User avatar
darkfrei
Party member
Posts: 1181
Joined: Sat Feb 08, 2020 11:09 pm

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

Post by darkfrei »

I am making translate and than scale, not in other order.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Nycleo
Prole
Posts: 3
Joined: Mon Dec 04, 2023 4:56 pm
Location: Latvia

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

Post 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?
Kindly, your average everyday purple scarf monster.
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

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

Post 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
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
User avatar
Nycleo
Prole
Posts: 3
Joined: Mon Dec 04, 2023 4:56 pm
Location: Latvia

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

Post 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
Kindly, your average everyday purple scarf monster.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], CPop and 62 guests