0

I need help with checking for intersections in a game I'm creating: When a character collides with a single obstacle on the screen all works as intended, but when multiple obstacles are added to the screen the character can only jump off of the last obstacle that is being checked for collision. The character still collides properly with the other obstacles (doesn't fall through, cant walk through) but can't jump off of them. Obs is an arraylist of obstacles. Ground is the boolean that determines if the character is allowed to jump.

public void checkIntersect(ArrayList<Obstacle> obs){
    for(Obstacle a: obs){


        if(a.getLeft().intersects(this.getRight())){
            if(getDx()>0){
                sidecollision=true;
                setDx(0);
                this.x-=1.5f;
            }
        } if (a.getRight().intersects(this.getLeft())){
                sidecollision = true;
                setDx(0);
                this.x+=1.5f;
        } if((a.getTop()).intersects(this.getBottom())){
            ground=true;
            setDy(0);
            this.y-=.10f;
        } else if(!(a.getTop()).intersects(this.getBottom())){ 
                ground = false;

                //return ground;        
        } if(a.getBottom().intersects(this.getTop())){
            ground=false;
            setDy(0);

            this.y+=.1f;
        }

    }
} 

and how collision is being checked on the game component:

bg.update(quote);
        win.fill(clear);
        bg.drawBG(win);
        for(Obstacle o: obs){
            o.draw(win);
            o.update(bg);
        }
        if(quote.isVisible()){

                quote.movedrawProtag(win, keys);
                quote.checkIntersect(obs);
        }
  • Welcome to stack overflow. Please can you edit your post to clarify what problem you're seeking help with. What do you expect to happen with your code and what is happening? If necessary, you might like to consider adding some example data for the case that's worrying you. Good luck! – nurdglaw Jun 08 '13 at 23:43
  • 1) You've described a problem and how you can't do it, but have so far not asked a question (let alone a specific, answerable question). What *is* your question? 2) Look at [Shape based collision detection](http://stackoverflow.com/a/14575043/418556). 3) For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Jun 09 '13 at 01:05

1 Answers1

0

The most likely cause of your bug is subsequent calls to checkIntersect() overriding the value of ground. This results in the value of ground only reflecting the last call to checkIntersect(). To fix this bug, you should set ground to a default value of either true or false before making any calls to checkIntersect(). Then, modify the checkIntersect() method so that it can only set ground to the non-default value. When ground should be at its default value, it does not need to be set.

Do something like this. I'm using a default value of false.

public void checkIntersect(ArrayList<Obstacle> obs){
    for(Obstacle a: obs){


        if(a.getLeft().intersects(this.getRight())){
            if(getDx()>0){
                sidecollision=true;
                setDx(0);
                this.x-=1.5f;
            }
        } if (a.getRight().intersects(this.getLeft())){
                sidecollision = true;
                setDx(0);
                this.x+=1.5f;
        } if((a.getTop()).intersects(this.getBottom())){
            ground=true;
            setDy(0);
            this.y-=.10f;
        } else if(!(a.getTop()).intersects(this.getBottom())){ 
                // ground = false;

                //return ground;        
        } if(a.getBottom().intersects(this.getTop())){
            // ground=false;
            // setDy(0);

            // this.y+=.1f;
        }

    }
} 

When you check for collisions:

bg.update(quote);
        win.fill(clear);
        bg.drawBG(win);
        for(Obstacle o: obs){
            o.draw(win);
            o.update(bg);
        }

        // Set ground to its default value
        quote.ground = false;  

        if(quote.isVisible()){

                quote.movedrawProtag(win, keys);
                quote.checkIntersect(obs);
        }

        // If ground has not been set to true, do the default action
        if (quote.ground == false) {
            setDy(0);
            quote.y += .1f;
        }
iforapsy
  • 302
  • 2
  • 5