mapeditor / tiled

Flexible level editor
https://www.mapeditor.org/
Other
11.22k stars 1.76k forks source link

Origin Points for Objects (X,Y-Offsets) #1710

Open Beelzebubele opened 7 years ago

Beelzebubele commented 7 years ago

Before it gets forgotten I thought I will post it here.

A lot of engines use custom origin points (the anchor points) for the game objects. In isometric and 3/4 View (JRPG Perspective) games it makes sense to set the anchor points around the center of the feet. This makes it easier to render the scene in the right order and also apply shadows at these coordinates for example. In side scrolling games in which you often mirror sprites horizontally depending on its direction it makes sense to set the y-origin point to the center so when you mirror the sprite the object will not jump to another position.

Right now Tiled always assumes that the top left corner of an object is its origin point.

The long term solution should be two standard property values for objects that define the offset for their render coordinates in Tiled itself.

A short term solution could be to simply add the custom property values to the coordinates on export. In my case I would like to use Tiled for GameMaker so it should be a pretty simple adjustment in the Game Maker Exporter Script.

Here a chatlog from a few days ago:

bjorn: For objects, do you mean to change the anchor/origin point? That's not possible yet.

Beelzebub: yes, basically the anchor/origin point if it's not possible for Tiled itself would it be possible to consider (custom) property values for x- and y-offset that will be simply added to the coordinates on (GameMaker) Export? In GameMaker its quite common to use center or buttom center as the origin point for objects, so when you import a level from tield with placed enemies there are all off

bjorn Yes, this could be considered.

Beelzebub in my naive imagination it should be only a little adjustment of a single line of code (eg. instead of export_object(name,x,y) it would be export_object(name,x+xoff,y+yoff))

bjorn Well, it will be a little more complicated since objects can be rotated. But you're right, it shouldn't be a big change in any case.

Personally I would be fine with ignoring the rotation for now.

bjorn commented 7 years ago

Right now Tiled always assumes that the top left corner of an object is its origin point.

Unfortunately it's worse than that. Shape objects have top-left corner origin, tile objects have bottom-left corner origin and tile objects on isometric maps have bottom-center origin. :-/

The long term solution should be two standard property values for objects that define the offset for their render coordinates in Tiled itself.

Yeah, such an offset is currently only supported at the tileset level. There's a patch that introduces it at the tile level (#1323). But of course it could be useful for non-tile objects as well.

A short term solution could be to simply add the custom property values to the coordinates on export. In my case I would like to use Tiled for GameMaker so it should be a pretty simple adjustment in the Game Maker Exporter Script.

Right, since GameMaker already supports changing the anchor point for each defined object, it would be useful to have the option to tell Tiled what that anchor point is, so that it can adjust the exported position accordingly. A custom property would be fine for this, and I imagine this property should be supported on both objects and tiles (applied to typed tile objects).

It could conceivably by possible to import a template group from the objects defined in a GameMaker project, but that would be quite a big project.

TrunXGIT commented 7 years ago

This is the only missing feature that prevents me from using Tiled for my Level Design.

Do I miss some workarounds other people use for their placement of objects? Or is the top-left-corner the common origin point for objects in level design even for such things like collectibles and npcs?

bjorn commented 7 years ago

Do I miss some workarounds other people use for their placement of objects? Or is the top-left-corner the common origin point for objects in level design even for such things like collectibles and npcs?

The "workaround" in case Tiled's origin does not match that of your own project, is to convert positions while or after loading the map.

TrunXGIT commented 7 years ago

Sound like I need to write an extra programm/script for that purpose that edits the coordinates in the level file so I can have a proper preview in the game engine itself :/

bjorn commented 7 years ago

@TrunXGIT Which game engine are you using?

TrunXGIT commented 7 years ago

GameMaker 1.4

Big thanks for implementing the originX and originY custom properties in the current developer build during our chat!

RichardEllicott commented 6 years ago

@bjorn

thanks for confirming that, i found this out last night deconstructing the xml... did this just occur in the development process of tiled by accident?

I found removing an object's gid, turning it therefore into a rectangle, moves it's position!

