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 some problems with collisions, the collision detection only works either on one x "line", or on one y "line".

Example

Heres the collision code:

public void checkCollision() {
    for (int i = 0; i < drawX.size(); i++) {
        if (player.getX() == drawX.get(i) + 16 && player.getY() == drawY.get(i)) {

            player.setLeft(false);
        } else if (player.getX() != drawX.get(i) + 16 && player.getY() != drawY.get(i)) {
            player.setLeft(true);
        }

        if (player.getX() == drawX.get(i) - 16 && player.getY() == drawY.get(i)) {

            player.setRight(false);
        } else if (player.getX() != drawX.get(i) - 16 && player.getY() != drawY.get(i)){
            player.setRight(true);
        }

        if (player.getX() == drawX.get(i) && player.getY() == drawY.get(i) - 16) {

            player.setDown(false);
        } else if (player.getX() != drawX.get(i) && player.getY() != drawY.get(i) - 16){
            player.setDown(true);
        }

        if (player.getX() == drawX.get(i) && player.getY() == drawY.get(i) + 16) {

            player.setUp(false);
        } else if (player.getX() != drawX.get(i) && player.getY() != drawY.get(i) + 16){
            player.setUp(true);
        }

    }
}

drawX and drawY store the positions of the tiles.

share|improve this question
1  
"Here's my code fix it for me" questions are almost always too localized. This one isn't an exception unfortunately. You need to step through your code with the debugger, see where your player is and see which statements it's falling into and which ones it's not. –  Byte56 Jul 26 '12 at 16:33

2 Answers 2

The code is not understood at all and what you want exactly? I can say only you try:

public void checkCollision() {
      player.setLeft(false);
      player.setRight(false);
      player.setUp(false);
      player.setDown(false);

      ...
      ...
}

and replace "==" to "<" or ">"

share|improve this answer
    
The collision only works in one x row/y col. –  user18417 Jul 26 '12 at 14:59

Player position is not getting increased by 1. So it overpasses the coordinates of the tiles. For example, consider a tile and the player at the following positions. I think size of your tile is 16.

|      |   |    |
|player|   |tile|
|      |   |    |
       5   10   26
Player moving right.

Now if player position would have been increased by 1. Then x = 6, 7, 8, 9, 10 (gotcha). But usually it is not the case.

Lets say it is being increased by 2. Then x = 7, 9, 11 (overpassed). So use <= and >= instead of comparing the positions exactly.

EDITED

public void checkCollision() {
    for (int i = 0; i < drawX.size(); i++) {
        //If tile on left
        if (player.getX() == drawX.get(i) + 16 && player.getY() == drawY.get(i)) {
            player.setLeft(false);
        }
        //Otherwise
        else{
            player.setLeft(true);
        }
        //If tile on right
        if (player.getX() == drawX.get(i) - 16 && player.getY() == drawY.get(i)) {
            player.setRight(false);
        }
        //Otherwise
        else{
            player.setRight(true);
        }
        //If tile below
        if (player.getX() == drawX.get(i) && player.getY() == drawY.get(i) - 16) {
            player.setDown(false);
        }
        //Otherwise
        else{
            player.setDown(true);
        }
        //If tile above
        if (player.getX() == drawX.get(i) && player.getY() == drawY.get(i) + 16) {
            player.setUp(false);
        }
        //Otherwise
        else{
            player.setUp(true);
        }
    }
}

Try modifying your code as above. Remove the else condition. It should work.

share|improve this answer
    
The tiles are placed every 16th pixel (16, 32, 48, 64) and the player x/y get in/decreased by 16. Player.setLeft(true) sets a boolean called "left" true. This boolean is being checked when you press the left arrow key. If it's true, decrease x by 16, if not, do nothing. –  user18417 Jul 27 '12 at 8:25
    
Edited my answer –  Shashwat Jul 27 '12 at 9:13
    
I can just walk through the tiles when I remove the else conditions. –  user18417 Jul 27 '12 at 9:31
    
You better check your set***() functions. I think it is setting 'left' to 'false' after moving the player. And then when left is pressed, it sets 'left' to 'true' and hence moves it over the tile. Debug the code and check which values it is getting when it overlaps the tile. –  Shashwat Jul 27 '12 at 9:42

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.