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?
Scaling/translating/rotating mouse coordinates
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Scaling/translating/rotating mouse coordinates
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.
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
- ghostwriter
- Prole
- Posts: 38
- Joined: Sat Dec 11, 2010 11:08 pm
Re: Scaling/translating/rotating mouse coordinates
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?
Re: Scaling/translating/rotating mouse coordinates
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.
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.
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: Scaling/translating/rotating mouse coordinates
Attachment is gone. I'd say the aliens stole it.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.
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.
Re: Scaling/translating/rotating mouse coordinates
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.
Re: Scaling/translating/rotating mouse coordinates
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.
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.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Scaling/translating/rotating mouse coordinates
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)
(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.
Re: Scaling/translating/rotating mouse coordinates
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
Merry Christmas
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 63 guests