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 need help with checking for intersections in a game I'm creating. When a character collides with a single obstacle on the screen everything 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.

My intersection checking code follow, obs is an ArrayList of obstacles, ground is the boolean that determines if the character is allowed to jump. The obstacle and character have a hitbox created by 4 rectangles.

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 this is 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);
}    
share|improve this question
    
What have you tried? Did you run this through a debugger? –  Laurent Couvidou Jun 10 '13 at 14:20
add comment

2 Answers 2

You are setting ground to false in the wrong place. You should set ground to false at the beginning of the method. Here is the modified code:

public void checkIntersect(ArrayList obs) {

    ground = false;

    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())) {

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

            this.y += .1f;
        }

    }
}
share|improve this answer
add comment

To answer your question, if you only reference the sides and collision boolean variables when the object is moving in that direction, it should help.

I.e. (pseudo-code):

if(moving right AND NOT collision on right){
    move right.
}

Or:

if(collision on right AND Dx > 0){
    Dx = 0.
}

Right now what you're doing wont allow you to move at all if there's a collision.

For example, it seems like if the player is standing on a platform, he can move left and right, but not up and down. However, if the player runs into a wall, every frame, collisions to the left or right will register true and prevent movement along the x axis as well.

share|improve this answer
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.