Touch Zoom

Hello,

I want to zoom an image with two fingers. My problem is that I can not find the x, y of each touch separately, touch 1 and touch 2.

Can anyone tell me how to do this?

Hi @Georgian, check out the Multitouch example, it captures all the touches in a table, rather than using CurrentTouch.

I tried, but not working for me. Maybe an example would be helpful. No, maybe two examples: first example about coordinates x, y and second example about Touch Zoom using two fingers. If you help me, I’m very grateful for your help. :slight_smile: Some things seem a little strange such as Multitouch.

Not sure this is my best code, but it should get y started. Working “Pinch to zoom” here.

function setup()
    s = 0
    touches = {}
    dist = 0
    tcount = 0
end

function draw()
    background(40, 40, 50)
    pushMatrix()
    translate(WIDTH/2 * s * -1, HEIGHT/2 * s * -1)
    scale(s+1,s+1)
    sprite("Planet Cute:Star",WIDTH/2, HEIGHT/2)
    popMatrix()
end

function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
        tcount = tcount - 1
    elseif touch.state == MOVING then
        touches[touch.id] = touch
        if tcount == 2 then
            local ct = 1
            for i,t in pairs(touches) do
                if ct == 1 then
                    firstpt = vec2(t.x,t.y)
                    ct = ct + 1
                else
                    secondpt = vec2(t.x,t.y)
                end
            end
            local newdist = firstpt:dist(secondpt)
            if newdist > dist and s < 6 then
                s = s + 0.1
            elseif newdist < dist and s > 0 then
                s = s - 0.1
            end
            dist = newdist
        end
    else
        tcount = tcount + 1
        if tcount == 1 then
            firstpt = vec2(touch.x,touch.y)
        elseif tcount == 2 then
            secondpt = vec2(touch.x,touch.y)
            dist = firstpt:dist(secondpt)
        end
    end
end 

Hope that helps.

Good job, Vega! It works fine, thanks!

I feel like a beginner in Codea when I look at your code.

Thank you again!

No problem. Maybe someday I will clean that code up, it is more complex than it needs to be. Good luck with Codea, it is my new favorite toy.

If you want some more general touch handling stuff, I have some libraries for handling them. My code is available at http://www.math.ntnu.no/~stacey/HowDidIDoThat/iPad/Codea.html (a lot of it is out of date, though), specifically the file Touch.lua.

Just use my zoom library http://twolivesleft.com/Codea/Talk/discussion/550/zoom-library

I hadn’t seen this before, very nice. I will be using it in future projects I am sure.