How to change screenspace origin?

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.
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: How to change screenspace origin?

Post by RNavega »

pgimeno wrote: Fri Dec 13, 2024 10:41 am This zooms around the centre of the screen (...)
Thanks for the correction bro. You're right, I tested it and if you do not set the translate part (the x,y) of the transform, it causes the image to snap its ox,oy point to the top-left corner of the screen.

I also tried using a point other than the center of the image to zoom (scale) from, and it adds a bunch of complexity: if the image is already drawn on screen in a transformed way (like positioned, scaled and rotated to some extent), then the screen ox,oy needs to be found within the image.
That is, you can't just use the screen point that you want the image to transform from: the ox,oy point should be relative to top-left corner of the image, not the screen. It's like an internal pixel coordinate within the image.

So if you find the screen point that you want the image to transform from (say, from a mouse click etc), you need to map that point to be internal to the already transformed image, and use that internal point as the ox,oy of the :setTransformation() call and so on.
Luckily this conversion is easily done like this:

Code: Select all

local screen_ox, screen_oy -- From a mouse click or some other screen-relative point.

-- Find the internal image point to use as the transform origin.
local image_ox, image_oy = zoomTF:inverseTransformPoint(screen_ox, screen_oy)

-- Position the image back where it was being drawn, but scaled from the specific transform origin.
zoomTF:setTransformation(screen_ox, screen_oy, 0, zoom, zoom, image_ox, image_oy)
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to change screenspace origin?

Post by darkfrei »

The zoom to mouse solution:
viewtopic.php?p=236724#p236724
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: How to change screenspace origin?

Post by RNavega »

darkfrei wrote: Mon Dec 16, 2024 8:05 am The zoom to mouse solution:
viewtopic.php?p=236724#p236724
Thanks for the link darkfrei!
I like how you're using a power of two (that dscale factor) to make the zoom steps always exact, I hadn't thought of that before.
If there's no rotation involved then, as your demo shows, the math can be done by hand to find the new position to draw the scaled image.

I was trying to understand how you got to the two formulas at the bottom, the ones for calculating translate.x and .y:

Code: Select all

self.zoom = 1
self.dscale = 2^(1/6)
	
function zam_window:wheelmoved(x, y)
	local mx = love.mouse.getX()
	local my = love.mouse.getY()
        if not (y == 0) then -- mouse wheel moved up or down
		-- zoom in to point or zoom out of point
		local mouse_x = mx - self.translate.x
		local mouse_y = my - self.translate.y
		local k = self.dscale^y
		self.zoom = self.zoom*k
		self.translate.x = math.floor(self.translate.x + mouse_x * (1 - k) + 0.5)
		self.translate.y = math.floor(self.translate.y + mouse_y * (1 - k) + 0.5)
Trying to come up with them again, I got to this instead:

Code: Select all

        self.translate.x = math.floor(mx + (self.translate.x - mx) * k + 0.5)
        self.translate.y = math.floor(my + (self.translate.y - my) * k + 0.5)
The result is the same, but reading them, the logic hits: "Start from the mouse point (mx), then move from that point towards the top-left corner of the image (translate.x - mx) but scaled by factor k."
So you're always repositioning the top-left corner of the scaled image so the picked point stays the same.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to change screenspace origin?

Post by darkfrei »

RNavega wrote: Tue Dec 17, 2024 3:40 am
darkfrei wrote: Mon Dec 16, 2024 8:05 am The zoom to mouse solution:
viewtopic.php?p=236724#p236724
Thanks for the link darkfrei!
I like how you're using a power of two (that dscale factor) to make the zoom steps always exact, I hadn't thought of that before.
Thanks! Your code looks much better!

The idea with zoom power of two factor was not mine, but it was in FFF-204: https://www.factorio.com/blog/post/fff-204
Related to HR entities, It turned out that our zooming system never showed an exact zoom of 2.0, which would be the 'pixel perfect' zoom level for the HR entities. By changing the zoom rate from 1.1, to the 7th root of 2 (1.104089...), the zoom now increments perfectly from 1.0 to 2.0 in 7 steps.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
RNavega
Party member
Posts: 385
Joined: Sun Aug 16, 2020 1:28 pm

Re: How to change screenspace origin?

Post by RNavega »

Brilliant, thanks for sharing.

PS it's music =D
https://forums.factorio.com/viewtopic.p ... 56#p303556
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 5 guests