I just created a tile map for my game, and its a side profile game. The game looks sort of like Terraria, where there is blocks you can place and break. I want the player to be able to collide with the blocks, and I don't know how to achieve something like this.

The game is in Javascript but I can understand other languages so feel free to give examples in the language of your choice.


closed as too broad by Alexandre Vaillancourt, MAnd, Philipp, Kromster, Anko Feb 3 '16 at 13:00

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • \$\begingroup\$ Man, so many questions that can be answered by modding Minecraft. <3 The way MC handles this is that "block" and "item" are singletons, where ItemStack is the object that shows up in the inventory: it holds a pointer to the block/item singleton and stores a count (plus has some other methods, like what to do when the user right-clicks with it, and so on). \$\endgroup\$ – Draco18s Feb 1 '16 at 21:45

I created a simple platformer that used rectangle collision in order to detect if the player is currently colliding with a tile. I made the game in c# but I believe the concept will be the same for Javascript. Here are the steps I took:

1: Create a new class named Tile

2: Generate a list of Tiles in order to fill up the tilemap

3: Run a loop through each tile to determine if the player is colliding with the tile.

                player.Left <= rectangle.Right &&
                player.Right >= rectangle.Left &&
                player.Top <= rectangle.Bottom &&
                player.Bottom >= rectangle.Top

4: If the previous condition returns true, determine if the player's midpoint is above/below/to the left of/to the right of the tile's midpoint. (This is used to determine if the tile is being used as the ground, a wall, or the ceiling)

5: Manipulate Player's position according to step 4

There is a more optimal way of detecting collision, but I found this method the easiest to understand.


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