Skip to main content
Formatting
Source Link
Laurent Couvidou
  • 9.2k
  • 2
  • 41
  • 57

2D Collision Detection Limiting a player's movement to the screen's dimensions

I have mya player, that iI can control using WASD. And what iI 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.

EDITSo I've tried the following: 1024 to 974 because my player1 is 50 wide

if (player1.getX()+5 < 974 && player1.getX()-5 > 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");
            }
        } else if(player1.getX() >= 974) {
            player1.addForce(5, "a");
        } else if(player1.getX() <= 0) {
            player1.addForce(5, "d");
        }

It actually works, but the player gets stuck at the edge of screen, and can't move anymore. Any ideas why? Or rather, how do iI make a proper collision detection with movement?

InfoNB: 1024 is the width of screen. And my player is 50 pixels wide, thus the 974. I am only working with X atmat the momemnt, to try getting it to work.

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.

EDIT: 1024 to 974 because my player1 is 50 wide

if(player1.getX()+5 < 974 && player1.getX()-5 > 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");
            }
        } else if(player1.getX() >= 974) {
            player1.addForce(5, "a");
        } else if(player1.getX() <= 0) {
            player1.addForce(5, "d");
        }

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.

Limiting a player's movement to the screen's dimensions

I have a player, that I can control using WASD. And I don't want him to leave the screen.

So I've tried the following:

if (player1.getX()+5 < 974 && player1.getX()-5 > 0) {
    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");
    }
} else if(player1.getX() >= 974) {
    player1.addForce(5, "a");
} else if(player1.getX() <= 0) {
    player1.addForce(5, "d");
}

It actually works, but the player gets stuck at the edge of screen, and can't move anymore. Any ideas why? Or rather, how do I make a proper collision detection?

NB: 1024 is the width of screen. And my player is 50 pixels wide, thus the 974. I am only working with X at the momemnt, to try getting it to work.

added 154 characters in body
Source Link

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.

EDIT: 1024 to 974 because my player1 is 50 wide

if(player1.getX()+5 < 1024974 && player1.getX()-5 > 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");
            }
        } else if(player1.getX() >= 974) {
            player1.addForce(5, "a");
        } else if(player1.getX() <= 0) {
            player1.addForce(5, "d");
        }

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.

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.

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.

EDIT: 1024 to 974 because my player1 is 50 wide

if(player1.getX()+5 < 974 && player1.getX()-5 > 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");
            }
        } else if(player1.getX() >= 974) {
            player1.addForce(5, "a");
        } else if(player1.getX() <= 0) {
            player1.addForce(5, "d");
        }

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.

Source Link

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.