Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

currently, I am working on a 2d rpg game which is similar to final fantasy 1-4. I can load up a tiled map and the sprite can walk freely on the map. However, I will like to create a wall for it to stop walking through it.

I created three tiled layer Background, Collision, Overhead and one Collision object layer with rectangles only.

"How do I handle collisions with the object layer in the tiled map?"

"Do I have to create every single rectangle that is in the object layer with Rectangle rectangle = new Rectangle() and rectangle.set(x, y, width, height)in the code?"

Thank you very much in advance. Any help is greatly appreciated!

share|improve this question
    
You might want to look into entity-component architecture. In that case, you would have a Collision object per-entity (in this case, wall would be an entity with a Sprite and a Collision component). –  ashes999 Apr 6 at 2:30

3 Answers 3

What I normally have for simple tile map collision, I have a separate tile set with only 2 types of tiles that I use to create a new layer called collision. When I load in a tiled map, I use this layer to create a boolean array version of the map where passability is determined by a tile's id.

share|improve this answer
    
for my tile map, I actually used a full image instead of tileset and copy and paste into the tile map area. I am not sure if that solution will work for me. Also, how can I determine the tile's id? –  user2875021 Mar 8 at 10:02
    
I do it the same way. I design the tilemap precisely in gimp as an image, and then place the image as an image layer. Then I load in a tilemap that's got two tiles (transparent and black) and on a tile layer I mask my image by tiles. The tile's id references the tileset used. You can set the mask tile layer to be invisible when saving your tile map so if you use something like LibGDX's standard tiledmap loader it won't render your collision layer. –  nhydock Mar 10 at 13:47

Yup there are multiple ways you can go around completing this and it really depends on how you are implementing a movement system and if there's a physics engine in the game. If there is no physics engine in your game and you move only by touching squares, I would suggest you implement a future check system. Where you have a large 2d array that hold the relevant collision for each square. Every time you move the sprite you check whether the move will push him into a new square. If so pull it up on the array and determine what to do! :)

share|improve this answer

If the map is loaded in through pixels, then the tile id's will be in a 2d array, so add something like this to your player class (if you have one, you might do tile-based rpgs completely differently):

public void move(int xs, int ys) {
    xo += xs;
    yo += ys;
    if(Level.tiles[xo / 32][yo / 32] == 6) {
        xo -= xs;
        yo -= ys;
    }

}

xs and ys is the speed that the character is travelling at.

xo and yo is the player position

Level.tiles[xo][yo] is the 2d array that stores the id of the tile

the move method is the method that sets the player position after the xs/ys values have been set

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.