I’ll handle them in another way. Personally I like to use tilemaps only for defining easy levels and I use grids to resolve collisions. In my games, I have two types of objects, one which collides with others and other which do not need collisions.
For example, if there are a lot of floor objects and a player object, I’ll make the player a Collision Listener and the floor objects Collidable, so every object has two getters which are overridden in the sub classes.
public boolean isCollisionListener()
{
return true;
}
public boolean isCollidable()
{
return true;
}
If an object has [icode]isCollisionListener()[/icode] flag, It’ll be checked against other collidable objects in the map. And in the map, I have two ArrayList’s for storing those objects and a full list of all objects (for rendering).
Then I’ll make a [icode]Grid[/icode] to easily check for collisions. You can see my article Using Grids for Collisions.
I’ll then use the grid whenever I need to check for collisions like,
grid.clear();
// I'll add all the collidables to the grid
for (Entity e : collidables)
{
grid.addEntity(e);
}
// Process collisions for all collision listeners
for (Entity e : collisionListeners)
{
List<Entity> checked = grid.retrieve(e);
for (Entity e2 : checked)
{
// More thorough detection here.
}
}
This is the base collision checking code in all my games. In the player’s [icode]collisionFloor()[/icode] method, I’ll process the way it is handled. For example,
public void collisionFloor(Floor floor)
{
// Calculate the intersection to check the side of collision
Rectangle intersection = this.getBounds().intersection(floor.getBounds());
// If the height of intersection is more than the width, it is a side collision
if (intersection.height > intersection.width)
{
// A horizontal collision with a floor object. Block the movement here.
}
// If the height of intersection is less than the width, it is a vertical collision.
else if (intersection.height < intersection.width)
{
// If the player is below the floor, he might be jumping.
if (player.y > floor.y)
{
jumping = false;
}
// If he is on top of the floor, align his feet to the wall.
else
{
this.y = floor.y - this.height;
}
}
// If it's not any of the above case, player collided the floor at an edge.
else
{
// If it's a top corner of the floor, let the player move. Else block his movement.
}
}
Hope this helps.