[Solved] Help Scaling (mouse 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.
Post Reply
User avatar
SelfDotX
Prole
Posts: 25
Joined: Sun Oct 02, 2022 5:06 pm

[Solved] Help Scaling (mouse origin)

Post 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:
Attachments
PoissonDiskSampler.love
(5.12 KiB) Downloaded 51 times
Last edited by SelfDotX on Wed Oct 12, 2022 10:51 am, edited 1 time in total.
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: Help Scaling (mouse origin)

Post 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?
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
SelfDotX
Prole
Posts: 25
Joined: Sun Oct 02, 2022 5:06 pm

Re: Help Scaling (mouse origin)

Post 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.
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: Help Scaling (mouse origin)

Post 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
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
User avatar
SelfDotX
Prole
Posts: 25
Joined: Sun Oct 02, 2022 5:06 pm

Re: Help Scaling (mouse origin)

Post 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!
User avatar
ReFreezed
Party member
Posts: 612
Joined: Sun Oct 25, 2015 11:32 pm
Location: Sweden
Contact:

Re: [Solved] Help Scaling (mouse origin)

Post 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:
Tools: Hot Particles, LuaPreprocess, InputField, (more) Games: Momento Temporis
"If each mistake being made is a new one, then progress is being made."
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests