Devin Wall
Devin Wall

Reputation: 180

Java 2D tile based game Collision (TOP/BOTTOM/LEFT/RIGHT)

Hello so i have recently learned Java and i am making a 2D tile-based game. I have set up the game loop and all the tile based stuff without any problem but i have been banging my head against the wall on this... It seems easy but i am having the hardest time with it. I want to be able to stop movement on the four direction but it dose not seem to work correctly. if i use it this way it will stop the up and down but as soon as i add the side collision it moves up because x < x if someone has a better solution or even a algorithm for this please let me know! Thanks!

Here is some more info about my set up

I have my main thread that lunches my game thread(game loop). The game loop spawns a "room" the room is for my screen manger. i have a class called MapScreen that extends room this "room" contains my tiles Array and a Player class. the collision is handled in the MapScreen in a method that overrides the rooms update method so the main game loop updates it all. player moment is controlled in a method in the player class I know i could pass a reference to the array into player then just check on the movement if the tile is not blocked i have done something like that before i just wanted to try something new.

Here is my current code:

for (int x = 0; x < 50;x++){
        for (int y = 0; y < 50; y++){
            if (player.rect.intersects(tiles[x][y].rect) && tiles[x][y].blocked == true ){
                //Top
                if (player.rect.y < tiles[x][y].rect.y){
                    player.rect.y -= 10;
                }
                if (player.rect.y > tiles[x][y].rect.y){
                    player.rect.y += 10;
                }

            }
        }
    }

For Example when i try to add side (Left) here:

                if (player.rect.y < tiles[x][y].rect.y){
                    player.rect.y -= 10;
                }

                if (player.rect.y > tiles[x][y].rect.y){
                    player.rect.y += 10;
                }

                if(player.rect.x < tiles[x][y].rect.x ){
                    player.rect.x -= 10;
                }

the above code works kinda Then it kinda just pushes around it to the top. -_-

Upvotes: 0

Views: 1803

Answers (2)

Simon Todd
Simon Todd

Reputation: 156

I use a velocity vector something like this:

Class Player {
    public float x, y; // velocity of player

    public void updatePosition() {
        // set x and y based on user inputs
        for (int i; i<50; i++) {
            for (int j; j<50; j++) {
                if (player.rect.intersects...) {
                    if (player.rect.y > tiles[i][j]) {
                        this.y = 0;
                    }
                    ...
                }
            }
        }
        // translate player by final velocity vector
    }
    ...
}

Upvotes: 1

Mono
Mono

Reputation: 48

Don't use '<=' use '<' if it is still pushing over the top. Another question would be how fast is your game loop? It might be your game loop is too slow to detect the changes until the rect have collided over. You might need to specify your rect's height and width and use it to do a more accurate collision-detection.

Upvotes: 0

Related Questions