Page 1 of 1

[Solved] Help Scaling (mouse origin)

Posted: Tue Oct 11, 2022 7:35 pm
by SelfDotX
I'm trying to zoom in/out on a set of points - A Good Example, without using any graphics.* functions. I've got close - I can zoom in and out with the mouse acting as the origin of the scaling, but if I move the mouse to another location and try scaling there - it breaks. I've attached what I have so far - the main.lua file has the scaling code. Any help would be greatly appreciated - I've been chasing my tail all day, need to take a break :death:

Re: Help Scaling (mouse origin)

Posted: Tue Oct 11, 2022 9:05 pm
by milon
SelfDotX wrote: Tue Oct 11, 2022 7:35 pm I'm trying to zoom in/out on a set of points - A Good Example, without using any graphics.* functions...
What do you mean without any graphics.* functions? Without love.graphics.* you can't output anything to the screen. Unless you mean something else?

Re: Help Scaling (mouse origin)

Posted: Tue Oct 11, 2022 9:12 pm
by SelfDotX
Without using graphics.transform or scale - I'm transforming data - points, displaying it is not a requirement. Put another way, I need the math behind it, not an api to do it for me.

Re: Help Scaling (mouse origin)

Posted: Wed Oct 12, 2022 4:17 am
by ReFreezed
You have to use the last scale value to calculate the difference between the old and new scale origins for the new offset. Here's an updated drawOffscreen() and love.wheelmoved():

Code: Select all

local panX, panY = 0, 0

local function drawOffscreen()
    local settings = PDS.GetSettings()

    love.graphics.setCanvas(oScreen)
    love.graphics.clear()
    love.graphics.setColor(1, 0, 1, 1)

    love.graphics.line(panX*scale,0, panX*scale,600) -- Updated.
    love.graphics.line(0,panY*scale, 800,panY*scale) -- Updated.

    for _, pos in ipairs(points) do
        if pos.c then
            love.graphics.setColor(1, 1, 1, 1)
        end
        love.graphics.points((pos.x+panX)*scale, (pos.y+panY)*scale) -- Updated.
        love.graphics.setColor(1, 0, 1, 1)
    end
    love.graphics.setColor(1, 1, 1, 1)
    love.graphics.print(
        " Attempts/Sample " .. tostring(settings.maxAttemptsPerSample) ..
        "\n" .. " minRadius  " .. tostring(settings.minRadius) ..
        "\n" .. " maxRadius  " .. tostring(settings.maxRadius) ..
        "\n" .. " stepRadius " .. tostring(settings.stepRadius) ..
        "\n" .. " spawnRadFac " .. tostring(settings.spawnRadiusFactor) ..
        "\n" .. " spawnType  " .. tostring(settings.spawnType) ..
        "\n" .. " scale      " .. tostring(scale)
    )
    love.graphics.print(
        " Attempts/Sample " .. tostring(settings.maxAttemptsPerSample) ..
        "\n" .. " minRadius  " .. tostring(settings.minRadius) ..
        "\n" .. " maxRadius  " .. tostring(settings.maxRadius) ..
        "\n" .. " stepRadius " .. tostring(settings.stepRadius) ..
        "\n" .. " spawnRadFac " .. tostring(settings.spawnRadiusFactor) ..
        "\n" .. " spawnType  " .. tostring(settings.spawnType) ..
        "\n" .. " scale      " .. tostring(scale)
    )
    love.graphics.setCanvas()
end

function love.wheelmoved(dx, dy)
    local mX, mY = love.mouse.getPosition()
    local oldMx  = mX / scale -- Use old scale.
    local oldMy  = mY / scale

    scale = scale * 1.3^dy -- Exponential zoom feels more natural.

    local newMx = mX / scale -- Use new scale.
    local newMy = mY / scale

    panX = panX - oldMx + newMx -- Add to the old pan values.
    panY = panY - oldMy + newMy

    print(tonumber(string.format("%.1f", scale)))
    drawOffscreen()
end

Re: Help Scaling (mouse origin)

Posted: Wed Oct 12, 2022 10:25 am
by SelfDotX
Thank you again Refreezed. Part of my frustration was that I knew the math was dead simple - I just couldn't put the pieces together. Anyway, at least I can start my day off on the right foot - really appreciate that!

Re: [Solved] Help Scaling (mouse origin)

Posted: Wed Oct 12, 2022 1:31 pm
by ReFreezed
I know what you mean. I remember when I initially tried to figure out this very problem in one program. It took longer than it really should have. :death: