Subscribe to Dogstopper's Code Mania

## Basic Bounding Boxes

In games, it is a very common operation to check whether or not two or more "sprites" (objects with an image) are colliding and if so, then they take action. I will now demonstrate this idea with a program that moves 2 balls closer toward each other until they meet, at which time, they will stop moving. I do this by testing a collision method in the Ball class.

But FIRST, let me tell what a bounding box is. A bounding box is a simple both (x, y, width, height) that represents the rectangular area of an image on screen. Now, in games, these images are encompassed by associated methods that aid in that image's movements, collisions, tracking, and so forth. Bounding boxes represent that area and can be checked against another to see if two sprites have collided.

So, now let me further explain using a program.

Firstly, the Ball sprite.
```class Ball {
// Set our bounds and movement speed
private Rectangle box;
private int xDir, yDir;

// Set a specific position and direction.
public Ball(int x, int y, int xSpeed, int ySpeed) {
box = new Rectangle(x,y,20,20);
xDir = xSpeed;
yDir = ySpeed;
}

// Move by incrementing the x and y locations.
public void move() {
box.setLocation((int) box.getX() + xDir,
(int) box.getY() + yDir);
}

// If one Ball's Rectangle intersects another, then a collision occurs
public boolean collides(Ball other) {
if (box.intersects(other.getBounds()))
return true;
else
return false;
}

// Returns the boundingBox
public Rectangle getBounds() {
return box;
}

// Draws the circle at given spot
public void draw(Graphics g) {
g.setColor(Color.red);
g.fillOval((int) box.getX(), (int) box.getY(),
(int) box.getWidth(), (int) box.getHeight());
}
}

```

As you can see, it tests collision by checking another Ball for intersection. IT also provides its own draw() and move() methods, as I show in my first lesson, "Frame Rate in Java".

Now, for the panel that encompasses the Balls.
```public class BallPanel extends JPanel implements ActionListener {
// Make the two balls.
private Ball ball1;
private Ball ball2;

// Run the balls toward each other.
public BallPanel() {
ball1 = new Ball(0,50,1,0);
ball2 = new Ball(500,50,-1,0);

// Set up a Timer for 15 milliseconds to call actionListener
Timer time = new Timer(15, this);
time.start();
}

// Check for collision, if not, then keep moving
public void actionPerformed(ActionEvent e) {
if (ball1.collides(ball2)) {
// stop the ball, so do nothing
}
else {
ball1.move();
ball2.move();
}
repaint();
}

// Controls the painting
public void paintComponent(Graphics g) {
// Paint the background
g.setColor(Color.darkGray);
g.fillRect(0, 0, 640, 480);

// Draw the balla
ball1.draw(g);
ball2.draw(g);

// makes sure monitors are up to date on *NIX systems
Toolkit.getDefaultToolkit().sync();
}
}

```

That's relatively straightforward. Every time actionPerformed() is executed, collision is checked, movement is made, and it is redrawn.

Now finally, the JFrame that does very minimal stuff.
```public class BallFrame extends JFrame {

BallFrame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(640, 480);
setResizable(false);

setVisible(true);
}

public static void main(String[] args) {
new BallFrame();
}

}

```

I hope that has helped you understand more about sprite collision using bounding boxes, which are the most basic of collision detection. Later, you may expect bounding circles and polygons!

~ Stephen Schwahn

### 2 Comments On This Entry

Page 1 of 1

#### Java Student

23 February 2010 - 08:53 PM
Did my question give you an idea for making this Bounding-box tut?

0

#### Dogstopper

24 February 2010 - 01:07 PM
Actually, this has been a draft for a good long while, and I only just published it
0
Page 1 of 1

S M T W T F S
12
3456789
101112131415 16
17181920212223
24252627282930
31