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've looked through a few other answers and it seems like I'm doing a dynamic timestep update and I'm checking about 10 pixels before the rectangle to see if it would intersect. However, the updating must be too fast, because it will register a detection (I checked via print statement debugging) it just wont fire the stop method.

My player update:

public void update(List<TouchEvent> touchEvents, int deltaTime) {
    gamescreen.xScroll += getSpeedX();
    gamescreen.yScroll += getSpeedY();

    this.xPos += getSpeedX();
    this.yPos += getSpeedY();

    if((gamescreen.getXScroll() + 800) >= (map.w * 64)) gamescreen.xScroll = (map.w * 64 - 800);
    if(this.getXPos() >= (map.w * 64)) this.xPos = (map.w * 64);

    if(gamescreen.getXScroll() <= 0) gamescreen.xScroll = 0;
    if(this.getXPos() <= 0) this.xPos = 0;

    if(gamescreen.getYScroll() <= 0) gamescreen.yScroll = 0;
    if(this.getYPos() > 0) this.yPos = 0;

    if((gamescreen.getYScroll() + 480) >= (map.h * 64)) gamescreen.yScroll = (map.h * 64 - 480);
    if(this.getYPos() >= (map.h * 64)) this.yPos = (map.h * 64);

    int len = touchEvents.size();
    for (int i = 0; i < len; i++) {
        TouchEvent event = touchEvents.get(i);

        if(event.type == TouchEvent.TOUCH_DOWN) {
            //Right.
            if (gamescreen.inBounds(event, 150, 330, 70, 70) && isMovingLeft() == false && (gamescreen.getXScroll() + 800) < (map.w * 64)) {
                if(gamescreen.checkEntities(map.trees).equals("right")) {
                    stopRight(deltaTime);
                }
                else {
                    moveRight(deltaTime);
                    setMovingRight(true);
                }
            }
            //Left.
            if (gamescreen.inBounds(event, 0, 350, 70, 70) && isMovingRight() == false && gamescreen.getXScroll() > 0) {
                if(gamescreen.checkEntities(map.trees).equals("left")) {
                    stopLeft(deltaTime);
                }
                else {
                    moveLeft(deltaTime);
                    setMovingLeft(true);
                }
            }
            //Up.
            if (gamescreen.inBounds(event, 50, 280, 70, 70) && isMovingDown() == false) {
                if(gamescreen.checkEntities(map.trees).equals("top")) {
                    stopUp(deltaTime);
                }
                else {
                    moveUp(deltaTime);
                    setMovingUp(true);
                }
            }
            //Down.
            if (gamescreen.inBounds(event, 50, 430, 70, 70) && isMovingUp() == false) {
                if(gamescreen.checkEntities(map.trees).equals("bottom")) {
                    stopDown(deltaTime);
                }
                else {
                    moveDown(deltaTime);
                    setMovingDown(true);
                }
            }
        }

        if (event.type == TouchEvent.TOUCH_UP) {
            if (gamescreen.inBounds(event, 150, 330, 80, 80)) {
                setMovingRight(false);
                stop(deltaTime);
            }
            if (gamescreen.inBounds(event, 0, 350, 80, 80)) {
                setMovingLeft(false);
                stop(deltaTime);
            }
            if (gamescreen.inBounds(event, 50, 280, 80, 80)) {
                setMovingUp(false);
                stop(deltaTime);
            }
            if (gamescreen.inBounds(event, 50, 430, 80, 80)) {
                setMovingDown(false);
                stop(deltaTime);
            }           
        }
    }
    gamescreen.checkEntities(map.trees);

    if (playerHealth == 0 || gamescreen.clock.minutes == 5)
    gamescreen.state = GameState.GameOver;
}

entity check (kind of a mess):

public String checkEntities(BigEntity[] trees) {
    for(int j = 0; j < trees.length; j++) {
        if(player.getRect().right + 10 >= trees[j].getRect().left && player.getRect().bottom >= trees[j].getRect().top && player.getRect().top <= trees[j].getRect().bottom) {
            return "right";
        }
        if(player.getRect().left - 10 <= trees[j].getRect().right && player.getRect().bottom >= trees[j].getRect().top && player.getRect().top <= trees[j].getRect().bottom){
            return "left";
        }
        if(player.getRect().bottom + 10 >= trees[j].getRect().top && player.getRect().right >= trees[j].getRect().left && player.getRect().left <= trees[j].getRect().right) {
            return "bottom";
        }
        if(player.getRect().top - 10 <= trees[j].getRect().bottom && player.getRect().right >= trees[j].getRect().left && player.getRect().left <= trees[j].getRect().right) {
            return "top";
        }
    }
    return "null";
}
share|improve this question
4  
This sounds much like a fix my code question. You've told us the problem and presented your code, what kind of answers are you expecting? What's the question? Other than the implied, "What's wrong with my code?". –  Byte56 Jun 15 '13 at 13:42
add comment

closed as too localized by Byte56, msell, Sean Middleditch, jhocking, Maik Semder Jun 15 '13 at 22:17

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 1 down vote accepted

It looks like checkEntities is behaving in an unexpected manner.

The first things I would check are:

  • trees has the right data
  • the coordinate system has y=0 at the top of the screen

If these are both true then place a breakpoint at the start of the method and see where it goes wrong.

share|improve this answer
    
Basically, a print statement will show that a collision has occurred, even if I pass through the object. I think I pass through the object because my player.update() method doesn't realize there's a collision untill the next iteration (which is why I can slowly move the player to the desired edge and it will stop no matter how many times I try to move). It basically seems to skip the stop method in player.update(). –  A13X Jun 15 '13 at 19:16
    
What are you doing when a collision is detected? Common strategies are to immediately correct the entities position so there is no overlap and to zero or mirror the velocity (stop or bounce). If you are using print to debug make sure you are not just marking execution of that code block but also dumping all key variable to screen. –  Kelly Thomas Jun 16 '13 at 0:46
add comment

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