[solved] Local mouse position for rotated panels.

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
SirRanjid
Prole
Posts: 39
Joined: Sun Nov 19, 2017 1:44 pm
Contact:

[solved] Local mouse position for rotated panels.

Post 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?
Last edited by SirRanjid on Tue Nov 28, 2017 9:52 pm, edited 2 times in total.
davisdude
Party member
Posts: 1154
Joined: Sun Apr 28, 2013 3:29 am
Location: North Carolina

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

Post 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.
GitHub | MLib - Math and shape intersections library | Walt - Animation library | Brady - Camera library with parallax scrolling | Vim-love-docs - Help files and syntax coloring for Vim
User avatar
SirRanjid
Prole
Posts: 39
Joined: Sun Nov 19, 2017 1:44 pm
Contact:

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

Post 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
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

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

Post 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.
User avatar
SirRanjid
Prole
Posts: 39
Joined: Sun Nov 19, 2017 1:44 pm
Contact:

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

Post 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...
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

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

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
SirRanjid
Prole
Posts: 39
Joined: Sun Nov 19, 2017 1:44 pm
Contact:

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

Post 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:
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot] and 33 guests