Search code examples
javalibgdxtmx

Libgdx getProperties().get("value") give me strange response


I have a strange response with getProperties().get("value").

You can see below my tmx file and values provided by my program.

 <objectgroup name="objects">   <object id="1" name="player" type="player" x="256" y="3072" width="1290" height="1290">    <properties>
    <property name="name" value="Ahhhh"/>
    <property name="nom" value="Bhhhh"/>
    <property name="velocity" value="1.0"/>    </properties>   </object>  </objectgroup>

Source code

    MapObject mapPlayer = currentMap.getLayers().get("objects").getObjects().get("player");
MapObjects mapObject = currentMap.getLayers().get("objects").getObjects();
for (Iterator<String> iter = mapPlayer.getProperties().getKeys(); iter.hasNext(); )
{
    System.out.println("#############"+iter.next());
}
System.out.println("**************** player name :  " + mapPlayer.getProperties().get("name", String.class));
RectangleMapObject rect = (RectangleMapObject) mapObject.get("player");
float x = (float) rect.getRectangle().x;
float y = (float) rect.getRectangle().y;
float width = rect.getRectangle().width;
float height = rect.getRectangle().height;

System.out.println("**************** player coordinates X :  " +x);
System.out.println("**************** player coordinates Y:  "+y);
System.out.println("**************** player coordinates width :  " +width);
System.out.println("**************** player coordinates height:  "+height);
System.out.println("**************** player coordinates X :  " +mapPlayer.getProperties().get("x", Integer.class));
System.out.println("**************** player coordinates Y:  "+mapPlayer.getProperties().get("y", Integer.class));
System.out.println("**************** player velocity :  "+mapPlayer.getProperties().get("velocity", Integer.class));
#######width #######name #######nom #######id #######velocity #######height #######x #######y #######type

**************** player name : Ahhhh **************** player coordinates X : 256.0 **************** player coordinates Y: 3318.0 **************** player coordinates width : 1290.0 **************** player coordinates height: 1290.0 **************** player coordinates X : 256.0 **************** player coordinates Y: 3318.0 **************** player velocity : 1.0

Why Y hasn't show the correct value ?

Thanks


Solution

  • It's probably because libgdx has the Y axis the other way around (pointing upwards instead of downwards like in Tiled) and is trying to be helpful by automatically converting the Y coordinates into its own coordinate space when loading the map.