Mouse/Selection system dependent on camera scale/position

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
Sosolol261
Party member
Posts: 125
Joined: Wed Nov 26, 2014 6:43 am

Mouse/Selection system dependent on camera scale/position

Post by Sosolol261 »

Hello lovers!
This problem of mine is rather complicated to explain but I'll try as hard as I can.
I am currently developing a map editor for my little RPG game.
The map editor currently supports a camera and zoom(scale).
What I am currently working on, is the map editing system which should be dependent on camera position and camera scale(zoom), however.. IT DOES NOT WORK AHH

What I want my code to do:
1. It gets user mouse position.
2. It translates the position to a usable value(like [1, 1] or [26, 13]) using camera position(camera.x, camera.y) and zoom(camera.scale)
3. It uses translated data to draw a "selection quad" and for future editing purposes.

Image

What It does:
1. It gets user mouse position.
2. It translates the position to a usable value.
-- logic error?
3. It uses translated data to draw a (bugged out) "selection quad" and for future editing purposes.

Image

Additional Details:
1. It works perfectly fine if the scale is 1(block size is 32px*32px)
2. Here is the code used to translate the mouse input to usable information.

Code: Select all

function edit:getMousePosition()
        -- translates data
	local x = math.floor((mouseX - camera.x) / (32 * camera.scale))
	local y = math.floor((mouseY - camera.y) / (32 * camera.scale))

        -- updates values ONLY if they are within the map width/height
	if x >= 0 and y >= 0 and x < maplist[map.current].totalX and y < maplist[map.current].totalY then
		edit.x = x
		edit.y = y
	end
end
3. The love file will be attached!
4. Do not try to read code in the love file from other files except src/edit.lua. (uncommented and hard to understand)
5. You can open an ingame console by pressing f1 and input lua commands by pressing f2.
6. The translation seems to be dependent on camera position and not on camera scale.
7. Thank you for your help :P
Attachments
Mapeditor.love
(1.4 MiB) Downloaded 163 times
User avatar
pgimeno
Party member
Posts: 3673
Joined: Sun Oct 18, 2015 2:58 pm

Re: Mouse/Selection system dependent on camera scale/positio

Post by pgimeno »

I needed to make a change to main.lua to be able to check:

Code: Select all

diff -ru --strip-trailing-cr a/main.lua b/main.lua
--- a/main.lua  2015-11-17 23:49:16.000000000 +0100
+++ b/main.lua  2015-11-18 18:38:52.000000000 +0100
@@ -26,7 +26,7 @@
        ui:load()
        camera:load()
        icl:load("f1", "f2", "Mapeditor", "0.0.1")
-       map:new("balizgud", "D:/Files/Coding/Projects", 31, 17, false)
+       map:new("balizgud", ".", 31, 17, false)
 end
 
 function love.update(dt)
The grid origin's displacement does not get the zoom applied to it. That displaces the selected cell. I can't tell you how exactly to fix it, because I don't know why it's working as it does now (I'm getting a very weird offset that moves depending on zoom).

You need to be very clear on where the origin of your grid is. It can depend on zoom, or not, depending on how your UI is organized, but in any case you need to be able to retrieve or calculate that origin for these things to work well. Once you have the grid origin expressed in the same coordinate system as the mouse (that would be screen coordinates, I think), you can subtract its coordinates from the mouse position and divide the result by (tile_size*zoom) to obtain the correct tile.
PeterTheHe
Prole
Posts: 1
Joined: Wed Nov 18, 2015 6:55 pm

Re: Mouse/Selection system dependent on camera scale/positio

Post by PeterTheHe »

You'll need to multiply the camera positional vector by camera.scale to get it on the same scale as the map.

Code: Select all

local x = math.floor((mouseX - (camera.x * camera.scale)) / (32 * camera.scale))
local y = math.floor((mouseY - (camera.y * camera.scale)) / (32 * camera.scale))
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Mouse/Selection system dependent on camera scale/positio

Post by kikito »

If you use my camera lib, gamera, it'll give you a function to get the inverse of scale, position, and even rotation.

viewtopic.php?f=5&t=12033

The function in question cam:toWorld
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 3 guests