Move and zoom with multi touch

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
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Move and zoom with multi touch

Post by darkfrei »

Hi all!

How you making the multi touch support in your games?

So I want to move the map with touch of one finger or move and zoom with two fingers.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1085
Joined: Thu Dec 10, 2020 1:57 am

Re: Move and zoom with multi touch

Post by Gunroar:Cannon() »

The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
Nikki
Citizen
Posts: 83
Joined: Wed Jan 25, 2017 5:42 pm

Re: Move and zoom with multi touch

Post by Nikki »

Here is a zoom to mouse function i have lying around, i dont have time currently to write you a multi touch example, but this function would still be the base:

Code: Select all

function love.keypressed(k)
   if k=='escape' then
      love.event.quit()
   end
end


function love.load()
   camera = {x=0, y=0, scale=1}
   transform = love.math.newTransform(camera.x, camera.y, 0, camera.scale, camera.scale)
end

function love.draw()
   love.graphics.applyTransform(transform)
   love.graphics.print('hello world!', 300,300)
end

function getGlobalDelta(transform, dx, dy)
   local dx1, dy1 = transform:transformPoint( 0, 0 )
   local dx2, dy2 = transform:transformPoint( dx, dy )
   local dx3 = dx2 - dx1
   local dy3 = dy2 - dy1
   return dx3, dy3
end

function love.wheelmoved(x,y)

   -- grab the current mouse screen position
   local posx, posy = love.mouse.getPosition()
   -- figure out what world position that is
   local invx, invy = transform:inverseTransformPoint(posx, posy)

   -- do the actual zoom
   if y >0 then
      camera.scale = camera.scale*1.1
   end
   if y <0 then
      camera.scale = camera.scale*.9
   end
   -- update the transform to be used in draw
   transform = love.math.newTransform(camera.x, camera.y, 0, camera.scale, camera.scale)

   -- with the new transform in place, find out what new world position the mouse points at now
   local invx2, invy2 = transform:inverseTransformPoint(posx, posy)
   -- figure out the difference between the old world position and the new
   local dx = invx - invx2
   local dy = invy - invy2

   -- figure out how much that differnce is in the context of the camera itself
   local dx2, dy2 = getGlobalDelta(transform, dx, dy)
   -- update the camera with that offset
   camera.x = camera.x - dx2
   camera.y = camera.y - dy2
   -- update the transform again, using the new offsets
   transform = love.math.newTransform(camera.x, camera.y, 0, camera.scale, camera.scale)
   
end

hope it helps.
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Move and zoom with multi touch

Post by darkfrei »

I have the zoom-to-mouse solution too, the movement with one finger too (actually it works native), but I have some problems by the two fingers move-and-zoom.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: Move and zoom with multi touch

Post by darkfrei »

Why the call

Code: Select all

love.window.setMode(384, 805)
will be not respected?

Also the

Code: Select all

self.x, self.y, self.w, self.h = love.window.getSafeArea()
returns wrong area.
Screenshot_20220128-232550.jpg
Screenshot_20220128-232550.jpg (164.79 KiB) Viewed 1528 times
multitouch-01.love
(2.58 KiB) Downloaded 57 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 15 guests