Simple-Tiled-Implementation icon indicating copy to clipboard operation
Simple-Tiled-Implementation copied to clipboard

Object positions aren't correct for isometric maps.

Open Sven65 opened this issue 3 years ago • 8 comments

Describe the bug As the title says, the position of the objects aren't correct.

To Reproduce Steps to reproduce the behavior:

  1. Create a 16x16 isometric map in Tiled, with tiles that measure 30*24px (w * h)
  2. Add an object point on the bottom centermost tile (15, 15), set it's name to "Test Object"
  3. Add a custom layer in your code,
  4. Set the draw function to draw a sprite that's 30 * 36px (w * h)
  5. See that it works on pixel coords (225, 225) from the exported lua from Tiled.
  6. Change the coords of the object point in Tiled to (135, 135).
  7. Export and run again.
  8. See that it doesn't line up properly.

** Example Code **

local layer = map:addCustomLayer("Layer", 3)

local obj

for k, object in pairs(map.objects) do
	if object.name == "Test Object" then
		obj = object
		break
	end
end

layer.obj = {
	sprite = love.graphics.newImage("sprite.png")
	x = obj.y,
	y = obj.x,
	ox = 0,
	oy = 0,
}

function layer:draw ()
	love.graphics.draw(
		self.obj.sprite,
		self.obj.x,
		self.obj.y,
		0,
		1,
		1,
		self.obj.ox,
		self.obj.oy
	)
end

Expected behavior I expect the object to line up properly in the tile coords.

Screenshots

Tiled: Tiled

Game: Game

Version

STI 1.2.3.0

Sven65 avatar Sep 13 '20 08:09 Sven65

After some more expermientation, I've concluded that it doesn't matter what size the object sprite is.

I've also found a rather strange issue, where one of the object points lines up properly, but the other doesn't.

Game: Game

Tiled Tiled

Sven65 avatar Sep 13 '20 16:09 Sven65

After even more testing, the positions of the rendering are correct in relation to the pixel coordinates exported from Tiled, but only in ortographic, and only if there's no scaling of the Map:draw function.

Sven65 avatar Sep 14 '20 06:09 Sven65

Some Tiled objects act a little funny in that when you place them, their location is static and if you move any of their vertices, those vertices get offset values. So even if you move all the vertices of an object to a far off place, the object's "location" remains where it was initialized. Could this be the issue you're seeing?


Landon Manning [email protected]

On Mon, 14 Sep 2020 at 03:27, Mackan [email protected] wrote:

After even more testing, the positions of the rendering are correct in relation to the pixel coordinates exported from Tiled, but only in ortographic, and only if there's no scaling of the Map:draw function.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/karai17/Simple-Tiled-Implementation/issues/242#issuecomment-691842625, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEQD7APJXE5M3RKSFJKWHLSFWZWBANCNFSM4RKRFYIQ .

karai17 avatar Sep 15 '20 04:09 karai17

I'm not entirely sure, as it seems the objects are using the ortographic coordinates exported from Tiled, but aren't being converted into isometric coords properly.

Sven65 avatar Sep 15 '20 05:09 Sven65

As can be seen in the screenshots below, the green tiles aren't put at the right positions in regards to the rest of the map, but are rendered in screen space.

The (n, n) prints are the x and y coords set on the object from STI.

Game: Game

Tiled:

Tiled

Sven65 avatar Sep 16 '20 07:09 Sven65

After some help from @bjorn in the Tiled Discord, I've managed to get a workaround going;

function convertScreenToTile (x, y, tileHeight)
	local xTile = math.ceil(x / tileHeight) - 2
	local yTile = math.ceil(y / tileHeight) - 1

	return xTile, yTile
end

local tileX, tileY = convertScreenToTile(object.x, object.y, map.tileheight)

local xPos, yPos = map:convertTileToPixel(tileX, tileY, map.tileheight)

Sven65 avatar Sep 16 '20 13:09 Sven65

The proposed solution by @Sven65 does not work for me. The object is still drawn outside the corresponding object layer. Also the convertTileToPixel does not accept that third argument as per sti init.lua file . Any suggestions ? Screenshot_20230209_001813

JoaoSobral avatar Feb 08 '23 23:02 JoaoSobral

can you share your code and map files?

karai17 avatar Feb 10 '23 09:02 karai17