Game collision detection with multiple objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Humakt
    New Member
    • Aug 2008
    • 42

    Game collision detection with multiple objects

    This is 2D game where there are breakable boxes in the middle two bats on both sides and one or more balls breaking the boxes. Kind of like a mixup of pong and breakout.

    Collision detection generally works find but I have trouble when the ball is hitting corners of the box. That is particularly annoying to check especially when levels are very different of each other.

    This is the function where I check collision with boxes.



    Code:
    public void checkCollideWithBox(int x, int y, int r, Ball ball, boolean second){
    		boolean collidedwithbox = false;
    		for(int j = 0; j<insidebox.length;j++){
    			if(insidebox[j]){
    				if((((x+r>=boxes[j].getPositionX()&&(x<=boxes[j].getPositionX()+20)
       &&ball.getDirectionX()>0))||((x<=boxes[j].getPositionX()+20
       &&(x+r>=boxes[j].getPositionX())
       &&ball.getDirectionX()<=0)))
       &&(y+r>=boxes[j].getPositionY()
       &&y<=boxes[j].getPositionY()+40)
       &&!boxes[j].getDestroyed())collidedwithbox = true;
    				else insidebox[j] = false;
    			}
    		}
    		boolean yhit = false, xhit = false;
    		for(int i = 0; i<boxes.length && !collidedwithbox&&!(yhit||xhit); i++){
    			if(!boxes[i].getDestroyed()){ //if box is not destroyed already
    				if((((x+r>=boxes[i].getPositionX()&&(x<=boxes[i].getPositionX()+20)
        &&ball.getDirectionX()>0))||((x<=boxes[i].getPositionX()+20
        &&(x+r>=boxes[i].getPositionX())&&ball.getDirectionX()<=0)))
        &&(y+r>=boxes[i].getPositionY()
        &&y<=boxes[i].getPositionY()+40)){ //if ball is touching or inside the box
    					if((y==boxes[i].getPositionY()+40&&!(ball.getDirectionY()>=0))
           ||(y+r==boxes[i].getPositionY()
           &&!(ball.getDirectionY()<=0))){ //if ball is on upper or lower side of the box
    						
    						//trying to solve corner cases, this is not enough:
    						if(!((x+r==boxes[i].getPositionX()&&ball.getDirectionX()<0)||(x==boxes[i].getPositionX()
         &&ball.getDirectionX()>0))){
    							yhit =  true;
    							xhit = false;
    						}else{
    							yhit = false;
    							xhit = true;
    						}
    					} else xhit = true;
    					if(boxes[i].getHits()<=1){
    						boxes[i].getEffect(ball, b1, b2, ballthread);
    						boxes[i].setDestroyed();
    						if(!boxes[i].isIndestructible()){
    							g.playSound(2);
    							boxesleft--;
    							if(lasthitplayer1ball1)scoreplayer1 +=10;
    							else scoreplayer2 +=10;
    							da.updateScoreBox(scoreplayer1, scoreplayer2);
    						}else g.playSound(3); //indestructible sound
    						if(boxesleft <= 0){
    							collidedwithbox = true;
    							this.changeLevel();
    						}
    					}else{
    						g.playSound(2);
    						boxes[i].setHits(boxes[i].getHits()-1);
    						boxes[i].setColor(boxes[i].getColor()-1);
    						if(!twoballs){
    							if(lasthitplayer1ball1)scoreplayer1 +=10;
    							else scoreplayer2 +=10;
    						}else{
    							if(!second){
    								if(lasthitplayer1ball1)scoreplayer1 +=10;
    								else scoreplayer2 +=10;
    							}else{
    								if(lasthitplayer1ball2)scoreplayer1 +=10;
    								else scoreplayer2 +=10;
    							}
    							
    						}
    						da.updateScoreBox(scoreplayer1, scoreplayer2);
    					}
    				insidebox[i] = true;
    				}
    			}
    		}
    		if(!collidedwithbox){
    			if(xhit) ball.setDirectionX(-ball.getDirectionX());
    			else if(yhit)ball.setDirectionY(-ball.getDirectionY());
    		}
    	}
    X is the ball's x location, y is likewise ball's y location, r is the radius of ball, boolean is just flag to check whether ball is first or second.

    Insidebox is boolean array for checking if ball is out of box's area.

    Any help to solve this problem?
    Last edited by Humakt; Jul 30 '09, 11:28 AM. Reason: Tried to made code more readable.
  • Humakt
    New Member
    • Aug 2008
    • 42

    #2
    Just found out that Java 2D has hit detection, maybe it will help?

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by Humakt
      Just found out that Java 2D has hit detection, maybe it will help?
      I thought it could only do an intersect() on rectangular shapes ... if your balls move fast enough you could try that method with the bounding rectangle of the ball; the user won't see the difference.

      kind regards,

      Jos

      Comment

      • Humakt
        New Member
        • Aug 2008
        • 42

        #4
        I don't think that is a problem (especially when I am already treating the ball as a square) and ellipse in Java 2D is a rectangular shape according to the link below so maybe I should use that.


        And to be specific, the problem doesnt' have as much to do with ball hitting corner but: ball hitting a corner of box when there is another box adjacent to it which ball is also hitting.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by Humakt
          And to be specific, the problem doesnt' have as much to do with ball hitting corner but: ball hitting a corner of box when there is another box adjacent to it which ball is also hitting.
          If you define that a ball can only hit one box at a time only and things move fast enough, a user may not even notice the (physical) inaccuracy.

          kind regards,

          Jos

          Comment

          • Humakt
            New Member
            • Aug 2008
            • 42

            #6
            Originally posted by JosAH
            If you define that a ball can only hit one box at a time only and things move fast enough, a user may not even notice the (physical) inaccuracy.

            kind regards,

            Jos

            Actually I've done that, but problem is whether ball should go to opposite direction x- or y-wise on coordinates and for that I have to check adjacent boxes as well. I just need to think it through but any external help is still appreciated. :)

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by Humakt
              Actually I've done that, but problem is whether ball should go to opposite direction x- or y-wise on coordinates and for that I have to check adjacent boxes as well. I just need to think it through but any external help is still appreciated. :)
              I need to have a visual on that because I don't understand what the problem is.

              kind regards,

              Jos

              Comment

              • Humakt
                New Member
                • Aug 2008
                • 42

                #8
                Originally posted by JosAH
                I need to have a visual on that because I don't understand what the problem is.

                kind regards,

                Jos
                No problem anymore, it was just a bad oversight on my side. Thanks for help though.

                Comment

                Working...