Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
    
Collision detection and Collision response are two different things. Keep that in mind –  Sidar Feb 8 '13 at 9:33
    
Voting to close for "debug my code for me". –  Trevor Powell Feb 13 '13 at 0:00
    
This sounds reasonable to me (provided there's no duplicate somewhere else). –  Laurent Couvidou Feb 13 '13 at 11:31
add comment

5 Answers 5

up vote 6 down vote accepted

What that code is doing is limiting motion to only if you're inside the screen. What you should instead do is something like this:

//control code here

if(player.getx() > 1024){
    player.setx(1024);
}else if(player.getx() < 0){
    player.setx(0);
}

that way if the player exceeds the bounds of the screen the position will be set back to the bounds.

share|improve this answer
    
That seemed to work, however, now the player is flickering, if i keep moving at 1024 or 0. –  Kevin Jensen Petersen Feb 6 '13 at 18:58
    
this is due to the player going off screen and then being teleported back, try instead of setting the location, adding force to it in the opposite direction it's going, smoothly pushing it back. I implemented this in my answer. –  Dylan Katz Feb 6 '13 at 19:02
    
Didn't seem to solve it. Still flickering –  Kevin Jensen Petersen Feb 6 '13 at 19:04
    
@KevinJensenPetersen Try using >= 1024 and <= 0 instead. this way the char probably won't teleport back a pixel each time it pushes. –  petervaz Feb 7 '13 at 13:06
add comment

Try this:

 if(player1.getX()+5 < 1024 && 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.xlocation>=1024) {
        player1.addForce(5,"a");
    } else if(player1.xlocation<=0) {
        player1.addForce(5,"d");
    }
share|improve this answer
    
addForce is doing it's work in background, with the "-" also. About the answer, it's still stuck! :D –  Kevin Jensen Petersen Feb 6 '13 at 18:42
    
When it hits the edge of screen –  Kevin Jensen Petersen Feb 6 '13 at 18:42
    
I think your player is getting off screen, lets reset him. –  Dylan Katz Feb 6 '13 at 18:48
    
hmm. try estimating where the player will be before moving, and then prevent the movement from happening if he is past 1024 or less than 0. then if he isn't, allow the move. –  Dylan Katz Feb 6 '13 at 19:04
    
Can't really see how to work that one out. –  Kevin Jensen Petersen Feb 6 '13 at 19:07
show 4 more comments

Compute for the future position first before updating the position of your player:

if( RIGHT is pressed )
{
    var futurePositionX = currentPositionX + increment;

    if( futurePositionX < rightScreenBound )
    {
        currentPositionX = futurePostionX;
    }
    else
    {
        // if futureposition will exceed bound, set position to bound
        // (rightScreenBound is the rightmost possible position)
        currentPositionX = rightScreenBound;
    }
}
share|improve this answer
add comment

Flickering behavior is correct. It is like throwing ball at the wall and when it bounces away, you punch it back to the wall. It is because you are setting forces instead of positions.

First, you must decide, wheter you want to control the position, or force (speed = derivative of position). If you want to set forces, then:

  • you can not simply add force when button is down. It may grow to infinity.

  • when object reaches the border, set force to zero! don't add force in another direction!

share|improve this answer
add comment

The reason it's flickering is probably because you update the possition, then update the image then check for collision then update the possition again and then update the image again.

Instead try to put the collision detection before you update the image.

share|improve this answer
    
Didn't @jmsch explain this in more detail half a year ago? –  Anko Aug 29 '13 at 9:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.