I am trying to create a game where you have a player that can move horizontally and jump. It's kind of like Mario but it isn't a side scroller. I'm using a 2D array to implement a tile map.

My problem is that I don't understand how to check for collisions using this implementation. After spending about two weeks thinking about it, I've got two possible solutions, but both of them have some problems.

Let's say that my map is defined by the following tiles:

0 = sky
1 = player 
2 = ground

The data for the map itself might look like:


For solution 1, I'd move the player (the 1) a complete tile and update the map directly. This make the collision easy because you can check if the player is touching the ground simply by looking at the tile directly below the player:

// x and y are the tile coordinates of the player. The tile origin is the upper-left.
if (grid[x][y+1] == 2){
   // The player is standing on top of a ground tile.

The problem with this approach is that the player moves in discrete tile steps, so the animation isn't smooth.

For solution 2, I thought about moving the player via pixel coordinates and not updating the tile map. This will make the animation much smoother because I have a smaller movement unit per frame. However, this means I can't really accurately store the player in the tile map because sometimes he would logically be between two tiles. But the bigger problem here is that I think the only way to check for collision is to use Java's intersection method, which means the player would need to be at least a single pixel "into" the ground to register collision, and that won't look good.

How can I solve this problem?

  • \$\begingroup\$ Are you making a tile based game? Meaning the player can only be at certain locations on a game board. Think chess or Fire Emblem. Or are you making a game that uses a tile map as a background. Meaning the player has smooth motion through a world. Think Mario or any other platformer. \$\endgroup\$ – mobo Jul 2 '13 at 5:24
  • \$\begingroup\$ I think it's clear that we wants smooth movement but is having some significant trouble understanding how to implement it or best practices for representing objects and the world in a tile-based game. I'd spend way too much time sweeping up the teenager-isms in his spelling and grammar or I'd edit to clear it up. Closing as "unclear" is just wrong here, in my opinion. \$\endgroup\$ – Sean Middleditch Jul 2 '13 at 8:53

I have recently implemented in my own game engine a system that handles side-scroller physics and rendering.

Addressing Solution 1 is pretty straightforward, but far less interesting. Just grab the player's intent to move, compare it to the collisionmask (essentially the grid you described above, but adjusted to just indicate whether the tile is collideable or not) for the tile he would move into, and either execute, adjust, or reject his intended movement.

Solution 2 is more challenging, but generally a better gameplay experience. Let me start by saying you are correct in identifying there is a problem with using pixels. You should separate the game logic from the render logic, and use generalized coordinates (doubles or something to that effect), and convert that to pixel-space when it's time for rendering (if necessary, I don't know what your rendering pipeline looks like).

In my implementation, I registered a simple struct describing the location and size of each tile with a physics manager, adding it to a list. I used the Axis-Aligned Bounding Box (AABB), which for tiles is the whole tile. I also registered a physics struct associated with all of the objects I wanted to collide with walls to a separate list. This struct contains the position, velocity, and size of each object.

Then, each update frame, you compare each registered physics object with each registered physics wall, and generate a list of colliding pairs. You can also compare the physics objects with eachother to generate physics object collisions. Then you pass the lists of collisions off to a collision resolution system.

You have several options with the collision resolution system. If everything is axis-aligned, then you don't need a finer collision comparison. If, however, you have slanted and curved surfaces, you'll want to do a finer test for exclusion. This should be fairly straightforward, but of course ask if you need further clarification.

When objects collide with walls, the tangential component of their velocity (often determined by wall penetration depth and AABB side overlap) are zero'd out, and they are offset so they're no longer inside the wall. When physics objects collide with eachother, it's up to you.

Improvements that can be made: Rather than using a list of walls and physics objects, you could use a QuadTree. This will allow you to skip comparing a large number of objects that have no potential to collide with eachother.


Just a short answer : i think you are confusing yourself by using x,y to define both the world coordinates and the tile coordinates.
Let us say that (x,y) are your world coordinates, then you have to build a function that returns you the tile coordinates for this point. It will look like :

tileX = IntegerPartOf ( ( x - offsetX) / tileSizeX ) ;
tileY = IntegerPartOf ( ( y - offsetY) / tileSizeY ) ;

So when checking if the player if standing, you check wether grid[tileX][tileY+1] == 2.

To check for collision, compute the next position, (nextX, nextY), and then compute the tile coordinates (nextTileX, nextTileY) of this next position. if grid[nextTileX][nextTileY] != 0 you have a collision you have to handle (stop or bounce or...)


This is one of my favourites.

This is one of the best tutorials IMHO for learning to implement tilebased games;regardless of the tutorial being done for flash actionscript. The fundament principles remain the same. This shows how to handle collision while jumping up and hitting floor when coming down, even collision with an overhead brick or blocked element while jumping and then suddenly falling down. Clouds, slopes, one-sided-entry platforms. You can find a lot of information for tile based games here 1.) TonyPa's Tile based game tutorials for flash

Also you might find this a good read Amit's Game Programming Information

  • 1
    \$\begingroup\$ Link-only answers tend to be relatively poor in quality, because they're subject to link rot. Could you perhaps include some of the relevant information from the link, especially as it pertains to the original question? \$\endgroup\$ – Josh Jul 2 '13 at 16:47
  • \$\begingroup\$ Can the person who downvoted please tell me the reason for downvote? I had already edited and detailed this answer when the comment above appeared.Thank you. \$\endgroup\$ – Vishnu Jul 11 '13 at 4:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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