camera zoom to point

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
ioxu
Prole
Posts: 8
Joined: Wed Mar 30, 2011 2:43 pm

camera zoom to point

Post by ioxu »

Hello lovelies!
This is my first ever post to the forums after about a year of LÖVING myself silly in my spare time.

I've recently had great frustration trying to create a transformation stack to make a 2d camera that has controls for panning across a background and zooming in on a point other than the center of the window.

What I desire to make is a camera that that zooms in on a right-mouse-click-drag, about the click location.

I have code that works well, using standard transform convention, for zooming about the center of the window with the code (vector care of HUMP's vector lib)

Code: Select all

love.graphics.push()
local zoomCenter = vector(love.graphics.getWidth(), love.graphics.getHeight()) / (main.view.zoom * 2)
love.graphics.scale( main.view.zoom )
love.graphics.translate( main.view.x, main.view.y )
-- draw stuff
love.graphics.pop()
What I've tried to do is many variations on

Code: Select all

love.graphics.push()
love.graphics.translate( main.mouseZoomPosition.x, main.mouseZoomPosition.y)
love.graphics.scale( main.view.zoom )
love.graphics.translate( -main.mouseZoomPosition.x , -main.mouseZoomPosition.y)
love.graphics.translate( main.view.x, main.view.y )
-- draw stuff
love.graphics.pop()
.. which seems to almost work. I can pan (via a mouse event modifying main.view.x and y) and zoom, using a mousepressed right-click event modifying main.view.zoom.

But it produces a bump to the view translation, introduced as soon as right-mouse is clicked. The bump seems proportional to the amount of zoom, as with no modification to zoom produces no bump. The frustrating thing is that after the bump it continues to zoom exactly around the right-mouse-click position as desired.

It seems I must factor in the zoom somewhere but after four days of spare time futzing about with 4 lines of transformation script I cannot make it work without the bump. I consider myself fairly familiar with transformation stacks, but this one has me tearing my hair out.

Any help would be very much appreciated.
If you don't feel like giving me a fish I would love to be taught how to yield the fishing pole.

I have attached a complete .love with code from the second example:
Middle-mouse-drag pans the view
-Right-mouse-drag zooms the view about the mouse-click position, after the bump.
'r' resets the view transform
(view transformations happen at line 73)

much löve,
ioxu
Attachments
graph.love
(6.81 KiB) Downloaded 260 times
Post Reply

Who is online

Users browsing this forum: No registered users and 112 guests