Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (535)
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  
  [SOLVED YAY ] Collision with .tmx ( wiki(ok) ) - Find (x,y) of a/all tiles  (Read 420 times)
0 Members and 1 Guest are viewing this topic.
Offline Andre Lopes
« Posted 2013-06-03 02:56:21 »

Err, hi. Sorry for another topic, but im really lost, and i cant move foward.

I researched many wiki, youtube...
I saw many deprecated stuff, and i was able to finnally draw that .tmx thing Cheesy;

But, i have some questions, i was wondering if any can help me, i already researched and researched and researched and found nothing, nothing at all.

Ok, the question is, supposing that i have a plane in that position, how can i check for collisions near him?

I mean, how can i get all x,y tile positions?
I tried some methods, but i got nothing, all i got was some hexadecimal memory stuff, or i found the id,which doesnt help much ?

Anyway, all i need to know is how to get the X,Y positions of the tiles, must be really really simple/ Obvious, because i just cant find any info related.


Do i need to create properties at the program to get x,y ? idk

obs, im using "TILED" program.



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  
public class AlphaColourLibgdx implements ApplicationListener {

    private SpriteBatch batch;
    private Texture plane;
    private Sprite planeSprite;
    private int xplane, yplane;
    private TiledMap map;
    private OrthographicCamera camera;
    private MapRenderer mr;

    @Override
    public void create() {
        batch = new SpriteBatch();
        plane = new Texture(Gdx.files.internal("plane.jpg"));
        planeSprite = new Sprite(plane);

        xplane = 270;
        yplane = 610;


        map = new TmxMapLoader().load("GrassLevel2.tmx");
       
       
        //Check/Positions of 1 Tile!
       TiledMapTileSet tileSet = map.getTileSets().getTileSet("Grass");
       
       
        System.out.println(tileSet.getTile(1));
       
       
        //
       
        mr = new OrthogonalTiledMapRenderer(map);

        camera = new OrthographicCamera(512, 512);
        camera.position.x = 270;
        camera.position.y = 10;
        camera.update();

    }

    @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);


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

    }
Offline Bassex96

Senior Member


Medals: 4



« Reply #1 - Posted 2013-06-03 03:05:12 »

Load the layer into memory and iterate through x and y.


What I did with my pacman game is, make an extra invisible layer which determines which tiles are blocked, load the blockable layer into memory, then check the tile ID to see if you can pass through it.
Offline Andre Lopes
« Reply #2 - Posted 2013-06-03 03:26:51 »

Load the layer into memory and iterate through x and y.


What I did with my pacman game is, make an extra invisible layer which determines which tiles are blocked, load the blockable layer into memory, then check the tile ID to see if you can pass through it.

Load the layer into memory and iterate through x and y ?

1  
2  
3  
4  
5  
6  
7  
 map = new TmxMapLoader().load("GrassLevel2.tmx");
       
       
        //Check/Positions of 1 Tile!
       TiledMapTileSet tileSet = map.getTileSets().getTileSet("Grass");
        map.getLayers().get(0). ?
       


How can i do that Huh?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Andre Lopes
« Reply #3 - Posted 2013-06-03 04:09:08 »

This doesnt work :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
Figuring out what tile is at a location

You may need to know what tile is at a certain location, for example if the user touches it, or if you have actors with pathfinding and you need to know which tiles are traversible or not.

You can do this by accessing the appropriate tiles array for the layer you are interested in :

   int tileid = map.layers.get(0).tiles[y][x];
Note: here the y and x are swapped around. Also, the x and y is the coordinate of the tile in the map, not in the world or screen so you'll have to do some calculations.

Note 2: Also, just to confuse things more, the map is stored in a y-down coordinate system, whereas your screen and world coordinates are y-up. That'
s why in the example we subtract the y from the map height.

For example, to find the tile at a certain screen location (for a touch for example) you would :
Offline Andre Lopes
« Reply #4 - Posted 2013-06-03 22:49:47 »

I just found out, after changing the words in google a nice tutorial, explaining how to Cheesy

Was a topic on this forum.

So , i started working on it, and figuring out how things worked. It was all fine , except...
Except when i ask the print the tiles positions, it miss some tiles.
Idk why, its just jump a y or a x;


For instance, i dont have a tile at pos(2;2) (as the editor "TILED" says), but it does print Cell 2  :: 2 :: 2

What am i doing wrong?Huh?


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  
package alphacolourlibgdx;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.maps.MapLayer;
import com.badlogic.gdx.maps.MapRenderer;
import com.badlogic.gdx.maps.tiled.*;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author André Vinícius Lopes
 */

public class AlphaColourLibgdx implements ApplicationListener {

    private SpriteBatch batch;
    private Texture plane;
    private Sprite planeSprite;
    private int xplane, yplane;
    private TiledMap map;
    private OrthographicCamera camera;
    private MapRenderer mr;

    @Override
    public void create() {
        batch = new SpriteBatch();
        plane = new Texture(Gdx.files.internal("plane.jpg"));
        planeSprite = new Sprite(plane);

        xplane = 270;
        yplane = 610;
       
        //FileHandle internal = Gdx.files.internal("grassLevelTide.tide");
       
        //map = new TideMapLoader().load("grassLevelTide.tide");
       map = new TmxMapLoader().load("GrassLevel3.tmx");


        mr = new OrthogonalTiledMapRenderer(map);

        camera = new OrthographicCamera(512, 512);
        camera.position.x = 250;
        camera.position.y = 10;
        camera.update();

        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) {
        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) {
                    continue; // There is no cell
               }
                if (cell.getTile() == null) {
                    continue; // No tile inside cell
               }

                if (cell.getTile() != null) {
                    System.out.println(cell.getTile().getId() + " :: " + x + ";" + y);

                }

                //System.out.println(cell.getTile().getId()); // Get the ID.

            }
        }
    }

    @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);


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


    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
    }
}
Offline Andre Lopes
« Reply #5 - Posted 2013-06-04 03:50:55 »

Ok, in tiled the y is inverted.
In Tiled the Cartesian Graphic starts at top left corner....

When you print with libgdx, it starts at bottom left corner.

Kinda stupid of me. But after a lot of debugging and thinking, i did it!!! Cheesy

Oh boy, jimmt will be kinda proud of me. Tongue
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.

pw (36 views)
2014-07-24 01:59:36

Riven (37 views)
2014-07-23 21:16:32

Riven (25 views)
2014-07-23 21:07:15

Riven (27 views)
2014-07-23 20:56:16

ctomni231 (57 views)
2014-07-18 06:55:21

Zero Volt (49 views)
2014-07-17 23:47:54

danieldean (39 views)
2014-07-17 23:41:23

MustardPeter (43 views)
2014-07-16 23:30:00

Cero (59 views)
2014-07-16 00:42:17

Riven (56 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!