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?
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?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);
}
