Page 1 of 1

Need help transforming screen-coordinates to isometric world-coordiantes

Posted: Thu Sep 21, 2023 3:08 pm
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:

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

Posted: Thu Sep 21, 2023 8:19 pm
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

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

Posted: Fri Sep 22, 2023 12:38 pm
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.

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

Posted: Fri Sep 22, 2023 2:13 pm
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.

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

Posted: Sat Sep 23, 2023 5:05 pm
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