3
\$\begingroup\$

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!

\$\endgroup\$
1
  • \$\begingroup\$ 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). \$\endgroup\$
    – ashes999
    Commented Apr 6, 2014 at 2:30

4 Answers 4

2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ 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? \$\endgroup\$ Commented Mar 8, 2014 at 10:02
  • \$\begingroup\$ 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. \$\endgroup\$
    – nhydock
    Commented Mar 10, 2014 at 13:47
2
\$\begingroup\$

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

\$\endgroup\$
1
\$\begingroup\$

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! :)

\$\endgroup\$
0
\$\begingroup\$

It's not that difficult, just loop through all the map objects in the layer and add if they're a rectangle map object then add them to an array of all the objects in the room. You can then add a move request system in which you create a method that will check if the space you are going to move to contains a collision object and if it does then deny the request else let them move.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .