Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (526)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (593)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [100%Solved] tmx (x,y) doesnt match libgdx(x,y)-How to? -Already tried several  (Read 1073 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-06-04 16:00:25 »

Hi!
Ok, this time i seriously got halt.
Idk whats happening, i tried messing with unit scale, etc.

The thing is, each tile of mine is 32 pix -width and 16 pix Height

The tmx is 9tile per 9tile;

When i get the x/y pos of each tile, it doesnt match the x/y of the libgdx cartesian graphic.
What should i do now?
I know the code below is somewhat not in good O.O practice, but its just for learning TMX

Please help!

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
public class AlphaColourLibgdx implements ApplicationListener {

    private SpriteBatch batch;
    private Texture plane;
    private Sprite planeSprite;
    private Rectangle planeRect;
    private int xplane, yplane;
    private TiledMap map;
    private OrthographicCamera camera;
    private MapRenderer mr;
    private ArrayList<OrtoPoint> collisionPoints = new ArrayList<>();
    private BitmapFont bf;

    @Override
    public void create() {
        batch = new SpriteBatch();

        //Plane
        plane = new Texture(Gdx.files.internal("plane16.jpg"));
        planeSprite = new Sprite(plane);

        xplane = 10;
        yplane = 10;
        planeRect = new Rectangle(xplane, yplane, 16, 16);


        //BitmapFont
        bf = new BitmapFont();
        bf.setColor(Color.MAGENTA);


        //MapWorkout
        map = new TmxMapLoader().load("GrassLevelOne.tmx");
        mr = new OrthogonalTiledMapRenderer(map);

        //Camera
        camera = new OrthographicCamera(900, 900);
       
        camera.position.set(900 / 2, 900 / 2, 0);
        camera.update();
        camera.apply(Gdx.gl10);



        try {
            whatevername(map);
        } catch (Exception ex) {
            Logger.getLogger(AlphaColourLibgdx.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    // First, we need to get the layer in which you create the tiles.
// In this case I assume you call the layer 'collision' layer in Tiled.
    private int computeCollisionLayerIndex(TiledMap map) {
        for (int i = 0; i < map.getLayers().getCount(); i++) {
            // This is used to get the layer with the name "collision" from all layers
            // (The layers name in Tiled)
            if (map.getLayers().get(i).getName().equalsIgnoreCase("collision")) {
                return i;
            }
        }
        return -1;
    }

// Now we surround getting the layer from the index
// with a little error handling:
    public void whatevername(TiledMap map) throws Exception {
        int collisionLayerIndex = computeCollisionLayerIndex(map);
        if (collisionLayerIndex != -1) {
            MapLayer layer = map.getLayers().get(collisionLayerIndex);

            if (layer instanceof TiledMapTileLayer) {
                getCollisionTilesFrom((TiledMapTileLayer) layer);
            } else {
                throw new Exception("");
            }

        } else {
            throw new Exception("");
        }

    }

// And finally we print the ID of each tile in the layer
// I don't know what you would do with the tiles in the end,
// and since I don't know what tile ID's your tiles have,
// I simply print them.
    public void getCollisionTilesFrom(TiledMapTileLayer layer) {

        int tileHeight = (int) layer.getTileHeight();
        int tileWidth = (int) layer.getTileWidth();
        int tileQuantity = 0;

        System.out.println("TileHeight : " + tileHeight);
        System.out.println("TileWidth : " + tileWidth);

        for (int x = 0; x < layer.getWidth(); x++) {
            for (int y = 0; y < layer.getHeight(); y++) {
                TiledMapTileLayer.Cell cell = layer.getCell(x, y);

                if ((cell != null) && (cell.getTile() != null)) {
                    System.out.println(cell.getTile().getId() + " x:" + x + " y:" + y);
                    collisionPoints.add(new OrtoPoint(x, y, tileWidth, tileHeight));
                    tileQuantity++;
                }
            }
        }
        System.out.println("Tile Qnt :: " + tileQuantity);

    }

    @Override
    public void render() {
        GL10 gl = Gdx.gl10;
        gl.glClearColor(1, 1, 1, 1);
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        mr.setView(camera);
        mr.render();


        batch.begin();
        planeSprite.setPosition(xplane, yplane);
        planeSprite.draw(batch, 1);

        bf.draw(batch, "[o]",1,1); // Doesnt Match

        batch.flush();
        batch.end();

        boolean moveRight = Gdx.input.isKeyPressed(Input.Keys.DPAD_RIGHT);
        boolean moveLeft = Gdx.input.isKeyPressed(Input.Keys.DPAD_LEFT);

        boolean moveUp = Gdx.input.isKeyPressed(Input.Keys.DPAD_UP);
        boolean moveDown = Gdx.input.isKeyPressed(Input.Keys.DPAD_DOWN);

        if (moveUp == true) {
            yplane = yplane + 5;
        }

        if (moveDown == true) {
            yplane = yplane - 5;
        }

        if (moveRight == true) {
            xplane = xplane + 5;
        }

        if (moveLeft == true) {
            xplane = xplane - 5;
        }

        for (int k = 0; k < collisionPoints.size(); k++) {
            OrtoPoint ortoAux = collisionPoints.get(k);
            Rectangle collisionRectangle = ortoAux.getCollisionRectangle();
            int x = ortoAux.getX();
            int y = ortoAux.getY();

            if (collisionRectangle.overlaps(planeRect)) {
                System.out.println("Collided : " + "(" + x + ";" + y + ")"); // All positions are collinding FIX NEEDED
            }

        }

    }

....
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #1 - Posted 2013-06-04 20:30:53 »

Are you using more than one tile sheet in TileD?

Offline Andre Lopes
« Reply #2 - Posted 2013-06-04 20:43:16 »

hey!!! thanks for the reply Cheesy
More than one Tile Sheet?

Well right now im using a grass.jpg 32x16 and redCollision32x16
Also two layers, one for collision which i use the redCollision Tiles and one for Grass, which i use grass.jpg.

And just one .tmx file Tongue

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #3 - Posted 2013-06-04 20:49:04 »

So each tile sheet is just one image? Or are they combined in one image?

Offline Andre Lopes
« Reply #4 - Posted 2013-06-04 20:51:01 »

im using two different images.
One Jpeg called grass.jpeg and one Jpeg called redsomething.jpeg

Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #5 - Posted 2013-06-04 20:55:27 »

X / Y position is not the actual coordinate where it is drawn rather it is the position. Does that help?

Offline Andre Lopes
« Reply #6 - Posted 2013-06-04 21:00:19 »

yes!
But, im having a hard time trying to find where is the pos that is being drawn.

I was able after some time to move the camera properly and make the tmx being drawn at (0,0), i mean, it starts the drawing at 0,0;

Heres a SS of Tiled
Offline Andre Lopes
« Reply #7 - Posted 2013-06-04 22:49:27 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
public class AlphaColourLibgdx implements ApplicationListener {

    private SpriteBatch batch;
    private Texture plane;
    private Sprite planeSprite;
    private Rectangle planeRect;
    private int xplane, yplane;
    private TiledMap map;
    private OrthographicCamera camera;
    private MapRenderer mr;
    private ArrayList<OrtoPoint> collisionPoints = new ArrayList<>();
    private BitmapFont bf;

    @Override
    public void create() {
        batch = new SpriteBatch();

        //Plane
        plane = new Texture(Gdx.files.internal("plane16.jpg"));
        planeSprite = new Sprite(plane);
        xplane = 10;
        yplane = 10;
        planeRect = new Rectangle(xplane, yplane, 16, 16);
     
       
        //BitmapFont
        bf = new BitmapFont();
        bf.setColor(Color.MAGENTA);


        //MapWorkout
        map = new TmxMapLoader().load("GrassLevelTwo.tmx");
        mr = new OrthogonalTiledMapRenderer(map,1f);

        //Camera
        camera = new OrthographicCamera(500, 500);
        //camera.setToOrtho(false,32,32);
       
        camera.position.set(500 / 2, 500 / 2, 0);
        camera.update();

        try {
            mapTheCollision(map);
        } catch (Exception ex) {
            Logger.getLogger(AlphaColourLibgdx.class.getName()).log(Level.SEVERE, null, ex);
        }

    }


Quote
run:
TileHeight :32
TileWidth :32
ID=2 x=0;y=0
ID=2 x=0;y=1
ID=2 x=0;y=2
ID=2 x=0;y=3
ID=2 x=0;y=4
ID=2 x=0;y=5
ID=2 x=0;y=6
ID=2 x=0;y=7
ID=2 x=0;y=8
ID=2 x=0;y=9
ID=2 x=1;y=0
ID=2 x=1;y=9
ID=2 x=2;y=0
ID=2 x=2;y=1
ID=2 x=2;y=4
ID=2 x=2;y=5
ID=2 x=2;y=6
ID=2 x=2;y=7
ID=2 x=2;y=8
ID=2 x=2;y=9
ID=2 x=3;y=0
ID=2 x=3;y=1
ID=2 x=3;y=4
ID=2 x=3;y=5
ID=2 x=3;y=6
ID=2 x=3;y=7
ID=2 x=3;y=8
ID=2 x=3;y=9
ID=2 x=4;y=0
ID=2 x=4;y=1
ID=2 x=4;y=2
ID=2 x=4;y=6
ID=2 x=4;y=7
ID=2 x=4;y=9
ID=2 x=5;y=0
ID=2 x=5;y=1
ID=2 x=5;y=2
ID=2 x=5;y=6
ID=2 x=5;y=7
ID=2 x=5;y=9
ID=2 x=6;y=0
ID=2 x=6;y=1
ID=2 x=6;y=2
ID=2 x=6;y=3
ID=2 x=6;y=9
ID=2 x=7;y=0
ID=2 x=7;y=1
ID=2 x=7;y=2
ID=2 x=7;y=3
ID=2 x=7;y=9
ID=2 x=8;y=0
ID=2 x=8;y=1
ID=2 x=8;y=2
ID=2 x=8;y=3
ID=2 x=8;y=4
ID=2 x=8;y=9
ID=2 x=9;y=0
ID=2 x=9;y=1
ID=2 x=9;y=2
ID=2 x=9;y=3
ID=2 x=9;y=4
ID=2 x=9;y=5
ID=2 x=9;y=6
ID=2 x=9;y=7
ID=2 x=9;y=8
ID=2 x=9;y=9
Tile Qnt :: 66
Java Result: -1
CONSTRU├ŹDO COM SUCESSO (tempo total: 2 segundos)

Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #8 - Posted 2013-06-04 23:38:10 »

But, im having a hard time trying to find where is the pos that is being drawn.
You could multiply the x position by the width of tiles and y position by height of tiles to get an accurate position of the tiles on screen

Offline Andre Lopes
« Reply #9 - Posted 2013-06-05 00:22:28 »

But, im having a hard time trying to find where is the pos that is being drawn.
You could multiply the x position by the width of tiles and y position by height of tiles to get an accurate position of the tiles on screen

Yay it worked.
I had tried that before but, i did with another Rectangle and was bugging, now it did really worked Tongue

I have a question, if you would not be troubled to answer...
What id i had drawn the tmx in 200,200 or 300,300 , in another x,y position?

How would i calculate the tile positions then ?



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
  for (int x = 0; x < layer.getWidth(); x++) {
            for (int y = 0; y < layer.getHeight(); y++) {
                TiledMapTileLayer.Cell cell = layer.getCell(x, y);

                if ((cell != null) && (cell.getTile() != null)) {
                    System.out.println("ID=" + cell.getTile().getId() + " " + "x=" + x * tileWidth + ";" + "y=" + y * tileHeight);
                    collisionPoints.add(new OrtoPoint(x * tileWidth, y * tileHeight, tileWidth, tileHeight));
                    tileQuantity++;
                }
            }
        }



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
 for (int k = 0; k < collisionPoints.size(); k++) {
            OrtoPoint ortoAux = collisionPoints.get(k);
           
            Rectangle collisionRectangle = ortoAux.getCollisionRectangle();
           
            int x = ortoAux.getX();
            int y = ortoAux.getY();

            planeRect = new Rectangle(xplane, yplane, plane.getWidth(),plane.getHeight());
           
            if(planeRect.overlaps(collisionRectangle))
            {
               System.out.println("Overlaps!!" + System.currentTimeMillis());  
            }
           
           
            if (xplane == x && yplane == y) {
                System.out.println("X;Y Collision" + x);
                bf.draw(batch,"X",xplane,yplane);
            }

         


        }
       
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #10 - Posted 2013-06-05 01:05:18 »

Just add the offset to the result of the position as returned by
Quote
You could multiply the x position by the width of tiles and y position by height of tiles to get an accurate position of the tiles on screen
So:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
   int tileWidth = 16;
   int tileHeight = 32;
   
   /**returns a vector of cordinate based tile (not in map units)
    * @param tileX x position of the tile. in map units
    * @param tileY y position of the tile. in map units
    * @param offsetX offset based off x cordinate
    * @param offsetY offset based off of y codinate
    * @return
    */

   public Vector2 position(int tileX, int tileY, int offsetX, int offsetY){
      int x, y;
     
      x = tileX * tileWidth + offsetX;
      y = tileY * tileHeight + offsetY;
     
      return new Vector2(x, y);
   }

should work for your task

Offline Andre Lopes
« Reply #11 - Posted 2013-06-05 01:10:13 »

ah, so im supposed to know that offset right? because the mr.render() dont give the x,y position to draw.
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #12 - Posted 2013-06-05 01:16:14 »

You can definitely set the position of the map. I don't exactly remember how, but the map will be drawn at 0,0 if you don't set coordinate.

Offline Andre Lopes
« Reply #13 - Posted 2013-06-05 01:17:16 »

You can definitely set the position of the map. I don't exactly remember how, but the map will be drawn at 0,0 if you don't set coordinate.

hm, idk how to get that working.

The only parameter that i can set is the camera.

Not sure if thats how to change it.
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #14 - Posted 2013-06-05 01:19:54 »

https://code.google.com/p/libgdx-users/wiki/Tiles#Render

tmp.set(x,y);

that should help, just spend time looking into this, it won't be hard to find

Offline Andre Lopes
« Reply #15 - Posted 2013-06-05 01:23:49 »

https://code.google.com/p/libgdx-users/wiki/Tiles#Render

tmp.set(x,y);

that should help, just spend time looking into this, it won't be hard to find


Jesus, how did you find that?
I love yOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

I looked for that in everywhere!

Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #16 - Posted 2013-06-05 01:27:42 »

Just googled "tilemap set position libgdx"

Offline Andre Lopes
« Reply #17 - Posted 2013-06-05 01:33:45 »

Just googled "tilemap set position libgdx"

i had googled it :

tmx position libgdx

or

tile position libgdx


No wonder i wasnt finding Tongue
Offline wreed12345

JGO Knight


Medals: 25
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #18 - Posted 2013-06-05 01:39:37 »

Well, you got it now. Goodluck with your game

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

toopeicgaming1999 (71 views)
2014-11-26 15:22:04

toopeicgaming1999 (60 views)
2014-11-26 15:20:36

toopeicgaming1999 (14 views)
2014-11-26 15:20:08

SHC (27 views)
2014-11-25 12:00:59

SHC (25 views)
2014-11-25 11:53:45

Norakomi (31 views)
2014-11-25 11:26:43

Gibbo3771 (25 views)
2014-11-24 19:59:16

trollwarrior1 (38 views)
2014-11-22 12:13:56

xFryIx (77 views)
2014-11-13 12:34:49

digdugdiggy (55 views)
2014-11-12 21:11:50
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!