Incorrect collision detection or something else?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Incorrect collision detection or something else?

  1. #1
    Join Date
    Oct 2006
    Posts
    2

    Incorrect collision detection or something else?

    I'm writing a simple Javanoid-type game. What happens is that when a collision is detected and the ball hits the block at certain places (I think) the ball sometimes moves directly through the block and I have no reason why it's doing this. Is my collision detection method incorrect (tried many ways) or is the way the ball is moving the problem, or something else?

    There is a Ball object which moves by adding the variable MoveY to it's Y coord, same for the X coord, whenever the screen refreshes. This is updated in the main thread when the game is running. When the ball hits a block, only it's y direction needs to change. I have the paddle-type thing at the bottom working perfectly, the balls collide with it perfectly, but I used plenty if statements which got quite tiresome and complicated and I thought there would be an easier way to check for a collision between the ball and the blocks.

    The blocks are stored in an int array. It's basically a map of the level which holds a certain int which corresponds to a block on the screen. When the map is drawn, the x and y coords are calulated to make it fit on the screen.

    Now the problem comes in when there is a collision between the ball and a block. Here is the code I have for this:

    Code:
    				Area box = new Area(new Rectangle2D.Double(x_, y_, ColWidth, RowHeight));
    				Area ballBox = new Area(new Rectangle2D.Double(x, y, ball.getRadius(), ball.getRadius()));
    				Area area = new Area(box);
    				
    				area.intersect(ballBox);
    				
    				
    /* -I tried this method of working out if there was a collision - I find the program
    responded more, but still doesn't solve the problem-				
    				//Rectangle box, ballBox;
     				// create the shape for the box					
    				box = new Rectangle(x_, y_, ColWidth(), RowHeight());
    				// create the shape for the ball
    				ballBox = new Rectangle(x, y, ball.getRadius(), ball.getRadius()); */
    				
    				if(levelMap[i][j] > 0 && !area.isEmpty()) {
    					collision = true;
    					c = i;
    					r = j;
    					break; // found collision, break
    				}
    levelMap[][] is the array which holds the blocks. x_ and y_ have been calculated to hold the x and y coords of the onscreen block. So basically I loop through the entire array (2 for loops) and check to see if the ball's x and y coords (passed to the method as int x, int y) have intersected with the block's coords. If a collision has been detected and the block (containted in levelMap[][j]) is not dead (ie not 0) then the boolean value collision is set to true and the row and column are set so the following code knows which block to kill...

    Code:
    		if (collision) {
    			// change direction of the ball
    			ball.setMoveY(ball.getMoveY() - ball.getMoveY() * 2);
    			ball.setY(ball.getY() + ball.getMoveY()*2);		
    			
    			// kill the block
    			if(levelMap[c][r] == 1) {
    				levelMap[c][r] = 0;
    			}
    			else if(levelMap[c][r] == 2) // this is a block that has to be hit twice to be killed
    				levelMap[c][r] = 1;
    // if levelMap[c][r] = 3, it means it is invincible so it can never be killed			
    		}
    So if there is a collision, and if the block is a type 1, then it will be killed (made type 0) - this then gets painted as black (the background color).

    My problem is when there is a block that doesn't disappear when hit (ie, the blocks that require 2 hits and the invincible blocks). At certain angles of collision the balls simple passes through the block in a straight line. It bobbles up and down a bit as well. I have no idea how to stop this from happening, and the entire game kind of doesn't work with this bug. Does anyone have any ideas? Please. Is there a problem with the collision detection, the way the ball moves or something else I'm missing?


    The entire method is below:
    Code:
    	//Collision detection for blocks - called every time the screen refreshes
    	// variable 'level' holds info regarding distance between blocks
    	private void checkCollideWithBlock(int x, int y) { 
    		boolean collision = false;
    		int x_ = 0;
    		int y_ = 0;
    		int c = 0, r = 0;
    		
    		for(int i = 0; i < levelMap.length; i++) {
    			if(i > 0)
    				y_ += level.getRowHeight() + level.getSpaceRow(); // works out real coord of block on screen
    			else
    				y_ = level.getSpaceRow(); // works out real coord of block on screen
    			
    			for(int j = 0; j < levelMap[i].length; j++) {
    				if(j > 0)
    					x_ += level.getColWidth() + level.getSpaceCol(); // works out real coord of block on screen
    				else
    					x_ = level.getSpaceCol(); // works out real coord of block on screen
    					
    				Area box = new Area(new Rectangle2D.Double(x_, y_, level.getColWidth(), level.getRowHeight()));
    				Area ballBox = new Area(new Rectangle2D.Double(x, y, ball.getRadius(), ball.getRadius()));
    				Area area = new Area(box);
    				
    				area.intersect(ballBox);
    				
    				
    /*	*-- this was another collision detection thing I tried - same problem --*
    				//Rectangle box, ballBox;
     				// create the shape for the box					
    				box = new Rectangle(x_, y_, level.getColWidth(), level.getRowHeight());
    				// create the shape for the ball
    				ballBox = new Rectangle(x, y, ball.getRadius(), ball.getRadius()); */
    				
    				if(levelMap[i][j] > 0 && !area.isEmpty()) {
    					collision = true;
    					c = i;
    					r = j;
    					break; // found collision, break from loops 
    				} 
    			}	
    		}	
    		
    		if (collision) {
    			// change direction of the ball
    			ball.setMoveY(ball.getMoveY() - ball.getMoveY() * 2);
    			ball.setY(ball.getY() + ball.getMoveY()*2);		
    			
    			// kill the block
    			// blocks only holds values
    			// 0 - dead
    			// 1 - normal block - one hit to kill
    			// 2 - 2 hits required to kill
    			// 3 - invincible
    			if(levelMap[c][r] == 1) {
    				levelMap[c][r] = 0;
    				levelCol[c][r] = Color.black;
    			}
    			else if(levelMap[c][r] == 2)
    				levelMap[c][r] = 1;			
    		} 
    	}

  2. #2
    Join Date
    Sep 2006
    Posts
    68
    Look... practice shows that nobody reads letters greater then 3 lines. I try to say that you have a very small change that somebody will answer for your question.

Similar Threads

  1. Collision Detection
    By Timothy in forum Java
    Replies: 8
    Last Post: 02-05-2006, 07:38 PM
  2. Replies: 0
    Last Post: 01-23-2002, 07:47 AM
  3. reliable detection (flash, etc..)
    By nico in forum Web
    Replies: 0
    Last Post: 10-18-2001, 03:39 PM
  4. reliable flash detection and other...
    By nico in forum Enterprise
    Replies: 0
    Last Post: 10-15-2001, 07:03 AM
  5. Re: Browser Detection and SSI
    By Jim McMicking in forum Web
    Replies: 0
    Last Post: 10-25-2000, 12:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center