Pookie
Pookie

Reputation: 1279

Collision in java not detecting fast enough in a grid based collision system

I am using a 2d boolean array for my collision between true spots in java libgdx. My collision checks and sees if the player can or can't move left or right or fall or jump. This all works however the collision is not fast enough to detect when the player should lets say stop falling. The player is pretty much 1/5 the way inside the platform by the time he stops.

Here is an image to describe better what I mean. The player is in pink the collidable platforms are in red. The following image is of the player just landing from a small drop onto the platform and very very slow gravity.

Player collision

My collision class is pretty straight forward, if the current position is already true don't allow movement, here is the class just for a reference. Note the class is fully functional, just slow.

    @Override
public void run() {
    if(mapGrid.getGrid()[(int) (entity.getGridPosition().x + 1)][(int) entity.getGridPosition().y + 1]){
        entity.setCanMoveRight(false);
    }else
        entity.setCanMoveRight(true);

    if(mapGrid.getGrid()[(int) (entity.getGridPosition().x - 1)][(int) entity.getGridPosition().y + 1]){
        entity.setCanMoveLeft(false);
    }else
        entity.setCanMoveLeft(true);



    if(mapGrid.getGrid()[(int) (entity.getGridPosition().x)][(int) entity.getGridPosition().y] ||
            (mapGrid.getGrid()[(int) (entity.getGridPosition().x + 1)][(int) entity.getGridPosition().y] &&
                    !mapGrid.getGrid()[(int) (entity.getGridPosition().x)][(int) entity.getGridPosition().y])){
        entity.setCanFall(false);
    }else
        entity.setCanFall(true);



}

and here is where everything is being updated.

    @Override
public void render () {
    collisionHandler.run();
    player.update();
    mapGrid.update();

    tmpLogger.log();
    camera.position.set(player.getPosition(), 0);
    camera.translate(player.getPosition());
    camera.update();
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    tmpRend.begin();
    mapGrid.render(tmpRend);
    player.render(tmpRend);
    tmpRend.end();
}

I have looked for hours and hours and I can't seem to get the player where I want him. Just for reference here is what I want the player to look like after his small drop, or any drop of any speed. enter image description here

Any help at all is extremely appreciated. I just need my collision to be very accurate, not sort of accurate. Thank you!!

Upvotes: 2

Views: 307

Answers (1)

Elemental
Elemental

Reputation: 7521

As I understand it it is never good enough just to stop the avatar after collision; in real life a slightly slow frame can then embed you avatar in the surface (as you are experiencing) - you need to detect the point of collision and then move the avatar only that far so that it just sits on the collision point.

Another way of saying this is that when you detect a collision with the ground you need to move the avatar back up so that it is just above the ground.

Upvotes: 2

Related Questions