Page 1 of 1

[solved] Local mouse position for rotated panels.

Posted: Tue Nov 28, 2017 3:22 pm
by SirRanjid
So I'm rotating my panels as I also plan to also use them as bones for 2d models. Everything works so far except the local mouse position has a weird offset.

Code: Select all

local x,y,w,h,r,rx,ry = v:Layout() --[x,y]=pos [w,h]=size [r]=rotation [rx,ry]=pivot point offset+pos
  
if r ~= 0 then
	love.graphics.translate(rx,ry)	--translate draw operation
	love.graphics.rotate(r)
	love.graphics.translate(-rx,-ry)
	
	--M3 Debug (global mouse pos):
	GAME:AddDebugID("m3","M3: "..tostring( love.mouse.getX())..", "..tostring( love.mouse.getY()),1)
	
	local s = sin(-r)	--rotated local mousepos
	local c = cos(-r)
	
	v.mx,v.my = MousePos()
	
	--M4 Debug ("local" mouse pos):
	GAME:AddDebugID("m4","M4: "..tostring(v.mx)..", "..tostring( v.my ),1)
	
	
	v.mx = v.mx - rx --translate mouse(?)
	v.my = v.my - ry
	
	v.mx = v.mx * c - v.my * s	--rotate mouse
	v.my = v.mx * s + v.my * c
	
	v.mx = v.mx + rx	--translate back(?)
	v.my = v.my + ry
	
	--M5 Debug (local mouse pos):
	GAME:AddDebugID("m5","M5: "..tostring(v.mx)..", "..tostring( v.my ),1)
else
The layout function:

Code: Select all

function UIObj:Layout()
	local p = self.Parent
	if p then
		local px,py=p:Layout()
		return self.x+px , self.y+py , self.w, self.h, self.r, self.rx+self.x+px, self.ry+self.y+py
	end
	
	return self.x, self.y, self.w, self.h, self.r, self.rx+self.x, self.ry+self.y
end
Merged 3 screenshots:
red point is the mouse, the white is the local mouse pos(should be on 1 place), the blue is the pivot point (panel's center)
Top left are debug prints (M3-M5 from the code above,M1 and M2 were between the love.graphics.translate to verify that it has no effect on the mouse )
Image


On the other side of the pivot point the red and white dots swap location (point reflected along the pivot).
I'm sitting on this problem for 2 days now but I don't get it :/

Any ideas?

Re: [need help] Local mouse position for rotated panels.

Posted: Tue Nov 28, 2017 6:15 pm
by davisdude
I'm not sure I quite understand, but have you tried making the rotation amount not negative? I.e.

Code: Select all

	local s = sin(r)	--rotated local mousepos
	local c = cos(r)
That's what looks like is going on to me.

Re: [need help] Local mouse position for rotated panels.

Posted: Tue Nov 28, 2017 8:32 pm
by SirRanjid
Yeah I did this, it doubles the rotation. But since love.graphics.rotate rotates the scene rather than the object it makes sense this way. Thanks though!
The local X position seems to be right while the local Y seems down scaled(with the pivot as [0,0]) as I just observed it again. Weird.
Heres a gif showing the behavior:
Image

Re: [need help] Local mouse position for rotated panels.

Posted: Tue Nov 28, 2017 9:23 pm
by grump
It would be helpful if you posted a minimal example that we can just run and that shows the error.

Generally speaking, I recommend using matrices for transformations. Makes this stuff much easier, especially when you have nested transforms. The problem becomes as easy as multiplying mouse coords with the inverse of the transformation matrix, and it solves scaling, rotation, shearing and translation all at once. Downside is, love 0.10.2 does not provide anything of the sorts.

Re: [need help] Local mouse position for rotated panels.

Posted: Tue Nov 28, 2017 9:49 pm
by SirRanjid
Ok I just *randomly* fixed it like this:

Not working:

Code: Select all

v.mx = v.mx - rx
v.my = v.my - ry
				
v.mx = v.mx * c - v.my * s
v.my = v.mx * s + v.my * c
				
v.mx = v.mx + rx
v.my = v.my + ry
Working:

Code: Select all

v.mx = v.mx - rx
v.my = v.my - ry
				
local vmx = v.mx * c - v.my * s
local vmy = v.mx * s + v.my * c
				
v.mx = vmx + rx
v.my = vmy + ry
I think the part of my brain responsible for logic just blue-screened...

Re: [solved] Local mouse position for rotated panels.

Posted: Tue Nov 28, 2017 10:50 pm
by zorg
The reason is that the middle two lines use both v.mx and v.my, and you already modified v.mx on the first line, so the second one will already use the modified value instead of the original, hence why it behaved wrongly.

Re: [solved] Local mouse position for rotated panels.

Posted: Tue Nov 28, 2017 11:16 pm
by SirRanjid
Ah wow now I see. Thats the kind of stupidity slipping through my stupidity radar :/ I would have just given +rep as stated in the rules to not bump again but it seems to be gone so: Thanks everyone for the help and clarification - much appreciated. c: