is it possibile to load polygon from tmx maps made by Tiled?

21 views
Skip to first unread message

Paolo

unread,
Apr 1, 2014, 3:13:03 AM4/1/14
to gummw...@googlegroups.com
Hello,
I was wondering if there is some way to load a polygon in a tiled object, I mean not only the rects but single points, to use them as a path for a moving animated image. I have seen that your editor can manage polygons but saves them in a separate file.
paolo

bw

unread,
Apr 1, 2014, 11:34:49 AM4/1/14
to gummw...@googlegroups.com
Hi,

Yes, this is the downside of polygons. Tiled doesn't handle them visually. You need the two editors and two files to work with them "nicely", but you can make Tiled keep the polygon points in objects.

If you know what the points of your polygon are you can enter them in a Tiled object's attributes. You would make an object layer, create an object which is natively like a rect in Tiled, right-click the object and edit its properties. Invent a name for the attribute, e.g. "poly", and enter a list of points. The tmxloader puts the name:value pair in the object.properties dict.

If you use Python tuple notation ("(0,0),(10,0),(10,10),(0,10)") you can use Python's builtin JSON module to parse the string. Or you can invent whatever representation parser you like.

If you don't like to enter points by hand in Tiled, you could write a merge utility in Python to take the polys from world_editor.py and add them to the tmx file. Just name your objects in Tiled so your utility can find them, and either add an empty attr so your utility can modify the value with the points, or add an attr. Python has an XML module if you want to be precise, or you can just hack the tmx contents as a text file.

I'm sure there are more ways to work with polys. Hopefully this helps you wrap your mind around making the editor(s) work.
--
You received this message because you are subscribed to the Google Groups "Gummworld2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gummworld2+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bw

unread,
Apr 1, 2014, 11:43:14 AM4/1/14
to gummw...@googlegroups.com
I wish to add one more thought. In a Tiled object your poly data can be relative points, just to define the shape. When the tmxloader imports the object it will have a rect for its position. Then you can add rect.topleft to each point in the poly to calculate the absolute position of the points. This could make working in Tiled nicer because you can define your shapes in a text file and load them programatically into your objects in a custom loader/processor whenever you encounter attrs named as such, e.g.: attr triangle1 gets triangle1 points; attr quad1 gets quad1 points. And you can do fancy things like scale=2.0, and rot=90 which your loader/processor can handle (or realtime calculations if more appropriate). Hope this helps.


On 4/1/2014 00:13, Paolo wrote:

Paolo

unread,
Apr 2, 2014, 4:14:37 AM4/2/14
to gummw...@googlegroups.com
Ok, I took a look to xml of my tmx map and I found this:
  <object name="wildboar" type="linea" x="509" y="81">
   <properties>
    <property name="durata_pausa" value="1500"/>
    <property name="id" value="3"/>
   </properties>
   <polyline points="-9,18 -16,65 34,73"/>
  </object>
As you write, from polyline points I can calculate the points on the map adding rect.topleft.
But my question now is this: have I got to hack the tiledtmxloader to load in its object this part of xml, or is it hidden in some part of the data which are already loaded?

bw

unread,
Apr 2, 2014, 11:17:46 AM4/2/14
to gummw...@googlegroups.com
Where did <polyline points=> come from, did you add the text or is it a new feature of Tiled? I don't have the newest Tiled yet.

When tmxloader creates an object from a tmx file, it also creates the object's rect attribute. In this instance your "real point" calculations would be done in-game either once in a batch after loading the tmx file, or in real-time whenever you use an object.

Paolo

unread,
Apr 3, 2014, 3:36:34 AM4/3/14
to gummw...@googlegroups.com
The object polyline edited with Tiled Editor 0.91 is an xml file in which a line is represented in this way:
 <objectgroup name="Objects" width="20" height="19">
  <object name="cinghiale" type="linea" x="164" y="166">
   <properties>
    <property name="durata_pausa" value="5000"/>
    <property name="id" value="1"/>

   </properties>
   <polyline points="-9,18 -16,65 34,73"/>
  </object>
 </objectgroup>
I attach the map file for a test.

I don't understand well what you write about calculating the points from the rect. To do this I need the 3 values stored in <polyline points> and I don't know if Tiledtmxloader can load them or I should load them separetly by myself.
map_with_one_poly.tmx

Paolo

unread,
Apr 3, 2014, 7:07:07 AM4/3/14
to gummw...@googlegroups.com
Hello again,
I found that this Tiled map renderer https://github.com/bitcraft/PyTMX
can read the polygonal lines points, it loads all objects in a group and stores points in a variable
(for example mymap.tiledmap.objectgroups[0][2].points)
Do you think it is possibile (but how easy) to add a similar method to tiledtmxloader used in gummworld2?

Paolo

unread,
Apr 3, 2014, 11:58:53 AM4/3/14
to gummw...@googlegroups.com
I was able to change the code by myself, I added a few lines to this function in tmxreader, give some hint if you can.
The new attribute is visible only in raw_map of gummworld2 object.

def _build_object_groups(self, object_group_node, world_map):
        object_group = MapObjectGroupLayer()
        self._set_attributes(object_group_node,  object_group)
        for node in self._get_nodes(object_group_node.childNodes, 'object'):
           
            #small hack to add points of polyline to tiled_object stored in world_map
            try:
                mypoints= node.getElementsByTagName('polyline')[0].attributes['points'].value
                mypoints_list=mypoints.split()
            except:
                mypoints_list=[]
            tiled_object = MapObject()
            tiled_object.mypoints_list=mypoints_list
            print "punti caricati"
            print tiled_object.mypoints_list
            #end of code change
           
            self._set_attributes(node, tiled_object)
            for img_node in self._get_nodes(node.childNodes, 'image'):
                tiled_object.image_source = \
                                        img_node.attributes['source'].nodeValue
            object_group.objects.append(tiled_object)

        # ISSUE 9
        world_map.layers.append(object_group)

bw

unread,
Apr 3, 2014, 4:02:09 PM4/3/14
to gummw...@googlegroups.com
It looks like you're well on your way. I would suggest creating a points list only if a poly node is found. Otherwise all objects will have a list, and many may be empty. I can't say at what point, if any, memory and garbage collection might be impacted.

Gummworld2 presently uses pytmxloader 2. I checked pytmxloader 3, it does not seem to handle the newest Tiled features. For now I think what you're doing is the best approach.

If you want to use PyTMX, I'd suggest copying or hacking TiledMap.py in gummworld2 to use the PyTMX API instead of pytmxloader's. Also, some of toolkit.py also utilizes parts of pytmxloader, but you would not have to remove or change any references that you're not going to use in the game. No other difficulties come to mind.

Gumm

Paolo

unread,
Apr 3, 2014, 4:16:10 PM4/3/14
to gummw...@googlegroups.com
I am thinking to add the attribute "points" to the existing attribute "properties" of gurmmworld2 tiled map, but to do so at now I have to add it first to tiledtmxloader "rawmap" as a separated attribute, void if it is not a poly type object. But in the maps I will use, there will be few objects so I don't think there could be memory problems. Probably it should be better to hack your library and when you decide to do it I will use your revision. About the PyTMX library I have no idea on how to link it to your library, I prefer to stay with tiledtmxloader.
PS: I was looking the new Tiled editor and it is ready to manage animanted tiles, do you think gummworld2 could render them?

bw

unread,
Apr 3, 2014, 7:24:45 PM4/3/14
to gummw...@googlegroups.com
Adding points to the properties dict is a good move.

pygame does not natively support animated GIFs. To use them one would have to incorporate another library such as PIL. I can't volunteer DR0ID, author of pytmxloader, but support for new Tiled features would need to happen there before Gummworld2 adopts them.

How I would handle animated GIFs right now would be to add PIL (something I haven't tried, but it should work fine after a little learning curve) and use Tiled objects to define the animated GIF object (note an object, not a tile) which your program would detect and load the asset with PIL.

Gumm


On 4/3/2014 13:16, Paolo wrote:
I am thinking to add the attribute "points" to the existing attribute "properties" of gurmmworld2 tiled map, but to do so at now I have to add it first to tiledtmxloader "rawmap" as a separated attribute, void if it is not a poly type object. But in the maps I will use, there will be few objects so I don't think there could be memory problems. Probably it should be better to hack your library and when you decide to do it I will use your revision. About the PyTMX library I have no idea on how to link it to your library, I prefer to stay with tiledtmxloader.
PS: I was looking the new Tiled editor and it is ready to manage animanted tiles, do you think gummworld2 could render them?
Reply all
Reply to author
Forward
0 new messages