Collision Detection


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Collision Detection

Threaded View

  1. #1
    Join Date
    Feb 2006
    Posts
    3

    Collision Detection

    I'm writing a "simple" program that creates boxes or rectangles in a bounded environment. The idea is for them to bounce off of the walls of the environment and each other. I've been toying with some collision detection which only seems to work some of the time. As you can see, i'm getting if a box was hit, and if so what side the box was hit on. Maybe you can find the mistake?

    Code:
    for(currentBox = firstBox; currentBox.nextBox != null; currentBox = currentBox.nextBox)
    		{
    			for(currentCheckBox = firstBox; currentCheckBox.nextBox != null; currentCheckBox = currentCheckBox.nextBox)
    			{
    				if (currentBox != currentCheckBox)
    				{			
    			        if(currentBox.getY()  == currentCheckBox.getY() + currentCheckBox.getHeight() &&
    			          ( (currentBox.getX() >= currentCheckBox.getX() &&
    			             currentBox.getX() <= currentCheckBox.getX() + currentCheckBox.getWidth()) ||
    			            (currentCheckBox.getX() >= currentBox.getX() &&
    			             currentCheckBox.getX() <= currentBox.getX() + currentBox.getWidth()) ) )
    			        currentBox.setCollision(north);
    			        
    			        if(currentBox.getY() + currentBox.getHeight() == currentCheckBox.getY() &&
    			          ( (currentBox.getX() >= currentCheckBox.getX() &&
    			             currentBox.getX() <= currentCheckBox.getX() + currentCheckBox.getWidth()) ||
    			            (currentCheckBox.getX() >= currentBox.getX() &&
    			             currentCheckBox.getX() <= currentBox.getX() + currentBox.getWidth()) ) )
    			        currentBox.setCollision(south);
    			        
    			        if(currentBox.getX() == currentCheckBox.getX() + currentCheckBox.getWidth() &&
    			          ( (currentBox.getY() >= currentCheckBox.getY() &&
    			             currentBox.getY() <= currentCheckBox.getY() + currentCheckBox.getHeight()) ||
    			            (currentCheckBox.getY() >= currentBox.getY() &&
    			             currentCheckBox.getY() <= currentBox.getY() + currentBox.getHeight()) ) )
    			        currentBox.setCollision(west);
    			        
    			        if(currentBox.getX() + currentBox.getWidth() == currentCheckBox.getX() &&
    			          ( (currentBox.getY() >= currentCheckBox.getY() &&
    			             currentBox.getY() <= currentCheckBox.getY() + currentCheckBox.getHeight()) ||
    			            (currentCheckBox.getY() >= currentBox.getY() &&
    			             currentCheckBox.getY() <= currentBox.getY() + currentBox.getHeight()) ) )
    			        currentBox.setCollision(east);
    			    }
    			}
    			
    			if(currentBox.getX() <= 1)
    		    	currentBox.setCollision(west);
    			if(currentBox.getX() + currentBox.getWidth() >= maximumX - 1)
    				currentBox.setCollision(east);
    			if(currentBox.getY() <= 1)
    				currentBox.setCollision(north);
    			if(currentBox.getY() + currentBox.getHeight() >= maximumY - 1)
    				currentBox.setCollision(south);
    		}
    Also, I'm currently drawing and erasing the boxes directly to the graphics buffer. I added the update method so that it wouldn't just erase everything and be super skippy, but it still isn't as nice as I would like it. I tried drawing everything to a seperate buffer, drawing that on an image, and then displaying the image, but it was really slow. Any suggestions here?
    Last edited by Timothy; 02-04-2006 at 05:22 PM.

Similar Threads

  1. Easy Collision
    By billionaire in forum VB Classic
    Replies: 2
    Last Post: 06-08-2005, 05:53 PM
  2. Replies: 5
    Last Post: 05-27-2005, 04:05 PM
  3. reliable detection (flash, etc..)
    By nico in forum Web
    Replies: 0
    Last Post: 10-18-2001, 04:39 PM
  4. reliable flash detection and other...
    By nico in forum Enterprise
    Replies: 0
    Last Post: 10-15-2001, 08:03 AM
  5. Re: Browser Detection and SSI
    By Jim McMicking in forum Web
    Replies: 0
    Last Post: 10-25-2000, 01: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