karai17 / Simple-Tiled-Implementation

Tiled library for LÖVE
Other
839 stars 121 forks source link

An issue with the coords #221

Closed mutantant closed 5 years ago

mutantant commented 5 years ago

Hey. So, trying to use this for a top-down old-school DOS era RPG.

Great stuff so far.

However, I keep running into one odd issue where the expected coords and the actual coords are off by 1.

For example, I make a little cave. Imports into Love2d like a champ. I followed the example and add a sprite layer and keep track of the player sprite position via this. I modify the position x - 1 for left, x + 1 for right, etc.

Then I set up a property on the cave wall to make it block. All good so far.

But in game what happens is the map is drawn correctly, the sprite is there, but when I go towards a wall to the left or top, I'm blocked 1 tile early.

So, basically, I check when the player hits a direction for the next tile and read its properties. Going left? Cool. Get the player position now, check the properties for playerx - 1, playery. Does the tile there have my blocking property? Yes? Then ignore the input. Standard stuff.

But in use it seems to be reading the tile I want it to read, but doing so at a position one greater than the actual map. So the floor tile before the left wall acts as the wall itself.

The only way I have found to fix this is by setting the offset for the tile layer in Tiled to 16 (tiles are 16x16 since they are from a very old game). If I set the offset to 16 x and 16 y in Tiled, now the collision happens against the wall, and not in the space before the wall.

It seems to me, since Tiled starts its maps at 0,0, and Lua uses 1, not 0, for its first element in a table, that maybe this is causing the issue? I have tried all sorts of things to make this work but I just can't seem to break it.

In short:

Is this just user error on my part? Or is anyone else having this issue?

karai17 commented 5 years ago

The xy values in sti are idiomatic to Lua, meaning that they are 1 based, not 0 based like in Tiled. If you're taking that into account, and are still having troubles, post a love file and i can take a look.

On Wed., Jul. 24, 2019, 00:03 Kristopher Neidecker, < notifications@github.com> wrote:

Hey. So, trying to use this for a top-down old-school DOS era RPG.

Great stuff so far.

However, I keep running into one odd issue where the expected coords and the actual coords are off by 1.

For example, I make a little cave. Imports into Love2d like a champ. I followed the example and add a sprite layer and keep track of the player sprite position via this. I modify the position x - 1 for left, x + 1 for right, etc.

Then I set up a property on the cave wall to make it block. All good so far.

But in game what happens is the map is drawn correctly, the sprite is there, but when I go towards a wall to the left or top, I'm blocked 1 tile early.

So, basically, I check when the player hits a direction for the next tile and read its properties. Going left? Cool. Get the player position now, check the properties for playerx - 1, playery. Does the tile there have my blocking property? Yes? Then ignore the input. Standard stuff.

But in use it seems to be reading the tile I want it to read, but doing so at a position one greater than the actual map. So the floor tile before the left wall acts as the wall itself.

The only way I have found to fix this is by setting the offset for the tile layer in Tiled to 16 (tiles are 16x16 since they are from a very old game). If I set the offset to 16 x and 16 y in Tiled, now the collision happens against the wall, and not in the space before the wall.

It seems to me, since Tiled starts its maps at 0,0, and Lua uses 1, not 0, for its first element in a table, that maybe this is causing the issue? I have tried all sorts of things to make this work but I just can't seem to break it.

In short:

  • Map with walls.
  • Walls are tagged with a property that lets me know they block.
  • In Love2d I read the arrow.
  • If an arrow is pressed, we check the next tile in the arrow's direction for special tiles, for example a blocking cave wall.
  • If blocked, don't move.
  • But unless I set an offset on bot the x and y in Tiled to equal the size in pixels of my tiles, checking the properties the tile located at PlayerX - 1, PlayerY seems to read a tile one farther than the one that's actually there, making floor tile act like a wall if I'm 1 tile away going left, or allowing me to step on a wall tile 1 deep on the right.
  • In other words, the tile map data doesn't seem to match the rendered tiles on the screen.

Is this just user error on my part? Or is anyone else having this issue?

— 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/221?email_source=notifications&email_token=AAEQD7D6OHJFACW6IOJNHQLQA7BBDA5CNFSM4IGLWAKKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HBDAC2A, or mute the thread https://github.com/notifications/unsubscribe-auth/AAEQD7EWHKLD6CA5ADVS5KDQA7BBDANCNFSM4IGLWAKA .

mutantant commented 5 years ago

Yes, thank you. While I had taken into account the differing "array" start at 1 in Lua (frankly, if we had only always used that notation and hadn't bound ourselves to the "0 starts array" which is a holdover from when programming and RAM locations were tied together in ways our current high-level languages don't need to follow, we'd all have less debugging to do...).

But, seemingly, I missed trying that angle out on one important calculation which munged the whole thing up. I'll use the excuse that I'd been working on this, including learning Tiled, getting it to work through STI, and building it all up on a lark today in one sitting, so my brain may have been fried.

But to those dealing with a similar issue later:

Tiled's first tile is tile 0, starting at coords 0,0. Not 1,1. STI makes most of this moot, and takes care of so much behind the scenes, but when reading a tile's property (for collision, or whatever you need it for), you will have to manually adjust the coords by 1, which will always catch you unaware!

local props = map:getTileProperties(1, x + 1, y + 1)

Thanks for the library and the help!