Java-Gaming.org    
Featured games (78)
games approved by the League of Dukes
Games in Showcase (427)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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  
  Tiled Map Collision Detection Help  (Read 901 times)
0 Members and 1 Guest are viewing this topic.
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Posted 2013-05-19 18:51:19 »

Hello forum, recently I began to use a Tiled map for my game. I was thinking about using a more traditional AABB collision method but then found out I can get tile ID's and so I would imagine I could figure out collision that way. I was trolling through past posts about Tiled and collision detection and came upon this:

Moving on. This is you would get the ID of a Tile with libgdx:
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  
// 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) {
    int collisionLayerIndex = computeCollisionLayerIndex(map);
    if (collisionLayerIndex != -1) {
        MapLayer layer = map.getLayers().get(collisionLayerIndex
        if (layer instanceof TiledMapTileLayer) {
            getCollisionTilesFrom((TiledMapTileLayer) layer);
        } else throw some kind of error
    } else throw some kind of error
}

// 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
           
            System.out.println(cell.getTile().getID()); // Get the ID.
       }
    }
}


With this code in particular
1  
 TiledMapTileLayer.Cell cell = layer.getCell(x, y);

You can get the ID from a specific cell. Now for the questions:
1. What would the cell ID turn up as ? A number or some string - how would this be relevant to what is exactly placed?
2. After I figure out when my player is next to a certain tile, how could i prevent the player from moving onto that tile? Would there be a simple way to determine if the tile is to the left of the character, and making a variable like isAbleToMoveLeft set to false if its near a tile where it can not move left.Maybe something like this?
1  
2  
3  
if(collisionTile.x < player.x){
     isAbleToMoveLeft = false;
}


3. Final question is this: I have items drawn in 4 layers in Tiled. Is this something that will effect the way collision detection works? In the method written by matheus23 the getCollisionTilesFrom method requires a layer. So I am curious if 4 layers will create problems.

Thanks for the help! Cool

Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #1 - Posted 2013-05-20 00:58:42 »

2. After I figure out when my player is next to a certain tile, how could i prevent the player from moving onto that tile? Would there be a simple way to determine if the tile is to the left of the character, and making a variable like isAbleToMoveLeft set to false if its near a tile where it can not move left.Maybe something like this?
1  
2  
3  
if(collisionTile.x < player.x){
     isAbleToMoveLeft = false;
}


2. What I did is I had a for-loop that would add 1 (or subtract 1) from the player's x coordinate for each value in the xspeed. It would keep adding until it hit something, and I did something similar for the y coordinate. That is one simple method of solving it, although I'm sure there are much better ways. (You should only check for collision with tiles in the immediate vicinity, however. You don't want to have to check too many tiles too often.)
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #2 - Posted 2013-05-20 01:29:37 »

I do not really understand what you mean by your usage of the foor loop Huh

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

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #3 - Posted 2013-05-20 01:36:21 »

Let's say that xspeed is an integer. If it is negative, this means x gets smaller and the character moves to the left. Otherwise, the character moves to the left.

Here is the pseudo code:
1  
2  
3  
4  
5  
6  
7  
if(xspeed != 0){
         for(int i = 0; i < Math.abs(xSpeed); i++){
            if(the character will not collide with anything){
               x += Math.signum(xspeed);//the Math.signnum() method will return -1 if xspeed < 0, or 1 if it is not
           }
         }
      }
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #4 - Posted 2013-05-20 01:55:45 »

I am still not understanding the usage of the for loop... The "i" variable is not even being used.

Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #5 - Posted 2013-05-20 04:09:12 »

The "i" is unimportant, it's just force of habit. All this does is make sure that you keep moving in the direction of the speed until you hit something.
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #6 - Posted 2013-05-20 04:26:22 »

Perhaps I misunderstood your questions. I thought you were talking about moving your player until it hit something, not just standing still.

However, my method would still work.

But yes, you could have a "canMoveLeft" boolean that would not let the player move left if there was a block right to the left of the player.
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #7 - Posted 2013-05-20 20:50:23 »

Here is what I was thinking to try out: Put all of the cell ID's that are passable into an array list. then do something like this:
1  
2  
3  
4  
5  
6  
7  
for(loop through above array list){
     if(tile to the left of character is passable)
          isAbleToMoveLeft = true;
     else
          isAbleToMoveLeft = false;
}
//repeat for each direction in for loop

i think something like this should work well

Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #8 - Posted 2013-05-20 21:30:07 »

I just got the actual tiled map working (before i just saved the tile map as an image and moved that aorund) but now when I move the camera everything is very grainy when moving. Is there a reason for this? Just to test things out I tried this:

1  
2  
3  
4  
camera.update();
      renderer.setView(camera);
      renderer.render();
      camera.translate(.01f,.01f);

Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #9 - Posted 2013-05-21 02:48:43 »

I would not suggest looping through the tiles, looking for collisions. In all honesty, I am unfamiliar with this library. However, I would imagine that there are ways to check for tiles at a specific point in space. There is no reason to check for EVERY tile, only the near ones.


I don't think I'll be of very much help from here on out, I have never used that library. Good Luck Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #10 - Posted 2013-05-21 03:16:16 »

I found out you can make properties for tiles and I am going to use those to help with collision. The last thing I am having great trouble with is why ".getCell(x, y)" returns a unique number even when trying it on the same tiles... This becomes a problem when trying to match it up with tile properties.

Offline relminator
« Reply #11 - Posted 2013-05-21 03:43:49 »

Are you looping through all your tiles to check for collisions?  If so, that's a bad idea.  You could directly query tiles based on player position.

Say if each tile is 32x32...

1  
2  
3  
4  
int tx = playerx/32;
int ty = playery/32;

int tile = map[tx][ty];

Offline wreed12345

JGO Knight


Medals: 22
Projects: 2
Exp: 2 years


http://linebylinecoding.blogspot.com/


« Reply #12 - Posted 2013-05-21 03:50:08 »

oh no of course im not doing that.

Offline relminator
« Reply #13 - Posted 2013-05-21 10:19:49 »

Take a look at this:

www.java-gaming.org/topics/basic-2d-tile-collision/29657/msg/272514/view/topicseen.html#msg272514
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.

xsi3rr4x (78 views)
2014-04-15 18:08:23

BurntPizza (70 views)
2014-04-15 03:46:01

UprightPath (81 views)
2014-04-14 17:39:50

UprightPath (66 views)
2014-04-14 17:35:47

Porlus (82 views)
2014-04-14 15:48:38

tom_mai78101 (106 views)
2014-04-10 04:04:31

BurntPizza (166 views)
2014-04-08 23:06:04

tom_mai78101 (262 views)
2014-04-05 13:34:39

trollwarrior1 (212 views)
2014-04-04 12:06:45

CJLetsGame (221 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
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!