It is a shame there seems a lack of holistic purity in the file format, however can I say the editor itself is a real achievement, despite having to convert the data it still saves me some time.

RichardEllicott commented 6 years ago

I dunno if it's just YET ANOTHER TMX program

but I am currently updating my script (python) that deconstructs TMX, but in what (I THINK) is a better way.

https://github.com/DarkShroom/tmx_loader

I take it apart and in particular do not like 3 flip flags, so change this to rotation 0-3, flip 0-1 which i feel reflects the editor itself (the ZX controls)... i now will likely be correcting the object/rectangle contradiction.

i also like to reflect the names used in the editor itself not the XML format. I put custom properties in line with all the other ones (but overwritten by existing properties)

I dunno, the script is for me really as I am working on a framework in Love 2D, i don't like doing advanced things inside lua itself, i like the idea of making an intermediate format that is as dumb as possible.

Anyway that was my rant, good work on tiled, good program :)

bjorn commented 6 years ago

thanks for confirming that, i found this out last night deconstructing the xml... did this just occur in the development process of tiled by accident?

Yes, more or less. It happened because tile objects were intended for graphical elements in a "top down" use-case, where an origin at the bottom made sense because of the sorting before rendering (by default objects render sorted by their y-coordinate). Similar for isometric maps, where tile objects just "stand" on the map whereas rectangle objects get isometric projection since they are "lying on the floor".

Essentially the tile objects are almost consistent with tiles on a tile layer (if you consider that tiles on a tile layer are aligned to the bottom-left of their cell).

I dunno, the script is for me really as I am working on a framework in Love 2D, i don't like doing advanced things inside lua itself, i like the idea of making an intermediate format that is as dumb as possible.

Ok, so you're writing some advanced Python script to keep the advanced stuff out of your Lua code? :-)

I take it apart and in particular do not like 3 flip flags, so change this to rotation 0-3, flip 0-1 which i feel reflects the editor itself (the ZX controls)... i now will likely be correcting the object/rectangle contradiction.

Indeed there is no "diagonal flip" key in Tiled because I think that would be confusing. On the implementation side though, just having three flip bits makes it easy to apply it to texture coordinates (though I don't benefit from this in Tiled, cause the rendering is done with QPainter).

Anyway that was my rant, good work on tiled, good program :)

Thanks! And thanks for sharing your script! :-)

RichardEllicott commented 6 years ago

no worries, yes exactly i need to keep the advanced stuff out of lua for my sanity :)

dandeto commented 3 years ago

Unfortunately it's worse than that. Shape objects have top-left corner origin, tile objects have bottom-left corner origin and tile objects on isometric maps have bottom-center origin. :-/

I noticed that today and now documented that in my .tmx parser. Do you think this will change at any point? More importantly, is this in the tiled manual? I was reading it last night because I was very confused but figured it out today with trial and error.

rgrams commented 3 years ago

Likewise, I also ran into this issue today. I'm using only Object Layers and I prefer to deal with object center positions in my game code. I wasn't surprised about the top-left origin and it wasn't hard to account for in my custom exporter, but having certain objects still be offset was a surprise.

Maybe there's a better way to work around it, I'm just checking if MapObject.tile exists and if so, I offset the Y-pos in the opposite direction.

bjorn commented 3 years ago

@dandeto @rgrams For tile objects, you can configure their origin with the "Object Alignment" property of their tileset. It defaults to "Unspecified", in which case the old inconsistent behavior is used, but you can set it to "Top Left" to have tile objects have the same origin as the shape objects, or to another origin if you prefer (but shape objects will still have top left origin).

Regarding the documentation, this feature was added in Tiled 1.4 and is mentioned in the Tileset Properties section as well as at the Insert Tile tool and the TMX format docs.

rgrams commented 3 years ago

Good to know, thanks.

Regarding the documentation: In my case, my first project after picking up Tiled was to write a custom exporter script, so instead of reading the manuals on how to use everything in the editor, I was coming at it from the scripting side, reading all the API docs. So it would be great if there could be links to that information from the API reference, probably under MapObject.x, .y, and .pos.