Need help transforming screen-coordinates to isometric world-coordiantes

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
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Need help transforming screen-coordinates to isometric world-coordiantes

Post by NoreoAlles »

Hello, i am working on an isometric game with the intention of making some sort of minecrafty game out of it. Now, the world to screen transform works great, but i cant get the screen to world transform (for mouse clicking tiles) to work. I have looked at multiple solutions and formulas but cant find any that work, even with fiddling around a ton.
.love attached
Thanks :awesome:
Attachments
iso.love
(5.21 KiB) Downloaded 68 times
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Need help transforming screen-coordinates to isometric world-coordiantes

Post by darkfrei »

NoreoAlles wrote: Thu Sep 21, 2023 3:08 pm Hello, i am working on an isometric game with the intention of making some sort of minecrafty game out of it. Now, the world to screen transform works great, but i cant get the screen to world transform (for mouse clicking tiles) to work. I have looked at multiple solutions and formulas but cant find any that work, even with fiddling around a ton.
.love attached
Thanks :awesome:
(tested by camera.x=0 camera.y=0)

Code: Select all

	local mouseX, mouseY = love.mouse.getPosition()
	local xa, xb = camera.x/block_width,  mouseX/block_width 
	local ya, yb = camera.y/block_height, mouseY/block_height
	
	local grid_x = math.floor(xa + xb + 2*yb - 0.5)
	local grid_y = math.floor(2*ya + 2*yb - xb + 0.5)

update:
it works very good:

Code: Select all

function love.update(dt)
	camera_move(dt)
	local screen = {}
	local mouseX, mouseY = love.mouse.getPosition()
	local xa, xb = camera.x/block_width,  mouseX/block_width 
	local ya, yb = camera.y/block_height, mouseY/block_height
	grid_x = math.floor(-xa -2*ya + xb + 2*yb - 0.5)
	grid_y = math.floor(-2*ya+xa + 2*yb - xb + 0.5)
	
	if grid_x < 1 or grid_x > grid_size or grid_y < 1 or grid_y >grid_size then 
--		return 
	else 
		grid[grid_x][grid_y]= 0
	end
end

function love.draw()
	for x = 1,grid_size do
		for y = 1,grid_size do
			local x0 = camera.x + (x-y) * (block_width / 2)
			local y0 = camera.y + (x+y) * (block_height / 4)
			if x == grid_x and y == grid_y then
				love.graphics.setColor (0.8,0.8,0.2)
			elseif x == grid_x then
				love.graphics.setColor (0.8,0.8,0.2)
			elseif y == grid_y then
				love.graphics.setColor (0.2,0.8,0.2)
			else
				love.graphics.setColor (1,1,1)
			end
			if grid[x][y] > 0 then
				love.graphics.draw(grass, x0, y0)
			end
			love.graphics.print (x..' '..y, x0, y0)
		end
	end
	Debug:draw()
end
Attachments
main.lua
(2.54 KiB) Downloaded 60 times
Last edited by darkfrei on Fri Sep 22, 2023 2:11 pm, edited 1 time in total.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: Need help transforming screen-coordinates to isometric world-coordiantes

Post by NoreoAlles »

darkfrei wrote: Thu Sep 21, 2023 8:19 pm (tested by camera.x=0 camera.y=0)

Code: Select all

	local mouseX, mouseY = love.mouse.getPosition()
	local xa, xb = camera.x/block_width,  mouseX/block_width 
	local ya, yb = camera.y/block_height, mouseY/block_height
	
	local grid_x = math.floor(xa + xb + 2*yb - 0.5)
	local grid_y = math.floor(2*ya + 2*yb - xb + 0.5)
Thank you very much!
I think I´ll just translate the whole screen instead of needing to account for that inside of the rendering.
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
User avatar
darkfrei
Party member
Posts: 1197
Joined: Sat Feb 08, 2020 11:09 pm

Re: Need help transforming screen-coordinates to isometric world-coordiantes

Post by darkfrei »

NoreoAlles wrote: Fri Sep 22, 2023 12:38 pm Thank you very much!
I think I´ll just translate the whole screen instead of needing to account for that inside of the rendering.
See updated file in attach.
Attachments
main.lua
(2.23 KiB) Downloaded 115 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
NoreoAlles
Party member
Posts: 130
Joined: Mon Jan 03, 2022 5:42 pm

Re: Need help transforming screen-coordinates to isometric world-coordiantes

Post by NoreoAlles »

darkfrei wrote: Fri Sep 22, 2023 2:13 pm
NoreoAlles wrote: Fri Sep 22, 2023 12:38 pm Thank you very much!
I think I´ll just translate the whole screen instead of needing to account for that inside of the rendering.
See updated file in attach.
Oh I already did that myself, but thank you very much

Code: Select all

function get_tile_mouse()
	mouseX, mouseY = love.mouse.getPosition()
	local xb = (-camera.x+mouseX)/block_width
	local yb = (-camera.y+mouseY)/block_height

	sumX = math.floor( xb + 2*yb - 0.5)
	sumY = math.floor( 2*yb - xb + 0.5)
	local grid_x = sumX
	local grid_y = sumY

	love.window.setTitle ("x:"..grid_x..' y:'..grid_y)
	return grid_x, grid_y
end
"Why do they call it oven when you of in the cold food of out hot eat the food?" - Jon Arbuckle
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 4 guests