Mouse Y is off after Translation

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
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Mouse Y is off after Translation

Post by yetneverdone »

Hi, in my love.draw() function, I have:

Code: Select all

love.graphics.push()
love.graphics.translate(0,width/2) -- to center the game view
love.graphics.scale(ratio,ratio)
love.graphics.pop()
and in my love.update(dt) function:

Code: Select all

local mx = love.mouse.getX()/ratio
local my = love.mouse.getY()ratio
--ps can I do
local mx,my = love.mouse.getPosition()/ratio? I just thought about it this instant
The problem is, without the translate function, it works, but after using the translate function, the mouse(specifically the Y) is off. How would I correct the mouse coordinates? Thanks alot
User avatar
Positive07
Party member
Posts: 1014
Joined: Sun Aug 12, 2012 4:34 pm
Location: Argentina

Re: Mouse Y is off after Translation

Post by Positive07 »

You subtract the offset first. Graphics translations are not related to mouse coordinates.

You basically do the opposite operations so if you added an offset you subtract it, if you scaled up then you scale down

Code: Select all

local mx = (love.mouse.getX() - tx) / sx
local my = (love.mouse.getY() - ty) / sy

Code: Select all

love.draw = function ()
   love.graphics.push()
   love.graphics.translate(tx, ty)
   love.graphics.scale(sx, sy)
   love.graphics.pop()
end
for i, person in ipairs(everybody) do
[tab]if not person.obey then person:setObey(true) end
end
love.system.openURL(github.com/pablomayobre)
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Mouse Y is off after Translation

Post by yetneverdone »

Positive07 wrote: Thu Feb 23, 2017 6:33 am You subtract the offset first. Graphics translations are not related to mouse coordinates.

You basically do the opposite operations so if you added an offset you subtract it, if you scaled up then you scale down

Code: Select all

local mx = (love.mouse.getX() - tx) / sx
local my = (love.mouse.getY() - ty) / sy

Code: Select all

love.draw = function ()
   love.graphics.push()
   love.graphics.translate(tx, ty)
   love.graphics.scale(sx, sy)
   love.graphics.pop()
end

Thanks. Actually ive tried that before posting here, i rechecked again, it was that i test at the wrong function hahaha. Silly me. Thanks anyways
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 83 guests