Skip to main content
1 of 3

2D Collision Detection

I have my player, that i can control using WASD. And what i don't want him to be able to. Is to leave the screen. So i've tried following, actually work, but the player gets stuck at the edge of screen, and can't move anymore.

if(player1.getX() < 1024 && player1.getX() > 0) {
        
            //Player 1
            if(Keyboard.isKeyDown(Input.KEY_D)) {
                player1.addForce(5, "d");
            }
            if(Keyboard.isKeyDown(Input.KEY_A)) {
                player1.addForce(5, "a");
            }
            if(Keyboard.isKeyDown(Input.KEY_W)) {
                player1.addForce(5, "w");
            }
            if(Keyboard.isKeyDown(Input.KEY_S)) {
                player1.addForce(5, "s");
            }
        }

Any ideas why? Or rather, how do i make a proper collision detection with movement?

Info: 1024 is the width of screen. I am only working with X atm, to try getting it to work.