Platform collisions problems

I NEED HELP!

Guys I need help with tile collisions, I think my problem is easy to fix, but I don’t know how fix it.
My problem is that I know how to “detect” collisions with tiles, but I don’t know how use them.
For example, imagine:

The player is above a platform (how can the player be above that platform without cross it?) and above that platform, there is another platform, then the player should check collisions between itself and the platform of the floor and besides it should check collisions between itself and the second platform above the floor.

Basically what I want to make is a platform game, but I don’t know how to make the collision system, My tiles are “squares” so it doesn’t has ramps.

Could someone explain me what to do or give me a guide or tutorial?

P.S. I hope you understood what I said

Thanks you very much! :smiley:

I was in the same situation 6 months ago. Here. It is the ONLY tutorial that will teach you right. Kudos to kevglass.

I’ve read it and I’ve tried to do what the guide says, but how can I use it with gravity?

Just add the gravity to your y-velocity before you do collision checking and the actual move. But I think you probably already found that out ;).

SIDE NOTE:
I use this method for different things than tilemaps. The only downside to it, is that if your object is going fast enough, it can go through things, since technically, it ‘stepped over’ the object you wanted to check for collisions. But this is great as long as you are not going as far per frame as the length of the shortest collide-able object in the game.

This might be useful…

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.

:’( This damn thing is very frustating! Guys, I’ve tried all you said and it “works” but not as I want, I know how to detect collisions, what I don’t know how to make is to use those “collisions”.

For example if the player is above a platform, then it shouldn’t be able to cross it, but if there’s another platform and the player collides with it “horizontally” then, it shouldn work too. My problem is that when the “player” detect the “floor” it doesn’t detect the “horizontal” collisions of the second. :S

This is my code, I hope you can understand it :slight_smile:

http://pastebin.java-gaming.org/7f90d1e7c64

O___o
Please! Paste bin!


platform_1 = new Platform(0, 595, 800, 5);
platform_2 = new Platform(700, 0, 50, 595);

Don’t ever do this! You can greatly simplify the collisions by using tiles or small objects and pair them one by one. Making a large one is not needed and will introduce bugs!

Thanks I made two “platform” objects to test but I don’t usually do that ;D
By the by, do you know how can I fix the error of the game to check horizontal collisions at the same time as vertical collisions?