Scaling/translating/rotating mouse coordinates

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
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Scaling/translating/rotating mouse coordinates

Post by Mud »

Say you've got a map which clickable items at certain coordinates. You could store those as x,y coordinates, then check the mouse position to see if a click happened, but what if the map is rotated/translated/scaled? Do you translate the mouse coordinates at the "driver level" (i.e. before passing to an object for handling)? Do you store the coordinates in the object some other way (such as percentages)?

Assuming anybody can make sense of this question... any ideas for a best practice here?
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Scaling/translating/rotating mouse coordinates

Post by tentus »

Since mouse x and y are pulled from the top left corner of the screen, starting at 0,0 (so a 800x600 window gives you results from 0x0 to 799x599), I would keep a couple of variables storing the offset of the camera. That way your translations are easy addition/subtraction that you can do last-step. Rotation and scaling add in a massive amount of complexity though.

Note that in fullscreen mode the dimensions are of course the dimensions of the window, since your desktop resolution is probably resized to fit the Love window resolution.
Kurosuke needs beta testers
User avatar
ghostwriter
Prole
Posts: 38
Joined: Sat Dec 11, 2010 11:08 pm

Re: Scaling/translating/rotating mouse coordinates

Post by ghostwriter »

If you apply the same transformations to the mouse coordinates as you do to the world, you should get the correct coordinates. It's important that you know the order of transformations and duplicate them exactly. Are you scaling/translating/rotating the world with your own code, or are you using a camera library or something?
pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

Re: Scaling/translating/rotating mouse coordinates

Post by pekka »

You need to invert the transformations to go from mouse coordinates to the world coordinates. I wrote about this previously and posted a bit of code too. The old thread is here.

http://love2d.org/forums/viewtopic.php? ... 00&p=14704

The code was for Löve 0.6.2 I think. It may not run with 0.7 straight away if you're unlucky, but it should be easy to make it run.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Scaling/translating/rotating mouse coordinates

Post by Robin »

pekka wrote:You need to invert the transformations to go from mouse coordinates to the world coordinates. I wrote about this previously and posted a bit of code too. The old thread is here.

http://love2d.org/forums/viewtopic.php? ... 00&p=14704

The code was for Löve 0.6.2 I think. It may not run with 0.7 straight away if you're unlucky, but it should be easy to make it run.
Attachment is gone. I'd say the aliens stole it.

In the pre-0.6 days, we used this library called CAMERA, which also took care of love.graphics.translate/rotate/scale before they existed. Perhaps we can have those same functions for love.mouse in 0.8.0?
Help us help you: attach a .love.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: Scaling/translating/rotating mouse coordinates

Post by vrld »

You could also take a look at hump's camera class, especially at camera:toWorldCoords(p). If you don't want to use it, you can still take a look at the code.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
pekka
Party member
Posts: 206
Joined: Thu Jan 07, 2010 6:48 am
Location: Oulu, Finland
Contact:

Re: Scaling/translating/rotating mouse coordinates

Post by pekka »

Kikito made some use of my code, so you can ask him if there is any of the original logic left. The formulas are at least still there. You can't get correct results for the inverse transformation unless you use the correct formulas, so we can expect that Kikito's and Vrld's math to be the same, even without looking at their code :) :)

I can't repost the attachment myself. I might have it around somewhere, but not here. And I won't go looking for it now.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Scaling/translating/rotating mouse coordinates

Post by kikito »

The code I created based on pekka's work is my Camera.lua file: https://github.com/kikito/passion/blob/ ... Camera.lua

(rambling on)
Lately I've been thinking these days that having a Camera class just to keep track of the mouse might not be the best way to go around it. I'm still undecided.

One can do lots of graphical operations inside love.draw, there must be a way to differentiate every "interesting group" of transformations, and know how to invert it. That is one problem. The class resolving that problem should deal with transforming mouse coordinates. I'd call that a ReferenceSystem.

Camera systems, on the other hand, deal with more things than just math transformations - they are (should be) more high-level: progressive pannings, zooms, scissoring, centering on the screen. Those kinds of things. And to me, that's a different problem.

To me, it doesn't feel right to put the "get me the transformed mouse coords" inside a Camera.
I'm not sure if Camera should be a subclass of ReferenceSystem or would just have one ReferenceSystem attribute.

I still have to think this through a bit more.
(rambling off)
When I write def I mean function.
User avatar
Mud
Citizen
Posts: 98
Joined: Fri Nov 05, 2010 4:54 am

Re: Scaling/translating/rotating mouse coordinates

Post by Mud »

Thanks for the feedback. I just started a new job today, so it'll be a while before I have a time to follow up on any of it, but I just wanted to acknowledge that I've seen and appreciate it!

Merry Christmas :)
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 63 guests